Example usage for com.fasterxml.jackson.core JsonParser close

List of usage examples for com.fasterxml.jackson.core JsonParser close

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser close.

Prototype

@Override
public abstract void close() throws IOException;

Source Link

Document

Closes the parser so that no further iteration or data access can be made; will also close the underlying input source if parser either owns the input source, or feature Feature#AUTO_CLOSE_SOURCE is enabled.

Usage

From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java

@Override
public List<Relative> parseRelatives(final InputStream inputStream) {
    checkNotNull(inputStream);/*from   ww  w  .  j a va2 s. c  o  m*/
    JsonParser parser = null;
    try {
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        List<Relative> relatives = new ArrayList<Relative>();

        String profileId = null;
        String matchId = null;
        double similarity = 0.0d;
        int sharedSegments = 0;
        Relationship relationship = null;
        Relationship userRelationship = null;
        Set<Relationship> range = new HashSet<Relationship>();

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String field = parser.getCurrentName();
            parser.nextToken();

            if ("id".equals(field)) {
                profileId = parser.getText();
            } else if ("relatives".equals(field)) {
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        String relativeField = parser.getCurrentName();
                        parser.nextToken();

                        if ("match_id".equals(relativeField)) {
                            matchId = parser.getText();
                        } else if ("similarity".equals(relativeField)) {
                            similarity = Double.parseDouble(parser.getText());
                        } else if ("shared_segments".equals(relativeField)) {
                            sharedSegments = parser.getIntValue();
                        } else if ("relationship".equals(relativeField)) {
                            relationship = Relationship.fromDescription(parser.getText());
                        } else if ("user_relationship_code".equals(relativeField)) {
                            String code = parser.getText();
                            userRelationship = code == "null" ? null
                                    : Relationship.fromCode(Integer.parseInt(code));
                        } else if ("predicted_relationship_code".equals(relativeField)) {
                            if (relationship == null) {
                                String code = parser.getText();
                                relationship = code == "null" ? null
                                        : Relationship.fromCode(Integer.parseInt(code));
                            }
                        } else if ("range".equals(relativeField)) {
                            while (parser.nextToken() != JsonToken.END_ARRAY) {
                                range.add(Relationship.fromDescription(parser.getText()));
                            }
                        }
                        // ignored nested fields
                        else if ("family_locations".equals(relativeField)) {
                            while (parser.nextToken() != JsonToken.END_ARRAY) {
                                // ignore
                            }
                        } else if ("family_surnames".equals(relativeField)) {
                            while (parser.nextToken() != JsonToken.END_ARRAY) {
                                // ignore
                            }
                        } else if ("profile_picture_urls".equals(relativeField)) {
                            while (parser.nextToken() != JsonToken.END_OBJECT) {
                                // ignore
                            }
                        }
                    }
                }
                relatives.add(new Relative(profileId, matchId, similarity, sharedSegments, relationship,
                        userRelationship, range));
                matchId = null;
                similarity = 0.0d;
                sharedSegments = 0;
                relationship = null;
                userRelationship = null;
                range.clear();
            }
        }
        return relatives;
    } catch (IOException e) {
        logger.warn("could not parse relatives", e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
    return null;
}

From source file:com.github.heuermh.ensemblrestclient.JacksonVariationConverter.java

static VariationConsequences parseVariationConsequences(final JsonFactory jsonFactory,
        final InputStream inputStream) throws IOException {
    JsonParser parser = null;
    try {/*  w  ww.  j  a v  a2  s  .  c  om*/
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String identifier = null;
        String referenceAllele = null;
        List<String> alternateAlleles = new ArrayList<String>();

        String locationName = null;
        String coordinateSystem = "chromosome";
        int start = -1;
        int end = -1;
        int strand = -1;

        List<TranscriptConsequences> transcriptConsequences = new ArrayList<TranscriptConsequences>();

        String alternateAllele = null;
        int transcriptStrand = -1;
        boolean canonical = false;
        String geneId = null;
        String transcriptId = null;
        String translationId = null;
        String transcriptAlleleString = null;
        String codons = null;
        String hgvsc = null;
        String aminoAcids = null;
        String hgvsp = null;
        List<String> consequenceTerms = new ArrayList<String>();

        while (parser.nextToken() != JsonToken.END_ARRAY) {
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                String field = parser.getCurrentName();
                parser.nextToken();

                if ("id".equals(field)) {
                    identifier = parser.getText();
                } else if ("seq_region_name".equals(field)) {
                    locationName = parser.getText();
                } else if ("start".equals(field)) {
                    start = parser.getIntValue();
                } else if ("end".equals(field)) {
                    end = parser.getIntValue();
                } else if ("strand".equals(field)) {
                    strand = parser.getIntValue();
                } else if ("allele_string".equals(field)) {
                    String[] tokens = parser.getText().split("/");
                    referenceAllele = tokens[0];
                    for (int i = 1; i < tokens.length; i++) {
                        alternateAlleles.add(tokens[i]);
                    }
                } else if ("transcript_consequences".equals(field)) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            String transcriptField = parser.getCurrentName();
                            parser.nextToken();

                            if ("variant_allele".equals(transcriptField)) {
                                alternateAllele = parser.getText();
                            } else if ("strand".equals(transcriptField)) {
                                transcriptStrand = parser.getIntValue();
                            } else if ("canonical".equals(transcriptField)) {
                                canonical = (Integer.parseInt(parser.getText()) > 0);
                            } else if ("gene_id".equals(transcriptField)) {
                                geneId = parser.getText();
                            } else if ("transcript_id".equals(transcriptField)) {
                                transcriptId = parser.getText();
                            } else if ("protein_id".equals(transcriptField)) {
                                translationId = parser.getText();
                            } else if ("codons".equals(transcriptField)) {
                                codons = parser.getText();
                            } else if ("hgvsc".equals(transcriptField)) {
                                hgvsc = parser.getText();
                            } else if ("amino_acids".equals(transcriptField)) {
                                aminoAcids = parser.getText();
                            } else if ("hgvsp".equals(transcriptField)) {
                                hgvsp = parser.getText();
                            } else if ("consequence_terms".equals(transcriptField)) {
                                while (parser.nextToken() != JsonToken.END_ARRAY) {
                                    consequenceTerms.add(parser.getText());
                                }
                            }
                        }

                        transcriptConsequences.add(new TranscriptConsequences(alternateAllele, transcriptStrand,
                                canonical, geneId, transcriptId, translationId, codons, hgvsc, aminoAcids,
                                hgvsp, consequenceTerms));

                        alternateAllele = null;
                        transcriptStrand = -1;
                        canonical = false;
                        geneId = null;
                        transcriptId = null;
                        translationId = null;
                        transcriptAlleleString = null;
                        codons = null;
                        hgvsc = null;
                        aminoAcids = null;
                        hgvsp = null;
                        consequenceTerms.clear();
                    }
                } else if ("colocated_variants".equals(field)) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            // ignore
                        }
                    }
                }
            }
        }
        Location location = new Location(locationName, coordinateSystem, start, end, strand);
        return new VariationConsequences(identifier, referenceAllele, alternateAlleles, location,
                transcriptConsequences);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:com.ntsync.shared.RawContact.java

/**
 * Creates and returns an instance of the RawContact from encrypted data
 * //  www  .j a va2 s  . co m
 * */
public static RawContact valueOf(String rowId, Map<Byte, ByteBuffer> values, Key privateKey)
        throws InvalidKeyException {
    try {
        String serverContactId = null;
        long rawContactId = -1;
        if (values.containsKey(ContactConstants.SERVERROW_ID)) {
            serverContactId = readRawString(values.get(ContactConstants.SERVERROW_ID));
        }
        String lastModStr = readRawString(values.get(ContactConstants.MODIFIED));
        Date lastModified = null;
        if (lastModStr != null) {
            lastModified = new Date(Long.parseLong(lastModStr));
        }

        if (serverContactId == null || !serverContactId.equals(rowId)) {
            // If ServerContactId is different, then rowId is the clientId
            rawContactId = Long.parseLong(rowId);
        }

        if (serverContactId == null && rawContactId < 0) {
            throw new IllegalArgumentException("Missing RowId in data");
        }

        AEADBlockCipher cipher = CryptoHelper.getCipher();
        final boolean deleted = values.containsKey(ContactConstants.DELETED);

        final String textData = CryptoHelper.decodeStringValue(ContactConstants.TEXTDATA, values, cipher,
                privateKey);

        if (textData == null && !deleted) {
            LOG.error("No textdata found for row with Id:" + rowId);
            return null;
        }

        String fullName = null;
        String firstName = null;
        String lastName = null;
        String middleName = null;
        String prefixName = null;
        String suffixName = null;
        String phonecticFirst = null;
        String phonecticMiddle = null;
        String phonecticLast = null;
        List<String> groupSourceIds = null;
        String note = null;
        List<ListRawData<PhoneType>> phones = null;
        List<ListRawData<EmailType>> emails = null;
        List<ListRawData<WebsiteType>> websites = null;
        List<ListRawData<EventType>> events = null;
        List<ListRawData<RelationType>> relations = null;
        List<ListRawData<SipAddressType>> sipaddresses = null;
        List<ListRawData<NicknameType>> nicknames = null;
        List<RawAddressData> addresses = null;
        List<RawImData> imAddresses = null;
        RawOrganizationData organization = null;
        boolean photoSuperPrimary = false;
        boolean starred = false;
        String customRingtone = null;
        boolean sendToVoiceMail = false;

        if (!SyncDataHelper.isEmpty(textData)) {
            JsonFactory fac = new JsonFactory();
            JsonParser jp = fac.createParser(textData);
            jp.nextToken();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldname = jp.getCurrentName();
                // move to value, or START_OBJECT/START_ARRAY
                jp.nextToken();
                if (ContactConstants.STRUCTUREDNAME.equals(fieldname)) {
                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                        String namefield = jp.getCurrentName();
                        // move to value
                        if (jp.nextToken() == null) {
                            throw new JsonParseException("Invalid JSON-Structure. End of Object missing.",
                                    jp.getCurrentLocation());
                        }
                        if (ContactConstants.DISPLAY_NAME.equals(namefield)) {
                            fullName = jp.getValueAsString();
                        } else if (ContactConstants.FAMILY_NAME.equals(namefield)) {
                            lastName = jp.getValueAsString();
                        } else if (ContactConstants.GIVEN_NAME.equals(namefield)) {
                            firstName = jp.getValueAsString();
                        } else if (ContactConstants.MIDDLE_NAME.equals(namefield)) {
                            middleName = jp.getValueAsString();
                        } else if (ContactConstants.SUFFIX_NAME.equals(namefield)) {
                            suffixName = jp.getValueAsString();
                        } else if (ContactConstants.PREFIX_NAME.equals(namefield)) {
                            prefixName = jp.getValueAsString();
                        } else if (ContactConstants.PHONETIC_FAMILY.equals(namefield)) {
                            phonecticLast = jp.getValueAsString();
                        } else if (ContactConstants.PHONETIC_GIVEN.equals(namefield)) {
                            phonecticFirst = jp.getValueAsString();
                        } else if (ContactConstants.PHONETIC_MIDDLE.equals(namefield)) {
                            phonecticMiddle = jp.getValueAsString();
                        } else {
                            LOG.error("Unrecognized structurednamefield for row with Id:" + rowId
                                    + " Fieldname:" + fieldname);
                            break;
                        }
                    }
                } else if (ContactConstants.STRUCTUREDPOSTAL.equals(fieldname)) {
                    addresses = readAddressList(rowId, addresses, jp);
                } else if (ContactConstants.PHONE.equals(fieldname)) {
                    phones = readJsonList(rowId, phones, jp, fieldname, PhoneType.TYPE_OTHER, PhoneType.class);
                } else if (ContactConstants.EMAIL.equals(fieldname)) {
                    emails = readJsonList(rowId, emails, jp, fieldname, EmailType.TYPE_OTHER, EmailType.class);
                } else if (ContactConstants.WEBSITE.equals(fieldname)) {
                    websites = readJsonList(rowId, websites, jp, fieldname, WebsiteType.TYPE_OTHER,
                            WebsiteType.class);
                } else if (ContactConstants.EVENT.equals(fieldname)) {
                    events = readJsonList(rowId, events, jp, fieldname, EventType.TYPE_OTHER, EventType.class);
                } else if (ContactConstants.RELATION.equals(fieldname)) {
                    relations = readJsonList(rowId, relations, jp, fieldname, RelationType.TYPE_CUSTOM,
                            RelationType.class);
                } else if (ContactConstants.SIPADDRESS.equals(fieldname)) {
                    sipaddresses = readJsonList(rowId, sipaddresses, jp, fieldname, SipAddressType.TYPE_OTHER,
                            SipAddressType.class);
                } else if (ContactConstants.NICKNAME.equals(fieldname)) {
                    nicknames = readJsonList(rowId, nicknames, jp, fieldname, NicknameType.TYPE_DEFAULT,
                            NicknameType.class);
                } else if (ContactConstants.IM.equals(fieldname)) {
                    imAddresses = readImList(rowId, imAddresses, jp);
                } else if (ContactConstants.NOTE.equals(fieldname)) {
                    note = jp.getValueAsString();
                } else if (ContactConstants.GROUPMEMBERSHIP.equals(fieldname)) {
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        String groupSourceId = jp.getValueAsString();
                        if (groupSourceIds == null) {
                            groupSourceIds = new ArrayList<String>();
                        }
                        groupSourceIds.add(groupSourceId);
                    }
                } else if (ContactConstants.ORGANIZATION.equals(fieldname)) {
                    organization = readOrg(rowId, jp);
                } else if (ContactConstants.PHOTO_SUPERPRIMARY.equals(fieldname)) {
                    photoSuperPrimary = jp.getValueAsBoolean();
                } else if (ContactConstants.STARRED.equals(fieldname)) {
                    starred = jp.getValueAsBoolean();
                } else if (ContactConstants.SEND_TO_VOICE_MAIL.equals(fieldname)) {
                    sendToVoiceMail = jp.getValueAsBoolean();
                } else if (ContactConstants.DROID_CUSTOM_RINGTONE.equals(fieldname)) {
                    customRingtone = jp.getValueAsString();
                } else {
                    LOG.error("Unrecognized field for row with Id:" + rowId + " Fieldname:" + fieldname);
                }
            }
            jp.close();
        }

        final byte[] photo = CryptoHelper.decodeValue(ContactConstants.PHOTO, values, cipher, privateKey);

        return new RawContact(fullName, firstName, lastName, middleName, prefixName, suffixName, phonecticFirst,
                phonecticMiddle, phonecticLast, phones, emails, websites, addresses, events, relations,
                sipaddresses, nicknames, imAddresses, note, organization, photo, photoSuperPrimary,
                groupSourceIds, null, starred, customRingtone, sendToVoiceMail, lastModified, deleted,
                serverContactId, rawContactId, false, -1);
    } catch (InvalidCipherTextException ex) {
        throw new InvalidKeyException("Invalid key detected.", ex);
    } catch (final IOException ex) {
        LOG.info("Error parsing contact data. Reason:" + ex.toString(), ex);
    } catch (IllegalArgumentException ex) {
        LOG.warn("Error parsing contact data. Reason:" + ex.toString(), ex);
    }

    return null;
}

From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java

protected boolean Sync(String service, String scope, String params) {
    final Date start = new Date();
    boolean hasError = false;
    if (params == null)
        params = "";
    final SQLiteDatabase db = mDB.getWritableDatabase();
    final ArrayList<TableInfo> notifyTableInfo = new ArrayList<TableInfo>();

    final String download = String.format(contentHelper.DOWNLOAD_SERVICE_URI, service, scope, params);
    final String upload = String.format(contentHelper.UPLOAD_SERVICE_URI, service, scope, params);
    final String scopeServerBlob = String.format("%s.%s.%s", service, scope, _.serverBlob);
    String serverBlob = null;//w w w .  ja  v a2s. c  o m
    Cursor cur = db.query(BlobsTable.NAME, new String[] { BlobsTable.C_VALUE }, BlobsTable.C_NAME + "=?",
            new String[] { scopeServerBlob }, null, null, null);
    final String originalBlob;
    if (cur.moveToFirst()) {
        originalBlob = serverBlob = cur.getString(0);
    } else {
        originalBlob = null;
    }
    cur.close();
    db.beginTransaction();
    try {
        boolean nochanges = false;
        if (serverBlob != null) {
            nochanges = !contentHelper.hasDirtTable(db, scope);
        }
        boolean resolve = false;
        final Metadata meta = new Metadata();
        final HashMap<String, Object> vals = new HashMap<String, Object>();
        final ContentValues cv = new ContentValues(2);
        JsonFactory jsonFactory = new JsonFactory();
        JsonToken current = null;
        String name = null;
        boolean moreChanges = false;
        boolean forceMoreChanges = false;
        do {
            final int requestMethod;
            final String serviceRequestUrl;
            final ContentProducer contentProducer;

            if (serverBlob != null) {
                requestMethod = HTTP_POST;
                if (nochanges) {
                    serviceRequestUrl = download;
                } else {
                    serviceRequestUrl = upload;
                    forceMoreChanges = true;
                }
                contentProducer = new SyncContentProducer(jsonFactory, db, scope, serverBlob, !nochanges,
                        notifyTableInfo, contentHelper);
                nochanges = true;
            } else {
                requestMethod = HTTP_GET;
                serviceRequestUrl = download;
                contentProducer = null;

            }
            if (moreChanges) {
                db.beginTransaction();
            }

            Result result = executeRequest(requestMethod, serviceRequestUrl, contentProducer);
            if (result.getStatus() == HttpStatus.SC_OK) {
                final JsonParser jp = jsonFactory.createParser(result.getInputStream());

                jp.nextToken(); // skip ("START_OBJECT(d) expected");
                jp.nextToken(); // skip ("FIELD_NAME(d) expected");
                if (jp.nextToken() != JsonToken.START_OBJECT)
                    throw new Exception("START_OBJECT(d - object) expected");
                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    name = jp.getCurrentName();
                    if (_.__sync.equals(name)) {
                        current = jp.nextToken();
                        while (jp.nextToken() != JsonToken.END_OBJECT) {
                            name = jp.getCurrentName();
                            current = jp.nextToken();
                            if (_.serverBlob.equals(name)) {
                                serverBlob = jp.getText();
                            } else if (_.moreChangesAvailable.equals(name)) {
                                moreChanges = jp.getBooleanValue() || forceMoreChanges;
                                forceMoreChanges = false;
                            } else if (_.resolveConflicts.equals(name)) {
                                resolve = jp.getBooleanValue();
                            }
                        }
                    } else if (_.results.equals(name)) {
                        if (jp.nextToken() != JsonToken.START_ARRAY)
                            throw new Exception("START_ARRAY(results) expected");
                        while (jp.nextToken() != JsonToken.END_ARRAY) {
                            meta.isDeleted = false;
                            meta.tempId = null;
                            vals.clear();
                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                name = jp.getCurrentName();
                                current = jp.nextToken();
                                if (current == JsonToken.VALUE_STRING) {
                                    vals.put(name, jp.getText());
                                } else if (current == JsonToken.VALUE_NUMBER_INT) {
                                    vals.put(name, jp.getLongValue());
                                } else if (current == JsonToken.VALUE_NUMBER_FLOAT) {
                                    vals.put(name, jp.getDoubleValue());
                                } else if (current == JsonToken.VALUE_FALSE) {
                                    vals.put(name, 0L);
                                } else if (current == JsonToken.VALUE_TRUE) {
                                    vals.put(name, 1L);
                                } else if (current == JsonToken.VALUE_NULL) {
                                    vals.put(name, null);
                                } else {
                                    if (current == JsonToken.START_OBJECT) {
                                        if (_.__metadata.equals(name)) {
                                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                name = jp.getCurrentName();
                                                jp.nextToken();
                                                if (_.uri.equals(name)) {
                                                    meta.uri = jp.getText();
                                                } else if (_.type.equals(name)) {
                                                    meta.type = jp.getText();
                                                } else if (_.isDeleted.equals(name)) {
                                                    meta.isDeleted = jp.getBooleanValue();
                                                } else if (_.tempId.equals(name)) {
                                                    meta.tempId = jp.getText();
                                                }
                                            }
                                        } else if (_.__syncConflict.equals(name)) {
                                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                name = jp.getCurrentName();
                                                jp.nextToken();
                                                if (_.isResolved.equals(name)) {
                                                } else if (_.conflictResolution.equals(name)) {
                                                } else if (_.conflictingChange.equals(name)) {
                                                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                        name = jp.getCurrentName();
                                                        current = jp.nextToken();
                                                        if (current == JsonToken.START_OBJECT) {
                                                            if (_.__metadata.equals(name)) {
                                                                while (jp.nextToken() != JsonToken.END_OBJECT) {

                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            // resolve conf

                                        } else if (_.__syncError.equals(name)) {
                                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                                name = jp.getCurrentName();
                                                jp.nextToken();
                                            }
                                        }
                                    }
                                }
                            }
                            TableInfo tab = contentHelper.getTableFromType(meta.type);
                            if (meta.isDeleted) {
                                tab.DeleteWithUri(meta.uri, db);
                            } else {
                                tab.SyncJSON(vals, meta, db);
                            }
                            if (!notifyTableInfo.contains(tab))
                                notifyTableInfo.add(tab);
                        }
                    }
                }
                jp.close();
                if (!hasError) {
                    cv.clear();
                    cv.put(BlobsTable.C_NAME, scopeServerBlob);
                    cv.put(BlobsTable.C_VALUE, serverBlob);
                    cv.put(BlobsTable.C_DATE, Calendar.getInstance().getTimeInMillis());
                    cv.put(BlobsTable.C_STATE, 0);
                    db.replace(BlobsTable.NAME, null, cv);
                    db.setTransactionSuccessful();
                    db.endTransaction();
                    if (DEBUG) {
                        Log.d(TAG, "CP-Sync: commit changes");
                    }
                    final ContentResolver cr = getContext().getContentResolver();
                    for (TableInfo t : notifyTableInfo) {
                        final Uri nu = contentHelper.getDirUri(t.name, false);
                        cr.notifyChange(nu, null, false);
                        // false - do not force sync cause we are in sync
                        if (DEBUG) {
                            Log.d(TAG, "CP-Sync: notifyChange table: " + t.name + ", uri: " + nu);
                        }

                        for (String n : t.notifyUris) {
                            cr.notifyChange(Uri.parse(n), null, false);
                            if (DEBUG) {
                                Log.d(TAG, "+uri: " + n);
                            }
                        }
                    }
                    notifyTableInfo.clear();
                }
            } else {
                if (DEBUG) {
                    Log.e(TAG, "Server error in fetching remote contacts: " + result.getStatus());
                }
                hasError = true;
                break;
            }
        } while (moreChanges);
    } catch (final ConnectTimeoutException e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, "ConnectTimeoutException", e);
        }
    } catch (final IOException e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } catch (final ParseException e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, "ParseException", e);
        }
    } catch (final Exception e) {
        hasError = true;
        if (DEBUG) {
            Log.e(TAG, "ParseException", e);
        }
    }
    if (hasError) {
        db.endTransaction();
        ContentValues cv = new ContentValues();
        cv.put(BlobsTable.C_NAME, scopeServerBlob);
        cv.put(BlobsTable.C_VALUE, originalBlob);
        cv.put(BlobsTable.C_DATE, Calendar.getInstance().getTimeInMillis());
        cv.put(BlobsTable.C_STATE, -1);
        db.replace(BlobsTable.NAME, null, cv);
    }
    /*-if (!hasError) {
    final ContentValues cv = new ContentValues(2);
     cv.put(BlobsTable.C_NAME, scopeServerBlob);
     cv.put(BlobsTable.C_VALUE, serverBlob);
     db.replace(BlobsTable.NAME, null, cv);
     db.setTransactionSuccessful();
    }
    db.endTransaction();
    if (!hasError) {
     for (String t : notifyTableInfo) {
    getContext().getContentResolver().notifyChange(getDirUri(t),
          null);
     }
    }*/
    if (DEBUG) {
        Helpers.LogInfo(start);
    }
    return !hasError;
}

From source file:org.kie.workbench.common.stunner.bpmn.backend.legacy.Bpmn2JsonUnmarshaller.java

/**
 * NOTE: This method has been set protected for Stunner support. Stunner bpmn implementation provides a custom JsonParser that
 * is used instead of the one used in jbpm-designer-backend.
 * <p>//w  w w . j  a v a 2  s.com
 * Start unmarshalling using the parser.
 * @param parser
 * @param preProcessingData
 * @return the root element of a bpmn2 document.
 * @throws JsonParseException
 * @throws IOException
 */
protected Bpmn2Resource unmarshall(JsonParser parser, String preProcessingData) throws IOException {
    try {
        parser.nextToken(); // open the object
        ResourceSet rSet = new ResourceSetImpl();
        rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bpmn2",
                new JBPMBpmn2ResourceFactoryImpl());
        Bpmn2Resource bpmn2 = (Bpmn2Resource) rSet.createResource(URI.createURI("virtual.bpmn2"));
        rSet.getResources().add(bpmn2);
        _currentResource = bpmn2;
        if (preProcessingData == null || preProcessingData.length() < 1) {
            preProcessingData = "ReadOnlyService";
        }
        // do the unmarshalling now:
        Definitions def = (Definitions) unmarshallItem(parser, preProcessingData);
        def.setExporter(exporterName);
        def.setExporterVersion(exporterVersion);
        revisitUserTasks(def);
        revisitServiceTasks(def);
        revisitMessages(def);
        revisitCatchEvents(def);
        revisitThrowEvents(def);
        revisitLanes(def);
        revisitSubProcessItemDefs(def);
        revisitArtifacts(def);
        revisitGroups(def);
        revisitTaskAssociations(def);
        revisitSendReceiveTasks(def);
        reconnectFlows();
        revisitGateways(def);
        revisitCatchEventsConvertToBoundary(def);
        revisitBoundaryEventsPositions(def);
        createDiagram(def);
        updateIDs(def);
        revisitDataObjects(def);
        revisitAssociationsIoSpec(def);
        revisitWsdlImports(def);
        revisitMultiInstanceTasks(def);
        addSimulation(def);
        revisitItemDefinitions(def);
        revisitProcessDoc(def);
        revisitDI(def);
        revisitSignalRef(def);
        orderDiagramElements(def);
        // return def;
        _currentResource.getContents().add(def);
        return _currentResource;
    } catch (Exception e) {
        _logger.error(e.getMessage());
        return _currentResource;
    } finally {
        parser.close();
        _objMap.clear();
        _idMap.clear();
        _outgoingFlows.clear();
        _sequenceFlowTargets.clear();
        _bounds.clear();
        _currentResource = null;
    }
}

From source file:org.jbpm.designer.bpmn2.impl.Bpmn2JsonUnmarshaller.java

/**
 * Start unmarshalling using the parser.
 * @param parser// w  ww  .j a v a 2 s .  c  o m
 * @param preProcessingData
 * @return the root element of a bpmn2 document.
 * @throws java.io.IOException
 */
private Bpmn2Resource unmarshall(JsonParser parser, String preProcessingData) throws IOException {
    try {
        parser.nextToken(); // open the object
        ResourceSet rSet = new ResourceSetImpl();
        rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bpmn2",
                new JBPMBpmn2ResourceFactoryImpl());
        Bpmn2Resource bpmn2 = (Bpmn2Resource) rSet.createResource(URI.createURI("virtual.bpmn2"));
        rSet.getResources().add(bpmn2);
        _currentResource = bpmn2;

        if (preProcessingData == null || preProcessingData.length() < 1) {
            preProcessingData = "ReadOnlyService";
        }

        // do the unmarshalling now:
        Definitions def = (Definitions) unmarshallItem(parser, preProcessingData);
        def.setExporter(exporterName);
        def.setExporterVersion(exporterVersion);
        revisitUserTasks(def);
        revisitServiceTasks(def);
        revisitMessages(def);
        revisitCatchEvents(def);
        revisitThrowEvents(def);
        revisitLanes(def);
        revisitSubProcessItemDefs(def);
        revisitArtifacts(def);
        revisitGroups(def);
        revisitTaskAssociations(def);
        revisitTaskIoSpecification(def);
        revisitSendReceiveTasks(def);
        reconnectFlows();
        revisitGateways(def);
        revisitCatchEventsConvertToBoundary(def);
        revisitBoundaryEventsPositions(def);
        createDiagram(def);
        updateIDs(def);
        revisitDataObjects(def);
        revisitAssociationsIoSpec(def);
        revisitWsdlImports(def);
        revisitMultiInstanceTasks(def);
        addSimulation(def);
        revisitItemDefinitions(def);
        revisitProcessDoc(def);
        revisitDI(def);
        revisitSignalRef(def);
        orderDiagramElements(def);

        // return def;
        _currentResource.getContents().add(def);
        return _currentResource;
    } catch (Exception e) {
        _logger.error(e.getMessage());
        return _currentResource;
    } finally {
        parser.close();
        _objMap.clear();
        _idMap.clear();
        _outgoingFlows.clear();
        _sequenceFlowTargets.clear();
        _bounds.clear();
        _currentResource = null;
    }
}

From source file:eu.project.ttc.models.index.JsonTermIndexIO.java

/**
 * Loads the json-serialized term index into the param {@link TermIndex} object.
 * /*from w  w w  . ja  v a 2s. c om*/
 * @param reader
 * @param options
 *          The deserialization {@link IOOptions}.
 * @return
 * @throws JsonParseException
 * @throws IOException
 */
public static TermIndex load(Reader reader, JsonOptions options) throws JsonParseException, IOException {
    TermIndex termIndex = null;
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jp = jsonFactory.createParser(reader); // or Stream, Reader
    jp.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    jp.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
    String fieldname;
    String compLemma = null;
    int fileSource = -1;
    String wordLemma = null;
    String syntacticLabel = null;
    int begin = -1;
    int end = -1;
    int nbWordAnnos = -1;
    int nbSpottedTerms = -1;
    Term b;
    Term v;
    String text;
    String base;
    String variant;
    //      String rule;
    String infoToken;
    String variantType;
    double variantScore;

    Map<Integer, String> inputSources = Maps.newTreeMap();

    Map<Integer, List<TempVecEntry>> contextVectors = Maps.newHashMap();

    OccurrenceStore occurrenceStore = null;

    // useful var for debug
    JsonToken tok;

    while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {

        fieldname = jp.getCurrentName();
        if (METADATA.equals(fieldname)) {
            jp.nextToken();
            String termIndexName = null;
            Lang lang = null;
            String corpusID = null;
            String occurrenceStorage = null;
            String occurrenceStoreURI = null;

            while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                fieldname = jp.getCurrentName();
                if (LANG.equals(fieldname)) {
                    lang = Lang.forName(jp.nextTextValue());
                } else if (NAME.equals(fieldname)) {
                    termIndexName = jp.nextTextValue();
                } else if (NB_WORD_ANNOTATIONS.equals(fieldname)) {
                    nbWordAnnos = jp.nextIntValue(-1);
                } else if (NB_SPOTTED_TERMS.equals(fieldname)) {
                    nbSpottedTerms = jp.nextIntValue(-1);
                } else if (CORPUS_ID.equals(fieldname)) {
                    corpusID = jp.nextTextValue();
                } else if (OCCURRENCE_STORAGE.equals(fieldname)) {
                    occurrenceStorage = jp.nextTextValue();
                } else if (OCCURRENCE_MONGODB_STORE_URI.equals(fieldname)) {
                    occurrenceStoreURI = jp.nextTextValue();
                }
            }
            Preconditions.checkState(lang != null, "The property meta.lang must be defined");
            Preconditions.checkState(termIndexName != null, "The property meta.name must be defined");

            if (occurrenceStorage != null && occurrenceStorage.equals(OCCURRENCE_STORAGE_MONGODB)) {
                Preconditions.checkNotNull(occurrenceStoreURI,
                        "Missing attribute " + OCCURRENCE_MONGODB_STORE_URI);
                occurrenceStore = new MongoDBOccurrenceStore(occurrenceStoreURI, OccurrenceStore.State.INDEXED);
            } else
                occurrenceStore = new MemoryOccurrenceStore();

            termIndex = new MemoryTermIndex(termIndexName, lang, occurrenceStore);
            if (corpusID != null)
                termIndex.setCorpusId(corpusID);
            if (nbWordAnnos != -1)
                termIndex.setWordAnnotationsNum(nbWordAnnos);
            if (nbSpottedTerms != -1)
                termIndex.setSpottedTermsNum(nbSpottedTerms);

            if (options.isMetadataOnly())
                return termIndex;

        } else if (WORDS.equals(fieldname)) {
            jp.nextToken();
            while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                WordBuilder wordBuilder = WordBuilder.start();
                while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                    fieldname = jp.getCurrentName();
                    if (LEMMA.equals(fieldname))
                        wordBuilder.setLemma(jp.nextTextValue());
                    else if (COMPOUND_TYPE.equals(fieldname))
                        wordBuilder.setCompoundType(CompoundType.fromName(jp.nextTextValue()));
                    else if (STEM.equals(fieldname))
                        wordBuilder.setStem(jp.nextTextValue());
                    else if (COMPONENTS.equals(fieldname)) {
                        while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                            while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                                fieldname = jp.getCurrentName();
                                if (LEMMA.equals(fieldname))
                                    compLemma = jp.nextTextValue();
                                else if (BEGIN.equals(fieldname))
                                    begin = jp.nextIntValue(-2);
                                else if (END.equals(fieldname))
                                    end = jp.nextIntValue(-2);
                            }
                            wordBuilder.addComponent(begin, end, compLemma);
                        }
                    }
                }
                try {
                    termIndex.addWord(wordBuilder.create());
                } catch (Exception e) {
                    LOGGER.error("Could not add word " + wordBuilder.getLemma() + " to term index", e);
                    LOGGER.warn("Error ignored, trying ton continue the loading of TermIndex");
                }
            }
        } else if (TERMS.equals(fieldname)) {
            jp.nextToken();
            while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                TermBuilder builder = TermBuilder.start(termIndex);
                List<TempVecEntry> currentContextVector = Lists.newArrayList();
                int currentTermId = -1;
                while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                    fieldname = jp.getCurrentName();
                    if (GROUPING_KEY.equals(fieldname))
                        builder.setGroupingKey(jp.nextTextValue());
                    else if (SPOTTING_RULE.equals(fieldname))
                        builder.setSpottingRule(jp.nextTextValue());
                    else if (ID.equals(fieldname)) {
                        currentTermId = jp.nextIntValue(-2);
                        builder.setId(currentTermId);
                    } else if (RANK.equals(fieldname)) {
                        builder.setRank(jp.nextIntValue(-1));
                    } else if (FREQUENCY.equals(fieldname)) {
                        builder.setFrequency(jp.nextIntValue(-1));
                    } else {
                        if (FREQ_NORM.equals(fieldname)) {
                            jp.nextToken();
                            builder.setFrequencyNorm((double) jp.getFloatValue());
                        } else if (SPECIFICITY.equals(fieldname)) {
                            jp.nextToken();
                            builder.setSpecificity((double) jp.getDoubleValue());
                        } else if (GENERAL_FREQ_NORM.equals(fieldname)) {
                            jp.nextToken();
                            builder.setGeneralFrequencyNorm((double) jp.getFloatValue());
                        } else if (WORDS.equals(fieldname)) {
                            while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                                wordLemma = null;
                                syntacticLabel = null;
                                while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                                    fieldname = jp.getCurrentName();
                                    if (LEMMA.equals(fieldname))
                                        wordLemma = jp.nextTextValue();
                                    else if (SYN.equals(fieldname))
                                        syntacticLabel = jp.nextTextValue();
                                }
                                Preconditions.checkArgument(wordLemma != null, MSG_EXPECT_PROP_FOR_TERM_WORD,
                                        LEMMA);
                                Preconditions.checkArgument(syntacticLabel != null,
                                        MSG_EXPECT_PROP_FOR_TERM_WORD, SYN);
                                builder.addWord(termIndex.getWord(wordLemma), syntacticLabel);
                            } // end words

                        } else if (OCCURRENCES.equals(fieldname)) {
                            tok = jp.nextToken();
                            if (tok == JsonToken.START_ARRAY) {

                                while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                                    begin = -1;
                                    end = -1;
                                    fileSource = -1;
                                    text = null;
                                    while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                                        fieldname = jp.getCurrentName();
                                        if (BEGIN.equals(fieldname))
                                            begin = jp.nextIntValue(-1);
                                        else if (TEXT.equals(fieldname))
                                            text = jp.nextTextValue();
                                        else if (END.equals(fieldname))
                                            end = jp.nextIntValue(-1);
                                        else if (FILE.equals(fieldname)) {
                                            fileSource = jp.nextIntValue(-1);
                                        }
                                    }

                                    Preconditions.checkArgument(begin != -1, MSG_EXPECT_PROP_FOR_OCCURRENCE,
                                            BEGIN);
                                    Preconditions.checkArgument(end != -1, MSG_EXPECT_PROP_FOR_OCCURRENCE, END);
                                    Preconditions.checkArgument(fileSource != -1,
                                            MSG_EXPECT_PROP_FOR_OCCURRENCE, FILE);
                                    Preconditions.checkNotNull(inputSources.get(fileSource),
                                            "No file source with id: %s", fileSource);
                                    Preconditions.checkNotNull(text, MSG_EXPECT_PROP_FOR_OCCURRENCE, TEXT);
                                    if (occurrenceStore.getStoreType() == OccurrenceStore.Type.MEMORY)
                                        builder.addOccurrence(begin, end,
                                                termIndex.getDocument(inputSources.get(fileSource)), text);
                                }
                            }
                            // end occurrences
                        } else if (CONTEXT.equals(fieldname)) {
                            @SuppressWarnings("unused")
                            int totalCooccs = 0;
                            while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                                fieldname = jp.getCurrentName();
                                if (TOTAL_COOCCURRENCES.equals(fieldname))
                                    /*
                                     * value never used since the total will 
                                     * be reincremented in the contextVector
                                     */
                                    totalCooccs = jp.nextIntValue(-1);
                                else if (CO_OCCURRENCES.equals(fieldname)) {
                                    jp.nextToken();
                                    while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                                        TempVecEntry entry = new TempVecEntry();
                                        while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                                            fieldname = jp.getCurrentName();
                                            if (NB_COCCS.equals(fieldname))
                                                entry.setNbCooccs(jp.nextIntValue(-1));
                                            else if (ASSOC_RATE.equals(fieldname)) {
                                                jp.nextToken();
                                                entry.setAssocRate(jp.getFloatValue());
                                            } else if (CO_TERM.equals(fieldname))
                                                entry.setTermGroupingKey(jp.nextTextValue());
                                            else if (FILE.equals(fieldname)) {
                                                fileSource = jp.nextIntValue(-1);
                                            }
                                        }
                                        currentContextVector.add(entry);
                                    }
                                }
                            }
                        }
                    } //end if fieldname

                } // end term object
                try {
                    builder.createAndAddToIndex();
                } catch (Exception e) {
                    LOGGER.error("Could not add term " + builder.getGroupingKey() + " to term index", e);
                    LOGGER.warn("Error ignored, trying ton continue the loading of TermIndex");
                }

                if (options.isWithContexts())
                    contextVectors.put(currentTermId, currentContextVector);

            } // end array of terms

        } else if (INPUT_SOURCES.equals(fieldname)) {
            jp.nextToken();
            while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                String id = jp.getCurrentName();
                try {
                    inputSources.put(Integer.parseInt(id), jp.nextTextValue());
                } catch (NumberFormatException e) {
                    IOUtils.closeQuietly(jp);
                    throw new IllegalArgumentException("Bad format for input source key: " + id);
                }
            }
        } else if (TERM_VARIATIONS.equals(fieldname)) {
            jp.nextToken();
            while ((tok = jp.nextToken()) != JsonToken.END_ARRAY) {
                base = null;
                variant = null;
                infoToken = null;
                variantType = null;
                variantScore = 0;
                while ((tok = jp.nextToken()) != JsonToken.END_OBJECT) {
                    fieldname = jp.getCurrentName();
                    if (BASE.equals(fieldname))
                        base = jp.nextTextValue();
                    else if (VARIANT.equals(fieldname))
                        variant = jp.nextTextValue();
                    else if (VARIANT_TYPE.equals(fieldname))
                        variantType = jp.nextTextValue();
                    else if (VARIANT_SCORE.equals(fieldname)) {
                        jp.nextToken();
                        variantScore = jp.getDoubleValue();
                    } else if (INFO.equals(fieldname))
                        infoToken = jp.nextTextValue();
                } // end syntactic variant object
                Preconditions.checkNotNull(base, MSG_EXPECT_PROP_FOR_VAR, BASE);
                Preconditions.checkNotNull(variant, MSG_EXPECT_PROP_FOR_VAR, VARIANT);
                Preconditions.checkNotNull(infoToken, MSG_EXPECT_PROP_FOR_VAR, INFO);
                b = termIndex.getTermByGroupingKey(base);
                v = termIndex.getTermByGroupingKey(variant);
                if (b != null && v != null) {

                    VariationType vType = VariationType.fromShortName(variantType);

                    TermVariation tv = new TermVariation(vType, b, v,
                            vType == VariationType.GRAPHICAL ? Double.parseDouble(infoToken) : infoToken);
                    tv.setScore(variantScore);
                    b.addTermVariation(tv);
                } else {
                    if (b == null)
                        LOGGER.warn("Could not build variant because term \"{}\" was not found.", base);
                    if (v == null)
                        LOGGER.warn("Could not build variant because term \"{}\" was not found.", variant);
                }

                //               Preconditions.checkNotNull(b, MSG_TERM_DOES_NOT_EXIST, base);
                //               Preconditions.checkNotNull(v, MSG_TERM_DOES_NOT_EXIST, variant);

            } // end syntactic variations array
        }
    }
    jp.close();

    if (options.isWithContexts()) {
        /*
         *  map term ids with terms in context vectors and
         *  set context vectors
         */
        List<TempVecEntry> currentTempVecList;
        Term term = null;
        Term coTerm = null;
        ContextVector contextVector;
        for (int termId : contextVectors.keySet()) {
            currentTempVecList = contextVectors.get(termId);
            term = termIndex.getTermById(termId);
            contextVector = new ContextVector(term);
            for (TempVecEntry tempVecEntry : currentTempVecList) {
                coTerm = termIndex.getTermByGroupingKey(tempVecEntry.getTermGroupingKey());
                contextVector.addEntry(coTerm, tempVecEntry.getNbCooccs(), tempVecEntry.getAssocRate());
            }
            if (!contextVector.getEntries().isEmpty())
                term.setContextVector(contextVector);
        }
    }

    return termIndex;
}