private Resource createResource(Resource parentResource, String childName, JSONObject jsonObject)
throws IOException, JSONException {
// collect all properties first
Map<String, Object> props = new HashMap<String, Object>();
JSONArray names = jsonObject.names();
for (int i = 0; names != null && i < names.length(); i++) {
final String name = names.getString(i);
if (!IGNORED_NAMES.contains(name)) {
Object obj = jsonObject.get(name);
if (!(obj instanceof JSONObject)) {
this.setProperty(props, name, obj);
}
}
}
// validate JCR primary type
Object primaryTypeObj = jsonObject.opt(JcrConstants.JCR_PRIMARYTYPE);
String primaryType = null;
if (primaryTypeObj != null) {
primaryType = String.valueOf(primaryTypeObj);
}
if (primaryType == null) {
primaryType = JcrConstants.NT_UNSTRUCTURED;
}
props.put(JcrConstants.JCR_PRIMARYTYPE, primaryType);
// create resource
Resource resource = resourceResolver.create(parentResource, childName, props);
// add child resources
for (int i = 0; names != null && i < names.length(); i++) {
final String name = names.getString(i);
if (!IGNORED_NAMES.contains(name)) {
Object obj = jsonObject.get(name);
if (obj instanceof JSONObject) {
createResource(resource, name, (JSONObject) obj);
}