Example usage for com.fasterxml.jackson.core JsonToken equals

List of usage examples for com.fasterxml.jackson.core JsonToken equals

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonToken equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:com.sdl.odata.unmarshaller.json.core.JsonProcessor.java

/**
 * Process all things that do not contain special ODataTags.
 *
 * @param jsonParser the parser//from   w  ww.  j av  a  2  s .  c  o  m
 * @throws ODataUnmarshallingException If unable to unmarshall
 * @throws IOException If unable to read input parser
 */
private void process(JsonParser jsonParser) throws IOException, ODataUnmarshallingException {
    if (jsonParser.getCurrentToken() == JsonToken.FIELD_NAME) {
        LOG.info("Starting to parse {} token", jsonParser.getCurrentName());
        String key = jsonParser.getCurrentName();
        jsonParser.nextToken();

        JsonToken token = jsonParser.getCurrentToken();
        if (token == JsonToken.START_ARRAY) {
            if (JsonConstants.VALUE.equals(key)) {
                throw new ODataUnmarshallingException("Feed is not supported");
            }
            values.put(key, getCollectionValue(jsonParser));
        } else if (token == JsonToken.START_OBJECT) {
            values.put(key, getEmbeddedObject(jsonParser));
        } else {
            if (token.equals(JsonToken.VALUE_NULL)) {
                values.put(key, null);
            } else {
                values.put(key, jsonParser.getText());
            }
        }
    }
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

protected void extractEvent(final JsonParser jsonParser, final Resource resource) throws IOException {

    String author = null;//from  ww  w.j  a  va  2  s .co  m
    final JSONObject requestParams = new JSONObject();
    try {
        jsonParser.nextToken();
        JsonToken token = jsonParser.getCurrentToken();
        while (token.equals(JsonToken.FIELD_NAME)) {
            String field = jsonParser.getCurrentName();
            jsonParser.nextToken();

            if (field.equals(PN_START) || field.equals(PN_END)) {
                final Long value = jsonParser.getValueAsLong();
                final Calendar calendar = new GregorianCalendar();
                calendar.setTimeInMillis(value);
                final Date date = calendar.getTime();
                final TimeZone tz = TimeZone.getTimeZone("UTC");
                // this is the ISO-8601 format expected by the CalendarOperations object
                final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+00:00");
                df.setTimeZone(tz);
                if (field.equals(PN_START)) {
                    requestParams.put(CalendarRequestConstants.START_DATE, df.format(date));
                } else {
                    requestParams.put(CalendarRequestConstants.END_DATE, df.format(date));
                }
            } else if (field.equals(CalendarRequestConstants.TAGS)) {
                List<String> tags = new ArrayList<String>();
                if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) {
                    token = jsonParser.nextToken();
                    while (!token.equals(JsonToken.END_ARRAY)) {
                        tags.add(URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8"));
                        token = jsonParser.nextToken();
                    }
                    requestParams.put(CalendarRequestConstants.TAGS, tags);
                } else {
                    LOG.warn("Tags field came in without an array of tags in it - not processed");
                    // do nothing, just log the error
                }
            } else if (field.equals("jcr:createdBy")) {
                author = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8");
            } else if (field.equals(ContentTypeDefinitions.LABEL_TIMESTAMP_FIELDS)) {
                jsonParser.skipChildren(); // we do nothing with these because they're going to be put into json
            } else {
                try {
                    final String value = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8");
                    requestParams.put(field, value);
                } catch (NullPointerException e) {
                    throw e;
                }
            }
            token = jsonParser.nextToken();
        }
    } catch (final JSONException e) {
        throw new IOException("Unable to build a JSON object with the inputs provided", e);
    }
    try {
        Map<String, Object> eventParams = new HashMap<String, Object>();
        eventParams.put("event", requestParams.toString());
        calendarOperations.create(resource, author, eventParams, null,
                resource.getResourceResolver().adaptTo(Session.class));
    } catch (final OperationException e) {
        //probably caused by creating a folder that already exists. We ignore it, but still log the event.
        LOG.info("There was an operation exception while creating an event");
    }
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public Resource extractResource(final JsonParser parser, final SocialResourceProvider provider,
        final ResourceResolver resolver, final String path) throws IOException {
    final Map<String, Object> properties = new HashMap<String, Object>();

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        parser.nextToken();/*  w w w .  jav  a  2  s . c  o m*/
        JsonToken token = parser.getCurrentToken();

        if (name.equals(ContentTypeDefinitions.LABEL_SUBNODES)) {
            if (token.equals(JsonToken.START_OBJECT)) {
                parser.nextToken();
                final String childPath = path + "/" + parser.getCurrentName();
                parser.nextToken(); // should equal JsonToken.START_OBJECT
                final Resource childResource = extractResource(parser, provider, resolver, childPath); // should
                // we do anything with this?
            }
        }
    }

    return provider.create(resolver, path, properties);
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

protected void extractTopic(final JsonParser jsonParser, final Resource resource,
        final ResourceResolver resolver, final CommentOperations operations)
        throws IOException, ServletException {
    if (jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) {
        return; // replies could just be an empty object (i.e. "ugc:replies":{} ) in which case, do nothing
    }//  w ww  . j  a  va 2  s  . c  om
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("social:key", jsonParser.getCurrentName());
    Resource post = null;
    jsonParser.nextToken();
    if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        jsonParser.nextToken();
        String author = null;
        List<DataSource> attachments = new ArrayList<DataSource>();
        while (!jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) {
            final String label = jsonParser.getCurrentName();
            JsonToken token = jsonParser.nextToken();
            if (jsonParser.getCurrentToken().isScalarValue()) {

                // either a string, boolean, or long value
                if (token.isNumeric()) {
                    properties.put(label, jsonParser.getValueAsLong());
                } else {
                    final String value = jsonParser.getValueAsString();
                    if (value.equals("true") || value.equals("false")) {
                        properties.put(label, jsonParser.getValueAsBoolean());
                    } else {
                        final String decodedValue = URLDecoder.decode(value, "UTF-8");
                        if (label.equals("language")) {
                            properties.put("mtlanguage", decodedValue);
                        } else {
                            properties.put(label, decodedValue);
                            if (label.equals("userIdentifier")) {
                                author = decodedValue;
                            } else if (label.equals("jcr:description")) {
                                properties.put("message", decodedValue);
                            }
                        }
                    }
                }
            } else if (label.equals(ContentTypeDefinitions.LABEL_ATTACHMENTS)) {
                attachments = getAttachments(jsonParser);
            } else if (label.equals(ContentTypeDefinitions.LABEL_REPLIES)
                    || label.equals(ContentTypeDefinitions.LABEL_TALLY)
                    || label.equals(ContentTypeDefinitions.LABEL_TRANSLATION)
                    || label.equals(ContentTypeDefinitions.LABEL_SUBNODES)) {
                // replies and sub-nodes ALWAYS come after all other properties and attachments have been listed,
                // so we can create the post now if we haven't already, and then dive in
                if (post == null) {
                    try {
                        post = createPost(resource, author, properties, attachments,
                                resolver.adaptTo(Session.class), operations);
                        resProvider = SocialResourceUtils.getSocialResource(post).getResourceProvider();
                    } catch (Exception e) {
                        throw new ServletException(e.getMessage(), e);
                    }
                }
                if (label.equals(ContentTypeDefinitions.LABEL_REPLIES)) {
                    if (token.equals(JsonToken.START_OBJECT)) {
                        jsonParser.nextToken();
                        while (!token.equals(JsonToken.END_OBJECT)) {
                            extractTopic(jsonParser, post, resolver, operations);
                            token = jsonParser.nextToken();
                        }
                    } else {
                        throw new IOException("Expected an object for the subnodes");
                    }
                } else if (label.equals(ContentTypeDefinitions.LABEL_SUBNODES)) {
                    if (token.equals(JsonToken.START_OBJECT)) {
                        token = jsonParser.nextToken();
                        try {
                            while (!token.equals(JsonToken.END_OBJECT)) {
                                final String subnodeType = jsonParser.getCurrentName();
                                token = jsonParser.nextToken();
                                if (token.equals(JsonToken.START_OBJECT)) {
                                    jsonParser.skipChildren();
                                    token = jsonParser.nextToken();
                                }
                            }
                        } catch (final IOException e) {
                            throw new IOException("unable to skip child of sub-nodes", e);
                        }
                    } else {
                        final String field = jsonParser.getValueAsString();
                        throw new IOException("Expected an object for the subnodes. Instead: " + field);
                    }
                } else if (label.equals(ContentTypeDefinitions.LABEL_TALLY)) {
                    UGCImportHelper.extractTally(post, jsonParser, resProvider, tallyOperationsService);
                } else if (label.equals(ContentTypeDefinitions.LABEL_TRANSLATION)) {
                    importTranslation(jsonParser, post);
                    resProvider.commit(post.getResourceResolver());
                }

            } else if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
                properties.put(label, UGCImportHelper.extractSubmap(jsonParser));
            } else if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) {
                jsonParser.nextToken(); // skip the START_ARRAY token
                if (label.equals(ContentTypeDefinitions.LABEL_TIMESTAMP_FIELDS)) {
                    while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) {
                        final String timestampLabel = jsonParser.getValueAsString();
                        if (properties.containsKey(timestampLabel)
                                && properties.get(timestampLabel) instanceof Long) {
                            final Calendar calendar = new GregorianCalendar();
                            calendar.setTimeInMillis((Long) properties.get(timestampLabel));
                            properties.put(timestampLabel, calendar.getTime());
                        }
                        jsonParser.nextToken();
                    }
                } else {
                    final List<String> subArray = new ArrayList<String>();
                    while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) {
                        subArray.add(jsonParser.getValueAsString());
                        jsonParser.nextToken();
                    }
                    String[] strings = new String[subArray.size()];
                    for (int i = 0; i < subArray.size(); i++) {
                        strings[i] = subArray.get(i);
                    }
                    properties.put(label, strings);
                }
            }
            jsonParser.nextToken();
        }
        if (post == null) {
            try {
                post = createPost(resource, author, properties, attachments, resolver.adaptTo(Session.class),
                        operations);
                if (null == resProvider) {
                    resProvider = SocialResourceUtils.getSocialResource(post).getResourceProvider();
                }
                // resProvider.commit(resolver);
            } catch (Exception e) {
                throw new ServletException(e.getMessage(), e);
            }
        }
    } else {
        throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

From source file:org.sead.repositories.reference.RefRepository.java

private JsonNode getAggregation(String id, File indexFile, CountingInputStream cis, boolean withChildren,
        Long oreFileSize) throws JsonParseException, JsonMappingException, IOException {
    log.debug("Getting Aggregation");

    long curPos = 0;

    // Always need to generate these
    ArrayList<String> entries = new ArrayList<String>();
    ArrayList<Long> offsets = new ArrayList<Long>();

    FileInputStream fis = new FileInputStream(indexFile);
    JsonFactory f = new MappingJsonFactory();
    JsonParser jp = f.createParser(fis);

    JsonToken current;
    log.debug("Reading Index file");
    current = jp.nextToken(); // Start object

    while ((current = jp.nextToken()) != null) {
        if (current.equals(JsonToken.FIELD_NAME)) {
            String fName = jp.getText();
            current = jp.nextToken(); // Get to start of
            // value
            long offset = jp.getLongValue();
            log.trace("Adding: " + fName + " : " + offset);
            entries.add(fName);/*from   w ww  .j  a v  a 2s .  c  o  m*/
            offsets.add(offset);
        }
    }
    IOUtils.closeQuietly(fis);

    File descFile = getDescFile(id);
    InputStream is = new FileInputStream(descFile);
    ObjectNode resultNode = (ObjectNode) mapper.readTree(is);
    IOUtils.closeQuietly(is);

    log.trace(resultNode.toString());
    if ((resultNode.has("Has Part")) && withChildren) {

        resultNode = getChildren(resultNode, indexFile, cis, oreFileSize, curPos, entries, offsets);
    } else {
        resultNode.remove("aggregates");
    }
    log.debug("Aggregation retrieved");
    return resultNode;
}

From source file:org.sead.repositories.reference.RefRepository.java

private JsonNode getItem(String item, File indexFile, CountingInputStream cis, boolean withChildren,
        Long oreFileSize, long curOffset, ArrayList<String> entries, ArrayList<Long> offsets)
        throws JsonParseException, JsonMappingException, IOException {
    log.trace("Getting: " + item + " with starting offset: " + curOffset);

    long curPos = curOffset;

    if ((entries == null) || (offsets == null)) {
        entries = new ArrayList<String>();
        offsets = new ArrayList<Long>();

        FileInputStream fis = new FileInputStream(indexFile);
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createParser(fis);

        JsonToken current;
        log.trace("Reading Index file");
        current = jp.nextToken(); // Start object

        while ((current = jp.nextToken()) != null) {
            if (current.equals(JsonToken.FIELD_NAME)) {
                String fName = jp.getText();
                current = jp.nextToken(); // Get to start of
                // value
                long offset = jp.getLongValue();
                log.trace("Adding: " + fName + " : " + offset);
                entries.add(fName);/* ww w. j  ava 2 s  .com*/
                offsets.add(offset);
            }
        }
        try {
            fis.close();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

    }

    byte[] b = null;
    int bytesRead = 0;

    int index = entries.indexOf(item);
    if (index == -1) {
        log.warn(item + " not in index");
    }
    // getSizeEstimateFor(index)
    int estSize;
    if (index < offsets.size() - 1) {
        estSize = (int) (offsets.get(index + 1) - offsets.get(index));
    } else {
        estSize = (int) (oreFileSize - offsets.get(index));
    }
    curPos += skipTo(cis, curPos, offsets.get(index));
    log.trace("Current Pos updated to : " + curPos);
    b = new byte[estSize];
    bytesRead = cis.read(b);
    log.trace("Read " + bytesRead + " bytes");
    if (bytesRead == estSize) {
        log.trace("Read: " + new String(b));
        InputStream is = new ByteArrayInputStream(b);
        // mapper seems to be OK ignoring a last char such as a comma after
        // the object/tree
        ObjectNode resultNode = (ObjectNode) mapper.readTree(is);
        try {
            is.close();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

        curPos += bytesRead;
        log.trace("curPos: " + curPos + " : count: " + cis.getByteCount());

        log.trace(resultNode.toString());
        if ((resultNode.has("Has Part")) && withChildren) {
            resultNode = getChildren(resultNode, indexFile, cis, oreFileSize, curPos, entries, offsets);
        } else {
            resultNode.remove("aggregates");
        }
        /*
         * if (args[2] != null) { long offset2 = Long.parseLong(args[2]);
         * sbc.position(offset2); b.clear(); sbc.read(b);
         * 
         * InputStream is2 = new ByteArrayInputStream(b.array());
         * 
         * JsonNode node2 = mapper.readTree(is2);
         * System.out.println(node2.toString()); is2.close(); }
         */
        return resultNode;
    } else {
        return null;
    }

}

From source file:org.sead.repositories.reference.RefRepository.java

protected static void generateIndex(InputStream ro, File descFile, File indexFile)
        throws JsonParseException, IOException {

    log.debug("Generating desc and index files");
    JsonFactory f = new MappingJsonFactory(); // reading
    JsonParser jp = f.createParser(ro);/* www. j a v  a  2  s.  c o m*/

    JsonGenerator generator = new JsonFactory().createGenerator(descFile, JsonEncoding.UTF8);

    JsonToken current;

    current = jp.nextToken();

    report(jp, current);
    while ((current = jp.nextToken()) != null) {
        if (current.equals(JsonToken.FIELD_NAME)) {
            String fName = jp.getText();
            if (fName.equals("describes")) {
                log.trace("describes");
                while (((current = jp.nextToken()) != null)) {
                    if (jp.isExpectedStartObjectToken()) {
                        generator.setCodec(new ObjectMapper());
                        generator.useDefaultPrettyPrinter();

                        generator.writeStartObject();

                        while (((current = jp.nextToken()) != JsonToken.END_OBJECT)) {
                            if (current != JsonToken.FIELD_NAME) {
                                log.warn("Unexpected Token!");
                                report(jp, current);

                            } else {
                                report(jp, current);
                                String name = jp.getText();
                                current = jp.nextToken(); // Get to start of
                                // value
                                if (!name.equals("aggregates")) {
                                    log.trace("Writing: " + name);
                                    generator.writeFieldName(name);
                                    generator.writeTree(jp.readValueAsTree());
                                } else {
                                    report(jp, current);
                                    log.trace("Skipping?");
                                    if (current.isStructStart()) {
                                        indexChildren(indexFile, jp);
                                        // jp.skipChildren();
                                    } else {
                                        log.warn("Was Not Struct start!");
                                    }
                                    log.trace("Hit aggregates");

                                }
                            }
                        }

                        generator.writeEndObject();

                        generator.close();
                    }
                }
            }
        }
    }
}

From source file:org.sead.repositories.reference.RefRepository.java

private static void indexChildren(File index, JsonParser jp) throws IOException {

    JsonGenerator generator = new JsonFactory().createGenerator(index, JsonEncoding.UTF8);
    generator.useDefaultPrettyPrinter();

    generator.writeStartObject();/*  ww w . ja v  a 2 s . com*/

    JsonToken cur = jp.nextToken();
    while (cur.equals(JsonToken.START_OBJECT)) {
        long start = jp.getTokenLocation().getByteOffset();
        int depth = 1;
        while (depth > 0) {
            cur = jp.nextToken();
            if (cur.equals(JsonToken.START_OBJECT)) {
                depth++;
            } else if (cur.equals(JsonToken.END_OBJECT)) {
                depth--;
            } else if (cur.equals(JsonToken.FIELD_NAME) && depth == 1) {
                if (jp.getText().equals("@id")) {
                    cur = jp.nextToken();

                    String vName = jp.getText();
                    generator.writeNumberField(vName, start);
                } else {
                    report(jp, cur);
                }
            }
        }
        cur = jp.nextToken();
    }
    generator.writeEndObject();
    generator.close();

}