Re: [xwiki-devs] [xwiki-notifications] r26602 - in platform/core/trunk/xwiki-rendering: xwiki-rendering-api xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/xhtml xwiki
On Tue, Feb 2, 2010 at 15:51, vmassol <[email protected]> wrote:
Author: vmassol Date: 2010-02-02 15:51:13 +0100 (Tue, 02 Feb 2010) New Revision: 26602
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/AttachURILabelGenerator.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/MailtoURILabelGenerator.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/URILabelGenerator.java Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/pom.xml platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultLinkLabelGenerator.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/DefaultXHTMLLinkRenderer.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/resources/META-INF/components.txt platform/core/trunk/xwiki-rendering/xwiki-rendering-tests/src/test/java/org/xwiki/rendering/SimpleRenderingTests.java Log: XWIKI-4839: Improve labels for mailto and attach links
* Refactored to make the implementation more generic...
Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/pom.xml =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/pom.xml 2010-02-02 14:38:44 UTC (rev 26601) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/pom.xml 2010-02-02 14:51:13 UTC (rev 26602) @@ -132,6 +132,9 @@ **/rendering/internal/macro/*.java, **/rendering/internal/parser/*.java, **/rendering/internal/renderer/BasicLinkRenderer.java, + **/rendering/internal/renderer/DefaultLinkLabelGenerator.java, + **/rendering/internal/renderer/AttachURILabelGenerator.java, + **/rendering/internal/renderer/MailtoURILabelGenerator.java, **/rendering/listener/*.java, **/rendering/listener/chaining/AbstractChainingListener.java, **/rendering/listener/chaining/ChainingListener.java,
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/AttachURILabelGenerator.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/AttachURILabelGenerator.java (rev 0) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/AttachURILabelGenerator.java 2010-02-02 14:51:13 UTC (rev 26602) @@ -0,0 +1,58 @@ +/* + * 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.rendering.internal.renderer; + +import org.xwiki.component.annotation.Component; +import org.xwiki.component.annotation.Requirement; +import org.xwiki.rendering.listener.Attachment; +import org.xwiki.rendering.listener.Link; +import org.xwiki.rendering.parser.AttachmentParser; +import org.xwiki.rendering.renderer.URILabelGenerator; + +/** + * Generate link labels for ATTACH URIs. + * + * @version $Id$ + * @since 2.2RC1 + */ +@Component("attach") +public class AttachURILabelGenerator implements URILabelGenerator +{ + /** + * The ATTACH URI prefix (the scheme followed by ":"). + */ + private static final String ATTACH = "attach:"; + + /** + * Used to extract the attachment name from the reference. + */ + @Requirement + private AttachmentParser attachmentParser; + + /** + * {@inheritDoc} + * @see org.xwiki.rendering.renderer.URILabelGenerator#generateLabel(org.xwiki.rendering.listener.Link) + */ + public String generateLabel(Link link) + { + Attachment attachment = this.attachmentParser.parse(link.getReference().substring(ATTACH.length())); + return attachment.getAttachmentName(); + } +}
Property changes on: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/AttachURILabelGenerator.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultLinkLabelGenerator.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultLinkLabelGenerator.java 2010-02-02 14:38:44 UTC (rev 26601) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultLinkLabelGenerator.java 2010-02-02 14:51:13 UTC (rev 26602) @@ -32,6 +32,10 @@ @Component public class DefaultLinkLabelGenerator implements LinkLabelGenerator { + /** + * {@inheritDoc} + * @see org.xwiki.rendering.renderer.LinkLabelGenerator#generate(org.xwiki.rendering.listener.Link) + */ public String generate(Link link) { return link.getReference();
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/MailtoURILabelGenerator.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/MailtoURILabelGenerator.java (rev 0) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/MailtoURILabelGenerator.java 2010-02-02 14:51:13 UTC (rev 26602) @@ -0,0 +1,54 @@ +/* + * 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.rendering.internal.renderer; + +import org.xwiki.component.annotation.Component; +import org.xwiki.rendering.listener.Link; +import org.xwiki.rendering.renderer.URILabelGenerator; + +/** + * Generate link labels for MAILTO URIs. + * + * @version $Id$ + * @since 2.2RC1 + */ +@Component("mailto") +public class MailtoURILabelGenerator implements URILabelGenerator +{ + /** + * The MAILTO URI prefix (the scheme followed by ":"). + */ + private static final String MAILTO = "mailto:"; + + /** + * {@inheritDoc} + * @see org.xwiki.rendering.renderer.URILabelGenerator#generateLabel(org.xwiki.rendering.listener.Link) + */ + public String generateLabel(Link link) + { + String label = link.getReference().substring(MAILTO.length()); + // Also remove the query string part from the label (we only want the email address). + int queryStringPosition = label.indexOf("?"); + if (queryStringPosition > -1) { + label = label.substring(0, queryStringPosition); + }
Since it's an URI why don't you use new URI(link.getReference()) to parse it ?
+ return label; + } +}
Property changes on: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/MailtoURILabelGenerator.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/DefaultXHTMLLinkRenderer.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/DefaultXHTMLLinkRenderer.java 2010-02-02 14:38:44 UTC (rev 26601) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/DefaultXHTMLLinkRenderer.java 2010-02-02 14:51:13 UTC (rev 26602) @@ -36,6 +36,7 @@ import org.xwiki.rendering.listener.LinkType; import org.xwiki.rendering.parser.AttachmentParser; import org.xwiki.rendering.renderer.LinkLabelGenerator; +import org.xwiki.rendering.renderer.URILabelGenerator; import org.xwiki.rendering.renderer.printer.XHTMLWikiPrinter; import org.xwiki.rendering.renderer.xhtml.XHTMLLinkRenderer; import org.xwiki.rendering.wiki.WikiModel; @@ -70,11 +71,6 @@ private static final String ATTACH = "attach:";
/** - * The link reference prefix indicating that the link is targeting a mail address. - */ - private static final String MAILTO = "mailto:"; - - /** * The class attribute 'wikilink'. */ private static final String WIKILINK = "wikilink"; @@ -276,23 +272,26 @@ if (link.getType() == LinkType.DOCUMENT) { getXHTMLWikiPrinter().printXML(this.linkLabelGenerator.generate(link)); } else if (link.getType() == LinkType.URI) { + + // Look for a component implementing URILabelGenerator with a role hint matching the URI scheme. + // If not found then use the full reference as the label. + int schemeSeparator = link.getReference().indexOf(":"); + + // If there's no scheme separator then use the full reference as the label. Note that this can happen + // when we're not in wiki mode (since all links are considered URIs when not in wiki mode). String label; - // Special handling for MAILTO and ATTACH URIs for which we don't want to print the scheme in the label - // (so that they appear displayed a nicer way for users). - if (link.getReference().startsWith(ATTACH)) { - // Only display the attachment name. - Attachment attachment = this.attachmentParser.parse(link.getReference().substring(ATTACH.length())); - label = attachment.getAttachmentName(); - } else if (link.getReference().startsWith(MAILTO)) { - label = link.getReference().substring(MAILTO.length()); - // For MAILTO also remove the query string part from the label (we only want the email address). - int queryStringPosition = label.indexOf("?"); - if (queryStringPosition > -1) { - label = label.substring(0, queryStringPosition); + if (schemeSeparator > -1) { + String scheme = link.getReference().substring(0, schemeSeparator); + try { + URILabelGenerator uriLabelGenerator = this.componentManager.lookup(URILabelGenerator.class, scheme); + label = uriLabelGenerator.generateLabel(link); + } catch (ComponentLookupException e) { + label = link.getReference(); } } else { label = link.getReference(); } + getXHTMLWikiPrinter().printXML(label); } else { getXHTMLWikiPrinter().printXML(link.getReference());
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/URILabelGenerator.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/URILabelGenerator.java (rev 0) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/URILabelGenerator.java 2010-02-02 14:51:13 UTC (rev 26602) @@ -0,0 +1,40 @@ +/* + * 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.rendering.renderer; + +import org.xwiki.component.annotation.ComponentRole; +import org.xwiki.rendering.listener.Link; + +/** + * Generate link labels for URIs. For example an implementation for MAILTO URIs would remove the scheme part and the + * query string part. + * + * @version $Id$ + * @since 2.2RC1 + */ +@ComponentRole +public interface URILabelGenerator +{ + /** + * @param link the link pointing to a URI for which we want to generate a label + * @return the URI label to display when rendering links + */ + String generateLabel(Link link); +}
Property changes on: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/URILabelGenerator.java ___________________________________________________________________ Name: svn:keywords + Author Id Revision HeadURL Name: svn:eol-style + native
Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/resources/META-INF/components.txt =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/resources/META-INF/components.txt 2010-02-02 14:38:44 UTC (rev 26601) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/resources/META-INF/components.txt 2010-02-02 14:51:13 UTC (rev 26602) @@ -9,6 +9,8 @@ org.xwiki.rendering.internal.configuration.DefaultRenderingConfiguration org.xwiki.rendering.internal.parser.DefaultSyntaxFactory org.xwiki.rendering.internal.renderer.DefaultLinkLabelGenerator +org.xwiki.rendering.internal.renderer.AttachURILabelGenerator +org.xwiki.rendering.internal.renderer.MailtoURILabelGenerator org.xwiki.rendering.internal.macro.DefaultMacroManager org.xwiki.rendering.internal.transformation.DefaultTransformationManager org.xwiki.rendering.internal.renderer.xwiki.XWikiSyntaxRenderer
Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-tests/src/test/java/org/xwiki/rendering/SimpleRenderingTests.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-tests/src/test/java/org/xwiki/rendering/SimpleRenderingTests.java 2010-02-02 14:38:44 UTC (rev 26601) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-tests/src/test/java/org/xwiki/rendering/SimpleRenderingTests.java 2010-02-02 14:51:13 UTC (rev 26602) @@ -37,6 +37,7 @@ public static junit.framework.Test suite() throws Exception { RenderingTestSuite suite = new RenderingTestSuite("Rendering tests not requiring the wiki notion"); + suite.addTestsFromResource("link/links24", false);
// Embedded documents suite.addTestsFromResource("group/group1", false); @@ -210,7 +211,7 @@ suite.addTestsFromResource("encoding/encoding1", false);
// Links without WikiModel - suite.addTestsFromResource("link/links24", false); +// suite.addTestsFromResource("link/links24", false);
return new ComponentManagerTestSetup(suite); }
_______________________________________________ notifications mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/notifications
-- Thomas Mortagne
On Feb 2, 2010, at 4:50 PM, Thomas Mortagne wrote:
On Tue, Feb 2, 2010 at 15:51, vmassol <[email protected]> wrote:
Author: vmassol Date: 2010-02-02 15:51:13 +0100 (Tue, 02 Feb 2010) New Revision: 26602
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/AttachURILabelGenerator.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/MailtoURILabelGenerator.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/URILabelGenerator.java Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/pom.xml platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultLinkLabelGenerator.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/DefaultXHTMLLinkRenderer.java platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/resources/META-INF/components.txt platform/core/trunk/xwiki-rendering/xwiki-rendering-tests/src/test/java/org/xwiki/rendering/SimpleRenderingTests.java Log: XWIKI-4839: Improve labels for mailto and attach links
* Refactored to make the implementation more generic...
[snip]
+/** + * Generate link labels for MAILTO URIs. + * + * @version $Id$ + * @since 2.2RC1 + */ +@Component("mailto") +public class MailtoURILabelGenerator implements URILabelGenerator +{ + /** + * The MAILTO URI prefix (the scheme followed by ":"). + */ + private static final String MAILTO = "mailto:"; + + /** + * {@inheritDoc} + * @see org.xwiki.rendering.renderer.URILabelGenerator#generateLabel(org.xwiki.rendering.listener.Link) + */ + public String generateLabel(Link link) + { + String label = link.getReference().substring(MAILTO.length()); + // Also remove the query string part from the label (we only want the email address). + int queryStringPosition = label.indexOf("?"); + if (queryStringPosition > -1) { + label = label.substring(0, queryStringPosition); + }
Since it's an URI why don't you use
new URI(link.getReference())
to parse it ?
Actually what would be best is to pass a URI and not a Link. The pb is that "attach:" may not resolve as a valid URI (since it can contain non encoded chars, we would need to encode it - which is a possibility). Thanks -Vincent [snip]
participants (2)
-
Thomas Mortagne -
Vincent Massol