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;
}
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()]);
}
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;
}
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));
}
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());
}
Aggregations