use of org.structr.core.GraphObjectMap in project structr by structr.
the class FromXmlFunction method convertNode.
// ----- private methods -----
private void convertNode(final GraphObjectMap map, final Node node) {
final NodeList nodeList = node.getChildNodes();
if (nodeList != null) {
final List<GraphObjectMap> children = new LinkedList<>();
final int length = nodeList.getLength();
for (int i = 0; i < length; i++) {
final GraphObjectMap childMap = new GraphObjectMap();
final Node childNode = nodeList.item(i);
convertNode(childMap, childNode);
children.add(childMap);
}
map.put(childrenProperty, children);
}
final NamedNodeMap attributeList = node.getAttributes();
if (attributeList != null) {
final List<GraphObjectMap> attributes = new LinkedList<>();
final int length = attributeList.getLength();
for (int i = 0; i < length; i++) {
final GraphObjectMap attributeMap = new GraphObjectMap();
final Node attributeNode = attributeList.item(i);
convertNode(attributeMap, attributeNode);
attributes.add(attributeMap);
}
map.put(attributesProperty, attributes);
}
map.put(typeProperty, node.getClass().getSimpleName());
final String nodeName = node.getNodeName();
if (nodeName != null) {
map.put(nameProperty, nodeName);
}
final String nodeValue = node.getNodeValue();
if (nodeValue != null) {
map.put(valueProperty, nodeValue);
}
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class FromXmlFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, Object[] sources) throws FrameworkException {
if (sources != null && sources.length > 0) {
if (sources[0] == null) {
return "";
}
try {
final GraphObjectMap result = new GraphObjectMap();
final XmlFunction xml = new XmlFunction();
final Document document = (Document) xml.apply(ctx, caller, sources);
if (document != null) {
convertNode(result, document);
return result;
} else {
logger.warn("Unable to parse XML document: {}", sources[0].toString());
}
} catch (Throwable t) {
logException(caller, t, sources);
}
return "";
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return usage(ctx.isJavaScriptContext());
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class HttpPutFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
final String uri = sources[0].toString();
final String body = sources[1].toString();
String contentType = "application/json";
String charset = "utf-8";
// override default content type
if (sources.length >= 3 && sources[2] != null) {
contentType = sources[2].toString();
}
// override default content type
if (sources.length >= 4 && sources[3] != null) {
charset = sources[3].toString();
}
final Map<String, String> responseData = HttpHelper.put(uri, body, null, null, ctx.getHeaders(), charset);
final int statusCode = Integer.parseInt(responseData.get("status"));
responseData.remove("status");
final String responseBody = responseData.get("body");
responseData.remove("body");
final GraphObjectMap response = new GraphObjectMap();
if ("application/json".equals(contentType)) {
final FromJsonFunction fromJsonFunction = new FromJsonFunction();
response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, caller, new Object[] { responseBody }));
} else {
response.setProperty(new StringProperty("body"), responseBody);
}
response.setProperty(new IntProperty("status"), statusCode);
final GraphObjectMap map = new GraphObjectMap();
for (final Map.Entry<String, String> entry : responseData.entrySet()) {
map.put(new StringProperty(entry.getKey()), entry.getValue());
}
response.setProperty(new StringProperty("headers"), map);
return response;
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class ImportGPXFunction method readRoute.
private void readRoute(final Element route, final List<GraphObjectMap> resultList) {
final List<GraphObjectMap> points = new LinkedList<>();
final GraphObjectMap result = new GraphObjectMap();
for (final Element elem : getChildren(route)) {
switch(elem.getTagName()) {
case "rtept":
final GraphObjectMap item = readPoint(elem);
if (item != null) {
points.add(item);
}
break;
}
readProperties(elem, result);
}
if (!points.isEmpty()) {
result.put(pointsProperty, points);
}
resultList.add(result);
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class ImportGPXFunction method readProperties.
private void readProperties(final Element child, final GraphObjectMap item) {
final String tagName = child.getTagName();
final Property property = fieldMapping.get(tagName);
if (property != null) {
final Class valueType = property.valueType();
if (valueType != null) {
switch(valueType.getSimpleName()) {
case "Double":
storeDouble(child, item, property);
break;
case "String":
storeString(child, item, property);
break;
case "Integer":
storeInt(child, item, property);
break;
}
}
} else {
if ("author".equals(tagName)) {
final GraphObjectMap author = readPoint(child);
if (!author.isEmpty()) {
item.put(authorProperty, author);
}
}
}
}
Aggregations