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

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

Introduction

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

Prototype

String FEATURE

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

Click Source Link

Document

If javax.xml.transform.TransformerFactory#getFeature returns true when passed this value as an argument, the Transformer supports Source input 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 www .j av a2 s  . c  om
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
    }
}

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

/**
 * Supplied features./*  w  ww.  j  a  v a2 s.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:Examples.java

/**
 * Show how to transform a DOM tree into another DOM tree.
 * This uses the javax.xml.parsers to parse an XML file into a
 * DOM, and create an output DOM./*ww  w .  jav  a 2  s  . c  o  m*/
 */
public static Node exampleDOM2DOM(String sourceID, String xslID) throws TransformerException,
        TransformerConfigurationException, SAXException, IOException, ParserConfigurationException {
    TransformerFactory tfactory = TransformerFactory.newInstance();

    if (tfactory.getFeature(DOMSource.FEATURE)) {
        Templates templates;

        {
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            dfactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
            org.w3c.dom.Document outNode = docBuilder.newDocument();
            Node doc = docBuilder.parse(new InputSource(xslID));

            DOMSource dsource = new DOMSource(doc);
            // If we don't do this, the transformer won't know how to 
            // resolve relative URLs in the stylesheet.
            dsource.setSystemId(xslID);

            templates = tfactory.newTemplates(dsource);
        }

        Transformer transformer = templates.newTransformer();
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        // Note you must always setNamespaceAware when building .xsl stylesheets
        dfactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
        org.w3c.dom.Document outNode = docBuilder.newDocument();
        Node doc = docBuilder.parse(new InputSource(sourceID));

        transformer.transform(new DOMSource(doc), new DOMResult(outNode));

        Transformer serializer = tfactory.newTransformer();
        serializer.transform(new DOMSource(outNode), new StreamResult(new OutputStreamWriter(System.out)));

        return outNode;
    } else {
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
    }
}

From source file:Examples.java

/**
 * Show the Transformer using SAX events in and DOM nodes out.
 *///from  w  ww  .j a va  2s  .  c  om
public static void exampleContentHandler2DOM(String sourceID, String xslID) throws TransformerException,
        TransformerConfigurationException, SAXException, IOException, ParserConfigurationException {
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // Make sure the transformer factory we obtained supports both
    // DOM and SAX.
    if (tfactory.getFeature(SAXSource.FEATURE) && tfactory.getFeature(DOMSource.FEATURE)) {
        // We can now safely cast to a SAXTransformerFactory.
        SAXTransformerFactory sfactory = (SAXTransformerFactory) tfactory;

        // Create an Document node as the root for the output.
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
        org.w3c.dom.Document outNode = docBuilder.newDocument();

        // Create a ContentHandler that can liston to SAX events 
        // and transform the output to DOM nodes.
        TransformerHandler handler = sfactory.newTransformerHandler(new StreamSource(xslID));
        handler.setResult(new DOMResult(outNode));

        // Create a reader and set it's ContentHandler to be the 
        // transformer.
        XMLReader reader = null;

        // Use JAXP1.1 ( if possible )
        try {
            javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
            reader = jaxpParser.getXMLReader();

        } catch (javax.xml.parsers.ParserConfigurationException ex) {
            throw new org.xml.sax.SAXException(ex);
        } catch (javax.xml.parsers.FactoryConfigurationError ex1) {
            throw new org.xml.sax.SAXException(ex1.toString());
        } catch (NoSuchMethodError ex2) {
        }
        if (reader == null)
            reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(handler);
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);

        // Send the SAX events from the parser to the transformer,
        // and thus to the DOM tree.
        reader.parse(sourceID);

        // Serialize the node for diagnosis.
        exampleSerializeNode(outNode);
    } else {
        System.out.println(
                "Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory");
    }
}

From source file:nl.nn.adapterframework.validation.xerces_2_11.XMLSchemaFactory.java

public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    if (name == null) {
        throw new NullPointerException(JAXPValidationMessageFormatter
                .formatMessage(fXMLSchemaLoader.getLocale(), "FeatureNameNull", null));
    }//  w w  w.  j  a v  a 2s  .  c om
    if (name.startsWith(JAXP_SOURCE_FEATURE_PREFIX)) {
        // Indicates to the caller that this SchemaFactory supports a specific JAXP Source.
        if (name.equals(StreamSource.FEATURE) || name.equals(SAXSource.FEATURE)
                || name.equals(DOMSource.FEATURE) || name.equals(StAXSource.FEATURE)) {
            return true;
        }
    }
    if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
        return (fSecurityManager != null);
    } else if (name.equals(USE_GRAMMAR_POOL_ONLY)) {
        return fUseGrammarPoolOnly;
    }
    try {
        return fXMLSchemaLoader.getFeature(name);
    } catch (XMLConfigurationException e) {
        String identifier = e.getIdentifier();
        if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
            throw new SAXNotRecognizedException(SAXMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(),
                    "feature-not-recognized", new Object[] { identifier }));
        } else {
            throw new SAXNotSupportedException(SAXMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(),
                    "feature-not-supported", new Object[] { identifier }));
        }
    }
}

From source file:nl.nn.adapterframework.validation.xerces_2_11.XMLSchemaFactory.java

public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    if (name == null) {
        throw new NullPointerException(JAXPValidationMessageFormatter
                .formatMessage(fXMLSchemaLoader.getLocale(), "FeatureNameNull", null));
    }//  w  w  w  .j  a v  a2 s  . co m
    if (name.startsWith(JAXP_SOURCE_FEATURE_PREFIX)) {
        if (name.equals(StreamSource.FEATURE) || name.equals(SAXSource.FEATURE)
                || name.equals(DOMSource.FEATURE) || name.equals(StAXSource.FEATURE)) {
            throw new SAXNotSupportedException(SAXMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(),
                    "feature-read-only", new Object[] { name }));
        }
    }
    if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
        fSecurityManager = value ? new SecurityManager() : null;
        fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager);
        return;
    } else if (name.equals(USE_GRAMMAR_POOL_ONLY)) {
        fUseGrammarPoolOnly = value;
        return;
    }
    try {
        fXMLSchemaLoader.setFeature(name, value);
    } catch (XMLConfigurationException e) {
        String identifier = e.getIdentifier();
        if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
            throw new SAXNotRecognizedException(SAXMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(),
                    "feature-not-recognized", new Object[] { identifier }));
        } else {
            throw new SAXNotSupportedException(SAXMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(),
                    "feature-not-supported", new Object[] { identifier }));
        }
    }
}

From source file:org.eclipse.smila.search.servlet.SMILASearchServlet.java

/**
 * create XSL transformer for stylesheet.
 *
 * @param xslDoc// www.  jav  a 2 s .c o m
 *          XSL DOM document
 * @return XSL transformer
 * @throws ServletException
 *           error.
 */
protected Transformer getXSLTransformer(final Document xslDoc) throws ServletException {
    final TransformerFactory tFactory = TransformerFactory.newInstance();
    if (tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(StreamResult.FEATURE)) {

        final DOMSource xslDomSource = new DOMSource(xslDoc);

        try {
            return tFactory.newTransformer(xslDomSource);
        } catch (final TransformerConfigurationException e) {
            throw new ServletException("error while creating the transformer", e);
        }
    } else {
        throw new ServletException("the transformer [" + tFactory.getClass().getName()
                + "] doesn't support the used DOMSource or StreamResult");
    }
}