use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class AbstractDataSchemaResolver method locateDataSchema.
/**
* Locate a {@link NamedDataSchema} with the specified name.
*
* @param name of schema to locate.
* @param errorMessageBuilder to append error messages to.
* @return the NamedDataSchema if it can be located, else return null.
*/
protected NamedDataSchema locateDataSchema(String name, StringBuilder errorMessageBuilder) {
NamedDataSchema schema = null;
Iterator<DataSchemaLocation> locations = possibleLocations(name);
while (locations.hasNext()) {
DataSchemaLocation location = locations.next();
if (location == null || isBadLocation(location)) {
continue;
}
InputStream inputStream = null;
try {
inputStream = locationToInputStream(location, errorMessageBuilder);
if (inputStream == null) {
addBadLocation(location);
} else {
schema = parse(inputStream, location, name, errorMessageBuilder);
if (schema != null) {
break;
}
}
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException exc) {
}
}
}
}
return schema;
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class ClassNameDataSchemaResolver method locateDataSchema.
@Override
protected NamedDataSchema locateDataSchema(String className, StringBuilder errorMessageBuilder) {
final DataSchemaLocation location = new ClassNameDataSchemaLocation(className);
if (isBadLocation(location)) {
return null;
}
final Class<?> clazz;
try {
clazz = _classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
addBadLocation(location);
errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" not found", className));
return null;
}
final DataSchema schema = DataTemplateUtil.getSchema(clazz);
if (schema instanceof NamedDataSchema) {
return (NamedDataSchema) schema;
}
if (DataTemplate.class.isAssignableFrom(clazz)) {
@SuppressWarnings("unchecked") final Class<? extends DataTemplate<?>> clazzWithTyperef = (Class<? extends DataTemplate<?>>) clazz;
final TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(clazzWithTyperef);
if (typerefInfo != null) {
return typerefInfo.getSchema();
}
}
addBadLocation(location);
errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" is not a NamedDataSchema", className));
return null;
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class ClasspathResourceDataSchemaResolver method locateDataSchema.
@Override
protected NamedDataSchema locateDataSchema(String schemaName, StringBuilder errorMessageBuilder) {
NamedDataSchema schema = null;
final String schemaResourcePath = getDataSchemaResourcePath(schemaName);
try (InputStream stream = _classLoader.getResourceAsStream(schemaResourcePath)) {
if (stream == null) {
errorMessageBuilder.append(String.format("Unable to find data schema file \"%s\" in classpath.", schemaResourcePath));
} else {
DataSchemaLocation location = new FileDataSchemaLocation(new File(schemaResourcePath));
schema = parse(stream, location, schemaName, errorMessageBuilder);
}
} catch (IOException e) {
errorMessageBuilder.append(String.format("Failed to read/close data schema file \"%s\" in classpath: \"%s\"", schemaResourcePath, e.getMessage()));
}
return schema;
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parseNamedType.
private NamedDataSchema parseNamedType(NamedTypeDeclarationContext namedType) throws ParseException {
NamedDataSchema schema;
if (namedType.recordDeclaration() != null) {
schema = parseRecord(namedType, namedType.recordDeclaration());
} else if (namedType.typerefDeclaration() != null) {
schema = parseTyperef(namedType, namedType.typerefDeclaration());
} else if (namedType.fixedDeclaration() != null) {
schema = parseFixed(namedType, namedType.fixedDeclaration());
} else if (namedType.enumDeclaration() != null) {
schema = parseEnum(namedType, namedType.enumDeclaration());
} else {
throw new ParseException(namedType, "Unrecognized named type parse node: " + namedType.getText());
}
schema.setPackage(getCurrentPackage());
return schema;
}
use of com.linkedin.data.schema.NamedDataSchema in project rest.li by linkedin.
the class FileFormatDataSchemaParser method parseJarEntry.
private void parseJarEntry(JarFile schemaJarFile, JarEntry jarEntry, DataSchemaParser.ParseResult result) throws IOException {
final DataSchemaLocation location = getSchemaLocation(schemaJarFile, jarEntry.getName());
if (_schemaResolver.locationResolved(location)) {
return;
}
final InputStream jarStream = schemaJarFile.getInputStream(jarEntry);
final List<DataSchema> schemas = parseSchemaStream(jarStream, location, result);
for (DataSchema schema : schemas) {
if (schema instanceof NamedDataSchema) {
validateSchemaWithPath(location.toString(), (NamedDataSchema) schema);
}
result.getSchemaAndLocations().put(schema, location);
}
}
Aggregations