Re: [xwiki-devs] [xwiki-notifications] r25544 - in contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core: . src/main/java/org/xwiki/annotation src/main/java/org/xwiki/annotation/internal src/main/java/org/xwiki/annotation/internal/content src/main/java/or
On Fri, Dec 4, 2009 at 16:25, lucaa <[email protected]> wrote:
Author: lucaa Date: 2009-12-04 16:25:46 +0100 (Fri, 04 Dec 2009) New Revision: 25544
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/WhiteSpaceContentAlterer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/filter/WhiteSpaceFilter.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java
XHTMLAnnotationRenderer seems wrong, it's a specialized XHTML renderer so it should be the other way around: AnnotationXHTMLRenderer
contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java Modified: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/pom.xml contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt Log: First prototype for the annotations renderer API with the default XHTML renderer.
Modified: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/pom.xml =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/pom.xml 2009-12-04 15:18:58 UTC (rev 25543) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/pom.xml 2009-12-04 15:25:46 UTC (rev 25544) @@ -21,11 +21,17 @@ </dependency> <dependency> <groupId>org.xwiki.platform</groupId> - <artifactId>xwiki-core-observation-local</artifactId> + <artifactId>xwiki-core-rendering-api</artifactId> <version>${platform.core.version}</version> <scope>provided</scope> </dependency> <dependency> + <groupId>org.xwiki.platform</groupId> + <artifactId>xwiki-core-rendering-syntax-wikimodel</artifactId> + <version>${platform.core.version}</version> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.xwiki.platform</groupId> <artifactId>xwiki-core-shared-tests</artifactId> <version>${platform.core.version}</version> @@ -52,7 +58,8 @@ <configuration> <excludes> org/xwiki/annotation/Annotation.java, - org/xwiki/annotation/internal/target/DefaultDocumentContentTarget.java + org/xwiki/annotation/internal/target/DefaultDocumentContentTarget.java, + org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java </excludes> </configuration> </plugin>
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/WhiteSpaceContentAlterer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/WhiteSpaceContentAlterer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/WhiteSpaceContentAlterer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,51 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.content; + +import org.xwiki.annotation.content.filter.Filter; +import org.xwiki.component.annotation.Component; +import org.xwiki.component.annotation.Requirement; + +/** + * Content alterer that filters out all spaces. + * + * @version $Id$ + */ +@Component("whitespace") +public class WhiteSpaceContentAlterer extends AbstractFilterContentAlterer +{ + /** + * Whitespace filter used by this alterer. + */ + @Requirement("whitespace") + private Filter whiteSpaceFilter; + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.internal.content.AbstractFilterContentAlterer#getFilter() + */ + @Override + protected Filter getFilter() + { + return whiteSpaceFilter; + } + +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/WhiteSpaceContentAlterer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/filter/WhiteSpaceFilter.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/filter/WhiteSpaceFilter.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/filter/WhiteSpaceFilter.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,43 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.content.filter; + +import org.xwiki.annotation.content.filter.Filter; +import org.xwiki.component.annotation.Component; + +/** + * Filters white spaces out of a text. + * + * @version $Id$ + */ +@Component("whitespace") +public class WhiteSpaceFilter implements Filter +{ + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.content.filter.Filter#accept(java.lang.Character) + */ + public boolean accept(Character c) + { + // check it not to be a whitespace but also not a space (should cover all whitespaces + unbreakable spaces) + return !Character.isWhitespace(c) && !Character.isSpaceChar(c); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/filter/WhiteSpaceFilter.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,372 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.renderer; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +import org.xwiki.annotation.Annotation; +import org.xwiki.annotation.content.AlteredContent; +import org.xwiki.annotation.content.ContentAlterer; +import org.xwiki.annotation.renderer.AnnotationListener; +import org.xwiki.rendering.internal.renderer.BasicLinkRenderer; +import org.xwiki.rendering.listener.Link; +import org.xwiki.rendering.listener.LinkType; +import org.xwiki.rendering.listener.Listener; +import org.xwiki.rendering.listener.QueueListener; +import org.xwiki.rendering.listener.chaining.ChainingListener; +import org.xwiki.rendering.listener.chaining.EmptyBlockChainingListener; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.LinkLabelGenerator; +import org.xwiki.rendering.syntax.Syntax; + +/** + * Generates annotation events (as per the {@link AnnotationListener} specification) in a chain, to the next listener in + * the chain. <br /> + * It operates by buffering all events, creating the plain text representation of the listened content, mapping the + * annotations on this content and identifying the events in the stream that hold the start and end of the annotations. + * On sending the buffered events, the events containing annotations will be split and annotation events will be sent + * for them. + * + * @version $Id$ + */ +public class AnnotationGeneratorChainingListener extends QueueListener implements ChainingListener +{ + /** + * Version number of this class. + */ + private static final long serialVersionUID = -2790330640900288463L; + + /** + * To generate a string representation of a link that we output when no link label generator exist or when the link + * is an external link (ie not a document link). + */ + private BasicLinkRenderer linkRenderer = new BasicLinkRenderer(); + + /** + * Generate link label. + */ + private LinkLabelGenerator linkLabelGenerator; + + /** + * The chain listener from which this listener is part of. + */ + private ListenerChain chain; + + /** + * Buffer to store the plain text version of the content to be rendered, so that annotations are mapped on it. + */ + private StringBuffer plainTextContent = new StringBuffer(); + + /** + * Map to store the ranges in the plainTextContent and their corresponding events. The ranges will be stored by + * their end index (inclusive) and ordered from smallest to biggest. + */ + private SortedMap<Integer, Event> eventsMapping = new TreeMap<Integer, Event>(); + + /** + * The collection of annotations to generate annotation events for, by default the empty list. + */ + private Collection<Annotation> annotations = Collections.<Annotation> emptyList(); + + /** + * Cleaner for the annotation selection text, so that it can be mapped on the content. + */ + private ContentAlterer selectionAlterer; + + /** + * Map holding the collection of annotations starting in the specified event. Used to send annotation events in the + * events stream when consuming queued events. + */ + private Map<Event, Collection<Annotation>> startEvents = new HashMap<Event, Collection<Annotation>>(); + + /** + * Map holding the collection of annotations ending in the specified event. Used to send annotation events in the + * events stream when consuming queued events. + */ + private Map<Event, Collection<Annotation>> endEvents = new HashMap<Event, Collection<Annotation>>(); + + /** + * Builds an annotation generator listener from the passed link generator in the passed chain. + * + * @param linkLabelGenerator the generator for link labels so that the annotation text can be recognized. + * @param selectionAlterer cleaner for the annotation selection text, so that it can be mapped on the content + * @param listenerChain the chain in which this listener is part of + */ + public AnnotationGeneratorChainingListener(LinkLabelGenerator linkLabelGenerator, ContentAlterer selectionAlterer, + ListenerChain listenerChain) + { + this.linkLabelGenerator = linkLabelGenerator; + this.chain = listenerChain; + this.selectionAlterer = selectionAlterer; + } + + /** + * @return the state of the current empty block. + */ + protected EmptyBlockChainingListener getEmptyBlockState() + { + return (EmptyBlockChainingListener) getListenerChain().getListener(EmptyBlockChainingListener.class); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.QueueListener#onWord(java.lang.String) + */ + @Override + public void onWord(String word) + { + // queue this event + super.onWord(word); + // put it in the buffer + plainTextContent.append(word); + // store the mapping of the range to the just added event + eventsMapping.put(plainTextContent.length() - 1, getLast()); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.QueueListener#onSpecialSymbol(char) + */ + @Override + public void onSpecialSymbol(char symbol) + { + super.onSpecialSymbol(symbol); + plainTextContent.append("" + symbol); + eventsMapping.put(plainTextContent.length() - 1, getLast()); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.QueueListener#onVerbatim(java.lang.String, boolean, java.util.Map) + */ + @Override + public void onVerbatim(String protectedString, boolean isInline, Map<String, String> parameters) + { + super.onVerbatim(protectedString, isInline, parameters); + plainTextContent.append(protectedString); + eventsMapping.put(plainTextContent.length() - 1, getLast()); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.QueueListener#onRawText(java.lang.String, org.xwiki.rendering.syntax.Syntax) + */ + @Override + public void onRawText(String text, Syntax syntax) + { + // Store the raw text as it is ftm. Should handle syntax in the future + super.onRawText(text, syntax); + plainTextContent.append(text); + eventsMapping.put(plainTextContent.length() - 1, getLast()); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.chaining.AbstractChainingListener#endLink(org.xwiki.rendering.listener.Link, + * boolean, java.util.Map) + */ + @Override + public void endLink(Link link, boolean isFreeStandingURI, Map<String, String> parameters) + { + super.endLink(link, isFreeStandingURI, parameters); + // special handling only if the link is empty + if (getEmptyBlockState().isCurrentContainerBlockEmpty()) { + String linkPlainText = ""; + if (link.getType() == LinkType.DOCUMENT && this.linkLabelGenerator != null) { + linkPlainText = this.linkLabelGenerator.generate(link); + } else { + linkPlainText = this.linkRenderer.renderLinkReference(link); + } + // TODO: should clean spaces out of the linkPlainText (but then the event offset inside this event wouldn't + // work anymore) + plainTextContent.append(linkPlainText); + // TODO: maybe should store the begin link event... + eventsMapping.put(plainTextContent.length() - 1, getLast()); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void endDocument(Map<String, String> parameters) + { + super.endDocument(parameters); + + mapAnnotations(); + + // now get the next listener in the chain and consume all events to it + ChainingListener renderer = chain.getNextListener(getClass()); + + // consumeEvents should check if the listener is annotation listener or not and send annotation events + consumeEvents(renderer); + } + + /** + * Helper method to map the annotations on the plainTextContent and identify the events where annotations start and + * end. + */ + private void mapAnnotations() + { + for (Annotation ann : annotations) { + // clean it up and its context + AlteredContent cleanedContext = selectionAlterer.alter(ann.getSelectionContext()); + // find it in the plaintextContent + int contextIndex = plainTextContent.indexOf(cleanedContext.getContent().toString()); + // find the selection inside the context in the plainTextContent + // assume at this point that the selection appears only once in the context + String alteredSelection = selectionAlterer.alter(ann.getInitialSelection()).getContent().toString(); + int selectionIndexInContext = cleanedContext.getContent().toString().indexOf(alteredSelection); + // check that the context is in the plainText representation and selection was found inside it + if (contextIndex >= 0 && selectionIndexInContext >= 0) { + // compute annotation index in the plain text repr + int annotationIndex = contextIndex + selectionIndexInContext; + // get the start and end events for the annotation + // TODO: should also store the offsets of the start and end annotation in the event + Event startEvt = getEventForIndex(annotationIndex).getValue(); + Event endEvt = getEventForIndex(annotationIndex + alteredSelection.length() - 1).getValue(); + if (startEvt != null & endEvt != null) { + // store them in the maps + addEventMapping(startEvt, ann, startEvents); + addEventMapping(endEvt, ann, endEvents); + } else { + // cannot find the events for the start and / or end of annotation, ignore it + // TODO: mark it somehow... + continue; + } + } else { + // cannot find the context of the annotation or the annotation selection cannot be found in the + // annotation context, ignore it + // TODO: mark it somehow... + continue; + } + } + } + + /** + * Adds a mapping for the passed event and annotation in the specified map. + * + * @param event the event to add mapping for + * @param annotation the annotation to add mapping for + * @param map the map in which to add mapping + */ + private void addEventMapping(Event event, Annotation annotation, Map<Event, Collection<Annotation>> map) + { + Collection<Annotation> mappedCollection = endEvents.get(event); + if (mappedCollection == null) { + mappedCollection = new ArrayList<Annotation>(); + map.put(event, mappedCollection); + } + mappedCollection.add(annotation); + } + + /** + * Finds and returns the event in whose range (generated text in the plainTextRepresentation) the passed index is + * falling. + * + * @param index the index to find event for + * @return the pair of startIndex, Event in whose range index falls + */ + private Map.Entry<Integer, Event> getEventForIndex(int index) + { + // iterate through all the mappings, for start index and event + Iterator<Map.Entry<Integer, Event>> rangeIt = eventsMapping.entrySet().iterator(); + Map.Entry<Integer, Event> range = rangeIt.next(); + while (rangeIt.hasNext() && range.getKey() < index) { + // while there is a next event and the index to find is after behind the end position, try the next range + range = rangeIt.next(); + } + return range; + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.QueueListener#consumeEvents(org.xwiki.rendering.listener.Listener) + */ + @Override + public void consumeEvents(Listener listener) + { + if (listener instanceof AnnotationListener) { + consumeAnnotationEvents((AnnotationListener) listener); + } else { + super.consumeEvents(listener); + } + } + + /** + * Helper function to consume the events queued in this listener with annotation awareness, i.e. send annotation + * events to the passed listener. + * + * @param listener the annotation listener to send the annotation events to + */ + private void consumeAnnotationEvents(AnnotationListener listener) + { + // FIXME: take annotation offsets into account and split events + while (!isEmpty()) { + Event event = remove(); + // annotations starting before + if (startEvents.get(event) != null) { + for (Annotation startAnn : startEvents.get(event)) { + listener.beginAnnotation(startAnn); + } + } + event.eventType.fireEvent(listener, event.eventParameters); + // annotations ending after + if (endEvents.get(event) != null) { + for (Annotation endAnn : endEvents.get(event)) { + listener.endAnnotation(endAnn); + } + } + } + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.listener.chaining.ChainingListener#getListenerChain() + */ + public ListenerChain getListenerChain() + { + return chain; + } + + /** + * Sets the collections of annotations to identify on the listened content and send notifications for. + * + * @param annotations the collection of annotations to generate events for + */ + public void setAnnotations(Collection<Annotation> annotations) + { + this.annotations = annotations; + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,94 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.renderer; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.xwiki.annotation.Annotation; +import org.xwiki.annotation.renderer.AnnotationChainingPrintRenderer; +import org.xwiki.rendering.internal.renderer.xhtml.XHTMLChainingRenderer; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.xhtml.XHTMLImageRenderer; +import org.xwiki.rendering.renderer.xhtml.XHTMLLinkRenderer; + +/** + * Extends the default XHTML renderer to add handling of annotations.<br /> + * FIXME: this implementation is a very simple handling of annotation events, it should handle annotation markers + * splitting & nesting in other elements. + * + * @version $Id$ + */ +public class XHTMLAnnotationChainingPrintRenderer extends XHTMLChainingRenderer implements + AnnotationChainingPrintRenderer +{ + /** + * The annotation marker element in HTML. + */ + private static final String ANNOTATION_MARKER = "span"; + + /** + * Constructor from super class. + * + * @param linkRenderer the renderer for links + * @param imageRenderer the renderer for images + * @param listenerChain the listener chain in which to add this listener + */ + public XHTMLAnnotationChainingPrintRenderer(XHTMLLinkRenderer linkRenderer, XHTMLImageRenderer imageRenderer, + ListenerChain listenerChain) + { + super(linkRenderer, imageRenderer, listenerChain); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AnnotationListener#beginAnnotation(Annotation) + */ + public void beginAnnotation(Annotation annotation) + { + Map<String, String> attributes = new LinkedHashMap<String, String>(); + + attributes.put("class", createAnnotationClass(annotation.getId()));
why don't you put the annotationId as an id instead of class if it's a unique id ?
+ attributes.put("title", annotation.getAnnotation().toString());
I generally try to avoid as much as possible using Object#toString() for anything else that debugging. If Annotation officially has a title it should have a getTitle() method or something like that.
+ getXHTMLWikiPrinter().printXMLStartElement(ANNOTATION_MARKER, attributes); + } + + /** + * Creates the class value of the annotation. + * + * @param annotationId the id of the annotation + * @return the class value for the passed annotation + */ + private String createAnnotationClass(int annotationId) + { + return "annotation ID" + annotationId; + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AnnotationListener#endAnnotation(Annotation) + */ + public void endAnnotation(Annotation annotation) + { + getXHTMLWikiPrinter().printXMLEndElement(ANNOTATION_MARKER); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,68 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.renderer; + +import org.xwiki.annotation.renderer.AbstractAnnotationRenderer; +import org.xwiki.annotation.renderer.AnnotationChainingPrintRenderer; +import org.xwiki.component.annotation.Component; +import org.xwiki.component.annotation.InstantiationStrategy; +import org.xwiki.component.annotation.Requirement; +import org.xwiki.component.descriptor.ComponentInstantiationStrategy; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.xhtml.XHTMLImageRenderer; +import org.xwiki.rendering.renderer.xhtml.XHTMLLinkRenderer; + +/** + * Renders annotations in the XHTML format. + * + * @version $Id$ + */ +@Component("annotations/xhtml/1.0")
You should not use / in the syntax name since it's the syntax name/syntax version separator also AnnotatedXHTMLBlockRenderer use "annotatedxhtml/1.0" so it's not consistant
+@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) +public class XHTMLAnnotationRenderer extends AbstractAnnotationRenderer +{ + /** + * To render link events into XHTML. This is done so that it's pluggable because link rendering depends on how the + * underlying system wants to handle it. For example for XWiki we check if the document exists, we get the document + * URL, etc. + */ + @Requirement + private XHTMLLinkRenderer linkRenderer; + + /** + * To render image events into XHTML. This is done so that it's pluggable because image rendering depends on how the + * underlying system wants to handle it. For example for XWiki we check if the image exists as a document + * attachments, we get its URL, etc. + */ + @Requirement + private XHTMLImageRenderer imageRenderer; + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AbstractAnnotationRenderer#getAnnotationPrintRenderer(ListenerChain) + */ + @Override + public AnnotationChainingPrintRenderer getAnnotationPrintRenderer(ListenerChain chain) + { + return new XHTMLAnnotationChainingPrintRenderer(linkRenderer, imageRenderer, chain); + } + +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,107 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import java.util.Collection; + +import org.xwiki.annotation.Annotation; +import org.xwiki.annotation.content.ContentAlterer; +import org.xwiki.annotation.internal.renderer.AnnotationGeneratorChainingListener; +import org.xwiki.component.annotation.Requirement; +import org.xwiki.component.phase.Initializable; +import org.xwiki.component.phase.InitializationException; +import org.xwiki.rendering.listener.chaining.EmptyBlockChainingListener; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.AbstractChainingPrintRenderer; +import org.xwiki.rendering.renderer.LinkLabelGenerator; + +/** + * Abstract class for annotation renderer, any specific syntax renderer should implement this class and provide the + * specific annotation listener. + * + * @version $Id$ + */ +public abstract class AbstractAnnotationRenderer extends AbstractChainingPrintRenderer implements Initializable, + AnnotationPrintRenderer +{ + /** + * The link label generator for this renderer. + */ + @Requirement + protected LinkLabelGenerator linkLabelGenerator; + + /** + * Selection cleaner so that the selection can be mapped on the content. <br /> + * TODO: not really sure if this is the right place for this pull, but the annotations generator is not a component + * so it cannot 'require' it. + */ + @Requirement("whitespace") + protected ContentAlterer selectionAlterer; + + /** + * The annotations generator listener to use in this renderer. + */ + private AnnotationGeneratorChainingListener annotationsGenerator; + + /** + * {@inheritDoc} + * + * @see org.xwiki.component.phase.Initializable#initialize() + */ + public void initialize() throws InitializationException + { + ListenerChain chain = new ListenerChain(); + setListenerChain(chain); + + // Construct the listener chain in the right order. Listeners early in the chain are called before listeners + // placed later in the chain. + chain.addListener(this); + chain.addListener(new EmptyBlockChainingListener(chain)); + // will generate annotation events to the next listener in chain + + // TODO: could also have the annotation listener passed to the annotationgenerator chaininglistener (which would + // become a print renderer and delegate all printing functions to the aggregated annotation print renderer). + // All buffered events will be consumed to this aggregated listener. This way the next element in chain is + // better coupled in the listener that knows how to send such events and we don't need to test in the annotation + // generator that the next listener in the chain can receive annotation events. However I like the chaining idea + // more (because one could add any number of other annotation listeners after this generator in the chain). + annotationsGenerator = new AnnotationGeneratorChainingListener(linkLabelGenerator, selectionAlterer, chain); + chain.addListener(annotationsGenerator); + // the actual annotations renderer + chain.addListener(getAnnotationPrintRenderer(chain)); + } + + /** + * @param chain the chain in which the renderer needs to be added. + * @return the annotation listener to which the annotation events generated by this renderer should be sent (the + * actual renderer of annotated content) + */ + public abstract AnnotationChainingPrintRenderer getAnnotationPrintRenderer(ListenerChain chain); + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AnnotationPrintRenderer#setAnnotations(java.util.Collection) + */ + public void setAnnotations(Collection<Annotation> annotations) + { + this.annotationsGenerator.setAnnotations(annotations); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,34 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import org.xwiki.rendering.listener.chaining.ChainingListener; +import org.xwiki.rendering.renderer.PrintRenderer; + +/** + * Define the chaining annotation print renderer. To be implemented by extending a ChainingPrintRenderer and + * implementing the functions in the {@link AnnotationListener} interface. + * + * @version $Id$ + */ +public interface AnnotationChainingPrintRenderer extends AnnotationListener, ChainingListener, PrintRenderer +{ + +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,49 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import org.xwiki.annotation.Annotation; +import org.xwiki.rendering.listener.Listener; + +/** + * Extends the standard XWiki events listener by adding a set of events for start and end of an annotation. + * + * @version $Id$ + */ +public interface AnnotationListener extends Listener +{ + /** + * Signals the start of an annotation. Note that this event will only be fired once, splitting the annotation + * markers in multiple pieces and correctly imbricating them in the rendered syntax (such as multiple spans across + * multiple block elements in the XHTML renderer) is the responsibility of the renderer. + * + * @param annotation the annotation whose selected text starts + */ + void beginAnnotation(Annotation annotation); + + /** + * Signals the end of an annotation. Note that this event will only be fired once, splitting the annotation markers + * in multiple pieces and correctly imbricating them in the rendered syntax (such as multiple spans across multiple + * block elements in the XHTML renderer) is the responsibility of the renderer. + * + * @param annotation the annotation whose selected text ends + */ + void endAnnotation(Annotation annotation); +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,42 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import java.util.Collection; + +import org.xwiki.annotation.Annotation; +import org.xwiki.component.annotation.ComponentRole; +import org.xwiki.rendering.renderer.PrintRenderer; + +/** + * An annotations print renderer is a {@link PrintRenderer} that also renders annotations on the content. + * + * @version $Id$ + */ +@ComponentRole +public interface AnnotationPrintRenderer extends PrintRenderer +{ + /** + * Sets the annotations to render on the content. + * + * @param annotations the collection of annotations to render + */ + void setAnnotations(Collection<Annotation> annotations); +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Modified: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt 2009-12-04 15:18:58 UTC (rev 25543) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt 2009-12-04 15:25:46 UTC (rev 25544) @@ -1,5 +1,8 @@ org.xwiki.annotation.internal.DefaultAnnotationService org.xwiki.annotation.internal.content.XWiki20SyntaxContentAlterer +org.xwiki.annotation.internal.content.WhiteSpaceContentAlterer org.xwiki.annotation.internal.content.filter.XWiki20SyntaxFilter +org.xwiki.annotation.internal.content.filter.WhiteSpaceFilter org.xwiki.annotation.internal.target.DefaultDocumentContentTarget -org.xwiki.annotation.internal.selection.DefaultSelectionService \ No newline at end of file +org.xwiki.annotation.internal.selection.DefaultSelectionService +org.xwiki.annotation.internal.renderer.XHTMLAnnotationRenderer \ No newline at end of file
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,105 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.content; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.xwiki.test.AbstractComponentTestCase; + +/** + * @version $Id$ + */ +@RunWith(Parameterized.class) +public class WhiteSpaceContentAltererTest extends AbstractComponentTestCase +{ + /** + * The initial String to alter. + */ + private String initial; + + /** + * The expected altered string. + */ + private String altered; + + /** + * The content alterer to test. + */ + private ContentAlterer alterer; + + /** + * @param initial the original string + * @param altered the altered string after being whitespace filtered + */ + public WhiteSpaceContentAltererTest(String initial, String altered) + { + this.initial = initial; + this.altered = altered; + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.test.AbstractComponentTestCase#setUp() + */ + @Override + public void setUp() throws Exception + { + super.setUp(); + alterer = getComponentManager().lookup(ContentAlterer.class, "whitespace"); + } + + /** + * @return list of corpus files to instantiate tests for + */ + @Parameters + public static Collection<String[]> data() + { + Collection<String[]> params = new ArrayList<String[]>(); + // unbreakable space + params.add(new String[] {"not to be", "nottobe"}); + // tabs + params.add(new String[] {"to be or not\tto be", "tobeornottobe"}); + // commas, signs with regular spaces + params.add(new String[] {"roses, see I in her cheeks;", "roses,seeIinhercheeks;"}); + // new lines + params.add(new String[] {"eyes nothing\nlike the sun", "eyesnothinglikethesun"}); + // new line carriage return + params.add(new String[] {"eyes\n\rnothing", "eyesnothing"}); + return params; + } + + /** + * Tests that the content alterer filters correctly the characters out of the Strings. + */ + @Test + public void testFiltering() + { + AlteredContent alteredContent = alterer.alter(initial); + assertEquals(altered, alteredContent.getContent().toString()); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,166 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import static org.junit.Assert.assertEquals; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.xwiki.annotation.TestDocumentFactory; +import org.xwiki.rendering.block.XDOM; +import org.xwiki.rendering.parser.Parser; +import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; +import org.xwiki.rendering.renderer.printer.WikiPrinter; +import org.xwiki.rendering.syntax.Syntax; +import org.xwiki.rendering.transformation.TransformationManager; +import org.xwiki.test.AbstractComponentTestCase; + +/** + * Renderer tests for the XHTML annotations renderer, from the test files. + * + * @version $Id$ + */ +@RunWith(Parameterized.class) +public class XHTMLAnnotationRendererTest extends AbstractComponentTestCase +{ + /** + * Document description files to run this test for. + */ + private static Collection<String[]> files = new ArrayList<String[]>(); + + /** + * The annotations renderer hint. + */ + private static final String ANNOTATIONS_RENDERER_HINT = "annotations/xhtml/1.0"; + + /** + * Mock document to run tests for. + */ + protected String docName; + + static { + // FIXME: checkstyle is so gonna shout when this will be longer than 30 files + addFileToTest("Document1"); + addFileToTest("Document2"); + addFileToTest("Document3"); + addFileToTest("Document4"); + addFileToTest("Document5"); + addFileToTest("Document6"); + addFileToTest("Document7"); + addFileToTest("Document8"); + addFileToTest("Document9"); + addFileToTest("Document10"); + addFileToTest("Document11"); + addFileToTest("Document12"); + addFileToTest("Document13"); + addFileToTest("Document14"); + addFileToTest("Document15"); + addFileToTest("Document16"); + addFileToTest("Document17"); + addFileToTest("Document18"); + } + + /** + * Creates a test for the passed document. Will be instantiated by the parameterized runner for all the parameters. + * + * @param docName the document (and corpus filename) to run tests for + */ + public XHTMLAnnotationRendererTest(String docName) + { + this.docName = docName; + } + + /** + * Adds a file to the list of files to run tests for. + * + * @param docName the name of the document / file to test + */ + private static void addFileToTest(String docName) + { + files.add(new String[] {docName}); + } + + /** + * @return list of corpus files to instantiate tests for + */ + @Parameters + public static Collection<String[]> data() + { + return files; + } + + /** + * Test rendering the annotations in the document description file results in the annotated html. + * + * @throws Exception in case something goes wrong looking up components and rendering + */ + @Test + public void getAnnotatedHTML() throws Exception + { + Parser parser = getComponentManager().lookup(Parser.class, Syntax.XWIKI_2_0.toIdString()); + XDOM xdom = parser.parse(new StringReader(TestDocumentFactory.getDocument(docName).getSource())); + + // run transformations + TransformationManager transformationManager = getComponentManager().lookup(TransformationManager.class); + transformationManager.performTransformations(xdom, Syntax.XWIKI_2_0); + + AnnotationPrintRenderer renderer = + getComponentManager().lookup(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT); + WikiPrinter printer = new DefaultWikiPrinter(); + renderer.setPrinter(printer); + // set the annotations for this renderer + renderer.setAnnotations(TestDocumentFactory.getDocument(docName).getSafeAnnotations()); + + xdom.traverse(renderer); + + assertEquals(TestDocumentFactory.getDocument(docName).getAnnotatedContent(), printer.toString()); + } + + /** + * Test rendering with the annotations renderer but without annotations doesn't alter the content. + * + * @throws Exception in case something goes wrong looking up components and rendering + */ + @Test + public void getAnnotatedHTMLWithoutAnnotations() throws Exception + { + Parser parser = getComponentManager().lookup(Parser.class, Syntax.XWIKI_2_0.toIdString()); + XDOM xdom = parser.parse(new StringReader(TestDocumentFactory.getDocument(docName).getSource())); + + // run transformations + TransformationManager transformationManager = getComponentManager().lookup(TransformationManager.class); + transformationManager.performTransformations(xdom, Syntax.XWIKI_2_0); + + AnnotationPrintRenderer renderer = + getComponentManager().lookup(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT); + WikiPrinter printer = new DefaultWikiPrinter(); + renderer.setPrinter(printer); + + xdom.traverse(renderer); + + assertEquals(TestDocumentFactory.getDocument(docName).getRenderedContent(), printer.toString()); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
_______________________________________________ notifications mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/notifications
-- Thomas Mortagne
Hi Thomas, thanks a lot for the review. On 12/04/2009 07:20 PM, Thomas Mortagne wrote:
On Fri, Dec 4, 2009 at 16:25, lucaa<[email protected]> wrote:
Author: lucaa Date: 2009-12-04 16:25:46 +0100 (Fri, 04 Dec 2009) New Revision: 25544
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/WhiteSpaceContentAlterer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/content/filter/WhiteSpaceFilter.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/AnnotationGeneratorChainingListener.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java
XHTMLAnnotationRenderer seems wrong, it's a specialized XHTML renderer so it should be the other way around: AnnotationXHTMLRenderer
contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java Modified: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/pom.xml contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt Log: First prototype for the annotations renderer API with the default XHTML renderer.
[snip]
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,94 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.renderer; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.xwiki.annotation.Annotation; +import org.xwiki.annotation.renderer.AnnotationChainingPrintRenderer; +import org.xwiki.rendering.internal.renderer.xhtml.XHTMLChainingRenderer; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.xhtml.XHTMLImageRenderer; +import org.xwiki.rendering.renderer.xhtml.XHTMLLinkRenderer; + +/** + * Extends the default XHTML renderer to add handling of annotations.<br /> + * FIXME: this implementation is a very simple handling of annotation events, it should handle annotation markers + * splitting& nesting in other elements. + * + * @version $Id$ + */ +public class XHTMLAnnotationChainingPrintRenderer extends XHTMLChainingRenderer implements + AnnotationChainingPrintRenderer +{ + /** + * The annotation marker element in HTML. + */ + private static final String ANNOTATION_MARKER = "span"; + + /** + * Constructor from super class. + * + * @param linkRenderer the renderer for links + * @param imageRenderer the renderer for images + * @param listenerChain the listener chain in which to add this listener + */ + public XHTMLAnnotationChainingPrintRenderer(XHTMLLinkRenderer linkRenderer, XHTMLImageRenderer imageRenderer, + ListenerChain listenerChain) + { + super(linkRenderer, imageRenderer, listenerChain); + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AnnotationListener#beginAnnotation(Annotation) + */ + public void beginAnnotation(Annotation annotation) + { + Map<String, String> attributes = new LinkedHashMap<String, String>(); + + attributes.put("class", createAnnotationClass(annotation.getId()));
why don't you put the annotationId as an id instead of class if it's a unique id ?
It's not going to be unique. There will be multiple annotation marker spans, when the implementation will be finished, to wrap all pieces of annotated text (in different block elements, for example).
+ attributes.put("title", annotation.getAnnotation().toString());
I generally try to avoid as much as possible using Object#toString() for anything else that debugging. If Annotation officially has a title it should have a getTitle() method or something like that.
It's just a bad naming for the 'getTitle()' method, it does return the title of the annotation but it's a CharSequence (I am still to refactor all those CharSequences around this code). Thanks, Anca
+ getXHTMLWikiPrinter().printXMLStartElement(ANNOTATION_MARKER, attributes); + } + + /** + * Creates the class value of the annotation. + * + * @param annotationId the id of the annotation + * @return the class value for the passed annotation + */ + private String createAnnotationClass(int annotationId) + { + return "annotation ID" + annotationId; + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AnnotationListener#endAnnotation(Annotation) + */ + public void endAnnotation(Annotation annotation) + { + getXHTMLWikiPrinter().printXMLEndElement(ANNOTATION_MARKER); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationChainingPrintRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,68 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.internal.renderer; + +import org.xwiki.annotation.renderer.AbstractAnnotationRenderer; +import org.xwiki.annotation.renderer.AnnotationChainingPrintRenderer; +import org.xwiki.component.annotation.Component; +import org.xwiki.component.annotation.InstantiationStrategy; +import org.xwiki.component.annotation.Requirement; +import org.xwiki.component.descriptor.ComponentInstantiationStrategy; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.xhtml.XHTMLImageRenderer; +import org.xwiki.rendering.renderer.xhtml.XHTMLLinkRenderer; + +/** + * Renders annotations in the XHTML format. + * + * @version $Id$ + */ +@Component("annotations/xhtml/1.0")
You should not use / in the syntax name since it's the syntax name/syntax version separator also AnnotatedXHTMLBlockRenderer use "annotatedxhtml/1.0" so it's not consistant
+@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) +public class XHTMLAnnotationRenderer extends AbstractAnnotationRenderer +{ + /** + * To render link events into XHTML. This is done so that it's pluggable because link rendering depends on how the + * underlying system wants to handle it. For example for XWiki we check if the document exists, we get the document + * URL, etc. + */ + @Requirement + private XHTMLLinkRenderer linkRenderer; + + /** + * To render image events into XHTML. This is done so that it's pluggable because image rendering depends on how the + * underlying system wants to handle it. For example for XWiki we check if the image exists as a document + * attachments, we get its URL, etc. + */ + @Requirement + private XHTMLImageRenderer imageRenderer; + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AbstractAnnotationRenderer#getAnnotationPrintRenderer(ListenerChain) + */ + @Override + public AnnotationChainingPrintRenderer getAnnotationPrintRenderer(ListenerChain chain) + { + return new XHTMLAnnotationChainingPrintRenderer(linkRenderer, imageRenderer, chain); + } + +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/internal/renderer/XHTMLAnnotationRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,107 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import java.util.Collection; + +import org.xwiki.annotation.Annotation; +import org.xwiki.annotation.content.ContentAlterer; +import org.xwiki.annotation.internal.renderer.AnnotationGeneratorChainingListener; +import org.xwiki.component.annotation.Requirement; +import org.xwiki.component.phase.Initializable; +import org.xwiki.component.phase.InitializationException; +import org.xwiki.rendering.listener.chaining.EmptyBlockChainingListener; +import org.xwiki.rendering.listener.chaining.ListenerChain; +import org.xwiki.rendering.renderer.AbstractChainingPrintRenderer; +import org.xwiki.rendering.renderer.LinkLabelGenerator; + +/** + * Abstract class for annotation renderer, any specific syntax renderer should implement this class and provide the + * specific annotation listener. + * + * @version $Id$ + */ +public abstract class AbstractAnnotationRenderer extends AbstractChainingPrintRenderer implements Initializable, + AnnotationPrintRenderer +{ + /** + * The link label generator for this renderer. + */ + @Requirement + protected LinkLabelGenerator linkLabelGenerator; + + /** + * Selection cleaner so that the selection can be mapped on the content.<br /> + * TODO: not really sure if this is the right place for this pull, but the annotations generator is not a component + * so it cannot 'require' it. + */ + @Requirement("whitespace") + protected ContentAlterer selectionAlterer; + + /** + * The annotations generator listener to use in this renderer. + */ + private AnnotationGeneratorChainingListener annotationsGenerator; + + /** + * {@inheritDoc} + * + * @see org.xwiki.component.phase.Initializable#initialize() + */ + public void initialize() throws InitializationException + { + ListenerChain chain = new ListenerChain(); + setListenerChain(chain); + + // Construct the listener chain in the right order. Listeners early in the chain are called before listeners + // placed later in the chain. + chain.addListener(this); + chain.addListener(new EmptyBlockChainingListener(chain)); + // will generate annotation events to the next listener in chain + + // TODO: could also have the annotation listener passed to the annotationgenerator chaininglistener (which would + // become a print renderer and delegate all printing functions to the aggregated annotation print renderer). + // All buffered events will be consumed to this aggregated listener. This way the next element in chain is + // better coupled in the listener that knows how to send such events and we don't need to test in the annotation + // generator that the next listener in the chain can receive annotation events. However I like the chaining idea + // more (because one could add any number of other annotation listeners after this generator in the chain). + annotationsGenerator = new AnnotationGeneratorChainingListener(linkLabelGenerator, selectionAlterer, chain); + chain.addListener(annotationsGenerator); + // the actual annotations renderer + chain.addListener(getAnnotationPrintRenderer(chain)); + } + + /** + * @param chain the chain in which the renderer needs to be added. + * @return the annotation listener to which the annotation events generated by this renderer should be sent (the + * actual renderer of annotated content) + */ + public abstract AnnotationChainingPrintRenderer getAnnotationPrintRenderer(ListenerChain chain); + + /** + * {@inheritDoc} + * + * @see org.xwiki.annotation.renderer.AnnotationPrintRenderer#setAnnotations(java.util.Collection) + */ + public void setAnnotations(Collection<Annotation> annotations) + { + this.annotationsGenerator.setAnnotations(annotations); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AbstractAnnotationRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,34 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import org.xwiki.rendering.listener.chaining.ChainingListener; +import org.xwiki.rendering.renderer.PrintRenderer; + +/** + * Define the chaining annotation print renderer. To be implemented by extending a ChainingPrintRenderer and + * implementing the functions in the {@link AnnotationListener} interface. + * + * @version $Id$ + */ +public interface AnnotationChainingPrintRenderer extends AnnotationListener, ChainingListener, PrintRenderer +{ + +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationChainingPrintRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,49 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import org.xwiki.annotation.Annotation; +import org.xwiki.rendering.listener.Listener; + +/** + * Extends the standard XWiki events listener by adding a set of events for start and end of an annotation. + * + * @version $Id$ + */ +public interface AnnotationListener extends Listener +{ + /** + * Signals the start of an annotation. Note that this event will only be fired once, splitting the annotation + * markers in multiple pieces and correctly imbricating them in the rendered syntax (such as multiple spans across + * multiple block elements in the XHTML renderer) is the responsibility of the renderer. + * + * @param annotation the annotation whose selected text starts + */ + void beginAnnotation(Annotation annotation); + + /** + * Signals the end of an annotation. Note that this event will only be fired once, splitting the annotation markers + * in multiple pieces and correctly imbricating them in the rendered syntax (such as multiple spans across multiple + * block elements in the XHTML renderer) is the responsibility of the renderer. + * + * @param annotation the annotation whose selected text ends + */ + void endAnnotation(Annotation annotation); +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationListener.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,42 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import java.util.Collection; + +import org.xwiki.annotation.Annotation; +import org.xwiki.component.annotation.ComponentRole; +import org.xwiki.rendering.renderer.PrintRenderer; + +/** + * An annotations print renderer is a {@link PrintRenderer} that also renders annotations on the content. + * + * @version $Id$ + */ +@ComponentRole +public interface AnnotationPrintRenderer extends PrintRenderer +{ + /** + * Sets the annotations to render on the content. + * + * @param annotations the collection of annotations to render + */ + void setAnnotations(Collection<Annotation> annotations); +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/java/org/xwiki/annotation/renderer/AnnotationPrintRenderer.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Modified: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt 2009-12-04 15:18:58 UTC (rev 25543) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/main/resources/META-INF/components.txt 2009-12-04 15:25:46 UTC (rev 25544) @@ -1,5 +1,8 @@ org.xwiki.annotation.internal.DefaultAnnotationService org.xwiki.annotation.internal.content.XWiki20SyntaxContentAlterer +org.xwiki.annotation.internal.content.WhiteSpaceContentAlterer org.xwiki.annotation.internal.content.filter.XWiki20SyntaxFilter +org.xwiki.annotation.internal.content.filter.WhiteSpaceFilter org.xwiki.annotation.internal.target.DefaultDocumentContentTarget -org.xwiki.annotation.internal.selection.DefaultSelectionService \ No newline at end of file +org.xwiki.annotation.internal.selection.DefaultSelectionService +org.xwiki.annotation.internal.renderer.XHTMLAnnotationRenderer \ No newline at end of file
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,105 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.content; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.xwiki.test.AbstractComponentTestCase; + +/** + * @version $Id$ + */ +@RunWith(Parameterized.class) +public class WhiteSpaceContentAltererTest extends AbstractComponentTestCase +{ + /** + * The initial String to alter. + */ + private String initial; + + /** + * The expected altered string. + */ + private String altered; + + /** + * The content alterer to test. + */ + private ContentAlterer alterer; + + /** + * @param initial the original string + * @param altered the altered string after being whitespace filtered + */ + public WhiteSpaceContentAltererTest(String initial, String altered) + { + this.initial = initial; + this.altered = altered; + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.test.AbstractComponentTestCase#setUp() + */ + @Override + public void setUp() throws Exception + { + super.setUp(); + alterer = getComponentManager().lookup(ContentAlterer.class, "whitespace"); + } + + /** + * @return list of corpus files to instantiate tests for + */ + @Parameters + public static Collection<String[]> data() + { + Collection<String[]> params = new ArrayList<String[]>(); + // unbreakable space + params.add(new String[] {"not to be", "nottobe"}); + // tabs + params.add(new String[] {"to be or not\tto be", "tobeornottobe"}); + // commas, signs with regular spaces + params.add(new String[] {"roses, see I in her cheeks;", "roses,seeIinhercheeks;"}); + // new lines + params.add(new String[] {"eyes nothing\nlike the sun", "eyesnothinglikethesun"}); + // new line carriage return + params.add(new String[] {"eyes\n\rnothing", "eyesnothing"}); + return params; + } + + /** + * Tests that the content alterer filters correctly the characters out of the Strings. + */ + @Test + public void testFiltering() + { + AlteredContent alteredContent = alterer.alter(initial); + assertEquals(altered, alteredContent.getContent().toString()); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/content/WhiteSpaceContentAltererTest.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Added: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java =================================================================== --- contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java (rev 0) +++ contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java 2009-12-04 15:25:46 UTC (rev 25544) @@ -0,0 +1,166 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.annotation.renderer; + +import static org.junit.Assert.assertEquals; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.xwiki.annotation.TestDocumentFactory; +import org.xwiki.rendering.block.XDOM; +import org.xwiki.rendering.parser.Parser; +import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; +import org.xwiki.rendering.renderer.printer.WikiPrinter; +import org.xwiki.rendering.syntax.Syntax; +import org.xwiki.rendering.transformation.TransformationManager; +import org.xwiki.test.AbstractComponentTestCase; + +/** + * Renderer tests for the XHTML annotations renderer, from the test files. + * + * @version $Id$ + */ +@RunWith(Parameterized.class) +public class XHTMLAnnotationRendererTest extends AbstractComponentTestCase +{ + /** + * Document description files to run this test for. + */ + private static Collection<String[]> files = new ArrayList<String[]>(); + + /** + * The annotations renderer hint. + */ + private static final String ANNOTATIONS_RENDERER_HINT = "annotations/xhtml/1.0"; + + /** + * Mock document to run tests for. + */ + protected String docName; + + static { + // FIXME: checkstyle is so gonna shout when this will be longer than 30 files + addFileToTest("Document1"); + addFileToTest("Document2"); + addFileToTest("Document3"); + addFileToTest("Document4"); + addFileToTest("Document5"); + addFileToTest("Document6"); + addFileToTest("Document7"); + addFileToTest("Document8"); + addFileToTest("Document9"); + addFileToTest("Document10"); + addFileToTest("Document11"); + addFileToTest("Document12"); + addFileToTest("Document13"); + addFileToTest("Document14"); + addFileToTest("Document15"); + addFileToTest("Document16"); + addFileToTest("Document17"); + addFileToTest("Document18"); + } + + /** + * Creates a test for the passed document. Will be instantiated by the parameterized runner for all the parameters. + * + * @param docName the document (and corpus filename) to run tests for + */ + public XHTMLAnnotationRendererTest(String docName) + { + this.docName = docName; + } + + /** + * Adds a file to the list of files to run tests for. + * + * @param docName the name of the document / file to test + */ + private static void addFileToTest(String docName) + { + files.add(new String[] {docName}); + } + + /** + * @return list of corpus files to instantiate tests for + */ + @Parameters + public static Collection<String[]> data() + { + return files; + } + + /** + * Test rendering the annotations in the document description file results in the annotated html. + * + * @throws Exception in case something goes wrong looking up components and rendering + */ + @Test + public void getAnnotatedHTML() throws Exception + { + Parser parser = getComponentManager().lookup(Parser.class, Syntax.XWIKI_2_0.toIdString()); + XDOM xdom = parser.parse(new StringReader(TestDocumentFactory.getDocument(docName).getSource())); + + // run transformations + TransformationManager transformationManager = getComponentManager().lookup(TransformationManager.class); + transformationManager.performTransformations(xdom, Syntax.XWIKI_2_0); + + AnnotationPrintRenderer renderer = + getComponentManager().lookup(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT); + WikiPrinter printer = new DefaultWikiPrinter(); + renderer.setPrinter(printer); + // set the annotations for this renderer + renderer.setAnnotations(TestDocumentFactory.getDocument(docName).getSafeAnnotations()); + + xdom.traverse(renderer); + + assertEquals(TestDocumentFactory.getDocument(docName).getAnnotatedContent(), printer.toString()); + } + + /** + * Test rendering with the annotations renderer but without annotations doesn't alter the content. + * + * @throws Exception in case something goes wrong looking up components and rendering + */ + @Test + public void getAnnotatedHTMLWithoutAnnotations() throws Exception + { + Parser parser = getComponentManager().lookup(Parser.class, Syntax.XWIKI_2_0.toIdString()); + XDOM xdom = parser.parse(new StringReader(TestDocumentFactory.getDocument(docName).getSource())); + + // run transformations + TransformationManager transformationManager = getComponentManager().lookup(TransformationManager.class); + transformationManager.performTransformations(xdom, Syntax.XWIKI_2_0); + + AnnotationPrintRenderer renderer = + getComponentManager().lookup(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT); + WikiPrinter printer = new DefaultWikiPrinter(); + renderer.setPrinter(printer); + + xdom.traverse(renderer); + + assertEquals(TestDocumentFactory.getDocument(docName).getRenderedContent(), printer.toString()); + } +}
Property changes on: contrib/sandbox/xwiki-annotation-parent/xwiki-annotation-core/src/test/java/org/xwiki/annotation/renderer/XHTMLAnnotationRendererTest.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
_______________________________________________ notifications mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/notifications
participants (2)
-
Anca Luca -
Thomas Mortagne