Search in sources :

Example 41 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class SitemapPortletController method displaySitemap.

/**
     * Display the user sitemap.
     *
     * @param request
     * @return "sitemapView" and a supporting model
     * @throws XMLStreamException
     * @throws IllegalStateException if components required for the SiteMap portlet fail to
     *     auto-wire
     */
@RequestMapping
public ModelAndView displaySitemap(PortletRequest request) throws XMLStreamException {
    Map<String, Object> model = new HashMap<String, Object>();
    if (this.xsltPortalUrlProvider == null) {
        throw new IllegalStateException("Sitemap portlet requires a XsltPortalUrlProvider but it failed to " + "auto-wire");
    }
    if (this.attributeIncorporationComponent == null) {
        throw new IllegalStateException("Sitemap portlet requires a StAXPipelineComponent with qualifier structureAttributeIncorporationComponent but it failed to auto-wire");
    }
    if (this.portalRequestUtils == null) {
        throw new IllegalStateException("Sitemap portlet requires an IPortalRequestUtils but it failed to " + "auto-wire");
    }
    // retrieve the user layout with structure attributes applied (required in order to display tab groups)
    final HttpServletRequest httpServletRequest = this.portalRequestUtils.getPortletHttpRequest(request);
    final HttpServletResponse httpServletResponse = this.portalRequestUtils.getOriginalPortalResponse(request);
    final PipelineEventReader<XMLEventReader, XMLEvent> reader = attributeIncorporationComponent.getEventReader(httpServletRequest, httpServletResponse);
    // create a Source from the user's layout document
    StAXSource source = new StAXSource(reader.getEventReader());
    model.put("source", source);
    model.put(XsltPortalUrlProvider.CURRENT_REQUEST, httpServletRequest);
    model.put(XsltPortalUrlProvider.XSLT_PORTAL_URL_PROVIDER, this.xsltPortalUrlProvider);
    model.put(USE_TAB_GROUPS, useTabGroups);
    model.put(USER_LANG, ObjectUtils.toString(request.getLocale()));
    return new ModelAndView("sitemapView", model);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) XMLEvent(javax.xml.stream.events.XMLEvent) ModelAndView(org.springframework.web.portlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 42 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class CachedCxfPayloadTest method testCachedCxfPayloadStAXSource.

@Test
public void testCachedCxfPayloadStAXSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException {
    StAXSource source = context.getTypeConverter().mandatoryConvertTo(StAXSource.class, PAYLOAD);
    doTest(source, PAYLOAD);
}
Also used : StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Example 43 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class XmlConverterTest method testToDomSourceByStAXSource.

public void testToDomSourceByStAXSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    // because of https://bugs.openjdk.java.net/show_bug.cgi?id=100228, we have to set the XML version explicitly
    StAXSource source = conv.toStAXSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>", null);
    DOMSource out = conv.toDOMSource(source);
    assertNotSame(source, out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StAXSource(javax.xml.transform.stax.StAXSource)

Example 44 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class XsltBuilder method process.

public void process(Exchange exchange) throws Exception {
    notNull(getTemplate(), "template");
    if (isDeleteOutputFile()) {
        // add on completion so we can delete the file when the Exchange is done
        String fileName = ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class);
        exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
    }
    Transformer transformer = getTransformer();
    configureTransformer(transformer, exchange);
    ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
    Result result = resultHandler.getResult();
    // let's copy the headers before we invoke the transform in case they modify them
    Message out = exchange.getOut();
    out.copyFrom(exchange.getIn());
    // the underlying input stream, which we need to close to avoid locking files or other resources
    InputStream is = null;
    try {
        Source source;
        // only convert to input stream if really needed
        if (isInputStreamNeeded(exchange)) {
            is = exchange.getIn().getBody(InputStream.class);
            source = getSource(exchange, is);
        } else {
            Object body = exchange.getIn().getBody();
            source = getSource(exchange, body);
        }
        if (source instanceof StAXSource) {
            // Always convert StAXSource to SAXSource.
            // * Xalan and Saxon-B don't support StAXSource.
            // * The JDK default implementation (XSLTC) doesn't handle CDATA events
            //   (see com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX).
            // * Saxon-HE/PE/EE seem to support StAXSource, but don't advertise this
            //   officially (via TransformerFactory.getFeature(StAXSource.FEATURE))
            source = new StAX2SAXSource(((StAXSource) source).getXMLStreamReader());
        }
        LOG.trace("Using {} as source", source);
        transformer.transform(source, result);
        LOG.trace("Transform complete with result {}", result);
        resultHandler.setBody(out);
    } finally {
        releaseTransformer(transformer);
        // IOHelper can handle if is is null
        IOHelper.close(is);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Message(org.apache.camel.Message) InputStream(java.io.InputStream) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Result(javax.xml.transform.Result)

Example 45 with StAXSource

use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.

the class XOPRoundtripTest method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    DataHandler dh = new DataHandler(new TestDataSource('x', Runtime.getRuntime().maxMemory()));
    OMElement element1 = factory.createOMElement(new QName("test"));
    element1.addChild(factory.createOMText(dh, true));
    XOPEncoded<XMLStreamReader> xopEncodedStream = element1.getXOPEncodedStreamReader(true);
    OMElement element2 = OMXMLBuilderFactory.createOMBuilder(factory, new StAXSource(xopEncodedStream.getRootPart()), xopEncodedStream.getAttachmentAccessor()).getDocumentElement();
    OMText child = (OMText) element2.getFirstOMChild();
    assertNotNull(child);
    assertTrue(child.isBinary());
    assertTrue(child.isOptimized());
    assertSame(dh, child.getDataHandler());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) StAXSource(javax.xml.transform.stax.StAXSource)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6