Esempio n. 1
0
    @Override
    public void passivateObject(Object object) throws Exception {
      javax.xml.transform.Transformer transformer = (javax.xml.transform.Transformer) object;

      // Clear transformation parameters before returning transformer to the pool
      transformer.clearParameters();

      // Clean up transformer before return it to the pool
      transformer.reset();

      super.passivateObject(transformer);
    }
Esempio n. 2
0
  /** Configures the transformer with exchange specific parameters */
  protected void configureTransformer(Transformer transformer, Exchange exchange) throws Exception {
    if (uriResolver == null) {
      uriResolver = new XsltUriResolver(exchange.getContext().getClassResolver(), null);
    }
    transformer.setURIResolver(uriResolver);
    if (errorListener == null) {
      // set our error listener so we can capture errors and report them back on the exchange
      transformer.setErrorListener(new DefaultTransformErrorHandler(exchange));
    } else {
      // use custom error listener
      transformer.setErrorListener(errorListener);
    }

    transformer.clearParameters();
    addParameters(transformer, exchange.getProperties());
    addParameters(transformer, exchange.getIn().getHeaders());
    addParameters(transformer, getParameters());
    transformer.setParameter("exchange", exchange);
    transformer.setParameter("in", exchange.getIn());
    transformer.setParameter("out", exchange.getOut());
  }
  protected void doTransform(
      MuleMessage message, String outputEncoding, Source sourceDoc, Result result)
      throws Exception {
    DefaultErrorListener errorListener = new DefaultErrorListener(this);
    javax.xml.transform.Transformer transformer = null;

    try {
      transformer = (javax.xml.transform.Transformer) transformerPool.borrowObject();

      transformer.setErrorListener(errorListener);
      transformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);

      // set transformation parameters
      if (contextProperties != null) {
        for (Entry<String, Object> parameter : contextProperties.entrySet()) {
          String key = parameter.getKey();
          transformer.setParameter(
              key, evaluateTransformParameter(key, parameter.getValue(), message));
        }
      }

      transformer.transform(sourceDoc, result);

      if (errorListener.isError()) {
        throw errorListener.getException();
      }
    } finally {
      if (transformer != null) {
        // clear transformation parameters before returning transformer to the
        // pool
        transformer.clearParameters();

        transformerPool.returnObject(transformer);
      }
    }
  }
Esempio n. 4
0
  private List getOutputFileList(URL resource, File xmlSourceDirectory, Map parameters)
      throws DocTransletException {

    try {
      List result;

      OutputStream out = new ByteArrayOutputStream();

      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      Document document = documentBuilder.newDocument();
      DOMResult domResult = new DOMResult(document);
      {
        StreamSource source = new StreamSource(resource.toExternalForm());

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = (Transformer) transformerFactory.newTransformer(source);

        transformer.clearParameters();
        for (Iterator pit = parameters.keySet().iterator(); pit.hasNext(); ) {
          String key = (String) pit.next();
          String value = (String) parameters.get(key);
          transformer.setParameter(key, value);
        }

        transformer.transform(
            new StreamSource(new File(xmlSourceDirectory, "index.xml").getAbsolutePath()),
            domResult);
      }

      {
        NodeList nodeList = document.getElementsByTagName("outputfile");
        result = new ArrayList(nodeList.getLength());

        for (int i = 0; i < nodeList.getLength(); ++i) {
          Element elem = (Element) nodeList.item(i);
          String name = getTextContent(elem.getElementsByTagName("name").item(0));
          String source =
              (null != elem.getElementsByTagName("source").item(0))
                  ? getTextContent(elem.getElementsByTagName("source").item(0))
                  : null;
          String sheet =
              (null != elem.getElementsByTagName("sheet").item(0))
                  ? getTextContent(elem.getElementsByTagName("sheet").item(0))
                  : null;
          String comment = getTextContent(elem.getElementsByTagName("comment").item(0));
          String info = null;
          if (elem.getElementsByTagName("info").getLength() > 0) {
            if (null != elem.getElementsByTagName("info").item(0).getFirstChild()) {
              info = getTextContent(elem.getElementsByTagName("info").item(0));
            } else {
              info = "";
            }
          }
          result.add(new OutputFileInfo(name, source, sheet, comment, info));
        }
      }
      return result;
    } catch (TransformerFactoryConfigurationError e) {
      throw new DocTransletException(e);
    } catch (TransformerException e) {
      throw new DocTransletException(e.getMessageAndLocation(), e);
    } catch (ParserConfigurationException e) {
      throw new DocTransletException(e);
    }
  }
Esempio n. 5
0
  public void apply(File xmlSourceDirectory, File targetDirectory, DocErrorReporter reporter)
      throws DocTransletException {

    PrintStream err = System.err;

    try {
      URL mainResourceURL =
          classLoader == null
              ? ClassLoader.getSystemResource(mainResourceFilename)
              : classLoader.getResource(mainResourceFilename);

      if (null == mainResourceURL) {
        throw new DocTransletException("Cannot find resource '" + mainResourceFilename + "'");
      }

      Map parameters = new HashMap();
      parameters.put("gjdoc.xmldoclet.version", Driver.XMLDOCLET_VERSION);

      parameters.put("gjdoc.option.nonavbar", xsltBoolean(options.nonavbar));
      parameters.put("gjdoc.option.noindex", xsltBoolean(options.noindex));
      parameters.put("gjdoc.option.notree", xsltBoolean(options.notree));
      parameters.put("gjdoc.option.nocomment", xsltBoolean(options.nocomment));
      parameters.put("gjdoc.option.nohelp", xsltBoolean(options.nohelp));
      parameters.put("gjdoc.option.splitindex", xsltBoolean(options.splitindex));
      parameters.put("gjdoc.option.linksource", xsltBoolean(options.linksource));
      parameters.put("gjdoc.option.nodeprecatedlist", xsltBoolean(options.nodeprecatedlist));
      parameters.put("gjdoc.option.uses", xsltBoolean(options.uses));
      parameters.put("gjdoc.option.windowtitle", options.windowtitle);
      parameters.put("gjdoc.option.helpfile", options.helpfile);
      parameters.put("gjdoc.option.stylesheetfile", options.stylesheetfile);
      parameters.put("gjdoc.option.header", options.header);
      parameters.put("gjdoc.option.footer", options.footer);
      parameters.put("gjdoc.option.bottom", options.bottom);
      parameters.put("gjdoc.option.doctitle", options.doctitle);

      List outputFileList = getOutputFileList(mainResourceURL, xmlSourceDirectory, parameters);

      reporter.printNotice("Running DocTranslet...");

      TransformerFactory transformerFactory = TransformerFactory.newInstance();

      transformerFactory.setErrorListener(this);

      boolean isLibxmlJ =
          transformerFactory
              .getClass()
              .getName()
              .equals("gnu.xml.libxmlj.transform.TransformerFactoryImpl");

      for (Iterator it = outputFileList.iterator(); it.hasNext(); ) {

        if (isLibxmlJ) {
          System.gc();
          Runtime.getRuntime().runFinalization();
        }

        OutputFileInfo fileInfo = (OutputFileInfo) it.next();

        File targetFile = new File(targetDirectory, fileInfo.getName());
        File packageTargetDir = getParentFile(targetFile);

        if (!packageTargetDir.exists() && !packageTargetDir.mkdirs()) {
          throw new DocTransletException(
              "Target directory " + packageTargetDir + " does not exist and cannot be created.");
        }

        if (options.linksource) {
          File sourceTargetDirectory = new File(targetDirectory, "src-html");
          File sourceTargetFile = new File(sourceTargetDirectory, fileInfo.getName());
          File sourcePackageTargetDir = getParentFile(sourceTargetFile);

          if (!sourcePackageTargetDir.exists() && !sourcePackageTargetDir.mkdirs()) {
            throw new DocTransletException(
                "Target directory " + packageTargetDir + " does not exist and cannot be created.");
          }
        }

        if (options.uses) {
          File usesTargetDirectory = new File(targetDirectory, "class-use");
          File usesTargetFile = new File(usesTargetDirectory, fileInfo.getName());
          File usesPackageTargetDir = getParentFile(usesTargetFile);

          if (!usesPackageTargetDir.exists() && !usesPackageTargetDir.mkdirs()) {
            throw new DocTransletException(
                "Target directory " + packageTargetDir + " does not exist and cannot be created.");
          }
        }

        if (null != fileInfo.getSource()) {

          reporter.printNotice("Copying " + fileInfo.getComment() + "...");
          InputStream in = new URL(mainResourceURL, fileInfo.getSource()).openStream();
          FileOutputStream out = new FileOutputStream(targetFile.getAbsolutePath());
          IOToolkit.copyStream(in, out);
          in.close();
          out.close();
        } else {

          reporter.printNotice("Generating " + fileInfo.getComment() + "...");

          String pathToRoot = "";
          for (File file = getParentFile(targetFile);
              !equalsFile(file, targetDirectory);
              file = getParentFile(file)) {
            pathToRoot += "../";
          }

          StreamResult out = new StreamResult(targetFile.getAbsolutePath());

          StreamSource in =
              new StreamSource(new File(xmlSourceDirectory, "index.xml").getAbsolutePath());
          URL resource = new URL(mainResourceURL, fileInfo.getSheet());

          StreamSource xsltSource = new StreamSource(resource.toExternalForm());

          if (null != fileInfo.getInfo()) {
            parameters.put("gjdoc.outputfile.info", fileInfo.getInfo());
          }
          parameters.put("gjdoc.pathtoroot", pathToRoot);

          Transformer transformer;
          transformer = (Transformer) transformerMap.get(xsltSource.getSystemId());
          if (null == transformer) {
            transformer = transformerFactory.newTransformer(xsltSource);
            if (cacheXSLTSheets) {
              transformerMap.put(xsltSource.getSystemId(), transformer);
            }
          }

          transformer.clearParameters();
          for (Iterator pit = parameters.keySet().iterator(); pit.hasNext(); ) {
            String key = (String) pit.next();
            String value = (String) parameters.get(key);
            transformer.setParameter(key, value);
          }

          transformer.setErrorListener(this);
          DocErrorReporterOutputStream errorReporterOut =
              new DocErrorReporterOutputStream(reporter);
          System.setErr(new PrintStream(errorReporterOut));

          transformer.transform(in, out);
          errorReporterOut.flush();
        }
      }
    } catch (MalformedURLException e) {
      throw new DocTransletException(e);
    } catch (TransformerFactoryConfigurationError e) {
      throw new DocTransletException(e);
    } catch (TransformerException e) {
      throw new DocTransletException(e.getMessageAndLocation(), e);
    } catch (IOException e) {
      throw new DocTransletException(e);
    } finally {
      System.setErr(err);
    }
  }
Esempio n. 6
0
 public void clearParameters() {
   transformer.clearParameters();
 }