import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * @deprecated class will be removed in Lithium release
+ */
+@Deprecated
class JsonToCompositeNodeReader {
- private static final Logger LOG = LoggerFactory.getLogger(JsonReader.class);
+ private static final Logger LOG = LoggerFactory.getLogger(JsonToCompositeNodeReader.class);
private static final Splitter COLON_SPLITTER = Splitter.on(':');
private JsonToCompositeNodeReader() {
}
public static CompositeNodeWrapper read(final InputStream entityStream) throws UnsupportedFormatException {
- JsonParser parser = new JsonParser();
+ final JsonParser parser = new JsonParser();
- JsonElement rootElement = parser.parse(new JsonReader(new InputStreamReader(entityStream)));
+ final JsonElement rootElement = parser.parse(new JsonReader(new InputStreamReader(entityStream)));
if (rootElement.isJsonNull()) {
// no content, so return null to indicate no input
return null;
throw new UnsupportedFormatException("Root element of Json has to be Object");
}
- Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
+ final Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
if (entrySetsOfRootJsonObject.size() != 1) {
throw new UnsupportedFormatException("Json Object should contain one element");
}
- Entry<String, JsonElement> childEntry = entrySetsOfRootJsonObject.iterator().next();
- String firstElementName = childEntry.getKey();
- JsonElement firstElementType = childEntry.getValue();
+ final Entry<String, JsonElement> childEntry = entrySetsOfRootJsonObject.iterator().next();
+ final String firstElementName = childEntry.getKey();
+ final JsonElement firstElementType = childEntry.getValue();
if (firstElementType.isJsonObject()) {
// container in yang
return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
if (firstElementType.isJsonArray()) {
// list in yang
if (firstElementType.getAsJsonArray().size() == 1) {
- JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
+ final JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
if (firstElementInArray.isJsonObject()) {
return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
}
}
private static CompositeNodeWrapper createStructureWithRoot(final String rootObjectName, final JsonObject rootObject) {
- CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
+ final CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
getLocalNameFor(rootObjectName));
- for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
+ for (final Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
}
return firstNode;
private static void addChildToParent(final String childName, final JsonElement childType,
final CompositeNodeWrapper parent) {
if (childType.isJsonObject()) {
- CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
+ final CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
getLocalNameFor(childName));
parent.addValue(child);
- for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
+ for (final Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
}
} else if (childType.isJsonArray()) {
parent.addValue(new EmptyNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName)));
} else {
- for (JsonElement childOfChildType : childType.getAsJsonArray()) {
+ for (final JsonElement childOfChildType : childType.getAsJsonArray()) {
addChildToParent(childName, childOfChildType, parent);
}
}
} else if (childType.isJsonPrimitive()) {
- JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
- String value = childPrimitive.getAsString().trim();
+ final JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
+ final String value = childPrimitive.getAsString().trim();
parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName),
resolveValueOfElement(value)));
} else {
}
}
+ /**
+ * Transform input value to URI instance.
+ *
+ * Input string has to be in format moduleName:localName. moduleName part is then transformed to URI instance.
+ * If moduleName part contains character like "<" or ">" then null value is returned because they
+ * aren't valid URI characters.
+ *
+ * @param jsonElementName
+ * value in format moduleName:localName
+ * @return
+ */
private static URI getNamespaceFor(final String jsonElementName) {
final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
- // The string needs to me in form "moduleName:localName"
+ // The string needs to be in form "moduleName:localName"
if (it.hasNext()) {
final String maybeURI = it.next();
if (Iterators.size(it) == 1) {
- return URI.create(maybeURI);
+ try {
+ return URI.create(maybeURI);
+ } catch (final IllegalArgumentException e) {
+ LOG.debug("Value {} couldn't be interpreted as URI.", maybeURI);
+ }
}
}
private static Object resolveValueOfElement(final String value) {
// it could be instance-identifier Built-In Type
if (!value.isEmpty() && value.charAt(0) == '/') {
- IdentityValuesDTO resolvedValue = RestUtil.asInstanceIdentifier(value, new PrefixMapingFromJson());
+ final IdentityValuesDTO resolvedValue = RestUtil.asInstanceIdentifier(value, new PrefixMapingFromJson());
if (resolvedValue != null) {
return resolvedValue;
}
}
- // it could be identityref Built-In Type
- URI namespace = getNamespaceFor(value);
+ // it could be identityref Built-In Type therefore it is necessary to look at value as module_name:local_name
+ final URI namespace = getNamespaceFor(value);
if (namespace != null) {
return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null, value);
}