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);
}
}
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());
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations