/** * Parse a {@link DataMap} to obtain a {@link Name}. * * Return an empty {@link Name} (see {@link Name#isEmpty()}) if * a name cannot be obtained from the {@link DataMap}. * * @param map to parse. * @param nameKey is the key used to find the name in the map. * @param currentNamespace is the current namespace. * @return a {@link Name} parsed from the {@link DataMap}. */ protected Name getNameFromDataMap(DataMap map, String nameKey, String currentNamespace) { String nameString = getString(map, nameKey, true); String namespaceString = getString(map, NAMESPACE_KEY, false); Name name = getName(nameString, namespaceString, currentNamespace); // associate a name with a location, // this allows error messages such re-definition of a name to include a location. addToDataLocationMap(name, lookupDataLocation(nameString)); return name; }
/** * Bind name and aliases to {@link NamedDataSchema}. * * @param name to bind. * @param aliasNames to bind. * @param schema to be bound to the name. * @return true if all names are bound to the specified {@link NamedDataSchema}. */ protected boolean bindNameToSchema(Name name, List<Name> aliasNames, NamedDataSchema schema) { boolean ok = true; ok &= bindNameToSchema(name, schema); if (aliasNames != null) { for (Name aliasName : aliasNames) { ok &= bindNameToSchema(aliasName, schema); } } return ok; }
/** * Return the location of an object in the input source. * * @param object provides the object. * @return the location of the object specified. */ protected DataLocation lookupDataLocation(Object object) { return dataLocationMap().get(object); }
/** * Start an error message by appending the location of the object (if available) to * {@link #errorMessageBuilder()}. * * If a location is not known for the specified object, the {@link #errorMessageBuilder()} * is not modified. * * @param object that to use to lookup for a location to append to {@link #errorMessageBuilder()}. * @return {@link #errorMessageBuilder()}. */ protected StringBuilder startErrorMessage(Object object) { if (object != null) { DataLocation dataLocation = lookupDataLocation(object); if (dataLocation != null) { errorMessageBuilder().append(dataLocation).append(": "); } } return errorMessageBuilder(); }
String fullName = computeFullName(name); DataSchema found = lookupName(fullName); if (found == null && !name.equals(fullName)) found = lookupName(name); StringBuilder sb = startErrorMessage(name).append("\"").append(name).append("\""); if (!name.equals(fullName))
startErrorMessage(name).append("\"").append(fullName).append("\" is a pre-defined type and cannot be redefined.\n"); ok = false; DataSchema found = getResolver().existingDataSchema(name.getFullName()); if (found != null) startErrorMessage(name).append("\"").append(name.getFullName()).append("\" already defined as " + found + ".\n"); ok = false; getResolver().bindNameToSchema(name, schema, getLocation());
public static PegasusSchemaParser parserForFile(File schemaSourceFile, DataSchemaResolver resolver) { return parserForFileExtension(FileUtil.getExtension(schemaSourceFile), resolver); }
/** * Parse an {@link InputStream} containing JSON to a list of Data objects. * * @param inputStream containing JSON. */ protected List<Object> jsonInputStreamToObjects(InputStream inputStream) { List<Object> objects; try { objects = _codec.parse(inputStream, errorMessageBuilder(), dataLocationMap()); } catch (IOException e) { errorMessageBuilder().append(e).append("\n"); e.printStackTrace(); return Collections.emptyList(); } return objects; }
/** * Look for {@link DataSchema} with the specified name. * * @param fullName to lookup. * @return the {@link DataSchema} if lookup was successful else return null. */ public DataSchema lookupName(String fullName) { DataSchema schema = DataSchemaUtil.typeStringToPrimitiveDataSchema(fullName); if (schema == null) { schema = getResolver().findDataSchema(fullName, errorMessageBuilder()); } return schema; }
DataList dataList = getDataList(map, key, required); List<String> list = null; if (dataList != null) startErrorMessage(o).append(o).append(" is not a string.\n");
n.setName(name, startCalleeMessageBuilder()); appendCalleeMessage(name); n.setName(name, namespace, startCalleeMessageBuilder()); appendCalleeMessage(name);
if (result.isValid() == false) startErrorMessage(value). append("Default value ").append(value). append(" of field \"").append(field.getName()). append("\" declared in record \"").append(recordSchema.getFullName()). append("\" failed validation.\n"); MessageUtil.appendMessages(errorMessageBuilder(), result.getMessages());
/** * Return the error message from parsing. * * @return the error message. */ public String errorMessage() { return errorMessageBuilder().toString(); }
/** * Compute the full name from a name. * * If the name identifies a primitive type, return the name. * If the name is unqualified, the full name is computed by * pre-pending the current namespace and "." to the input name. * If the name is a full name, i.e. it contains a ".", then * return the name. * * @param name as input to compute the full name. * @return the computed full name. */ public String computeFullName(String name) { String fullname; DataSchema schema = DataSchemaUtil.typeStringToPrimitiveDataSchema(name); if (schema != null) { fullname = name; } else if (Name.isFullName(name) || getCurrentNamespace().isEmpty()) { fullname = name; } else { fullname = getCurrentNamespace() + "." + name; } return fullname; }
private void translateFile(File sourceFile, File destinationFile, String schemaFullname) throws IOException { // When translating files 1:1, a new resolver and parser are required for each file translated // so that a single top level output schema is matched to each input file. MultiFormatDataSchemaResolver resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(_resolverPath); PegasusSchemaParser parser = AbstractSchemaParser.parserForFileExtension(_sourceFormat, resolver); parser.parse(new FileInputStream(sourceFile)); checkForErrors(_resolverPath, sourceFile, schemaFullname, parser); List<DataSchema> topLevelSchemas = parser.topLevelDataSchemas(); if (topLevelSchemas.size() == 1) { DataSchema schema = topLevelSchemas.get(0); String encoded = encode(schema, _destFormat); _log.debug("Writing " + destinationFile.getAbsolutePath()); FileUtils.writeStringToFile(destinationFile, encoded); } else { _log.error("Expected one top level schema for " + destinationFile.getAbsolutePath() + " but got " + topLevelSchemas.size()); } }
/** * Parse an {@link Reader} containing JSON to a list of Data objects. * * @param reader containing JSON. */ protected List<Object> jsonReaderToObjects(Reader reader) { List<Object> objects; try { objects = _codec.parse(reader, errorMessageBuilder(), dataLocationMap()); } catch (IOException e) { errorMessageBuilder().append(e).append("\n"); e.printStackTrace(); return Collections.emptyList(); } return objects; }
/** * Return whether any error occurred during parsing. * * @return true if at least one error occurred during parsing. */ public boolean hasError() { return errorMessageBuilder().length() != 0; }
/** * Add a new mapping to the map of Data object to their locations in the input source. * * The new mapping is added only if both arguments are not {@code null}. * * @param object provides the object. * @param dataLocation provides the location associated with the object. */ protected void addToDataLocationMap(Object object, DataLocation dataLocation) { if (object != null && dataLocation != null) { dataLocationMap().put(object, dataLocation); } }