Search in sources :

Example 1 with ParseException

use of org.apache.sling.jcr.contentparser.ParseException in project sling by apache.

the class JsonContentParser method toJsonObjectWithJsonTicks.

private JsonObject toJsonObjectWithJsonTicks(InputStream is) {
    String jsonString;
    try {
        jsonString = IOUtils.toString(is, CharEncoding.UTF_8);
    } catch (IOException ex) {
        throw new ParseException("Error getting JSON string.", ex);
    }
    // convert ticks to double quotes
    jsonString = JsonTicksConverter.tickToDoubleQuote(jsonString);
    try (JsonReader reader = jsonReaderFactory.createReader(new StringReader(jsonString))) {
        return reader.readObject();
    } catch (JsonParsingException ex) {
        throw new ParseException("Error parsing JSON content: " + ex.getMessage(), ex);
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString) IOException(java.io.IOException) ParseException(org.apache.sling.jcr.contentparser.ParseException) JsonParsingException(javax.json.stream.JsonParsingException)

Example 2 with ParseException

use of org.apache.sling.jcr.contentparser.ParseException in project sling by apache.

the class JsonContentParser method convertValue.

private Object convertValue(JsonValue value) {
    switch(value.getValueType()) {
        case STRING:
            String stringValue = ((JsonString) value).getString();
            Calendar calendarValue = helper.tryParseCalendar(stringValue);
            if (calendarValue != null) {
                return calendarValue;
            } else {
                return stringValue;
            }
        case NUMBER:
            JsonNumber numberValue = (JsonNumber) value;
            if (numberValue.isIntegral()) {
                return numberValue.longValue();
            } else {
                return numberValue.bigDecimalValue();
            }
        case TRUE:
            return true;
        case FALSE:
            return false;
        case NULL:
            return null;
        case ARRAY:
            JsonArray arrayValue = (JsonArray) value;
            Object[] values = new Object[arrayValue.size()];
            for (int i = 0; i < values.length; i++) {
                values[i] = convertValue(arrayValue.get(i));
            }
            return helper.convertSingleTypeArray(values);
        case OBJECT:
            return (JsonObject) value;
        default:
            throw new ParseException("Unexpected JSON value type: " + value.getValueType());
    }
}
Also used : JsonArray(javax.json.JsonArray) Calendar(java.util.Calendar) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) ParseException(org.apache.sling.jcr.contentparser.ParseException)

Example 3 with ParseException

use of org.apache.sling.jcr.contentparser.ParseException in project sling by apache.

the class XmlContentParser method parse.

@Override
public void parse(ContentHandler handler, InputStream is) throws IOException, ParseException {
    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document doc = documentBuilder.parse(is);
        parse(handler, doc.getDocumentElement(), null);
    } catch (ParserConfigurationException | SAXException ex) {
        throw new ParseException("Error parsing JCR XML content.", ex);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParseException(org.apache.sling.jcr.contentparser.ParseException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 4 with ParseException

use of org.apache.sling.jcr.contentparser.ParseException in project sling by apache.

the class XmlContentParser method parse.

private void parse(ContentHandler handler, Element element, String parentPath) {
    // build node path
    String path;
    if (parentPath == null) {
        path = "/";
    } else {
        String name = getChildText(element, "name");
        if (StringUtils.isEmpty(name)) {
            throw new ParseException("Child node without name detected below path " + parentPath);
        }
        path = helper.concatenatePath(parentPath, name);
        if (helper.ignoreResource(name)) {
            return;
        }
    }
    Map<String, Object> properties = new HashMap<>();
    // primary node type and mixins
    String primaryType = getChildText(element, "primaryNodeType");
    if (StringUtils.isNotBlank(primaryType) && !helper.ignoreProperty("jcr:primaryType")) {
        properties.put("jcr:primaryType", primaryType);
    }
    String[] mixins = getChildTextArray(element, "mixinNodeType");
    if (mixins.length > 0 && !helper.ignoreProperty("jcr:mixinTypes")) {
        properties.put("jcr:mixinTypes", mixins);
    }
    // properties
    List<Element> propertyElements = getChildren(element, "property");
    for (Element propertyElement : propertyElements) {
        // property name
        String name = getChildText(propertyElement, "name");
        if (StringUtils.isBlank(name)) {
            throw new ParseException("Property without name detected at path " + path);
        }
        if (helper.ignoreProperty(name)) {
            continue;
        }
        // property type
        String typeString = getChildText(propertyElement, "type");
        if (StringUtils.isBlank(typeString)) {
            throw new ParseException("Property '" + name + "' has no type at path " + path);
        }
        int type;
        try {
            type = PropertyType.valueFromName(typeString);
        } catch (IllegalArgumentException ex) {
            throw new ParseException("Property '" + name + "' has illegal type '" + typeString + "' at path " + path);
        }
        // property value
        Object value;
        List<Element> valuesElements = getChildren(propertyElement, "values");
        if (!valuesElements.isEmpty()) {
            Element valuesElement = valuesElements.get(0);
            List<Element> valueElements = getChildren(valuesElement, "value");
            String[] stringValues = new String[valueElements.size()];
            for (int i = 0; i < valueElements.size(); i++) {
                stringValues[i] = valueElements.get(i).getTextContent();
            }
            value = convertMultiValue(stringValues, type);
        } else {
            String stringValue = getChildText(propertyElement, "value");
            value = convertValue(stringValue, type);
        }
        properties.put(name, value);
    }
    // report current JSON object
    helper.ensureDefaultPrimaryType(properties);
    handler.resource(path, properties);
    // child nodes
    List<Element> nodeElements = getChildren(element, "node");
    for (Element node : nodeElements) {
        parse(handler, node, path);
    }
}
Also used : HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ParseException(org.apache.sling.jcr.contentparser.ParseException)

Example 5 with ParseException

use of org.apache.sling.jcr.contentparser.ParseException in project sling by apache.

the class ContentLoader method json.

/**
     * Import content of JSON file into repository. Auto-creates parent
     * hierarchies as nt:unstrucured nodes if missing.
     * @param inputStream Input stream with JSON content
     * @param destPath Path to import the JSON content to
     * @return Resource
     */
public Resource json(InputStream inputStream, String destPath) {
    try {
        String parentPath = ResourceUtil.getParent(destPath);
        String childName = ResourceUtil.getName(destPath);
        Resource parentResource = resourceResolver.getResource(parentPath);
        if (parentResource == null) {
            parentResource = createResourceHierarchy(parentPath);
        }
        if (parentResource.getChild(childName) != null) {
            throw new IllegalArgumentException("Resource does already exist: " + destPath);
        }
        LoaderContentHandler contentHandler = new LoaderContentHandler(destPath, resourceResolver);
        JSON_PARSER.parse(contentHandler, inputStream);
        if (autoCommit) {
            resourceResolver.commit();
        }
        return resourceResolver.getResource(destPath);
    } catch (ParseException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ParseException(org.apache.sling.jcr.contentparser.ParseException) IOException(java.io.IOException)

Aggregations

ParseException (org.apache.sling.jcr.contentparser.ParseException)7 IOException (java.io.IOException)2 JsonObject (javax.json.JsonObject)2 JsonString (javax.json.JsonString)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXException (org.xml.sax.SAXException)2 StringReader (java.io.StringReader)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonArray (javax.json.JsonArray)1 JsonNumber (javax.json.JsonNumber)1 JsonReader (javax.json.JsonReader)1 JsonParsingException (javax.json.stream.JsonParsingException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 SAXParser (javax.xml.parsers.SAXParser)1 Resource (org.apache.sling.api.resource.Resource)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 SAXParseException (org.xml.sax.SAXParseException)1