Search in sources :

Example 36 with GraphObjectMap

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);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) GraphObjectMap(org.structr.core.GraphObjectMap) Node(org.w3c.dom.Node) LinkedList(java.util.LinkedList)

Example 37 with GraphObjectMap

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());
}
Also used : XmlFunction(org.structr.core.function.XmlFunction) GraphObjectMap(org.structr.core.GraphObjectMap) Document(org.w3c.dom.Document)

Example 38 with GraphObjectMap

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());
    }
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 39 with GraphObjectMap

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);
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) Element(org.w3c.dom.Element) LinkedList(java.util.LinkedList)

Example 40 with GraphObjectMap

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);
            }
        }
    }
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) Property(org.structr.core.property.Property) DoubleProperty(org.structr.core.property.DoubleProperty) IntProperty(org.structr.core.property.IntProperty) GenericProperty(org.structr.core.property.GenericProperty) StringProperty(org.structr.core.property.StringProperty)

Aggregations

GraphObjectMap (org.structr.core.GraphObjectMap)60 LinkedList (java.util.LinkedList)27 GraphObject (org.structr.core.GraphObject)24 Map (java.util.Map)18 FrameworkException (org.structr.common.error.FrameworkException)16 StringProperty (org.structr.core.property.StringProperty)15 GenericProperty (org.structr.core.property.GenericProperty)13 PropertyKey (org.structr.core.property.PropertyKey)12 List (java.util.List)11 Result (org.structr.core.Result)10 SecurityContext (org.structr.common.SecurityContext)8 IntProperty (org.structr.core.property.IntProperty)8 RestMethodResult (org.structr.rest.RestMethodResult)8 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Test (org.junit.Test)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 HashMap (java.util.HashMap)5 LinkedHashSet (java.util.LinkedHashSet)5 Tx (org.structr.core.graph.Tx)5