Exemple #1
0
  /** @return whether or not the MetaDD should be written out at the end. */
  private static boolean processMetaDD(Config config) throws IOException {
    boolean needToWriteMetaDD = false;
    MetaDD mdd;
    //
    //	Either creates the metaDD file or read the existing one
    //
    if (config.getMddFile() != null || config.getMddIn() != null) {
      File file = config.getMddFile();

      if (config.getMddIn() == null && !file.exists()) {
        config.setDoGeneration(false);
        if (!config.isAuto()) {
          if (!askYesOrNo(
              config.messageOut,
              "The mdd file "
                  + config.getMddFile()
                  + // NOI18N
                  " doesn't exist. Should we create it (y/n)? ")) { // NOI18N
            config.messageOut.println("Generation aborted."); // NOI18N
            return false;
          }
        }
        needToWriteMetaDD = true;
        mdd = new MetaDD();
      } else {
        try {
          InputStream is = config.getMddIn();
          if (config.getMddIn() == null) {
            is = new FileInputStream(config.getMddFile());
            config.messageOut.println(Common.getMessage("MSG_UsingMdd", config.getMddFile()));
          }
          mdd = MetaDD.read(is);
          if (config.getMddIn() == null) {
            is.close();
          }
        } catch (IOException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        } catch (javax.xml.parsers.ParserConfigurationException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        } catch (org.xml.sax.SAXException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        }
      }
    } else {
      // Create a MetaDD to look stuff up in later on, even though
      // it wasn't read in, and we're not going to write it out.
      mdd = new MetaDD();
    }
    config.setMetaDD(mdd);
    return needToWriteMetaDD;
  }
Exemple #2
0
  private static void calculateNewestSourceTime(Config config) {
    if (config.getFilename() != null) {
      config.setIfNewerSourceTime(config.getFilename().lastModified());
    }
    if (config.getMddFile() != null) {
      config.setIfNewerSourceTime(config.getMddFile().lastModified());
    }
    for (Iterator it = config.readBeanGraphFiles(); it.hasNext(); ) {
      File f = (File) it.next();
      config.setIfNewerSourceTime(f.lastModified());
    }

    // Need to also check the times on schema2beans.jar & schema2beansdev.jar
    config.setIfNewerSourceTime(getLastModified(org.netbeans.modules.schema2beans.BaseBean.class));
    config.setIfNewerSourceTime(getLastModified(BeanClass.class));
    config.setIfNewerSourceTime(getLastModified(GenBeans.class));
    config.setIfNewerSourceTime(getLastModified(BeanBuilder.class));
    config.setIfNewerSourceTime(getLastModified(TreeBuilder.class));
    config.setIfNewerSourceTime(getLastModified(GraphLink.class));
    config.setIfNewerSourceTime(getLastModified(GraphNode.class));
    config.setIfNewerSourceTime(getLastModified(JavaBeanClass.class));
    // System.out.println("config.getNewestSourceTime="+config.getNewestSourceTime());
  }
Exemple #3
0
  public static void doIt(Config config) throws java.io.IOException, Schema2BeansException {
    normalizeConfig(config);

    calculateNewestSourceTime(config);

    boolean needToWriteMetaDD = processMetaDD(config);

    //  The class that build the DTD object graph
    TreeBuilder tree = new TreeBuilder(config);

    //  The file parser calling back the handler
    SchemaParser parser = null;
    boolean tryAgain;
    int schemaType = config.getSchemaTypeNum();
    SchemaParseException lastException = null;
    do {
      tryAgain = false;
      if (schemaType == Config.XML_SCHEMA) {
        XMLSchemaParser xmlSchemaParser = new XMLSchemaParser(config, tree);
        if (config.getInputURI() != null) xmlSchemaParser.setInputURI(config.getInputURI());
        parser = xmlSchemaParser;
      } else {
        parser = new DocDefParser(config, tree);
      }
      readBeanGraphs(config);

      try {
        //  parse the DTD, building the object graph
        parser.process();
      } catch (SchemaParseException e) {
        if (schemaType == Config.DTD) {
          // Retry as XML Schema
          tryAgain = true;
          schemaType = Config.XML_SCHEMA;
        } else {
          if (lastException == null) throw e;
          else throw lastException;
        }
        lastException = e;
      }
    } while (tryAgain);
    config.setSchemaTypeNum(schemaType);

    // Build the beans from the graph, and code generate them out to disk.
    BeanBuilder builder = new BeanBuilder(tree, config, config.getCodeGeneratorFactory());
    builder.process();

    if (needToWriteMetaDD) {
      try {
        config.messageOut.println("Writing metaDD XML file"); // NOI18N
        FileOutputStream mddOut = new FileOutputStream(config.getMddFile());
        config.getMetaDD().write(mddOut);
        mddOut.close();
      } catch (IOException e) {
        config.messageOut.println("Failed to write the mdd file: " + e.getMessage()); // NOI18N
        throw e;
      }
    }

    if (config.isDoCompile()
        && config.getOutputStreamProvider() instanceof DefaultOutputStreamProvider) {
      DefaultOutputStreamProvider out =
          (DefaultOutputStreamProvider) config.getOutputStreamProvider();
      String[] javacArgs = new String[out.getGeneratedFiles().size()];
      int javaFileCount = 0;
      for (Iterator it = out.getGeneratedFiles().iterator(); it.hasNext(); ) {
        javacArgs[javaFileCount] = (String) it.next();
        ++javaFileCount;
      }

      if (javaFileCount == 0) {
        if (!config.isQuiet()) config.messageOut.println(Common.getMessage("MSG_NothingToCompile"));
      } else {
        if (!config.isQuiet()) config.messageOut.println(Common.getMessage("MSG_Compiling"));
        try {
          Class javacClass = Class.forName("com.sun.tools.javac.Main");
          java.lang.reflect.Method compileMethod =
              javacClass.getDeclaredMethod(
                  "compile", new Class[] {String[].class, PrintWriter.class});
          // com.sun.tools.javac.Main.compile(javacArgs, pw);
          PrintWriter pw = new PrintWriter(config.messageOut, true);
          Object result = compileMethod.invoke(null, new Object[] {javacArgs, pw});
          pw.flush();
          int compileExitCode = 0;
          if (result instanceof Integer) compileExitCode = ((Integer) result).intValue();
          if (compileExitCode != 0)
            throw new RuntimeException(
                "Compile errors: javac had an exit code of " + compileExitCode);
        } catch (java.lang.Exception e) {
          // Maybe it's just a missing $JRE/tools.jar from
          // the CLASSPATH.
          // config.messageOut.println(Common.getMessage("MSG_UnableToCompile"));
          // config.messageOut.println(e.getClass().getName()+": "+e.getMessage());	// NOI18N
          // e.printStackTrace();
          if (e instanceof IOException) throw (IOException) e;
          if (e instanceof Schema2BeansException) throw (Schema2BeansException) e;
          throw new Schema2BeansNestedException(Common.getMessage("MSG_UnableToCompile"), e);
        }
      }
    }
  }