Example usage for javax.xml.transform OutputKeys METHOD

List of usage examples for javax.xml.transform OutputKeys METHOD

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys METHOD.

Prototype

String METHOD

To view the source code for javax.xml.transform OutputKeys METHOD.

Click Source Link

Document

method = "xml" | "html" | "text" | expanded name.

Usage

From source file:org.fhaes.neofhchart.svg.FireChartSVG.java

/**
 * Handles printing of the SVG document.
 * /*from w ww .j  a va 2s . c om*/
 * @param doc
 * @param out
 */
public static void printDocument(Document doc, OutputStream out) {

    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));

        log.debug("Document printed successfully.");
    } catch (Exception ex) {
        System.out.println("Error: Could not printDocument\n");
    }
}

From source file:org.fireflow.pdl.fpdl.io.FPDLSerializer.java

/**
 * xmloutString//from  www.  ja  v  a  2 s . com
 * @param workflowProcess
 * @param out
 * @return
 * @throws IOException
 * @throws SerializerException
 * @throws InvalidModelException
 */
public String serialize(WorkflowProcess workflowProcess, OutputStream out)
        throws IOException, SerializerException, InvalidModelException {
    try {
        Document document = serializeToDOM(workflowProcess);

        //??
        Element root = document.getDocumentElement();
        String displayname = root.getAttribute(DISPLAY_NAME);
        //============

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        if (JDK_TRANSFORMER_CLASS.equals(transformerFactory.getClass().getName())) {
            useJDKTransformerFactory = true;
        }

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, charset);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENT_LIST);

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

        transformer.transform(new DOMSource(document), new StreamResult(out));

        out.flush();

        return charset;
    } catch (TransformerConfigurationException e) {
        throw new SerializerException(e);
    } catch (TransformerException e) {
        throw new SerializerException(e);
    } finally {

    }

}

From source file:org.fireflow.pdl.fpdl20.io.FPDLSerializer.java

public void serialize(WorkflowProcess workflowProcess, OutputStream out)
        throws IOException, SerializerException, InvalidModelException {
    try {/*  ww w. java  2s.  co m*/
        Document document = serializeToDOM(workflowProcess);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        if (JDK_TRANSFORMER_CLASS.equals(transformerFactory.getClass().getName())) {
            useJDKTransformerFactory = true;
        }

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, charset);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENT_LIST);

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

        transformer.transform(new DOMSource(document), new StreamResult(out));
        out.flush();
    } catch (TransformerConfigurationException e) {
        throw new SerializerException(e);
    } catch (TransformerException e) {
        throw new SerializerException(e);
    } finally {

    }

}

From source file:org.geoserver.security.xml.XMLRoleStore.java

@Override
protected void serialize() throws IOException {

    DocumentBuilder builder = null;
    try {/*  w w w.j  ava2 s. com*/
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        throw new IOException(e1);
    }
    Document doc = builder.newDocument();

    Element rolereg = doc.createElement(E_ROLEREGISTRY_RR);
    doc.appendChild(rolereg);
    rolereg.setAttribute(javax.xml.XMLConstants.XMLNS_ATTRIBUTE, NS_RR);
    rolereg.setAttribute(A_VERSION_RR, VERSION_RR_1_0);

    Element rolelist = doc.createElement(E_ROLELIST_RR);
    rolereg.appendChild(rolelist);

    for (GeoServerRole roleObject : helper.roleMap.values()) {
        Element role = doc.createElement(E_ROLE_RR);
        rolelist.appendChild(role);
        role.setAttribute(A_ROLEID_RR, roleObject.getAuthority());
        GeoServerRole parentObject = helper.role_parentMap.get(roleObject);
        if (parentObject != null) {
            role.setAttribute(A_PARENTID_RR, parentObject.getAuthority());
        }
        for (Object key : roleObject.getProperties().keySet()) {
            Element property = doc.createElement(E_PROPERTY_RR);
            role.appendChild(property);
            property.setAttribute(A_PROPERTY_NAME_RR, key.toString());
            property.setTextContent(roleObject.getProperties().getProperty(key.toString()));
        }
    }

    Element userList = doc.createElement(E_USERLIST_RR);
    rolereg.appendChild(userList);
    for (String userName : helper.user_roleMap.keySet()) {
        Element userroles = doc.createElement(E_USERROLES_RR);
        userList.appendChild(userroles);
        userroles.setAttribute(A_USERNAME_RR, userName);
        SortedSet<GeoServerRole> roleObjects = helper.user_roleMap.get(userName);
        for (GeoServerRole roleObject : roleObjects) {
            Element ref = doc.createElement(E_ROLEREF_RR);
            userroles.appendChild(ref);
            ref.setAttribute(A_ROLEREFID_RR, roleObject.getAuthority());
        }
    }

    Element groupList = doc.createElement(E_GROUPLIST_RR);
    rolereg.appendChild(groupList);

    for (String groupName : helper.group_roleMap.keySet()) {
        Element grouproles = doc.createElement(E_GROUPROLES_RR);
        groupList.appendChild(grouproles);
        grouproles.setAttribute(A_GROUPNAME_RR, groupName);
        SortedSet<GeoServerRole> roleObjects = helper.group_roleMap.get(groupName);
        for (GeoServerRole roleObject : roleObjects) {
            Element ref = doc.createElement(E_ROLEREF_RR);
            grouproles.appendChild(ref);
            ref.setAttribute(A_ROLEREFID_RR, roleObject.getAuthority());
        }
    }

    // serialize the dom
    try {
        //            TODO, wait for JAVA 6
        //            if (isValidatingXMLSchema()) {
        //                XMLValidator.Singleton.validateRoleRegistry(doc);
        //            }

        Transformer tx = TransformerFactory.newInstance().newTransformer();
        tx.setOutputProperty(OutputKeys.METHOD, "XML");
        tx.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tx.setOutputProperty(OutputKeys.INDENT, "yes");

        OutputStream out = new BufferedOutputStream(new FileOutputStream(roleFile));
        try {
            tx.transform(new DOMSource(doc), new StreamResult(out));
            out.flush();
        } finally {
            IOUtils.closeQuietly(out);
        }

        /* standard java, but there is no possibility to set 
         * the number of chars to indent, each line is starting at 
         * column 0
        Source source = new DOMSource(doc);
        // Prepare the output file            
        Result result = new StreamResult(
            new OutputStreamWriter(new FileOutputStream(roleFile),"UTF-8"));
                
        TransformerFactory fac = TransformerFactory.newInstance();
        Transformer xformer = fac.newTransformer();                        
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");            
        xformer.transform(source, result);
        */

    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:org.geoserver.security.xml.XMLUserGroupStore.java

@Override
protected void serialize() throws IOException {

    DocumentBuilder builder = null;
    try {// www .ja v a 2 s. c om
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        builder = fac.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        throw new IOException(e1);
    }
    Document doc = builder.newDocument();

    Element userreg = doc.createElement(E_USERREGISTRY_UR);
    doc.appendChild(userreg);
    userreg.setAttribute(javax.xml.XMLConstants.XMLNS_ATTRIBUTE, NS_UR);
    userreg.setAttribute(A_VERSION_UR, VERSION_UR_1_0);

    Element users = doc.createElement(E_USERS_UR);
    userreg.appendChild(users);
    for (GeoServerUser userObject : helper.userMap.values()) {
        Element user = doc.createElement(E_USER_UR);
        users.appendChild(user);
        user.setAttribute(A_USER_NAME_UR, userObject.getUsername());
        if (userObject.getPassword() != null) {
            user.setAttribute(A_USER_PASSWORD_UR, userObject.getPassword());
        }
        user.setAttribute(A_USER_ENABLED_UR, String.valueOf(userObject.isEnabled()));

        for (Object key : userObject.getProperties().keySet()) {
            Element property = doc.createElement(E_PROPERTY_UR);
            user.appendChild(property);
            property.setAttribute(A_PROPERTY_NAME_UR, key.toString());
            property.setTextContent(userObject.getProperties().getProperty(key.toString()));
        }
    }

    Element groups = doc.createElement(E_GROUPS_UR);
    userreg.appendChild(groups);
    for (GeoServerUserGroup groupObject : helper.groupMap.values()) {
        Element group = doc.createElement(E_GROUP_UR);
        groups.appendChild(group);
        group.setAttribute(A_GROUP_NAME_UR, groupObject.getGroupname());
        group.setAttribute(A_GROUP_ENABLED_UR, groupObject.isEnabled() ? "true" : "false");
        SortedSet<GeoServerUser> userObjects = helper.group_userMap.get(groupObject);
        if (userObjects != null) {
            for (GeoServerUser userObject : userObjects) {
                Element member = doc.createElement(E_MEMBER_UR);
                group.appendChild(member);
                member.setAttribute(A_MEMBER_NAME_UR, userObject.getUsername());
            }
        }
    }

    // serialize the dom
    try {
        //            TODO, wait for JAVA 6
        //            if (isValidatingXMLSchema()) {
        //                XMLValidator.Singleton.validateUserGroupRegistry(doc);
        //            }            

        Transformer tx = TransformerFactory.newInstance().newTransformer();
        tx.setOutputProperty(OutputKeys.METHOD, "XML");
        tx.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tx.setOutputProperty(OutputKeys.INDENT, "yes");

        OutputStream out = new BufferedOutputStream(new FileOutputStream(userFile));
        try {
            tx.transform(new DOMSource(doc), new StreamResult(out));
            out.flush();
        } finally {
            IOUtils.closeQuietly(out);
        }
        /* standard java, but there is no possiTbility to set 
         * the number of chars to indent, each line is starting at 
         * column 0            
        Source source = new DOMSource(doc);
        Result result = new StreamResult(
            new OutputStreamWriter(new FileOutputStream(userFile),"UTF-8"));
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
        */

    } catch (Exception e) {
        throw new IOException(e);
    }

}

From source file:org.geoserver.test.GeoServerAbstractTestSupport.java

/**
 * Utility method to print out a dom./*from   ww w.j a  v  a  2 s .  c om*/
 */
protected void print(Document dom) throws Exception {
    TransformerFactory txFactory = TransformerFactory.newInstance();
    try {
        txFactory.setAttribute("{http://xml.apache.org/xalan}indent-number", new Integer(2));
    } catch (Exception e) {
        // some 
    }

    Transformer tx = txFactory.newTransformer();
    tx.setOutputProperty(OutputKeys.METHOD, "xml");
    tx.setOutputProperty(OutputKeys.INDENT, "yes");

    tx.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(System.out, "utf-8")));
}

From source file:org.gluu.saml.Response.java

public void printDocument(OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(xmlDoc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ScaleImageOperation.java

private void writeSvgDocument(final File file, final Document svgDocument) {
    try {//  www  .j  a v a  2 s . c o m
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, svgDocument.getInputEncoding());
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(2));
        Result output = new StreamResult(file);
        Source input = new DOMSource(svgDocument);
        transformer.transform(input, output);
    } catch (TransformerConfigurationException e) {
        log.info("Writing SVG file " + file.getName() + " failed, using original instead", e);
    } catch (TransformerException e) {
        log.info("Writing SVG file " + file.getName() + " failed, using original instead", e);
    }
}

From source file:org.hippoecm.repository.impl.SessionDecorator.java

private ContentHandler getExportContentHandler(OutputStream stream) throws RepositoryException {
    try {/*from   w  w  w .j ava  2s .  co  m*/
        SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler handler = stf.newTransformerHandler();

        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(2));
        handler.setResult(new StreamResult(stream));
        return handler;
    } catch (TransformerFactoryConfigurationError e) {
        throw new RepositoryException("SAX transformer implementation not available", e);
    } catch (TransformerException e) {
        throw new RepositoryException("Error creating an XML export content handler", e);
    }
}

From source file:org.jahia.services.content.JCRSessionWrapper.java

/**
 * Creates a {@link ContentHandler} instance that serializes the
 * received SAX events to the given output stream.
 *
 * @param stream output stream to which the SAX events are serialized
 * @return SAX content handler//from w  w w .ja v  a 2s. c  o m
 * @throws RepositoryException if an error occurs
 */
private ContentHandler getExportContentHandler(OutputStream stream) throws RepositoryException {
    try {
        SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler handler = stf.newTransformerHandler();

        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");

        handler.setResult(new StreamResult(stream));
        return handler;
    } catch (TransformerFactoryConfigurationError e) {
        throw new RepositoryException("SAX transformer implementation not available", e);
    } catch (TransformerException e) {
        throw new RepositoryException("Error creating an XML export content handler", e);
    }
}