Search in sources :

Example 1 with DataSchemaLocation

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;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayList(java.util.ArrayList) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

Example 2 with DataSchemaLocation

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;
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) InJarFileDataSchemaLocation(com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JarFile(java.util.jar.JarFile) File(java.io.File) FileUtil(com.linkedin.util.FileUtil) Map(java.util.Map)

Example 3 with DataSchemaLocation

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);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InJarFileDataSchemaLocation(com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation)

Example 4 with DataSchemaLocation

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);
}
Also used : DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) Map(java.util.Map) File(java.io.File)

Example 5 with DataSchemaLocation

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);
        }
    }
}
Also used : Map(java.util.Map) File(java.io.File) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation)

Aggregations

DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)10 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)9 DataSchema (com.linkedin.data.schema.DataSchema)6 FileDataSchemaLocation (com.linkedin.data.schema.resolver.FileDataSchemaLocation)5 File (java.io.File)5 InputStream (java.io.InputStream)4 Map (java.util.Map)4 InJarFileDataSchemaLocation (com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation)3 IOException (java.io.IOException)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 FileInputStream (java.io.FileInputStream)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)1 SchemaParser (com.linkedin.data.schema.SchemaParser)1 DataTemplate (com.linkedin.data.template.DataTemplate)1 TyperefInfo (com.linkedin.data.template.TyperefInfo)1 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)1 FileUtil (com.linkedin.util.FileUtil)1 FileCodeWriter (com.sun.codemodel.writer.FileCodeWriter)1 FilterInputStream (java.io.FilterInputStream)1