diff --git a/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultWantedLinkTitleGenerator.java b/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultWantedLinkTitleGenerator.java new file mode 100644 index 0000000000..6c7e587b82 --- /dev/null +++ b/xwiki-rendering-api/src/main/java/org/xwiki/rendering/internal/renderer/DefaultWantedLinkTitleGenerator.java @@ -0,0 +1,53 @@ +/* + * 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 javax.inject.Singleton; + +import org.xwiki.component.annotation.Component; +import org.xwiki.rendering.listener.reference.ResourceReference; +import org.xwiki.rendering.renderer.reference.link.WantedLinkTitleGenerator; + +/** + * Generates wanted link titles for resource references. + * Using this implementation should be avoided, another implementation should be used instead. + * E.g. XWikiDocumentWantedLinkTitleGenerator in xwiki-platform which is used to provide translations. + * This implementation is a fallback and should only be used when xwiki-rendering is running by itself. + * This implementation uses the reference as the title. + * @version $Id$ + * @since 16.3.0RC1 + */ +@Component +@Singleton +public class DefaultWantedLinkTitleGenerator implements WantedLinkTitleGenerator +{ + private static final String DEFAULT_TITLE = "Create resource: %s"; + + /** + * Generates wanted link titles for resource references. + * @param reference the reference for which we want to generate a wanted link title + * @return the wanted link title used when rendering a resource reference. + */ + @Override + public String generateWantedLinkTitle(ResourceReference reference) + { + return String.format(DEFAULT_TITLE, reference.getReference()); + } +} diff --git a/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/reference/link/WantedLinkTitleGenerator.java b/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/reference/link/WantedLinkTitleGenerator.java new file mode 100644 index 0000000000..60750bfe8b --- /dev/null +++ b/xwiki-rendering-api/src/main/java/org/xwiki/rendering/renderer/reference/link/WantedLinkTitleGenerator.java @@ -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.rendering.renderer.reference.link; + +import org.xwiki.component.annotation.Role; +import org.xwiki.rendering.listener.reference.ResourceReference; +import org.xwiki.stability.Unstable; + +/** + * Generate Resource Reference titles for wanted links. + * The implementations should be named according to the kind of reference they process. + * + * @version $Id$ + * @since 16.3.0RC1 + */ +@Role +@Unstable +public interface WantedLinkTitleGenerator +{ + /** + * @param reference the reference for which we want to generate a wanted link title + * @return the title to display when rendering the resource reference wanted link + */ + String generateWantedLinkTitle(ResourceReference reference); +} diff --git a/xwiki-rendering-api/src/main/resources/META-INF/components.txt b/xwiki-rendering-api/src/main/resources/META-INF/components.txt index a8e6bab0fb..6c233d52e4 100644 --- a/xwiki-rendering-api/src/main/resources/META-INF/components.txt +++ b/xwiki-rendering-api/src/main/resources/META-INF/components.txt @@ -29,6 +29,7 @@ org.xwiki.rendering.internal.renderer.DefaultAttachmentURILabelGenerator org.xwiki.rendering.internal.renderer.MailtoURILabelGenerator org.xwiki.rendering.internal.renderer.DataURILabelGenerator org.xwiki.rendering.internal.renderer.DefaultPageAttachmentURILabelGenerator +org.xwiki.rendering.internal.renderer.DefaultWantedLinkTitleGenerator org.xwiki.rendering.internal.renderer.reference.DefaultResourceReferenceTypeSerializer org.xwiki.rendering.internal.syntax.SyntaxConverter org.xwiki.rendering.internal.syntax.DefaultSyntaxRegistry diff --git a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links20.test b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links20.test index 1369b09032..709600fd7a 100644 --- a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links20.test +++ b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links20.test @@ -15,11 +15,11 @@ endDocument .#----------------------------------------------------- .expect|xhtml/1.0 .#----------------------------------------------------- -

\

+

\

.#----------------------------------------------------- .expect|annotatedxhtml/1.0 .#----------------------------------------------------- -

\

+

\

.#----------------------------------------------------- .expect|xwiki/2.0 .#----------------------------------------------------- @@ -27,4 +27,4 @@ endDocument .#----------------------------------------------------- .input|xhtml/1.0 .#----------------------------------------------------- -

\

\ No newline at end of file +

\

\ No newline at end of file diff --git a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links22.test b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links22.test index dd1d428621..304a598ae8 100644 --- a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links22.test +++ b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links22.test @@ -6,4 +6,4 @@ .#----------------------------------------------------- .expect|xhtml/1.0 .#----------------------------------------------------- -

label

\ No newline at end of file +

label

\ No newline at end of file diff --git a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links3.test b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links3.test index 6838c3bf81..9f47b1177d 100644 --- a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links3.test +++ b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links3.test @@ -15,11 +15,11 @@ endDocument .#----------------------------------------------------- .expect|xhtml/1.0 .#----------------------------------------------------- -

label

+

label

.#----------------------------------------------------- .expect|annotatedxhtml/1.0 .#----------------------------------------------------- -

label

+

label

.#----------------------------------------------------- .expect|xwiki/2.0 .#----------------------------------------------------- @@ -27,7 +27,7 @@ endDocument .#----------------------------------------------------- .input|xhtml/1.0 .#----------------------------------------------------- -

label

+

label

.#----------------------------------------------------- .expect|plain/1.0 .#----------------------------------------------------- diff --git a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links6.test b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links6.test index 2a281b92f7..0c5adf17e0 100644 --- a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links6.test +++ b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links6.test @@ -51,11 +51,11 @@ endDocument .#----------------------------------------------------- .expect|xhtml/1.0 .#----------------------------------------------------- -

{{macro}}
Label>Reference
Reference|Param=Value
[[no link>>Not Reference]]
[[whatever
not:link

+

{{macro}}
Label>Reference
Reference|Param=Value
[[no link>>Not Reference]]
[[whatever
not:link

.#----------------------------------------------------- .expect|annotatedxhtml/1.0 .#----------------------------------------------------- -

{{macro}}
Label>Reference
Reference|Param=Value
[[no link>>Not Reference]]
[[whatever
not:link

+

{{macro}}
Label>Reference
Reference|Param=Value
[[no link>>Not Reference]]
[[whatever
not:link

.#----------------------------------------------------- .expect|xwiki/2.0 .#----------------------------------------------------- @@ -68,4 +68,4 @@ not:link .#----------------------------------------------------- .input|xhtml/1.0 .#----------------------------------------------------- -

{{macro}}
Label>Reference
Reference|Param=Value
[[no link>>Not Reference]]
[[whatever
not:link

\ No newline at end of file +

{{macro}}
Label>Reference
Reference|Param=Value
[[no link>>Not Reference]]
[[whatever
not:link

\ No newline at end of file diff --git a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links7.test b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links7.test index 5f1799092b..208f7cd86b 100644 --- a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links7.test +++ b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links7.test @@ -29,11 +29,11 @@ endDocument .#----------------------------------------------------- .expect|xhtml/1.0 .#----------------------------------------------------- -

hello
= notheader

+

hello
= notheader

.#----------------------------------------------------- .expect|annotatedxhtml/1.0 .#----------------------------------------------------- -

hello
= notheader

+

hello
= notheader

.#----------------------------------------------------- .expect|xwiki/2.0 .#----------------------------------------------------- @@ -43,4 +43,4 @@ endDocument .#----------------------------------------------------- .input|xhtml/1.0 .#----------------------------------------------------- -

hello
= notheader

\ No newline at end of file +

hello
= notheader

\ No newline at end of file diff --git a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links8.test b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links8.test index 17ee7f72b7..3e63591331 100644 --- a/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links8.test +++ b/xwiki-rendering-integration-tests/src/test/resources/wiki/link/links8.test @@ -19,11 +19,11 @@ endDocument .#----------------------------------------------------- .expect|xhtml/1.0 .#----------------------------------------------------- -

Space.ExistingPage
label

+

Space.ExistingPage
label

.#----------------------------------------------------- .expect|annotatedxhtml/1.0 .#----------------------------------------------------- -

Space.ExistingPage
label

+

Space.ExistingPage
label

.#----------------------------------------------------- .expect|xwiki/2.0 .#----------------------------------------------------- @@ -32,4 +32,4 @@ endDocument .#----------------------------------------------------- .input|xhtml/1.0 .#----------------------------------------------------- -

Space.ExistingPage
label

\ No newline at end of file +

Space.ExistingPage
label

\ No newline at end of file diff --git a/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/AbstractXHTMLLinkTypeRenderer.java b/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/AbstractXHTMLLinkTypeRenderer.java index 6e3e2022e2..7166cf3caf 100644 --- a/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/AbstractXHTMLLinkTypeRenderer.java +++ b/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/AbstractXHTMLLinkTypeRenderer.java @@ -47,6 +47,12 @@ public abstract class AbstractXHTMLLinkTypeRenderer implements XHTMLLinkTypeRend */ protected static final String CLASS = "class"; + /** + * The XHTML element {@code title} parameter. + * @since 16.3.0RC1 + */ + protected static final String TITLE = "title"; + /** * The name of the XHTML format element. */ diff --git a/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/DocumentXHTMLLinkTypeRenderer.java b/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/DocumentXHTMLLinkTypeRenderer.java index 722708c151..027f57e9ce 100644 --- a/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/DocumentXHTMLLinkTypeRenderer.java +++ b/xwiki-rendering-syntaxes/xwiki-rendering-syntax-xhtml/src/main/java/org/xwiki/rendering/internal/renderer/xhtml/link/DocumentXHTMLLinkTypeRenderer.java @@ -25,6 +25,8 @@ import javax.inject.Inject; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.slf4j.Logger; import org.xwiki.component.annotation.Component; import org.xwiki.component.annotation.InstantiationStrategy; import org.xwiki.component.descriptor.ComponentInstantiationStrategy; @@ -34,6 +36,7 @@ import org.xwiki.rendering.listener.reference.DocumentResourceReference; import org.xwiki.rendering.listener.reference.ResourceReference; import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator; +import org.xwiki.rendering.renderer.reference.link.WantedLinkTitleGenerator; import org.xwiki.rendering.wiki.WikiModel; /** @@ -51,6 +54,9 @@ public class DocumentXHTMLLinkTypeRenderer extends AbstractXHTMLLinkTypeRenderer */ private static final String WIKILINK = "wikilink"; + @Inject + private Logger logger; + /** * Used to generate the link targeting a local document. */ @@ -62,6 +68,12 @@ public class DocumentXHTMLLinkTypeRenderer extends AbstractXHTMLLinkTypeRenderer @Inject private LinkLabelGenerator linkLabelGenerator; + /** + * Used to generate a link title. + */ + @Inject + private WantedLinkTitleGenerator defaultTitleGenerator; + @Override public void initialize() throws InitializationException { @@ -90,6 +102,37 @@ protected String computeLabel(ResourceReference reference) return this.linkLabelGenerator.generate(reference); } + private WantedLinkTitleGenerator getTitleGenerator(ResourceReference reference) + { + WantedLinkTitleGenerator titleGenerator = this.defaultTitleGenerator; + try { + titleGenerator = this.componentManager.getInstance(WantedLinkTitleGenerator.class, + reference.getType().getScheme()); + } catch (Exception e) { + String message = String.format("Could not find a [%s] component to generate the wanted " + + "link title for [{}].", WantedLinkTitleGenerator.class.getName()); + if (logger.isDebugEnabled()) { + logger.debug(message, reference, e); + } else { + logger.warn(String.format("%s: [{}]", message), reference, ExceptionUtils.getRootCauseMessage(e)); + } + } + return titleGenerator; + } + + /** + * Implementation for computing a wanted link title. + * Looks for a component implementing WantedLinkTitleGenerator with a role hint matching the reference scheme. + * @param reference the reference for which to compute the title + * @return the wanted link title + * @since 16.3.0RC1 + */ + private String computeWantedLinkTitle(ResourceReference reference) + { + WantedLinkTitleGenerator titleGenerator = getTitleGenerator(reference); + return titleGenerator.generateWantedLinkTitle(reference); + } + @Override protected void beginLinkExtraAttributes(ResourceReference reference, Map spanAttributes, Map anchorAttributes) @@ -126,6 +169,7 @@ private void beginInternalLink(ResourceReference reference, boolean freestanding } else { // The wiki document doesn't exist spanAttributes.put(CLASS, "wikicreatelink"); + spanAttributes.put(TITLE, computeWantedLinkTitle(reference)); anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentEditURL(reference)); }