Java Code Examples for javax.xml.transform.sax.SAXSource#sourceToInputSource()

The following examples show how to use javax.xml.transform.sax.SAXSource#sourceToInputSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SAX2DOMTest.java    From openjdk-jdk9 with GNU General Public License v2.0 7 votes vote down vote up
@Test
public void test() throws Exception {
    SAXParserFactory fac = SAXParserFactory.newInstance();
    fac.setNamespaceAware(true);
    SAXParser saxParser = fac.newSAXParser();

    StreamSource sr = new StreamSource(this.getClass().getResourceAsStream("SAX2DOMTest.xml"));
    InputSource is = SAXSource.sourceToInputSource(sr);
    RejectDoctypeSaxFilter rf = new RejectDoctypeSaxFilter(saxParser);
    SAXSource src = new SAXSource(rf, is);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DOMResult result = new DOMResult();
    transformer.transform(src, result);

    Document doc = (Document) result.getNode();
    System.out.println("Name" + doc.getDocumentElement().getLocalName());

    String id = "XWSSGID-11605791027261938254268";
    Element selement = doc.getElementById(id);
    if (selement == null) {
        System.out.println("getElementById returned null");
    }

}
 
Example 2
Source File: TemplatesParser.java    From everrest with Eclipse Public License 2.0 6 votes vote down vote up
public Templates parseTemplates(Source source) throws TransformerConfigurationException, SAXException, IOException {
    InputSource inputSource = SAXSource.sourceToInputSource(source);
    if (inputSource == null) {
        throw new RuntimeException("Unable convert to Input Source");
    }

    SAXTransformerFactory saxTransformerFactory = createFeaturedSaxTransformerFactory();
    TemplatesHandler templateHandler = saxTransformerFactory.newTemplatesHandler();
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    if (resolver != null) {
        xmlReader.setEntityResolver(resolver);
    }
    xmlReader.setContentHandler(templateHandler);

    xmlReader.parse(inputSource);

    Templates templates = templateHandler.getTemplates();
    if (templates == null) {
        throw new RuntimeException("Unable create templates from given source");
    }
    return templates;
}
 
Example 3
Source File: TransformerFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Get the stylesheet specification(s) associated via the xml-stylesheet
 * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
 * the document document specified in the source parameter, and that match
 * the given criteria.
 *
 * @param source The XML source document.
 * @param media The media attribute to be matched. May be null, in which
 * case the prefered templates will be used (i.e. alternate = no).
 * @param title The value of the title attribute to match. May be null.
 * @param charset The value of the charset attribute to match. May be null.
 * @return A Source object suitable for passing to the TransformerFactory.
 * @throws TransformerConfigurationException
 */
@Override
public Source  getAssociatedStylesheet(Source source, String media,
                                      String title, String charset)
    throws TransformerConfigurationException {

    String baseId;
    XMLReader reader = null;
    InputSource isource;

    /**
     * Fix for bugzilla bug 24187
     */
    StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);

    try {

        if (source instanceof DOMSource ) {
            final DOMSource domsrc = (DOMSource) source;
            baseId = domsrc.getSystemId();
            final org.w3c.dom.Node node = domsrc.getNode();
            final DOM2SAX dom2sax = new DOM2SAX(node);

            _stylesheetPIHandler.setBaseId(baseId);

            dom2sax.setContentHandler( _stylesheetPIHandler);
            dom2sax.parse();
        } else {
            if (source instanceof SAXSource) {
                reader = ((SAXSource)source).getXMLReader();
            }
            isource = SAXSource.sourceToInputSource(source);
            baseId = isource.getSystemId();

            if (reader == null) {
                reader = JdkXmlUtils.getXMLReader(_overrideDefaultParser,
                        !_isNotSecureProcessing);
            }

            _stylesheetPIHandler.setBaseId(baseId);
            reader.setContentHandler(_stylesheetPIHandler);
            reader.parse(isource);

        }

        if (_uriResolver != null ) {
            _stylesheetPIHandler.setURIResolver(_uriResolver);
        }

    } catch (StopParseException e ) {
      // startElement encountered so do not parse further
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", se);
    } catch (IOException ioe ) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", ioe);
    }

     return _stylesheetPIHandler.getAssociatedStylesheet();

}
 
Example 4
Source File: TransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Get the stylesheet specification(s) associated via the xml-stylesheet
 * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
 * the document document specified in the source parameter, and that match
 * the given criteria.
 *
 * @param source The XML source document.
 * @param media The media attribute to be matched. May be null, in which
 * case the prefered templates will be used (i.e. alternate = no).
 * @param title The value of the title attribute to match. May be null.
 * @param charset The value of the charset attribute to match. May be null.
 * @return A Source object suitable for passing to the TransformerFactory.
 * @throws TransformerConfigurationException
 */
@Override
public Source  getAssociatedStylesheet(Source source, String media,
                                      String title, String charset)
    throws TransformerConfigurationException {

    String baseId;
    XMLReader reader = null;
    InputSource isource;

    /**
     * Fix for bugzilla bug 24187
     */
    StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);

    try {

        if (source instanceof DOMSource ) {
            final DOMSource domsrc = (DOMSource) source;
            baseId = domsrc.getSystemId();
            final org.w3c.dom.Node node = domsrc.getNode();
            final DOM2SAX dom2sax = new DOM2SAX(node);

            _stylesheetPIHandler.setBaseId(baseId);

            dom2sax.setContentHandler( _stylesheetPIHandler);
            dom2sax.parse();
        } else {
            if (source instanceof SAXSource) {
                reader = ((SAXSource)source).getXMLReader();
            }
            isource = SAXSource.sourceToInputSource(source);
            baseId = isource.getSystemId();

            if (reader == null) {
                reader = JdkXmlUtils.getXMLReader(_overrideDefaultParser,
                        !_isNotSecureProcessing);
            }

            _stylesheetPIHandler.setBaseId(baseId);
            reader.setContentHandler(_stylesheetPIHandler);
            reader.parse(isource);

        }

        if (_uriResolver != null ) {
            _stylesheetPIHandler.setURIResolver(_uriResolver);
        }

    } catch (StopParseException e ) {
      // startElement encountered so do not parse further
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", se);
    } catch (IOException ioe ) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", ioe);
    }

     return _stylesheetPIHandler.getAssociatedStylesheet();

}
 
Example 5
Source File: TransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Get the stylesheet specification(s) associated via the xml-stylesheet
 * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
 * the document document specified in the source parameter, and that match
 * the given criteria.
 *
 * @param source The XML source document.
 * @param media The media attribute to be matched. May be null, in which
 * case the prefered templates will be used (i.e. alternate = no).
 * @param title The value of the title attribute to match. May be null.
 * @param charset The value of the charset attribute to match. May be null.
 * @return A Source object suitable for passing to the TransformerFactory.
 * @throws TransformerConfigurationException
 */
@Override
public Source  getAssociatedStylesheet(Source source, String media,
                                      String title, String charset)
    throws TransformerConfigurationException {

    String baseId;
    XMLReader reader = null;
    InputSource isource;

    /**
     * Fix for bugzilla bug 24187
     */
    StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);

    try {

        if (source instanceof DOMSource ) {
            final DOMSource domsrc = (DOMSource) source;
            baseId = domsrc.getSystemId();
            final org.w3c.dom.Node node = domsrc.getNode();
            final DOM2SAX dom2sax = new DOM2SAX(node);

            _stylesheetPIHandler.setBaseId(baseId);

            dom2sax.setContentHandler( _stylesheetPIHandler);
            dom2sax.parse();
        } else {
            if (source instanceof SAXSource) {
                reader = ((SAXSource)source).getXMLReader();
            }
            isource = SAXSource.sourceToInputSource(source);
            baseId = isource.getSystemId();

            if (reader == null) {
                reader = JdkXmlUtils.getXMLReader(_overrideDefaultParser,
                        !_isNotSecureProcessing);
            }

            _stylesheetPIHandler.setBaseId(baseId);
            reader.setContentHandler(_stylesheetPIHandler);
            reader.parse(isource);

        }

        if (_uriResolver != null ) {
            _stylesheetPIHandler.setURIResolver(_uriResolver);
        }

    } catch (StopParseException e ) {
      // startElement encountered so do not parse further
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", se);
    } catch (IOException ioe ) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", ioe);
    }

     return _stylesheetPIHandler.getAssociatedStylesheet();

}
 
Example 6
Source File: TransformerFactoryImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Get the stylesheet specification(s) associated via the xml-stylesheet
 * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
 * the document document specified in the source parameter, and that match
 * the given criteria.
 *
 * @param source The XML source document.
 * @param media The media attribute to be matched. May be null, in which
 * case the prefered templates will be used (i.e. alternate = no).
 * @param title The value of the title attribute to match. May be null.
 * @param charset The value of the charset attribute to match. May be null.
 * @return A Source object suitable for passing to the TransformerFactory.
 * @throws TransformerConfigurationException
 */
@Override
public Source  getAssociatedStylesheet(Source source, String media,
                                      String title, String charset)
    throws TransformerConfigurationException {

    String baseId;
    XMLReader reader = null;
    InputSource isource;

    /**
     * Fix for bugzilla bug 24187
     */
    StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);

    try {

        if (source instanceof DOMSource ) {
            final DOMSource domsrc = (DOMSource) source;
            baseId = domsrc.getSystemId();
            final org.w3c.dom.Node node = domsrc.getNode();
            final DOM2SAX dom2sax = new DOM2SAX(node);

            _stylesheetPIHandler.setBaseId(baseId);

            dom2sax.setContentHandler( _stylesheetPIHandler);
            dom2sax.parse();
        } else {
            if (source instanceof SAXSource) {
                reader = ((SAXSource)source).getXMLReader();
            }
            isource = SAXSource.sourceToInputSource(source);
            baseId = isource.getSystemId();

            if (reader == null) {
                reader = JdkXmlUtils.getXMLReader(_overrideDefaultParser,
                        !_isNotSecureProcessing);
            }

            _stylesheetPIHandler.setBaseId(baseId);
            reader.setContentHandler(_stylesheetPIHandler);
            reader.parse(isource);

        }

        if (_uriResolver != null ) {
            _stylesheetPIHandler.setURIResolver(_uriResolver);
        }

    } catch (StopParseException e ) {
      // startElement encountered so do not parse further
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", se);
    } catch (IOException ioe ) {
        throw new TransformerConfigurationException(
            "getAssociatedStylesheets failed", ioe);
    }

     return _stylesheetPIHandler.getAssociatedStylesheet();

}