private void write()

in src/main/java/org/apache/sling/feature/cpconverter/index/IndexDefinitionsJsonWriter.java [99:177]


    private void write(JsonGenerator json, DocViewNode2 index, String parentPath) throws IOException {

        String nodeName = indexDefinitions.toShortName(index.getName());
        String objectKey = parentPath.equals(IndexDefinitions.OAK_INDEX_PATH) ?
                IndexDefinitions.OAK_INDEX_PATH + "/" + nodeName : nodeName;

        // 1. start object
        json.writeStartObject(objectKey);

        // 2. write properties
        for ( DocViewProperty2 property : index.getProperties() ) {

            String propertyName = indexDefinitions.toShortName(property.getName());

            switch ( property.getType() ) {
                case PropertyType.STRING:
                case PropertyType.UNDEFINED:
                    write(json, propertyName, property.getStringValues(), STRING_MAPPER);
                    break;
                case PropertyType.LONG:
                    write(json, propertyName, property.getStringValues(), SAFE_LONG_MAPPER );
                    break;
                case PropertyType.BOOLEAN:
                    write(json, propertyName, property.getStringValues(), s -> ( Boolean.parseBoolean(s) ? JsonValue.TRUE : JsonValue.FALSE)  );
                    break;
                case PropertyType.NAME:
                    write(json, propertyName, property.getStringValues(), s -> Json.createValue("nam:" + s ));
                    break;
                case PropertyType.DOUBLE:
                    write(json, propertyName, property.getStringValues(), s -> Json.createValue(Double.parseDouble(s) ));
                    break;
                case PropertyType.DATE:
                    write(json, propertyName, property.getStringValues(), s -> Json.createValue("dat:" + s) );
                    break;
                case PropertyType.PATH:
                    write(json, propertyName, property.getStringValues(), s -> Json.createValue("pat:" + s) );
                    break;
                case PropertyType.URI:
                    write(json, propertyName, property.getStringValues(), s -> Json.createValue("uri:" + s) );
                    break;
                case PropertyType.BINARY:
                    write(json, propertyName, property.getStringValues(), BLOB_MAPPER );
                    break;
                default:
                    logger.warn("Skipping property {}, don't know how to handle type {}; values: {}", property.getName(), property.getType(), property.getStringValues());

            }
        }

        String nodePath = parentPath + "/" + nodeName;  // NOSONAR - java:S1075 does not apply as this is not a filesystem path

        // 3. write nt:data entries for nt:resource children of nt:files
        // in this case, this is the nt:resource node
        Optional<byte[]> binary = indexDefinitions.getBinary(nodePath);
        if ( binary.isPresent() ) {
            write(json, JcrConstants.JCR_PRIMARYTYPE, Collections.singletonList(JcrConstants.NT_FILE),  s -> Json.createValue("nam:" + s ));
            json.writeStartObject(JcrConstants.JCR_CONTENT);
            String blobAsString = new String(binary.get(), StandardCharsets.UTF_8);
            write(json, JcrConstants.JCR_PRIMARYTYPE, Collections.singletonList(JcrConstants.NT_RESOURCE),  s -> Json.createValue("nam:" + s ));
            write(json, JcrConstants.JCR_MIMETYPE,Collections.singletonList(Files.probeContentType(Paths.get(PlatformNameFormat.getPlatformName(nodeName)))), Json::createValue );
            write(json, JcrConstants.JCR_DATA, Collections.singletonList(blobAsString), BLOB_MAPPER);
            json.writeEnd();
        }

        // 4. write children
        for ( DocViewNode2 child : indexDefinitions.getChildren(nodePath)) {

            if( binary.isPresent()){
                String childNodeName = indexDefinitions.toShortName(child.getName());
                if(childNodeName.equalsIgnoreCase(JcrConstants.JCR_CONTENT)) {
                    continue;
                }
            }
            write(json, child, nodePath);
        }

        // 5. end object
        json.writeEnd();
    }