Search in sources :

Example 1 with TransformSpecificationMissingArgumentException

use of org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method getMatch.

private TypedEntityMatch getMatch(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    if (!json.has(ARG_MATCH)) {
        throw new TransformSpecificationMissingArgumentException(line, ARG_MATCH);
    }
    JSONObject match = json.getJSONObject(ARG_MATCH);
    if (match.has(ARG_COLLECTION)) {
        return getCollectionMatch(line, match.getString(ARG_COLLECTION), match);
    }
    Class<?> entityType = getEntityClassFromJsonSpec(line, match);
    if (entityType == null) {
        throw new TransformSpecificationMissingArgumentException(line, new String[] { ARG_FILE, ARG_CLASS }, ARG_MATCH);
    }
    Map<String, DeferredValueMatcher> propertyMatches = getPropertyValueMatchersFromJsonObject(match, _excludeForMatchSpec);
    List<EntityMatch> matches = new ArrayList<EntityMatch>();
    for (Map.Entry<String, DeferredValueMatcher> entry : propertyMatches.entrySet()) {
        String property = entry.getKey();
        Matcher m = _anyMatcher.matcher(property);
        if (m.matches()) {
            PropertyPathCollectionExpression expression = new PropertyPathCollectionExpression(m.group(1));
            expression.setPropertyMethodResolver(_propertyMethodResolver);
            matches.add(new PropertyAnyValueEntityMatch(expression, entry.getValue()));
        } else {
            PropertyPathExpression expression = new PropertyPathExpression(property);
            expression.setPropertyMethodResolver(_propertyMethodResolver);
            matches.add(new PropertyValueEntityMatch(expression, entry.getValue()));
        }
    }
    return new TypedEntityMatch(entityType, new EntityMatchCollection(matches));
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityMatchCollection(org.onebusaway.gtfs_transformer.match.EntityMatchCollection) Matcher(java.util.regex.Matcher) DeferredValueMatcher(org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher) ArrayList(java.util.ArrayList) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityMatch(org.onebusaway.gtfs_transformer.match.EntityMatch) PropertyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch) PropertyAnyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyAnyValueEntityMatch) PropertyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch) PropertyPathCollectionExpression(org.onebusaway.collections.beans.PropertyPathCollectionExpression) PropertyAnyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyAnyValueEntityMatch) JSONObject(org.json.JSONObject) DeferredValueMatcher(org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) PropertyPathExpression(org.onebusaway.collections.beans.PropertyPathExpression) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with TransformSpecificationMissingArgumentException

use of org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method handleTransformOperation.

private void handleTransformOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    if (!json.has(ARG_CLASS)) {
        throw new TransformSpecificationMissingArgumentException(line, ARG_CLASS);
    }
    String value = json.getString(ARG_CLASS);
    Object factoryObj = null;
    try {
        Class<?> clazz = Class.forName(value);
        factoryObj = clazz.newInstance();
    } catch (Exception ex) {
        throw new TransformSpecificationException("error instantiating class: " + value, ex, line);
    }
    handleTransformOperation(line, json, factoryObj);
}
Also used : TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) JSONObject(org.json.JSONObject) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException) JSONException(org.json.JSONException) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException) MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) IOException(java.io.IOException) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)

Example 3 with TransformSpecificationMissingArgumentException

use of org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method handleTrimOperation.

private void handleTrimOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    TypedEntityMatch match = getMatch(line, json);
    if (match.getType() != Trip.class) {
        throw new TransformSpecificationException("the trim_trip op only supports matching against trips", line);
    }
    TrimTripTransformStrategy strategy = getStrategy(TrimTripTransformStrategy.class);
    TrimOperation operation = new TrimTripTransformStrategy.TrimOperation();
    operation.setMatch(match);
    if (json.has("to_stop_id")) {
        operation.setToStopId(json.getString("to_stop_id"));
    }
    if (json.has("from_stop_id")) {
        operation.setFromStopId(json.getString("from_stop_id"));
    }
    if (operation.getToStopId() == null && operation.getFromStopId() == null) {
        throw new TransformSpecificationMissingArgumentException(line, new String[] { "to_stop_id", "from_stop_id" });
    }
    strategy.addOperation(operation);
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) TrimTripTransformStrategy(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException)

Example 4 with TransformSpecificationMissingArgumentException

use of org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method setObjectPropertiesFromJsonUsingCsvFields.

private void setObjectPropertiesFromJsonUsingCsvFields(Object object, JSONObject json, String line) throws JSONException, TransformSpecificationMissingArgumentException {
    EntitySchemaFactory entitySchemaFactory = _transformer.getReader().getEntitySchemaFactory();
    EntitySchema schema = entitySchemaFactory.getSchema(object.getClass());
    BeanWrapper wrapped = BeanWrapperFactory.wrap(object);
    Map<String, Object> values = new HashMap<String, Object>();
    for (Iterator<?> it = json.keys(); it.hasNext(); ) {
        String key = (String) it.next();
        Object v = json.get(key);
        if (v instanceof JSONArray) {
            JSONArray array = (JSONArray) v;
            List<Object> asList = new ArrayList<Object>();
            for (int i = 0; i < array.length(); ++i) {
                asList.add(array.get(i));
            }
            v = asList;
        }
        values.put(key, v);
    }
    CsvEntityContext context = new CsvEntityContextImpl();
    for (FieldMapping mapping : schema.getFields()) {
        try {
            mapping.translateFromCSVToObject(context, values, wrapped);
        } catch (MissingRequiredFieldException ex) {
            String verboseMessage = "line=" + line + ", context=" + context + ", json=" + json + ", object=" + object;
            _log.error("missing required field; details:" + verboseMessage);
            throw new TransformSpecificationMissingArgumentException(verboseMessage, ex.getFieldName());
        }
    }
}
Also used : MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) HashMap(java.util.HashMap) FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) SingleFieldMapping(org.onebusaway.csv_entities.schema.SingleFieldMapping) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) CsvEntityContextImpl(org.onebusaway.csv_entities.CsvEntityContextImpl) BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) EntitySchemaFactory(org.onebusaway.csv_entities.schema.EntitySchemaFactory) CsvEntityContext(org.onebusaway.csv_entities.CsvEntityContext) JSONObject(org.json.JSONObject) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema)

Example 5 with TransformSpecificationMissingArgumentException

use of org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method addModificationsFromReader.

public void addModificationsFromReader(BufferedReader reader) throws IOException, TransformSpecificationException {
    String line = null;
    while ((line = reader.readLine()) != null) {
        try {
            line = line.trim();
            if (line.length() == 0 || line.startsWith("#") || line.equals("{{{") || line.equals("}}}"))
                continue;
            JSONObject json = new JSONObject(line);
            if (!json.has(ARG_OP)) {
                throw new TransformSpecificationMissingArgumentException(line, ARG_OP);
            }
            String opType = json.getString(ARG_OP);
            if (opType.equals("add")) {
                handleAddOperation(line, json);
            } else if (opType.equals(ARG_UPDATE) || opType.equals("change") || opType.equals("modify")) {
                handleUpdateOperation(line, json);
            } else if (opType.equals("remove") || opType.equals("delete")) {
                handleRemoveOperation(line, json);
            } else if (opType.equals("retain")) {
                handleRetainOperation(line, json);
            } else if (opType.equals("subsection")) {
                handleSubsectionOperation(line, json);
            } else if (opType.equals("trim_trip")) {
                handleTrimOperation(line, json);
            } else if (opType.equals("stop_times_factory")) {
                handleStopTimesOperation(line, json);
            } else if (opType.equals("calendar_extension")) {
                handleTransformOperation(line, json, new CalendarExtensionStrategy());
            } else if (opType.equals("calendar_simplification")) {
                handleTransformOperation(line, json, new CalendarSimplicationStrategy());
            } else if (opType.equals("deduplicate_service_ids")) {
                handleTransformOperation(line, json, new DeduplicateServiceIdsStrategy());
            } else if (opType.equals("shift_negative_stop_times")) {
                handleTransformOperation(line, json, new ShiftNegativeStopTimesUpdateStrategy());
            } else if (opType.equals("shape_direction")) {
                handleTransformOperation(line, json, new ShapeDirectionTransformStrategy());
            } else if (opType.equals("transform")) {
                handleTransformOperation(line, json);
            } else {
                throw new TransformSpecificationException("unknown transform op \"" + opType + "\"", line);
            }
        } catch (JSONException ex) {
            throw new TransformSpecificationException("error parsing json", ex, line);
        }
    }
}
Also used : ShiftNegativeStopTimesUpdateStrategy(org.onebusaway.gtfs_transformer.updates.ShiftNegativeStopTimesUpdateStrategy) JSONObject(org.json.JSONObject) CalendarSimplicationStrategy(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationStrategy) ShapeDirectionTransformStrategy(org.onebusaway.gtfs_transformer.updates.ShapeDirectionTransformStrategy) JSONException(org.json.JSONException) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) DeduplicateServiceIdsStrategy(org.onebusaway.gtfs_transformer.updates.DeduplicateServiceIdsStrategy) CalendarExtensionStrategy(org.onebusaway.gtfs_transformer.updates.CalendarExtensionStrategy) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException)

Aggregations

TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)6 JSONObject (org.json.JSONObject)5 TransformSpecificationException (org.onebusaway.gtfs_transformer.TransformSpecificationException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JSONException (org.json.JSONException)2 MissingRequiredFieldException (org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException)2 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)2 IOException (java.io.IOException)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 JSONArray (org.json.JSONArray)1 PropertyPathCollectionExpression (org.onebusaway.collections.beans.PropertyPathCollectionExpression)1 PropertyPathExpression (org.onebusaway.collections.beans.PropertyPathExpression)1 CsvEntityContext (org.onebusaway.csv_entities.CsvEntityContext)1 CsvEntityContextImpl (org.onebusaway.csv_entities.CsvEntityContextImpl)1 BeanWrapper (org.onebusaway.csv_entities.schema.BeanWrapper)1 EntitySchema (org.onebusaway.csv_entities.schema.EntitySchema)1 EntitySchemaFactory (org.onebusaway.csv_entities.schema.EntitySchemaFactory)1 FieldMapping (org.onebusaway.csv_entities.schema.FieldMapping)1