Examples of TransformerHandler


Examples of javax.xml.transform.sax.TransformerHandler

     */
    public static ContentHandler getSerializer(final File file)
    throws TransformerException, IOException {
        final Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");

        final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
        final Transformer transformer = transformerHandler.getTransformer();

        final Properties format = new Properties();
        format.put(OutputKeys.METHOD, "xml");
        format.put(OutputKeys.OMIT_XML_DECLARATION, "no");
        format.put(OutputKeys.ENCODING, "UTF-8");
        format.put(OutputKeys.INDENT, "yes");

        transformer.setOutputProperties(format);

        transformerHandler.setResult(new StreamResult(writer));

        return transformerHandler;
    }
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

    private static TransformerHandler getTransformerHandler(
            String method, String encoding)
            throws TransformerConfigurationException {
        SAXTransformerFactory factory = (SAXTransformerFactory)
                SAXTransformerFactory.newInstance();
        TransformerHandler handler = factory.newTransformerHandler();
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, method);
        handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        if (encoding != null) {
            handler.getTransformer().setOutputProperty(
                    OutputKeys.ENCODING, encoding);
        }
        return handler;
    }
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

    }

    protected synchronized void init() throws TransformerConfigurationException {
        if (_contentHandler == null) {
            _stringWriter = new StringWriter();
            TransformerHandler handler = getTransformerHandler(_method, "UTF-8");
            handler.setResult(new StreamResult(_stringWriter));
            _contentHandler = handler;

        }
    }
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

      reader.parse("birds.xsl");

      //Get the Templates object from the ContentHandler.
      Templates templates = templatesHandler.getTemplates();
      // Create a ContentHandler to handle parsing of the XML source. 
      TransformerHandler handler
        = saxTFactory.newTransformerHandler(templates);
      // Reset the XMLReader's ContentHandler.
      reader.setContentHandler(handler)

      // Set the ContentHandler to also function as a LexicalHandler, which
      // includes "lexical" events (e.g., comments and CDATA).
      reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
     
       FileOutputStream fos = new FileOutputStream("birds.out");
     
      Serializer serializer = SerializerFactory.getSerializer
                              (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
      serializer.setOutputStream(fos);
  
     
      // Set the result handling to be a serialization to the file output stream.
      Result result = new SAXResult(serializer.asContentHandler());
      handler.setResult(result);
     
      // Parse the XML input document.
      reader.parse("birds.xml");
     
      System.out.println("************* The result is in birds.out *************")
 
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

     */
    protected ContentHandler createContentHandler( final Result result )
    {
        try
        {
            TransformerHandler handler = getTransformerFactory().newTransformerHandler();

            m_format.put( OutputKeys.METHOD, "xml" );
            handler.setResult( result );
            handler.getTransformer().setOutputProperties( m_format );

            return handler;
        }
        catch( final Exception e )
        {
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

        // e is a wrapper class.
        e = e.getCause();
      }
      // remote error msg
      ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
      TransformerHandler response = null;
      StreamResult requestResult = new StreamResult(responseBody);

      SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory
          .newInstance();
      response = tf.newTransformerHandler();
      response.setResult(requestResult);
      response.startDocument();
      response.startElement("", "exception", "exception",
          new AttributesImpl());

      response.startElement("", "msg", "msg", new AttributesImpl());
      String msg = (e.getMessage() == null ? "unknown error" : e
          .getMessage());
      response.characters(msg.toCharArray(), 0, msg.length());
      response.endElement("", "msg", "msg");

      response.startElement("", "stacktrace", "stacktrace",
          new AttributesImpl());
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw, true);
      e.printStackTrace(pw);
      pw.flush();
      sw.flush();
      String trace = sw.toString();
      response.characters(trace.toCharArray(), 0, trace.length());
      response.endElement("", "stacktrace", "stacktrace");

      response.endElement("", "exception", "exception");
      response.endDocument();

      http.sendResponseHeaders(status, responseBody.size());
      responseBody.writeTo(http.getResponseBody());
      http.getResponseBody().close();
      http.close();
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

    private static void writeXML(Metadata meta, Result res,
                    boolean asXMPPacket, boolean readOnlyXMP)
                            throws TransformerConfigurationException, SAXException {
        SAXTransformerFactory tFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
        TransformerHandler handler = tFactory.newTransformerHandler();
        Transformer transformer = handler.getTransformer();
        if (asXMPPacket) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
        try {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        } catch (IllegalArgumentException iae) {
            //INDENT key is not supported by implementation. That's not tragic, so just ignore.
        }
        handler.setResult(res);
        handler.startDocument();
        if (asXMPPacket) {
            handler.processingInstruction("xpacket",
                    "begin=\"\uFEFF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"");
        }
        meta.toSAX(handler);
        if (asXMPPacket) {
            if (readOnlyXMP) {
                handler.processingInstruction("xpacket", "end=\"r\"");
            } else {
                //Create padding string (40 * 101 characters is more or less the recommended 4KB)
                StringBuffer sb = new StringBuffer(101);
                sb.append('\n');
                for (int i = 0; i < 100; i++) {
                    sb.append(" ");
                }
                char[] padding = sb.toString().toCharArray();
                for (int i = 0; i < 40; i++) {
                    handler.characters(padding, 0, padding.length);
                }
                handler.characters(new char[] {'\n'}, 0, 1);
                handler.processingInstruction("xpacket", "end=\"w\"");
            }

        }
        handler.endDocument();
    }
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

public class OBRXMLWriter {

    public static ContentHandler newHandler(OutputStream out, String encoding, boolean indent)
            throws TransformerConfigurationException {
        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler hd = tf.newTransformerHandler();
        Transformer serializer = tf.newTransformer();
        StreamResult stream = new StreamResult(out);
        serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
        serializer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
        hd.setResult(stream);
        return hd;
    }
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

    private void generateXml(IvyNode[] dependencies, Map moduleRevToArtifactsMap,
            Map artifactsToCopy) {
        try {
            FileOutputStream fileOuputStream = new FileOutputStream(tofile);
            try {
                TransformerHandler saxHandler = createTransformerHandler(fileOuputStream);

                saxHandler.startDocument();
                saxHandler.startElement(null, "modules", "modules", new AttributesImpl());

                for (int i = 0; i < dependencies.length; i++) {
                    IvyNode dependency = dependencies[i];
                    if (dependency.getModuleRevision() == null || dependency.isCompletelyEvicted()) {
                        continue;
                    }

                    startModule(saxHandler, dependency);

                    Set artifactsOfModuleRev = (Set) moduleRevToArtifactsMap.get(dependency
                            .getModuleRevision().getId());
                    if (artifactsOfModuleRev != null) {
                        for (Iterator iter = artifactsOfModuleRev.iterator(); iter.hasNext();) {
                            ArtifactDownloadReport artifact = (ArtifactDownloadReport) iter.next();

                            RepositoryCacheManager cache = dependency.getModuleRevision()
                                    .getArtifactResolver().getRepositoryCacheManager();

                            startArtifact(saxHandler, artifact.getArtifact());

                            writeOriginLocationIfPresent(cache, saxHandler, artifact);
                            writeCacheLocationIfPresent(cache, saxHandler, artifact);

                            Set artifactDestPaths = (Set) artifactsToCopy.get(artifact);
                            for (Iterator iterator = artifactDestPaths.iterator(); iterator
                                    .hasNext();) {
                                String artifactDestPath = (String) iterator.next();
                                writeRetrieveLocation(saxHandler, artifactDestPath);
                            }
                            saxHandler.endElement(null, "artifact", "artifact");
                        }
                    }
                    saxHandler.endElement(null, "module", "module");
                }
                saxHandler.endElement(null, "modules", "modules");
                saxHandler.endDocument();
            } finally {
                fileOuputStream.close();
            }
        } catch (SAXException e) {
            throw new BuildException("impossible to generate report", e);
View Full Code Here

Examples of javax.xml.transform.sax.TransformerHandler

    private TransformerHandler createTransformerHandler(FileOutputStream fileOuputStream)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException,
            SAXException {
        SAXTransformerFactory transformerFact = (SAXTransformerFactory) SAXTransformerFactory
                .newInstance();
        TransformerHandler saxHandler = transformerFact.newTransformerHandler();
        saxHandler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        saxHandler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        saxHandler.setResult(new StreamResult(fileOuputStream));
        return saxHandler;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.