示例#1
0
  /**
   * @param spec
   * @param dir
   */
  public static File generateSchema(
      final BeanModel spec, final String namespaceParam, final File fileOrDir) {
    final SchemaGenerator gen = newGenerator(fileOrDir);

    final String namespace = String.format("%s.xml", spec.getCommonNamespace());
    final Collection<SchemaElement> elements =
        Collections2.transform(
            spec.getDefinitions(),
            new Function<Definition, SchemaElement>() {
              @Override
              public SchemaElement apply(final Definition def) {
                return new SchemaElement(SchemaContext.getSchemaType(def), SchemaContext.elm(def));
              }
            });
    final SchemaContext context = new SchemaContext(spec, namespace, elements);

    final File schemaFile;
    // in our case the target directory may actually be the schema file
    if (fileOrDir.exists() && fileOrDir.isFile()) {
      schemaFile = fileOrDir;
    } else {
      final File subDir = IO.assertDirExists(new File(fileOrDir, namespace.replace('.', '/')));
      schemaFile = new File(subDir, "schema.xsd");
    }

    gen.generateFile(context, schemaFile, schemaTemplate);
    return schemaFile;
  }
示例#2
0
 private void generateFile(
     final SchemaContext context, final File targetFile, final CodeTemplate generator) {
   boolean generate = true;
   if (targetFile.exists()) {
     if (isOverwrite()) {
       info("Overwriting %s", targetFile.getName());
     } else {
       info("Skipping %s", targetFile.getName());
       generate = false;
     }
   } else {
     info("Creating %s", targetFile.getName());
   }
   if (generate) {
     final String code = generator.generate(context);
     IO.write(code, targetFile);
   }
 }