1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.renderer.xwiki20.reference

File XWikiSyntaxLinkReferenceSerializer.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
10
2
1
102
47
4
0.4
5
2
2

Classes

Class Line # Actions
XWikiSyntaxLinkReferenceSerializer 49 10 0% 4 1
0.916666791.7%
 

Contributing tests

This file is covered by 74 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.renderer.xwiki20.reference;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.rendering.listener.reference.ResourceReference;
30    import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
31    import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer;
32   
33    /**
34    * Generate a string representation of a Link reference, in XWiki Syntax 2.0. This implementation is pluggable by using
35    * internally implementations of {@link org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer}, each in
36    * charge of serializing a given {@link org.xwiki.rendering.listener.reference.ResourceType}.
37    * <p>
38    * Note that {@link org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer} component implementations
39    * must use a role hint equal to the XWiki Syntax id followed by "/" and then Link Type name (eg "doc" for document
40    * links, "attach" for attachment links, etc).
41    * </p>
42    *
43    * @version $Id: b847c7cfd45103ab58a404fa2826de7479b7eb6a $
44    * @since 2.5RC1
45    */
46    @Component
47    @Named("xwiki/2.0/link")
48    @Singleton
 
49    public class XWikiSyntaxLinkReferenceSerializer implements ResourceReferenceSerializer
50    {
51    /**
52    * Prefix to use for {@link org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer} role hints.
53    */
54    private static final String COMPONENT_PREFIX = "xwiki/2.0";
55   
56    /**
57    * Used to lookup {@link org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer} implementations.
58    */
59    @Inject
60    private ComponentManager componentManager;
61   
62    /**
63    * Default serializer to use when no serializer is found for the link type.
64    */
65    @Inject
66    @Named("xwiki/2.0")
67    private ResourceReferenceTypeSerializer defaultResourceReferenceTypeSerializer;
68   
 
69  128 toggle @Override
70    public String serialize(ResourceReference reference)
71    {
72  128 String result;
73   
74  128 try {
75  128 ResourceReferenceTypeSerializer serializer =
76    this.componentManager.getInstance(ResourceReferenceTypeSerializer.class,
77    getLinkTypeSerializerComponentPrefix() + "/" + reference.getType().getScheme());
78  25 result = serializer.serialize(reference);
79    } catch (ComponentLookupException e) {
80  103 try {
81    // Failed to find serializer for the passed link type. Use the default serializer.
82  103 ResourceReferenceTypeSerializer serializer =
83    this.componentManager.getInstance(ResourceReferenceTypeSerializer.class,
84    getLinkTypeSerializerComponentPrefix());
85  103 result = serializer.serialize(reference);
86    } catch (ComponentLookupException e2) {
87    // Failed to find a default serializer for the current syntax. Use xwiki/2.0 default serializer.
88  0 result = this.defaultResourceReferenceTypeSerializer.serialize(reference);
89    }
90    }
91  128 return result;
92    }
93   
94    /**
95    * @return the role hint prefix to use when looking up
96    * {@link org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer} implementations.
97    */
 
98  151 toggle protected String getLinkTypeSerializerComponentPrefix()
99    {
100  151 return COMPONENT_PREFIX;
101    }
102    }