Example #1
0
 /**
  * Generates a C++ source file for classes, and compiles everything in one shared library when
  * {@code compile == true}.
  *
  * @param classes the Class objects as input to Generator
  * @param outputName the output name of the shared library
  * @return the actual File generated, either the compiled library or its source
  * @throws IOException
  * @throws InterruptedException
  */
 File generateAndCompile(Class[] classes, String outputName)
     throws IOException, InterruptedException {
   File outputFile = null, outputPath = outputDirectory;
   ClassProperties p = Loader.loadProperties(classes, properties, true);
   String platform = p.getProperty("platform");
   String sourcePrefix = new File(outputPath, outputName).getPath();
   String sourceSuffix = p.getProperty("platform.source.suffix", ".cpp");
   String libraryPath = p.getProperty("platform.library.path", "");
   String libraryName =
       p.getProperty("platform.library.prefix", "")
           + outputName
           + p.getProperty("platform.library.suffix", "");
   if (outputPath == null) {
     try {
       String resourceName =
           '/' + classes[classes.length - 1].getName().replace('.', '/') + ".class";
       String resourceURL = classes[classes.length - 1].getResource(resourceName).toString();
       File packageDir =
           new File(new URI(resourceURL.substring(0, resourceURL.lastIndexOf('/') + 1)));
       File targetDir =
           libraryPath.length() > 0
               ? new File(
                   new URI(
                       resourceURL.substring(0, resourceURL.length() - resourceName.length() + 1)))
               : new File(packageDir, platform);
       outputPath = new File(targetDir, libraryPath);
       sourcePrefix = new File(packageDir, outputName).getPath();
     } catch (URISyntaxException e) {
       throw new RuntimeException(e);
     }
   }
   if (!outputPath.exists()) {
     outputPath.mkdirs();
   }
   Generator generator = new Generator(logger, p);
   String sourceFilename = sourcePrefix + sourceSuffix;
   String headerFilename = header ? sourcePrefix + ".h" : null;
   String classPath = System.getProperty("java.class.path");
   for (String s : classScanner.getClassLoader().getPaths()) {
     classPath += File.pathSeparator + s;
   }
   logger.info("Generating " + sourceFilename);
   if (generator.generate(sourceFilename, headerFilename, classPath, classes)) {
     generator.close();
     if (compile) {
       String libraryFilename = outputPath.getPath() + File.separator + libraryName;
       logger.info("Compiling " + libraryFilename);
       int exitValue = compile(sourceFilename, libraryFilename, p);
       if (exitValue == 0) {
         new File(sourceFilename).delete();
         outputFile = new File(libraryFilename);
       } else {
         System.exit(exitValue);
       }
     } else {
       outputFile = new File(sourceFilename);
     }
   } else {
     logger.info("Nothing generated for " + sourceFilename);
   }
   return outputFile;
 }
  /**
   * Generates a compiled stylesheet from a parsed XSL document.
   *
   * @param xsl the parsed xsl document.
   * @param path the path to the document.
   */
  Templates generate(Node xsl, Path path) throws TransformerConfigurationException {
    log.fine("Generating XSL from " + path);

    // The code generation needs a static lock because the
    // application might have a separate factory object
    // for each thread.  The static lock protects the code generation
    // from overwriting its own code.
    synchronized (AbstractStylesheetFactory.class) {
      Generator gen = null;

      try {
        if (path == null && xsl != null) {
          Document doc = xsl.getOwnerDocument();
          if (doc == null && xsl instanceof Document) doc = (Document) xsl;

          DocumentType dtd = doc.getDoctype();
          String systemId = null;
          if (dtd != null) systemId = dtd.getSystemId();

          if (systemId != null) path = getStylePath().lookup(systemId);
        }

        if (path == null && xsl instanceof CauchoNode) {
          String filename = ((CauchoNode) xsl).getFilename();
          if (filename != null) path = getStylePath().lookup(filename);
        }

        if (path == null) path = getStylePath().lookup("anonymous.xsl");

        Path stylePath = path.getParent();

        Expr expr = XPath.parseExpr("//xtp:directive.page/@language");
        String language = expr.evalString(xsl);

        String userName = path.getUserPath();
        String mangledName = getMangledName(userName);

        String encoding = XPath.evalString("//xsl:output/@encoding", xsl);
        if (encoding != null && encoding.equals("")) encoding = null;

        if (language == null || language.equals("") || language.equals("java")) {
          language = "java";
          gen = new JavaGenerator(this, mangledName, encoding);
        } else throw new XslParseException(L.l("unsupported language `{0}'", language));

        gen.setPath(path);

        Iterator iter = XPath.select("//xtp:directive.page/@*", xsl);
        while (iter.hasNext()) {
          Attr attr = (Attr) iter.next();
          String name = attr.getNodeName();
          String value = attr.getNodeValue();

          if (name.equals("errorPage")) gen.setErrorPage(value);
          else if (name.equals("import")) gen.addImport(value);
          else if (name.equals("contentType")) gen.setContentType(value);
          else if (name.equals("language")) {
            if (!language.equalsIgnoreCase(value))
              throw new XslParseException(L.l("mismatched language `{0}'", value));
          } else if (name.equals("xml:space")) {
          } else throw new XslParseException(L.l("unknown directive `{0}'", name));
        }

        StylesheetImpl stylesheet = gen.generate(xsl);
        gen = null;
        stylesheet.init(path);
        // XXX: why was this here? stylesheet.init(getStylePath());
        stylesheet.setURIResolver(_uriResolver);

        return stylesheet;
      } catch (TransformerConfigurationException e) {
        throw e;
      } catch (Exception e) {
        throw new XslParseException(e);
      } finally {
        try {
          if (gen != null) gen.close();
        } catch (IOException e) {
        }
      }
    }
  }