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));
}
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);
}
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);
}
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());
}
}
}
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);
}
}
}
Aggregations