Search in sources :

Example 26 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class WindowStateSettingsStAXComponent method getEventReader.

/* (non-Javadoc)
     * @see org.apereo.portal.rendering.PipelineComponent#getEventReader(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
    final XMLEventReader eventReader = pipelineEventReader.getEventReader();
    final IStylesheetDescriptor stylesheetDescriptor = stylesheetAttributeSource.getStylesheetDescriptor(request);
    final IPortalRequestInfo portalRequestInfo = this.urlSyntaxProvider.getPortalRequestInfo(request);
    final XMLEventReader filteredEventReader;
    if (portalRequestInfo.getTargetedPortletWindowId() == null) {
        final IStylesheetParameterDescriptor defaultWindowStateParam = stylesheetDescriptor.getStylesheetParameterDescriptor("dashboardForcedWindowState");
        if (defaultWindowStateParam != null) {
            //Set all window states to the specified default
            final WindowState windowState = PortletUtils.getWindowState(defaultWindowStateParam.getDefaultValue());
            filteredEventReader = new SinglePortletWindowStateSettingXMLEventReader(request, eventReader, windowState);
        } else {
            //Make sure there aren't any portlets in a "targeted" window state
            filteredEventReader = new NonTargetedPortletWindowStateSettingXMLEventReader(request, eventReader);
        }
    } else {
        //Not mobile, don't bother filtering
        filteredEventReader = eventReader;
    }
    final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
    return new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(filteredEventReader, outputProperties);
}
Also used : WindowState(javax.portlet.WindowState) IStylesheetParameterDescriptor(org.apereo.portal.layout.om.IStylesheetParameterDescriptor) IPortalRequestInfo(org.apereo.portal.url.IPortalRequestInfo) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FilteringXMLEventReader(org.apereo.portal.xml.stream.FilteringXMLEventReader) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor)

Example 27 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class XsltPortalUrlProviderTest method setup.

@Before
public void setup() throws Exception {
    final ResourceLoaderURIResolver resolver = new ResourceLoaderURIResolver(new ClassRelativeResourceLoader(getClass()));
    this.tFactory = TransformerFactory.newInstance();
    this.tFactory.setURIResolver(resolver);
    this.tFactory.setErrorListener(new SimpleTransformErrorListener(LogFactory.getLog(getClass())));
    //Load the XML document so it reads the same way the rendering pipeline reads XML
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(this.getClass().getResourceAsStream("test.xml"));
    final XMLStreamReader streamReader = new FixedXMLEventStreamReader(eventReader);
    xmlSource = new StAXSource(streamReader);
    xslTemplate = tFactory.newTemplates(new StreamSource(this.getClass().getResourceAsStream("test.xsl")));
    expected = IOUtils.toString(this.getClass().getResourceAsStream("result.xml"));
}
Also used : ResourceLoaderURIResolver(org.apereo.portal.xml.ResourceLoaderURIResolver) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) XMLStreamReader(javax.xml.stream.XMLStreamReader) SimpleTransformErrorListener(org.springframework.util.xml.SimpleTransformErrorListener) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Before(org.junit.Before)

Example 28 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class XSLTComponentTest method getXmlEventReader.

protected XMLEventReader getXmlEventReader(String file) throws SAXException, IOException, ParserConfigurationException {
    final InputStream xmlStream = this.getClass().getResourceAsStream(file);
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    final Document document = documentBuilder.parse(xmlStream);
    final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    final XMLEventReader xmlEventReader;
    try {
        xmlEventReader = inputFactory.createXMLEventReader(new DOMSource(document, "foobar"));
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLStreamReader for file '" + file + "'", e);
    }
    return xmlEventReader;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) XMLEventReader(javax.xml.stream.XMLEventReader) Document(org.w3c.dom.Document) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 29 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class CachingStAXPipelineComponentTest method testCacheMiss.

@Test
public void testCacheMiss() {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockRes = new MockHttpServletResponse();
    final CacheKey cacheKey = CacheKey.build("testCacheKey");
    final List<XMLEvent> eventBuffer = Collections.emptyList();
    final PipelineEventReader<XMLEventReader, XMLEvent> eventReader = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(new XMLEventBufferReader(eventBuffer.listIterator()));
    final Ehcache cache = createMock(Ehcache.class);
    final StAXPipelineComponent targetComponent = createMock(StAXPipelineComponent.class);
    final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
    expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
    expect(targetComponent.getCacheKey(mockReq, mockRes)).andReturn(cacheKey);
    expect(cache.get(cacheKey)).andReturn(null);
    expect(targetComponent.getEventReader(mockReq, mockRes)).andReturn(eventReader);
    cache.put((Element) notNull());
    expectLastCall();
    replay(cache, targetComponent, elementsProvider);
    final CachingStAXPipelineComponent cachingComponent = new CachingStAXPipelineComponent();
    cachingComponent.setCache(cache);
    cachingComponent.setWrappedComponent(targetComponent);
    cachingComponent.setResourcesElementsProvider(elementsProvider);
    final PipelineEventReader<XMLEventReader, XMLEvent> actualEventReader = cachingComponent.getEventReader(mockReq, mockRes);
    Assert.assertNotNull(actualEventReader);
    Assert.assertNotNull(actualEventReader.getEventReader());
    Assert.assertFalse(actualEventReader.getEventReader().hasNext());
    verify(cache, targetComponent, elementsProvider);
}
Also used : StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) XMLEventBufferReader(org.apereo.portal.xml.stream.XMLEventBufferReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Ehcache(net.sf.ehcache.Ehcache) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) CacheKey(org.apereo.portal.utils.cache.CacheKey) Test(org.junit.Test)

Example 30 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project galley by Commonjava.

the class XMLInfrastructure method fallbackParseDocument.

private Document fallbackParseDocument(String xml, final Object docSource, final Exception e) throws GalleyMavenXMLException {
    logger.debug("Failed to parse: {}. DOM error: {}. Trying STaX parse with IS_REPLACING_ENTITY_REFERENCES == false...", e, docSource, e.getMessage());
    try {
        Source source;
        if (safeInputFactory != null) {
            xml = repairXmlDeclaration(xml);
            final XMLEventReader eventReader = safeInputFactory.createXMLEventReader(new StringReader(xml));
            source = new StAXSource(eventReader);
        } else {
            // Deal with &oslash; and other undeclared entities...
            xml = escapeNonXMLEntityRefs(xml);
            final XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setFeature("http://xml.org/sax/features/validation", false);
            source = new SAXSource(reader, new InputSource(new StringReader(xml)));
        }
        final DOMResult result = new DOMResult();
        final Transformer transformer = newTransformer();
        transformer.transform(source, result);
        return (Document) result.getNode();
    } catch (final TransformerException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. Transformer error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    } catch (final SAXException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. SAX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    } catch (final XMLStreamException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. STaX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) Transformer(javax.xml.transform.Transformer) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)62 XMLInputFactory (javax.xml.stream.XMLInputFactory)30 XMLEvent (javax.xml.stream.events.XMLEvent)30 XMLStreamException (javax.xml.stream.XMLStreamException)21 StringReader (java.io.StringReader)18 InputStream (java.io.InputStream)15 Test (org.junit.Test)14 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)10 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Attribute (javax.xml.stream.events.Attribute)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 JAXBContext (javax.xml.bind.JAXBContext)5 Diff (org.custommonkey.xmlunit.Diff)5 InputSource (org.xml.sax.InputSource)5 JAXBException (javax.xml.bind.JAXBException)4 DOMSource (javax.xml.transform.dom.DOMSource)4