use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.
the class AvroSchemaGenerator method targetFiles.
protected List<File> targetFiles(File targetDirectory) {
ArrayList<File> generatedFiles = new ArrayList<File>();
DataSchemaResolver resolver = getSchemaResolver();
Map<String, DataSchemaLocation> nameToLocations = resolver.nameToDataSchemaLocations();
Map<String, NamedDataSchema> nameToSchema = resolver.bindings();
for (Map.Entry<String, DataSchemaLocation> entry : nameToLocations.entrySet()) {
String fullName = entry.getKey();
DataSchemaLocation location = entry.getValue();
if (_sourceLocations.contains(location) || _sources.contains(fullName)) {
NamedDataSchema schema = nameToSchema.get(fullName);
if (schema instanceof RecordDataSchema) {
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
File generatedFile = fileForAvroSchema(fullName, targetDirectory);
generatedFiles.add(generatedFile);
String preTranslateSchemaText = recordDataSchema.toString();
String avroSchemaText = SchemaTranslator.dataToAvroSchemaJson(recordDataSchema, _options);
_fileToAvroSchemaMap.put(generatedFile, avroSchemaText);
String postTranslateSchemaText = recordDataSchema.toString();
assert (preTranslateSchemaText.equals(postTranslateSchemaText));
}
}
}
return generatedFiles;
}
use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.
the class FileFormatDataSchemaParser method parseSources.
public DataSchemaParser.ParseResult parseSources(String[] sources) throws IOException {
final DataSchemaParser.ParseResult result = new DataSchemaParser.ParseResult();
try {
for (String source : sources) {
final File sourceFile = new File(source);
if (sourceFile.exists()) {
if (sourceFile.isDirectory()) {
final FileUtil.FileExtensionFilter filter = new FileUtil.FileExtensionFilter(_schemaParserFactory.getLanguageExtension());
final List<File> sourceFilesInDirectory = FileUtil.listFiles(sourceFile, filter);
for (File f : sourceFilesInDirectory) {
parseFile(f, result);
result.getSourceFiles().add(f);
}
} else {
if (sourceFile.getName().endsWith(".jar")) {
final JarFile jarFile = new JarFile(sourceFile);
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
if (!entry.isDirectory() && entry.getName().endsWith(_schemaParserFactory.getLanguageExtension())) {
parseJarEntry(jarFile, entry, result);
}
}
} else {
parseFile(sourceFile, result);
}
result.getSourceFiles().add(sourceFile);
}
} else {
final StringBuilder errorMessage = new StringBuilder();
final DataSchema schema = _schemaResolver.findDataSchema(source, errorMessage);
if (schema == null) {
result._messageBuilder.append("File cannot be opened or schema name cannot be resolved: ").append(source).append("\n");
}
if (errorMessage.length() > 0) {
result._messageBuilder.append(errorMessage.toString());
}
}
}
if (result._messageBuilder.length() > 0) {
throw new IOException(result.getMessage());
}
for (Map.Entry<String, DataSchemaLocation> entry : _schemaResolver.nameToDataSchemaLocations().entrySet()) {
final DataSchema schema = _schemaResolver.bindings().get(entry.getKey());
result.getSchemaAndLocations().put(schema, entry.getValue());
}
return result;
} catch (RuntimeException e) {
if (result._messageBuilder.length() > 0) {
e = new RuntimeException("Unexpected " + e.getClass().getSimpleName() + " encountered.\n" + "This may be caused by the following parsing or processing errors:\n" + result.getMessage(), e);
}
throw e;
}
}
use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.
the class FileFormatDataSchemaParser method parseFile.
/**
* Parse a source that specifies a file (not a fully qualified schema name).
*
* @param schemaSourceFile provides the source file.
* @throws IOException if there is a file access error.
*/
private void parseFile(File schemaSourceFile, DataSchemaParser.ParseResult result) throws IOException {
final DataSchemaLocation location = getSchemaLocation(schemaSourceFile);
// if a the data schema has been resolved before, must skip parsing again, because one name can't be bound to two data schemas
if (_schemaResolver.locationResolved(location)) {
return;
}
final InputStream inputStream = new SchemaFileInputStream(schemaSourceFile);
final List<DataSchema> schemas = parseSchemaStream(inputStream, location, result);
for (DataSchema schema : schemas) {
if (schema instanceof NamedDataSchema) {
validateSchemaWithPath(schemaSourceFile.getAbsolutePath(), (NamedDataSchema) schema);
}
result.getSchemaAndLocations().put(schema, location);
}
}
use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.
the class PegasusDataTemplateGenerator method run.
public static GeneratorResult run(String resolverPath, String defaultPackage, final boolean generateImported, String targetDirectoryPath, String[] sources) throws IOException {
final DataSchemaParser schemaParser = new DataSchemaParser(resolverPath);
final TemplateSpecGenerator specGenerator = new TemplateSpecGenerator(schemaParser.getSchemaResolver());
final JavaDataTemplateGenerator dataTemplateGenerator = new JavaDataTemplateGenerator(defaultPackage);
for (DataSchema predefinedSchema : JavaDataTemplateGenerator.PredefinedJavaClasses.keySet()) {
specGenerator.registerDefinedSchema(predefinedSchema);
}
final DataSchemaParser.ParseResult parseResult = schemaParser.parseSources(sources);
for (Map.Entry<DataSchema, DataSchemaLocation> entry : parseResult.getSchemaAndLocations().entrySet()) {
specGenerator.generate(entry.getKey(), entry.getValue());
}
for (ClassTemplateSpec spec : specGenerator.getGeneratedSpecs()) {
dataTemplateGenerator.generate(spec);
}
final JavaCodeUtil.PersistentClassChecker checker = new DataTemplatePersistentClassChecker(generateImported, specGenerator, dataTemplateGenerator, parseResult.getSourceFiles());
final File targetDirectory = new File(targetDirectoryPath);
final List<File> targetFiles = JavaCodeUtil.targetFiles(targetDirectory, dataTemplateGenerator.getCodeModel(), JavaCodeUtil.classLoaderFromResolverPath(schemaParser.getResolverPath()), checker);
final List<File> modifiedFiles;
if (FileUtil.upToDate(parseResult.getSourceFiles(), targetFiles)) {
modifiedFiles = Collections.emptyList();
_log.info("Target files are up-to-date: " + targetFiles);
} else {
modifiedFiles = targetFiles;
_log.info("Generating " + targetFiles.size() + " files: " + targetFiles);
validateDefinedClassRegistration(dataTemplateGenerator.getCodeModel(), dataTemplateGenerator.getGeneratedClasses().keySet());
dataTemplateGenerator.getCodeModel().build(new FileCodeWriter(targetDirectory, true));
}
return new DefaultGeneratorResult(parseResult.getSourceFiles(), targetFiles, modifiedFiles);
}
use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.
the class AbstractGenerator method appendSourceFilesFromSchemaResolver.
/**
* Append source files that were resolved through {@link DataSchemaResolver} to the provided list.
*
* @param sourceFiles to append the files that were resolved through {@link DataSchemaResolver}.
*/
protected void appendSourceFilesFromSchemaResolver(List<File> sourceFiles) {
for (Map.Entry<String, DataSchemaLocation> entry : getSchemaResolver().nameToDataSchemaLocations().entrySet()) {
DataSchemaLocation location = entry.getValue();
File sourceFile = location.getSourceFile();
if (sourceFile != null) {
sourceFiles.add(sourceFile);
}
}
}
Aggregations