Example usage for javax.xml.transform.dom DOMResult FEATURE

List of usage examples for javax.xml.transform.dom DOMResult FEATURE

Introduction

In this page you can find the example usage for javax.xml.transform.dom DOMResult FEATURE.

Prototype

String FEATURE

To view the source code for javax.xml.transform.dom DOMResult FEATURE.

Click Source Link

Document

If javax.xml.transform.TransformerFactory#getFeature returns true when passed this value as an argument, the Transformer supports Result output of this type.

Usage

From source file:DOM2DOM.java

public static void main(String[] args) throws TransformerException, TransformerConfigurationException,
        FileNotFoundException, ParserConfigurationException, SAXException, IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();

    if (tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE)) {
        //Instantiate a DocumentBuilderFactory.
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

        // And setNamespaceAware, which is required when parsing xsl files
        dFactory.setNamespaceAware(true);

        //Use the DocumentBuilderFactory to create a DocumentBuilder.
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

        //Use the DocumentBuilder to parse the XSL stylesheet.
        Document xslDoc = dBuilder.parse("birds.xsl");

        // Use the DOM Document to define a DOMSource object.
        DOMSource xslDomSource = new DOMSource(xslDoc);

        // Set the systemId: note this is actually a URL, not a local filename
        xslDomSource.setSystemId("birds.xsl");

        // Process the stylesheet DOMSource and generate a Transformer.
        Transformer transformer = tFactory.newTransformer(xslDomSource);

        //Use the DocumentBuilder to parse the XML input.
        Document xmlDoc = dBuilder.parse("birds.xml");

        // Use the DOM Document to define a DOMSource object.
        DOMSource xmlDomSource = new DOMSource(xmlDoc);

        // Set the base URI for the DOMSource so any relative URIs it contains can
        // be resolved.
        xmlDomSource.setSystemId("birds.xml");

        // Create an empty DOMResult for the Result.
        DOMResult domResult = new DOMResult();

        // Perform the transformation, placing the output in the DOMResult.
        transformer.transform(xmlDomSource, domResult);

        //Instantiate an Xalan XML serializer and use it to serialize the output DOM to System.out
        // using the default output format, except for indent="yes"
        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        xmlProps.setProperty("indent", "yes");
        xmlProps.setProperty("standalone", "no");
        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
        serializer.setOutputStream(System.out);
        serializer.asDOMSerializer().serialize(domResult.getNode());
    } else {//from  w ww . jav a  2  s . c  o m
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
    }
}

From source file:net.sf.joost.trax.TransformerFactoryImpl.java

/**
 * Supplied features.//from  w  w  w.j  ava2s  .  c  o  m
 * @param name Name of the feature.
 * @return true if feature is supported.
 */
public boolean getFeature(String name) {

    if (name.equals(SAXSource.FEATURE)) {
        return true;
    }
    if (name.equals(SAXResult.FEATURE)) {
        return true;
    }
    if (name.equals(DOMSource.FEATURE)) {
        return true;
    }
    if (name.equals(DOMResult.FEATURE)) {
        return true;
    }
    if (name.equals(StreamSource.FEATURE)) {
        return true;
    }
    if (name.equals(StreamResult.FEATURE)) {
        return true;
    }
    if (name.equals(SAXTransformerFactory.FEATURE)) {
        return true;
    }
    if (name.equals(SAXTransformerFactory.FEATURE_XMLFILTER)) {
        return true;
    }

    String errMsg = "Unknown feature " + name;
    TransformerConfigurationException tE = new TransformerConfigurationException(errMsg);

    try {
        defaultErrorListener.error(tE);
        return false;
    } catch (TransformerException e) {
        throw new IllegalArgumentException(errMsg);
    }
}

From source file:org.lilyproject.runtime.tools.plugin.genclassloader.ClassloaderMojo.java

private void parseClassloaderTemplate(File file) {
    DOMResult domResult = null;/*from w  w w . j av  a2s  .co  m*/
    Transformer transformer = null;
    try {
        Source xmlSource = new StreamSource(file);
        TransformerFactory tfFactory = TransformerFactory.newInstance();
        if (tfFactory.getFeature(DOMResult.FEATURE)) {
            transformer = tfFactory.newTransformer();
            domResult = new DOMResult();
            transformer.transform(xmlSource, domResult);
        }
    } catch (TransformerException ex) {
        throw new RuntimeException("Error parsing file '" + file + "'.");
    }
    if (domResult != null && transformer != null) {
        Document docu = (Document) domResult.getNode();
        Element classpath;
        NodeList list;

        String shareSelf = docu.getDocumentElement().getAttribute("share-self").trim();
        if (shareSelf.length() > 0) {
            this.shareSelf = shareSelf;
        }

        try {
            classpath = (Element) docu.getElementsByTagName("classpath").item(0);
            list = classpath.getElementsByTagName("artifact");
        } catch (NullPointerException npex) {
            throw new RuntimeException("Classloader template is invalid.");
        }
        NamedNodeMap map;
        Entry entry;
        String groupId;
        String classifier;
        String artifactId;
        Element domArtifact;
        for (int i = 0; i < list.getLength(); i++) {
            domArtifact = (Element) list.item(i);
            map = domArtifact.getAttributes();
            groupId = extractAttVal(map, "groupId");
            artifactId = extractAttVal(map, "artifactId");
            classifier = extractAttVal(map, "classifier");
            entry = new Entry(groupId, artifactId, classifier);
            entryMap.put(entry, domArtifact);
        }
    }

}