Example usage for javax.xml.transform.sax SAXSource setXMLReader

List of usage examples for javax.xml.transform.sax SAXSource setXMLReader

Introduction

In this page you can find the example usage for javax.xml.transform.sax SAXSource setXMLReader.

Prototype

public void setXMLReader(XMLReader reader) 

Source Link

Document

Set the XMLReader to be used for the Source.

Usage

From source file:Main.java

/**
 * @param reader//ww w.ja  va  2 s. c o  m
 * @param parser
 * @param output
 * @throws TransformerConfigurationException
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerException
 */
public static void write(Reader reader, XMLReader parser, Result output)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
    SAXSource input = new SAXSource();
    InputSource inputSource = new InputSource();
    inputSource.setCharacterStream(reader);
    input.setInputSource(inputSource);
    input.setXMLReader(parser);
    write(input, output);
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Template method for handling {@code SAXSource}s.
 * <p>This implementation delegates to {@code unmarshalSaxReader}.
 * @param saxSource the {@code SAXSource}
 * @return the object graph/*from   w w w  . j  a v  a2s .  c o m*/
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IOException if an I/O Exception occurs
 * @see #unmarshalSaxReader(org.xml.sax.XMLReader, org.xml.sax.InputSource)
 */
protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException {
    if (saxSource.getXMLReader() == null) {
        try {
            saxSource.setXMLReader(createXmlReader());
        } catch (SAXException ex) {
            throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource", ex);
        }
    }
    if (saxSource.getInputSource() == null) {
        saxSource.setInputSource(new InputSource());
    }
    try {
        return unmarshalSaxReader(saxSource.getXMLReader(), saxSource.getInputSource());
    } catch (NullPointerException ex) {
        if (!isSupportDtd()) {
            throw new UnmarshallingFailureException(
                    "NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD "
                            + "declarations, which are disabled.");
        }
        throw ex;
    }
}

From source file:org.theospi.portfolio.shared.control.ContentResourceUriResolver.java

public Source resolve(String href, String base) throws TransformerException {
    try {/*  w  w w . ja  va  2s  .c om*/
        String accessUrl = getServerConfigurationService().getAccessUrl();
        String url = href.replaceAll(accessUrl, "");

        // We depend on these resolving as content entities (e.g., /content/group/<site> or /content/user/<user>),
        // so provide some assistance and consistency with metaobj resolution.

        // Check if this already resolves as a content entity and, if not, try again by prepending /content/.
        Reference ref = getEntityManager().newReference(url);
        Object entity = ref.getEntity();
        if (entity == null || !(entity instanceof ContentResource)) {
            if (!url.startsWith("/content/")) {
                url = "/content" + (url.startsWith("/") ? "" : "/") + url;
                ref = getEntityManager().newReference(url);
                entity = ref.getEntity();
            }
            if (entity == null || !(entity instanceof ContentResource)) {
                String msg = "Could not resolve URI as a content resource: " + href;
                logger.info(msg);
                throw new TransformerException(msg);
            }
        }

        ContentResource res = (ContentResource) entity;
        StreamSource strs = new StreamSource(res.streamContent());
        SAXSource ss = new SAXSource(new InputSource(strs.getInputStream()));
        CatalogResolver resolver = new CatalogResolver();
        String appUrl = getServerConfigurationService().getServerUrl();
        try {
            resolver.getCatalog().parseCatalog(appUrl + "/osp-common-tool/dtd/catalog.xml");
            XMLReader xread = XMLReaderFactory.createXMLReader();
            xread.setEntityResolver(resolver);
            ss.setXMLReader(xread);
        } catch (MalformedURLException e) {
            logger.error(e);
        } catch (IOException e) {
            logger.error(e);
        } catch (SAXException e) {
            logger.error(e);
        }

        return ss;
    } catch (ServerOverloadException e) {
        logger.error("", e);
    }
    return null;
}