JPathNavigator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "JPathNavigator.h"
25 
26 #include <SimoxUtility/algorithm/string/string_tools.h>
27 
30 
31 namespace armarx
32 {
33  JPathNavigator::JPathNavigator(const JsonDataPtr& data, int index, const std::string& key) :
34  data(data), index(index), key(key)
35  {
36  }
37 
38  JPathNavigator::JPathNavigator(const JsonDataPtr& data) : JPathNavigator(data, -1, "")
39  {
40  }
41 
42  bool
44  {
45  return std::dynamic_pointer_cast<JsonArray>(data) != 0;
46  }
47 
48  bool
50  {
51  return std::dynamic_pointer_cast<JsonObject>(data) != 0;
52  }
53 
54  bool
56  {
57  return std::dynamic_pointer_cast<JsonValue>(data) != 0;
58  }
59 
60  bool
62  {
63  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
64  return val != 0 && val->getType() == JsonValue::eString;
65  }
66 
67  bool
69  {
70  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
71  return val != 0 && val->getType() == JsonValue::eBool;
72  }
73 
74  bool
76  {
77  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
78  return val != 0 && val->getType() == JsonValue::eNull;
79  }
80 
81  bool
83  {
84  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
85  return val != 0 && val->getType() == JsonValue::eNumber;
86  }
87 
88  bool
90  {
91  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
92  return val != 0 && val->getType() == JsonValue::eNumber &&
93  JsonValue::CheckInt(val->rawValue());
94  }
95 
96  bool
98  {
99  return data != 0;
100  }
101 
102  void
104  {
105  if (!isValid())
106  {
107  throw LocalException("JPathNavigator is not valid.");
108  }
109  }
110 
111  std::vector<JPathNavigator>
112  JPathNavigator::select(const std::string& expr, bool limitToOne) const
113  {
114  checkValid();
115  std::vector<std::string> parts = simox::alg::split(expr, "/");
116  std::vector<JPathNavigator> result;
117  select(parts, 0, result, limitToOne);
118  return result;
119  }
120 
122  JPathNavigator::selectSingleNode(const std::string& expr) const
123  {
124  std::vector<JPathNavigator> result = select(expr, true);
125  return result.size() > 0 ? result.at(0) : JPathNavigator(0);
126  }
127 
128  void
129  JPathNavigator::select(const std::vector<std::string>& parts,
130  size_t partIndex,
131  std::vector<JPathNavigator>& result,
132  bool limitToOne) const
133  {
134  if (partIndex >= parts.size())
135  {
136  result.push_back(JPathNavigator(data, index, key));
137  return;
138  }
139  std::string part = parts.at(partIndex);
140  if (part == "*" && isArray())
141  {
142  JsonArrayPtr a = asArray();
143  for (size_t i = 0; i < a->elements.size(); i++)
144  {
145  JPathNavigator(a->elements.at(i), i, "").select(parts, partIndex + 1, result);
146  if (limitToOne && result.size() > 0)
147  {
148  return;
149  }
150  }
151  }
152  else if (part == "*" && isObject())
153  {
154  JsonObjectPtr o = asObject();
155  for (const std::pair<std::string, JsonDataPtr>& pair : o->elements)
156  {
157  JPathNavigator(pair.second, -1, pair.first).select(parts, partIndex + 1, result);
158  if (limitToOne && result.size() > 0)
159  {
160  return;
161  }
162  }
163  }
164  else if (isObject())
165  {
166  JsonObjectPtr o = asObject();
167  JsonDataPtr child = o->get(part);
168  if (child)
169  {
170  JPathNavigator(child, -1, part).select(parts, partIndex + 1, result);
171  }
172  }
173  }
174 
175  bool
176  JPathNavigator::remove(const std::string& key)
177  {
178  return asObject()->remove(key);
179  }
180 
181  void
183  {
184  asArray()->remove(index);
185  }
186 
189  {
190  checkValid();
191  JsonObjectPtr o = std::dynamic_pointer_cast<JsonObject>(data);
192  if (o == 0)
193  {
194  throw LocalException("Invalid cast");
195  }
196  return o;
197  }
198 
201  {
202  checkValid();
203  JsonArrayPtr a = std::dynamic_pointer_cast<JsonArray>(data);
204  if (a == 0)
205  {
206  throw LocalException("Invalid cast");
207  }
208  return a;
209  }
210 
213  {
214  checkValid();
215  JsonValuePtr v = std::dynamic_pointer_cast<JsonValue>(data);
216  if (v == 0)
217  {
218  throw LocalException("Invalid cast");
219  }
220  return v;
221  }
222 
223  std::string
225  {
226  return asValue()->asString();
227  }
228 
229  float
231  {
232  return asValue()->asFloat();
233  }
234 
235  int
237  {
238  return asValue()->asInt();
239  }
240 
241  bool
243  {
244  return asValue()->asBool();
245  }
246 
249  {
250  checkValid();
251  return data;
252  }
253 
254  int
256  {
257  checkValid();
258  return index;
259  }
260 
261  std::string
263  {
264  checkValid();
265  return key;
266  }
267 } // namespace armarx
armarx::JsonValue::eNumber
@ eNumber
Definition: JsonValue.h:42
armarx::JPathNavigator::isObject
bool isObject() const
Definition: JPathNavigator.cpp:49
armarx::JPathNavigator::isString
bool isString() const
Definition: JPathNavigator.cpp:61
armarx::JsonValuePtr
std::shared_ptr< JsonValue > JsonValuePtr
Definition: JsonValue.h:30
armarx::JPathNavigator::isValue
bool isValue() const
Definition: JPathNavigator.cpp:55
index
uint8_t index
Definition: EtherCATFrame.h:59
part
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in part
Definition: license.txt:18
armarx::JPathNavigator::asBool
bool asBool() const
Definition: JPathNavigator.cpp:242
armarx::JPathNavigator::isNull
bool isNull() const
Definition: JPathNavigator.cpp:75
armarx::JPathNavigator::asString
std::string asString() const
Definition: JPathNavigator.cpp:224
armarx::JsonArrayPtr
std::shared_ptr< JsonArray > JsonArrayPtr
Definition: JsonArray.h:32
armarx::JsonValue::CheckInt
static bool CheckInt(const std::string &value)
Definition: JsonValue.cpp:146
armarx::JPathNavigator
Definition: JPathNavigator.h:35
armarx::JPathNavigator::getKey
std::string getKey() const
Definition: JPathNavigator.cpp:262
StringHelpers.h
armarx::JsonValue::eString
@ eString
Definition: JsonValue.h:41
armarx::JPathNavigator::remove
bool remove(const std::string &key)
Definition: JPathNavigator.cpp:176
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
armarx::JPathNavigator::select
std::vector< JPathNavigator > select(const std::string &expr, bool limitToOne=false) const
Definition: JPathNavigator.cpp:112
armarx::JPathNavigator::checkValid
void checkValid() const
Definition: JPathNavigator.cpp:103
armarx::JsonValue::eNull
@ eNull
Definition: JsonValue.h:44
JPathNavigator.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::JPathNavigator::isBool
bool isBool() const
Definition: JPathNavigator.cpp:68
armarx::JPathNavigator::asArray
JsonArrayPtr asArray() const
Definition: JPathNavigator.cpp:200
armarx::JsonValue::eBool
@ eBool
Definition: JsonValue.h:43
armarx::JPathNavigator::isArray
bool isArray() const
Definition: JPathNavigator.cpp:43
armarx::JPathNavigator::getIndex
int getIndex() const
Definition: JPathNavigator.cpp:255
armarx::JPathNavigator::isInt
bool isInt() const
Definition: JPathNavigator.cpp:89
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::JPathNavigator::getData
JsonDataPtr getData() const
Definition: JPathNavigator.cpp:248
armarx::JPathNavigator::selectSingleNode
JPathNavigator selectSingleNode(const std::string &expr) const
Definition: JPathNavigator.cpp:122
armarx::JsonDataPtr
std::shared_ptr< JsonData > JsonDataPtr
Definition: JsonData.h:31
armarx::JPathNavigator::asFloat
float asFloat() const
Definition: JPathNavigator.cpp:230
armarx::JPathNavigator::asValue
JsonValuePtr asValue() const
Definition: JPathNavigator.cpp:212
armarx::JPathNavigator::isNumber
bool isNumber() const
Definition: JPathNavigator.cpp:82
armarx::JPathNavigator::isValid
bool isValid() const
Definition: JPathNavigator.cpp:97
armarx::JPathNavigator::asInt
int asInt() const
Definition: JPathNavigator.cpp:236
armarx::JPathNavigator::asObject
JsonObjectPtr asObject() const
Definition: JPathNavigator.cpp:188
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
Exception.h
armarx::JsonObjectPtr
std::shared_ptr< JsonObject > JsonObjectPtr
Definition: JsonObject.h:34
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38