public OutputStream getStream(String dir, String name, String extension) throws java.io.IOException { String filename = getFilename(dir, name, extension); if (!config.isQuiet()) config.messageOut.println(Common.getMessage("MSG_GeneratingClass", filename)); // NOI18N generatedFiles.add(filename); // return new FileOutputStream(filename); return new WriteIfDifferentOutputStream(filename); }
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); } } } }