Search in sources :

Example 1 with CharacterEventSource

use of org.apereo.portal.character.stream.CharacterEventSource in project uPortal by Jasig.

the class RenderingPipelineConfiguration method getStAXSerializingComponent.

@Bean(name = "staxSerializingComponent")
public CharacterPipelineComponent getStAXSerializingComponent() {
    final StAXSerializingComponent rslt = new StAXSerializingComponent();
    rslt.setWrappedComponent(getPostThemeTransformLogger());
    final Map<String, CharacterEventSource> chunkingElements = new HashMap<>();
    chunkingElements.put(IUserLayoutManager.CHANNEL, getPortletContentPlaceholderEventSource());
    chunkingElements.put(IUserLayoutManager.CHANNEL_HEADER, getPortletHeaderPlaceholderEventSource());
    chunkingElements.put(ChunkPointPlaceholderEventSource.CHUNK_POINT, getChunkPointPlaceholderEventSource());
    chunkingElements.put(PortletAnalyticsDataPlaceholderEventSource.PORTLET_ANALYTICS_SCRIPT, getPortletAnalyticsDataPlaceholderEventSource());
    chunkingElements.put(PageAnalyticsDataPlaceholderEventSource.PAGE_ANALYTICS_SCRIPT, getPageAnalyticsDataPlaceholderEventSource());
    rslt.setChunkingElements(chunkingElements);
    final Map<String, CharacterEventSource> chunkingPatterns = new HashMap<>();
    chunkingPatterns.put(PORTLET_TITLE_PATTERN, getPortletTitlePlaceholderEventSource());
    chunkingPatterns.put(PORTLET_HELP_PATTERN, getPortletHelpPlaceholderEventSource());
    chunkingPatterns.put(PORTLET_NEW_ITEM_COUNT_PATTERN, getPortletNewItemCountPlaceholderEventSource());
    chunkingPatterns.put(PORTLET_LINK_PATTERN, getPortletLinkPlaceholderEventSource());
    rslt.setChunkingPatterns(chunkingPatterns);
    return rslt;
}
Also used : HashMap(java.util.HashMap) StAXSerializingComponent(org.apereo.portal.rendering.StAXSerializingComponent) CharacterEventSource(org.apereo.portal.character.stream.CharacterEventSource) Bean(org.springframework.context.annotation.Bean)

Example 2 with CharacterEventSource

use of org.apereo.portal.character.stream.CharacterEventSource in project uPortal by Jasig.

the class StAXSerializingComponent method setChunkingPatterns.

public void setChunkingPatterns(Map<String, CharacterEventSource> chunkingPatterns) {
    final Map<Pattern, CharacterEventSource> compiledChunkingPatternEventSources = new LinkedHashMap<Pattern, CharacterEventSource>();
    for (final Map.Entry<String, CharacterEventSource> chunkingPatternEntry : chunkingPatterns.entrySet()) {
        final String key = chunkingPatternEntry.getKey();
        final Pattern pattern = Pattern.compile(key);
        final CharacterEventSource value = chunkingPatternEntry.getValue();
        compiledChunkingPatternEventSources.put(pattern, value);
    }
    this.chunkingPatternEventSources = compiledChunkingPatternEventSources;
    this.chunkingPatterns = this.chunkingPatternEventSources.keySet().toArray(new Pattern[this.chunkingPatternEventSources.size()]);
}
Also used : Pattern(java.util.regex.Pattern) CharacterEventSource(org.apereo.portal.character.stream.CharacterEventSource) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with CharacterEventSource

use of org.apereo.portal.character.stream.CharacterEventSource in project uPortal by Jasig.

the class ChunkingEventReader method tryChunking.

private XMLEvent tryChunking(StartElement startElement) throws XMLStreamException {
    QName elementName = startElement.getName();
    CharacterEventSource characterEventSource = this.chunkingElements.get(elementName.getLocalPart());
    while (characterEventSource != null) {
        final XMLEvent previousEvent = this.getPreviousEvent();
        if (previousEvent != null && previousEvent.isStartElement()) {
            this.captureEvent = startElement;
            // It is left open since ATTRIBUTE events can follow a START_ELEMENT event.
            return EVENT_FACTORY.createCharacters("");
        }
        // Capture the characters written out to this point then clear the buffer
        this.captureCharacterDataEvent();
        // Get the generated events for the element
        final XMLEventReader parent = this.getParent();
        characterEventSource.generateCharacterEvents(this.request, parent, startElement, this.characterEvents);
        // Read the next event off the reader
        final XMLEvent nextEvent = parent.nextEvent();
        if (nextEvent.isStartElement()) {
            startElement = nextEvent.asStartElement();
            elementName = startElement.getName();
            characterEventSource = this.chunkingElements.get(elementName.getLocalPart());
        } else {
            return nextEvent;
        }
    }
    return startElement;
}
Also used : QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) CharacterEventSource(org.apereo.portal.character.stream.CharacterEventSource)

Example 4 with CharacterEventSource

use of org.apereo.portal.character.stream.CharacterEventSource in project uPortal by Jasig.

the class ChunkingEventReader method chunkString.

/**
 * Breaks up the String into a List of CharacterEvents based on the configured Map of Patterns
 * to CharacterEventSources
 */
private void chunkString(final List<CharacterEvent> characterEvents, final CharSequence buffer, int patternIndex) {
    // Iterate over the chunking patterns
    for (; patternIndex < this.chunkingPatterns.length; patternIndex++) {
        final Pattern pattern = this.chunkingPatterns[patternIndex];
        final Matcher matcher = pattern.matcher(buffer);
        if (matcher.find()) {
            final CharacterEventSource eventSource = this.chunkingPatternEventSources.get(pattern);
            int prevMatchEnd = 0;
            do {
                // Add all of the text up to the match as a new chunk, use subSequence to avoid
                // extra string alloc
                this.chunkString(characterEvents, buffer.subSequence(prevMatchEnd, matcher.start()), patternIndex + 1);
                // Get the generated CharacterEvents for the match
                final MatchResult matchResult = matcher.toMatchResult();
                eventSource.generateCharacterEvents(this.request, matchResult, characterEvents);
                prevMatchEnd = matcher.end();
            } while (matcher.find());
            // Add any remaining text from the original CharacterDataEvent
            if (prevMatchEnd < buffer.length()) {
                this.chunkString(characterEvents, buffer.subSequence(prevMatchEnd, buffer.length()), patternIndex + 1);
            }
            return;
        }
    }
    // Buffer didn't match anything, just append the string data
    // de-duplication of the event string data
    final String eventString = buffer.toString();
    characterEvents.add(CharacterDataEventImpl.create(eventString));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CharacterEventSource(org.apereo.portal.character.stream.CharacterEventSource) MatchResult(java.util.regex.MatchResult)

Example 5 with CharacterEventSource

use of org.apereo.portal.character.stream.CharacterEventSource in project uPortal by Jasig.

the class StAXSerializingComponentTest method testSerializing.

@Test
public void testSerializing() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final IPortletWindowRegistry portletWindowRegistry = mock(IPortletWindowRegistry.class);
    when(xmlUtilities.getHtmlOutputFactory()).thenReturn(XMLOutputFactory.newFactory());
    // Setup a simple pass-through parent
    staxSerializingComponent.setWrappedComponent(new SimpleStAXSource());
    staxSerializingComponent.setXmlUtilities(xmlUtilities);
    final IPortletWindow portletWindow = mock(IPortletWindow.class);
    when(portletWindowRegistry.getPortletWindow(ArgumentMatchers.eq(request), ArgumentMatchers.any(StartElement.class))).thenReturn(new Tuple<IPortletWindow, StartElement>(portletWindow, null));
    when(portletWindowRegistry.getOrCreateDefaultPortletWindowByLayoutNodeId(ArgumentMatchers.eq(request), ArgumentMatchers.anyString())).thenReturn(portletWindow);
    final PortletContentPlaceholderEventSource contentPlaceholderEventSource = new PortletContentPlaceholderEventSource();
    contentPlaceholderEventSource.setPortletWindowRegistry(portletWindowRegistry);
    final PortletHeaderPlaceholderEventSource headerPlaceholderEventSource = new PortletHeaderPlaceholderEventSource();
    headerPlaceholderEventSource.setPortletWindowRegistry(portletWindowRegistry);
    final PortletTitlePlaceholderEventSource portletTitlePlaceholderEventSource = new PortletTitlePlaceholderEventSource();
    portletTitlePlaceholderEventSource.setPortletWindowRegistry(portletWindowRegistry);
    final PortletHelpPlaceholderEventSource portletHelpPlaceholderEventSource = new PortletHelpPlaceholderEventSource();
    portletHelpPlaceholderEventSource.setPortletWindowRegistry(portletWindowRegistry);
    final Map<String, CharacterEventSource> chunkingElements = new LinkedHashMap<String, CharacterEventSource>();
    chunkingElements.put("portlet", contentPlaceholderEventSource);
    chunkingElements.put("portlet-header", headerPlaceholderEventSource);
    staxSerializingComponent.setChunkingElements(chunkingElements);
    final Map<String, CharacterEventSource> chunkingPatterns = new LinkedHashMap<String, CharacterEventSource>();
    chunkingPatterns.put("\\{up-portlet-title\\(([^\\)]+)\\)\\}", portletTitlePlaceholderEventSource);
    chunkingPatterns.put("\\{up-portlet-help\\(([^\\)]+)\\)\\}", portletHelpPlaceholderEventSource);
    staxSerializingComponent.setChunkingPatterns(chunkingPatterns);
    final PipelineEventReader<CharacterEventReader, CharacterEvent> eventReader = staxSerializingComponent.getEventReader(request, response);
    // Expected events structure, leaving the data out to make it at least a little simpler
    final List<? extends CharacterEvent> expectedEvents = this.getExpectedEvents();
    final Iterator<CharacterEvent> eventItr = eventReader.iterator();
    final Iterator<? extends CharacterEvent> expectedEventsItr = expectedEvents.iterator();
    int eventCount = 0;
    while (expectedEventsItr.hasNext()) {
        eventCount++;
        assertTrue("The number of events returned by the eventReader less than the expected event count of: " + expectedEvents.size() + " was: " + eventCount, eventItr.hasNext());
        final CharacterEvent expectedEvent = expectedEventsItr.next();
        final CharacterEvent event = eventItr.next();
        assertEquals("Events at index " + eventCount + " do not match\n" + expectedEvent + "\n" + event, expectedEvent, event);
    }
    assertFalse("The number of events returned by the eventReader is more than the expected event count of: " + expectedEvents.size(), eventItr.hasNext());
}
Also used : PortletHeaderPlaceholderEventSource(org.apereo.portal.character.stream.PortletHeaderPlaceholderEventSource) IPortletWindowRegistry(org.apereo.portal.portlet.registry.IPortletWindowRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) LinkedHashMap(java.util.LinkedHashMap) StartElement(javax.xml.stream.events.StartElement) CharacterEventReader(org.apereo.portal.character.stream.CharacterEventReader) PortletHelpPlaceholderEventSource(org.apereo.portal.character.stream.PortletHelpPlaceholderEventSource) CharacterEvent(org.apereo.portal.character.stream.events.CharacterEvent) PortletTitlePlaceholderEventSource(org.apereo.portal.character.stream.PortletTitlePlaceholderEventSource) CharacterEventSource(org.apereo.portal.character.stream.CharacterEventSource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PortletContentPlaceholderEventSource(org.apereo.portal.character.stream.PortletContentPlaceholderEventSource) Test(org.junit.Test)

Aggregations

CharacterEventSource (org.apereo.portal.character.stream.CharacterEventSource)5 LinkedHashMap (java.util.LinkedHashMap)2 Pattern (java.util.regex.Pattern)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MatchResult (java.util.regex.MatchResult)1 Matcher (java.util.regex.Matcher)1 QName (javax.xml.namespace.QName)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 CharacterEventReader (org.apereo.portal.character.stream.CharacterEventReader)1 PortletContentPlaceholderEventSource (org.apereo.portal.character.stream.PortletContentPlaceholderEventSource)1 PortletHeaderPlaceholderEventSource (org.apereo.portal.character.stream.PortletHeaderPlaceholderEventSource)1 PortletHelpPlaceholderEventSource (org.apereo.portal.character.stream.PortletHelpPlaceholderEventSource)1 PortletTitlePlaceholderEventSource (org.apereo.portal.character.stream.PortletTitlePlaceholderEventSource)1 CharacterEvent (org.apereo.portal.character.stream.events.CharacterEvent)1 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)1 IPortletWindowRegistry (org.apereo.portal.portlet.registry.IPortletWindowRegistry)1 StAXSerializingComponent (org.apereo.portal.rendering.StAXSerializingComponent)1