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

File DefaultResourceReferenceParser.java

 

Coverage histogram

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

Code metrics

8
17
1
1
116
54
7
0.41
17
1
7

Classes

Class Line # Actions
DefaultResourceReferenceParser 51 17 0% 7 1
0.9615384396.2%
 

Contributing tests

This file is covered by 79 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.parser.reference;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Singleton;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.rendering.listener.reference.ResourceReference;
33    import org.xwiki.rendering.listener.reference.ResourceType;
34    import org.xwiki.rendering.parser.ResourceReferenceTypeParser;
35   
36    /**
37    * Parses the content of resource references. The format of a resource reference is the following:
38    * {@code (type):(reference)} where {@code type} represents the type (see
39    * {@link org.xwiki.rendering.listener.reference.ResourceType} of the resource pointed to (e.g. document, space, mailto,
40    * attachment, image, document in another wiki, etc), and {@code reference} defines the target. The syntax of
41    * {@code reference} depends on the Resource type and is documented in the javadoc of the various
42    * {@link org.xwiki.rendering.parser.ResourceReferenceTypeParser} implementations. Note that the implementation is
43    * pluggable and it's allowed plug new resource reference types by implementing
44    * {@link org.xwiki.rendering.parser.ResourceReferenceTypeParser}s and registering the implementation as a component.
45    *
46    * @version $Id: 89ff455f2b778d02cbbd3ff9f2e05f91bf56590e $
47    * @since 2.6M1
48    */
49    @Component
50    @Singleton
 
51    public class DefaultResourceReferenceParser extends AbstractResourceReferenceParser
52    {
53    /**
54    * Link Reference Type separator (eg "mailto:mail@address").
55    */
56    public static final String TYPE_SEPARATOR = ":";
57   
58    /**
59    * Types of references that are dependent on running in wiki mode.
60    */
61    /* @formatter:off */
62    private static final List<ResourceType> WIKI_REFERENCE_TYPES = Arrays.asList(
63    ResourceType.DOCUMENT,
64    ResourceType.SPACE,
65    ResourceType.ATTACHMENT,
66    ResourceType.ICON
67    );
68    // @formatter:on
69   
70    @Inject
71    private Logger logger;
72   
73    /**
74    * {@inheritDoc}
75    *
76    * @return the parsed resource reference or a Resource Reference with {@link ResourceType#UNKNOWN} if no reference
77    * type was specified
78    * @see org.xwiki.rendering.parser.ResourceReferenceParser#parse(String)
79    */
 
80  1158 toggle @Override
81    public ResourceReference parse(String rawReference)
82    {
83  1158 ResourceReference parsedResourceReference = null;
84   
85    // Step 1: Find the type parser matching the specified prefix type (if any).
86  1158 int pos = rawReference.indexOf(TYPE_SEPARATOR);
87  1158 if (pos > -1) {
88  679 String typePrefix = rawReference.substring(0, pos);
89  679 String reference = rawReference.substring(pos + 1);
90   
91  679 ComponentManager componentManager = this.componentManagerProvider.get();
92  679 if (componentManager.hasComponent(ResourceReferenceTypeParser.class, typePrefix)) {
93  639 try {
94  639 ResourceReferenceTypeParser parser =
95    componentManager.getInstance(ResourceReferenceTypeParser.class, typePrefix);
96  639 parsedResourceReference = parser.parse(reference);
97    } catch (ComponentLookupException e) {
98  0 this.logger.error("Failed to initialize resource type parser", e);
99    }
100    }
101    }
102   
103    // Step 2: If there's no specific type parser found, then consider it's an Unknown type.
104  1158 if (parsedResourceReference == null) {
105  524 parsedResourceReference = new ResourceReference(rawReference, ResourceType.UNKNOWN);
106    }
107   
108    // Step 3: If we're not in wiki mode then wiki references are considered URLs.
109  1158 if (!isInWikiMode() && WIKI_REFERENCE_TYPES.contains(parsedResourceReference.getType())) {
110  11 parsedResourceReference = new ResourceReference(rawReference, ResourceType.URL);
111  11 parsedResourceReference.setTyped(false);
112    }
113   
114  1158 return parsedResourceReference;
115    }
116    }