コード例 #1
0
ファイル: Xml.java プロジェクト: fxprunayre/core-geonetwork
  /**
   * Transforms an xml tree putting the result to a stream with optional parameters.
   *
   * @param xml
   * @param styleSheetPath
   * @param result
   * @param params
   * @throws Exception
   */
  public static void transform(
      Element xml, String styleSheetPath, Result result, Map<String, String> params)
      throws Exception {
    File styleSheet = new File(styleSheetPath);
    Source srcXml = new JDOMSource(new Document((Element) xml.detach()));
    Source srcSheet = new StreamSource(styleSheet);

    // Dear old saxon likes to yell loudly about each and every XSLT 1.0
    // stylesheet so switch it off but trap any exceptions because this
    // code is run on transformers other than saxon
    TransformerFactory transFact = TransformerFactoryFactory.getTransformerFactory();
    transFact.setURIResolver(new JeevesURIResolver());
    try {
      transFact.setAttribute(FeatureKeys.VERSION_WARNING, false);
      transFact.setAttribute(FeatureKeys.LINE_NUMBERING, true);
      transFact.setAttribute(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, false);
      transFact.setAttribute(FeatureKeys.RECOVERY_POLICY, Configuration.RECOVER_SILENTLY);
      // Add the following to get timing info on xslt transformations
      // transFact.setAttribute(FeatureKeys.TIMING,true);
    } catch (IllegalArgumentException e) {
      Log.warning(Log.ENGINE, "WARNING: transformerfactory doesnt like saxon attributes!");
      // e.printStackTrace();
    } finally {
      Transformer t = transFact.newTransformer(srcSheet);
      if (params != null) {
        for (Map.Entry<String, String> param : params.entrySet()) {
          t.setParameter(param.getKey(), param.getValue());
        }
      }
      t.transform(srcXml, result);
    }
  }
コード例 #2
0
ファイル: Xml.java プロジェクト: fxprunayre/core-geonetwork
  /**
   * Transform an xml tree to PDF using XSL-FOP     * putting the result to a stream (uses a
   * stylesheet on disk)
   */
  public static String transformFOP(String uploadDir, Element xml, String styleSheetPath)
      throws Exception {
    String file = uploadDir + UUID.randomUUID().toString() + ".pdf";

    // Step 1: Construct a FopFactory
    // (reuse if you plan to render multiple documents!)
    FopFactory fopFactory = FopFactory.newInstance();

    // Step 2: Set up output stream.
    // Note: Using BufferedOutputStream for performance reasons (helpful
    // with FileOutputStreams).
    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file)));

    try {
      // Step 3: Construct fop with desired output format
      Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

      // Step 4: Setup JAXP using identity transformer
      TransformerFactory factory = TransformerFactoryFactory.getTransformerFactory();
      factory.setURIResolver(new JeevesURIResolver());
      Source xslt = new StreamSource(new File(styleSheetPath));
      try {
        factory.setAttribute(FeatureKeys.VERSION_WARNING, false);
        factory.setAttribute(FeatureKeys.LINE_NUMBERING, true);
        factory.setAttribute(FeatureKeys.RECOVERY_POLICY, Configuration.RECOVER_SILENTLY);
      } catch (IllegalArgumentException e) {
        Log.warning(Log.ENGINE, "WARNING: transformerfactory doesnt like saxon attributes!");
        // e.printStackTrace();
      } finally {
        Transformer transformer = factory.newTransformer(xslt);

        // Step 5: Setup input and output for XSLT transformation
        // Setup input stream
        Source src = new JDOMSource(new Document((Element) xml.detach()));

        // Resulting SAX events (the generated FO) must be piped through to
        // FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        // Step 6: Start XSLT transformation and FOP processing
        transformer.transform(src, res);
      }

    } finally {
      // Clean-up
      out.close();
    }

    return file;
  }