Exemple #1
0
  static {
    DocumentBuilderFactory factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
    try {
      builder = factory.newDocumentBuilder();
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      // Create an instance of our own transformer factory impl
      TransformerFactory transFactory =
          new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();

      // create Transformer
      transformer = transFactory.newTransformer();

    } catch (TransformerConfigurationException tce) {
      // Error generated by the parser
      System.out.println("* Transformer Factory error");
      System.out.println("  " + tce.getMessage());

      // Use the contained exception, if any
      Throwable x = tce;
      if (tce.getException() != null) x = tce.getException();
      x.printStackTrace();
    }
  }
Exemple #2
0
  public static String format(String xmlStr) { // Instantiate transformer input
    Source xmlInput = new StreamSource(new StringReader(xmlStr));
    StreamResult xmlOutput = new StreamResult(new StringWriter());

    // Configure transformer
    Transformer transformer;
    try {
      transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
      logger.error(e.getMessage(), e);
      return xmlStr;
    } catch (TransformerFactoryConfigurationError e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage(), e);
      return xmlStr;
    } // An identity transformer

    try {
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

      transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
      logger.error(e.getMessage(), e);
      return xmlStr;
    }

    return xmlOutput.getWriter().toString();
  }
  public void configure(final JSONObject config) throws PluginBuildException {
    if (null == config) {
      throw new PluginBuildException("Cannot build plugin with null configuration");
    }

    String xsltUrl = config.getString("stylesheet_url");
    InputStream content;
    try {
      URLConnection connection = new URL(xsltUrl).openConnection();
      connection.connect();
      content = (InputStream) connection.getContent();
    } catch (IOException e) {
      log.fatal(e);
      String msg = "Unable to read stylesheet";
      notifier.notify(NotificationType.Unavailable, msg + ": " + e.getMessage());
      throw new PluginBuildException(msg + ".", e);
    }
    StreamSource xsltSource = new StreamSource(content);
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setErrorListener(xsltErrorLogger);
    Transformer temp;
    try {
      temp = factory.newTransformer(xsltSource);
    } catch (TransformerConfigurationException e) {
      log.fatal(e);
      String msg = "Cannot compile configured stylesheet";
      notifier.notify(NotificationType.FatalError, msg + ":" + e.getMessage());
      throw new PluginBuildException(msg, e);
    }
    transformer = temp;
    transformer.setErrorListener(xsltErrorLogger);

    registerInput("input", input);
  }
  public void write(HttpServletResponse response) {
    StreamResult streamResult;
    SAXTransformerFactory tf;
    TransformerHandler hd;
    Transformer serializer;

    try {
      try {
        streamResult = new StreamResult(response.getWriter());
        tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        hd = tf.newTransformerHandler();
        serializer = hd.getTransformer();

        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(
            OutputKeys.DOCTYPE_SYSTEM, "http://labs.omniti.com/resmon/trunk/resources/resmon.dtd");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");

        hd.setResult(streamResult);
        hd.startDocument();
        AttributesImpl atts = new AttributesImpl();
        hd.startElement("", "", "ResmonResults", atts);
        for (ResmonResult r : s) {
          r.write(hd);
        }
        hd.endElement("", "", "ResmonResults");
        hd.endDocument();
      } catch (TransformerConfigurationException tce) {
        response.getWriter().println(tce.getMessage());
      } catch (SAXException se) {
        response.getWriter().println(se.getMessage());
      }
    } catch (IOException ioe) {
    }
  }
  private String formatXML(String str) {
    try {
      // Use a Transformer for output
      TransformerFactory tFactory = TransformerFactory.newInstance();
      // Surround this setting in a try/catch for compatibility with Java 1.4. This setting is
      // required
      // for Java 1.5
      try {
        tFactory.setAttribute("indent-number", 2);
      } catch (IllegalArgumentException e) {
        // Ignore
      }
      Transformer transformer = tFactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

      // Transform the requested string into a nice formatted XML string
      StreamSource source = new StreamSource(new StringReader(str));
      StringWriter sw = new StringWriter();
      StreamResult result = new StreamResult(sw);
      transformer.transform(source, result);
      return sw.toString();

    } catch (TransformerConfigurationException tce) {
      // Error generated by the parser
      System.out.println("\n** Transformer Factory error");
      System.out.println("   " + tce.getMessage());

      // Use the contained exception, if any
      Throwable x = tce;
      if (tce.getException() != null) x = tce.getException();
      x.printStackTrace();

    } catch (TransformerException te) {
      // Error generated by the parser
      System.out.println("\n** Transformation error");
      System.out.println("   " + te.getMessage());

      // Use the contained exception, if any
      Throwable x = te;
      if (te.getException() != null) x = te.getException();
      x.printStackTrace();
    }
    return str;
  }
 /**
  * Writes an XML file from a Document object.
  *
  * @param doc the Document object to be written to file
  * @param file the file to be written
  * @throws IOException
  * @author Klaus Meffert
  * @since 2.0
  */
 public static void writeFile(Document doc, File file) throws IOException {
   // Use a Transformer for output
   TransformerFactory tFactory = TransformerFactory.newInstance();
   Transformer transformer;
   try {
     transformer = tFactory.newTransformer();
   } catch (TransformerConfigurationException tex) {
     throw new IOException(tex.getMessage());
   }
   DOMSource source = new DOMSource(doc);
   FileOutputStream fos = new FileOutputStream(file);
   StreamResult result = new StreamResult(fos);
   try {
     transformer.transform(source, result);
     fos.close();
   } catch (TransformerException tex) {
     throw new IOException(tex.getMessage());
   }
 }