Package javax.xml.transform

Examples of javax.xml.transform.Transformer


        TransformerFactory tf = TransformerFactory.newInstance();

        StreamSource source = new StreamSource(xsl);
        Templates templates = tf.newTemplates(source);

        Transformer trf = templates.newTransformer();

        trf.transform(new StreamSource(input), new StreamResult(output));
    }
View Full Code Here


                // create XSLT Source and Result objects
                Source data = new StreamSource(dataStream);
                Source style = new StreamSource(styleStream);
                Result output = new StreamResult(System.out);
                // create Transformer and perform the tranfomation
                Transformer xslt =  TransformerFactory.newInstance().newTransformer(style);
                xslt.transform(data, output);
            }
            catch (Exception e) {
                System.err.println("JiCal -xsltonly error in translation: " + e);
                e.printStackTrace();
            }
View Full Code Here

    for (int i=0; i < clazzes.length; i++) {
      if (clazzes[i] != null) {
        System.setProperty("javax.xml.transform.TransformerFactory", clazzes[i]);
      }
      try {
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        System.out.println("idTransform = " + trans.getClass().getName());
        return trans;
      } catch (TransformerConfigurationException e) {
        t = e; // keep on trying
      } catch (TransformerFactoryConfigurationError e) {
        t = new RuntimeException(e); // keep on trying
View Full Code Here

        builder = new Builder(XOMUtil.getNullNodeFactory());
      }
     
      // saxon
      StaticQueryContext context = null;
      Transformer saxonSerializer = null;
      if (mode.equals("saxon")) {
        context = new StaticQueryContext(new Configuration());
        String clazz = "net.sf.saxon.TransformerFactoryImpl";
        System.setProperty("javax.xml.transform.TransformerFactory", clazz);
        saxonSerializer = TransformerFactory.newInstance().newTransformer();
      }
     
      // DOM
      DocumentBuilder domBuilder = null;
      Transformer domSerializer = null;
      if (mode.equals("dom")) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE);
        domBuilder = factory.newDocumentBuilder();
        String clazz = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
//        String clazz = "org.apache.xalan.processor.TransformerFactoryImpl";
        System.setProperty("javax.xml.transform.TransformerFactory", clazz);
        domSerializer = TransformerFactory.newInstance().newTransformer();
        System.err.println(domSerializer.getClass().getName());
      }
     
      // FastInfoSet
      Object fiSerializer = null;
//      XMLStreamWriter fiStaxSerializer = null;
      Builder fiBuilder = null
      Method fiMethod = null;
     
      XMLStreamReader fistaxReader = null;
      Method fistaxMethod = null;
      if (mode.startsWith("fi")) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        XMLReader parser = (XMLReader) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentParser").newInstance();
        fiBuilder = new Builder(parser, false, factory);
       
        if (mode.indexOf("stax") >= 0) {
          fiSerializer = (XMLStreamWriter) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentSerializer").newInstance();       
        } else {
          fiSerializer = (ContentHandler) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentSerializer").newInstance();
        }
       
        if (mode.startsWith("fi1")) { // enable "full indexing"
          Method method;
          method = fiSerializer.getClass().getMethod(
              "setAttributeValueSizeLimit", new Class[] {Integer.TYPE});
          method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
          method = fiSerializer.getClass().getMethod(
              "setCharacterContentChunkSizeLimit", new Class[] {Integer.TYPE});
          method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
        }
       
        fiMethod = fiSerializer.getClass().getMethod(
            "setOutputStream", new Class[] { OutputStream.class});
       
        if (mode.indexOf("stax") >= 0) {
          fistaxReader = (XMLStreamReader) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentParser").newInstance();
          fistaxMethod = fistaxReader.getClass().getMethod(
              "setInputStream", new Class[] { InputStream.class});
        }
      }
     
      // StAX
      Builder staxBuilder = null;
      if (mode.indexOf("stax") >= 0) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        staxBuilder = StaxUtil.createBuilder(staxInputFactory, factory);
      }
     
      for (int j=5; j < args.length; j++) {
        try {
          File[] files = IOTestUtil.listXMLFiles(args[j]);
          for (int k=0; k < files.length; k++) {
            File file = files[k];
            if (bogus(file) || ignore(file) || file.isDirectory()) {
              System.out.println("\n" + ": IGNORING " + file + " ...");
              continue;
            }
           
            System.out.println("now processing " + file);
  //          System.out.print(".");
           
            // prepare
            // file:/path/to/file --> file:///path/to/file
            String baseURI = file.toURI().toASCIIString();
            if (baseURI.startsWith("file:/")) {
              baseURI = baseURI.substring("file:/".length());
              if (!baseURI.startsWith("//")) baseURI = "//" + baseURI;
              baseURI = "file:/" + baseURI;
            }
  //          System.out.println("baseURI: " + baseURI);
            Document doc = new Builder().build(file);
           
            byte[] data = codec.serialize(doc, compressionLevel);
            if (!cmd.equals("test")) doc = new BinaryXMLCodec().deserialize(data); // use "interned" strings
            byte[] fileData = FileUtil.toByteArray(new FileInputStream(file));
            long fileLength = file.length();
            int encodedSize = 0;
           
            org.w3c.dom.Document domDoc = null;
            if (mode.equals("dom")) {
              domDoc = domBuilder.parse(file);
            }
           
            NodeInfo saxonDoc = null;
            if (mode.equals("saxon")) {
              saxonDoc = context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
            }
   
            if (mode.startsWith("fi")) {
              if (mode.indexOf("stax") >= 0) {
//                data = serializeWithStax(doc, staxOutputFactory);
                data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, new ByteArrayOutputStream());
              } else {
                data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, new ByteArrayOutputStream());
              }
            }
   
            // run the benchmark
            long start = System.currentTimeMillis();
            for (int i = 0; i < iterations; i++) {
              try {
                // serialize
                if (cmd.equals("ser") || cmd.equals("serdeser") || cmd.equals("test")) {
                  ByteArrayOutputStream out = createOutputStream(cmd.equals("ser"));
                  if (mode.startsWith("bnux")) {
                    if (mode.indexOf("stream") < 0) {
                      codec.serialize(doc, compressionLevel, out);
                      data = out.toByteArray();
                    } else {
                      data = serializeWithStreamingBnux(doc, compressionLevel, out);
                    }
                  } else if (mode.startsWith("xom")) {
                    if (mode.indexOf("stax") >= 0) {
                      data = serializeWithStax(doc, staxOutputFactory, out);
                    } else if (mode.indexOf("stream") < 0) {
                      data = serializeWithXOM(doc, out);
                    } else {
                      data = serializeWithStreamingXOM(doc, out);
                    }
                  } else if (mode.equals("saxon")) {
                    saxonSerializer.transform(saxonDoc, new StreamResult(out));
                    data = out.toByteArray();
                  } else if (mode.equals("dom")) {
                    domSerializer.transform(new DOMSource(domDoc), new StreamResult(out));
                    data = out.toByteArray();
                  } else if (mode.startsWith("fi")) {
                    if (mode.indexOf("stax") >= 0) {
//                      data = serializeWithStax(doc, staxOutputFactory);
                      data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, out);
View Full Code Here

    NodeList list =
      step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/text/1.0", "text");
    Node node = list.item(0);

    TransformerFactory streamerfactory = TransformerFactory.newInstance();
    Transformer streamer = streamerfactory.newTransformer();

    SAXTransformerFactory serializerfactory =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    TransformerHandler serializer = serializerfactory.newTransformerHandler();
    DOMResult result = new DOMResult();
    serializer.setResult(result);

    processor.setContentHandler(new WhitespaceFilter(serializer, new ConsoleLog(ConsoleLog.INFO)));

    streamer.transform(new DOMSource(node), new SAXResult(processor));

    return (Document)result.getNode();
  }
View Full Code Here

    if (node==null)
      return null;

    TransformerFactory streamerfactory = TransformerFactory.newInstance();
    Transformer streamer = streamerfactory.newTransformer();

    DOMResult result = new DOMResult();

    streamer.transform(new DOMSource(node), result);

    return (Document)result.getNode();
  }
View Full Code Here

    {
        // Write the DOM back to file
        try
        {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer m = tf.newTransformer();
            DOMSource source = new DOMSource(doc);
            FileOutputStream os = new FileOutputStream(fileUrl.getFile());
            StreamResult result = new StreamResult(os);
            m.setOutputProperty(OutputKeys.INDENT, "yes");
            m.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, XMLAutoStarterEntityResolver.PUBLIC_ID_KEY);
            m.transform(source, result);
            os.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
View Full Code Here

  public static String getText(Element element) throws TransformerException
  {
    if (transFactory == null) {
      transFactory = TransformerFactory.newInstance();     
    }
      Transformer trans = transFactory.newTransformer();
      StringWriter sw = new StringWriter();
      trans.transform(new DOMSource(element), new StreamResult(sw));
      return new String(sw.toString());
  }
View Full Code Here

    requestHandler.setOperationClass(opHandler.getParameter());

      Node n = requestHandler.invoke(reqElem);
     
      StringWriter sw = new StringWriter();
        Transformer t = TransformerFactory.newInstance().newTransformer();
      t.transform(new DOMSource(n), new StreamResult(sw));
      return sw.toString();
  }
View Full Code Here

  public static String toString(Document doc, String encoding, boolean indent) throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
      xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }

    xformer.transform(source, result);

    return sw.getBuffer().toString();
  }
View Full Code Here

TOP

Related Classes of javax.xml.transform.Transformer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.