1. Project Clover database Sat Feb 2 2019 06:45:20 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
117
55
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: 17ac5b5617fe530ceb25888ff38a1e863c923a33 $
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 char 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.PAGE,
66    ResourceType.ATTACHMENT,
67    ResourceType.ICON
68    );
69    // @formatter:on
70   
71    @Inject
72    private Logger logger;
73   
74    /**
75    * {@inheritDoc}
76    *
77    * @return the parsed resource reference or a Resource Reference with {@link ResourceType#UNKNOWN} if no reference
78    * type was specified
79    * @see org.xwiki.rendering.parser.ResourceReferenceParser#parse(String)
80    */
 
81  26159 toggle @Override
82    public ResourceReference parse(String rawReference)
83    {
84  26159 ResourceReference parsedResourceReference = null;
85   
86    // Step 1: Find the type parser matching the specified prefix type (if any).
87  26160 int pos = rawReference.indexOf(TYPE_SEPARATOR);
88  26157 if (pos > -1) {
89  19806 String typePrefix = rawReference.substring(0, pos);
90  19806 String reference = rawReference.substring(pos + 1);
91   
92  19806 ComponentManager componentManager = this.componentManagerProvider.get();
93  19805 if (componentManager.hasComponent(ResourceReferenceTypeParser.class, typePrefix)) {
94  4498 try {
95  4498 ResourceReferenceTypeParser parser =
96    componentManager.getInstance(ResourceReferenceTypeParser.class, typePrefix);
97  4498 parsedResourceReference = parser.parse(reference);
98    } catch (ComponentLookupException e) {
99  0 this.logger.error("Failed to initialize resource type parser", e);
100    }
101    }
102    }
103   
104    // Step 2: If there's no specific type parser found, then consider it's an Unknown type.
105  26160 if (parsedResourceReference == null) {
106  21665 parsedResourceReference = new ResourceReference(rawReference, ResourceType.UNKNOWN);
107    }
108   
109    // Step 3: If we're not in wiki mode then wiki references are considered URLs.
110  26158 if (!isInWikiMode() && WIKI_REFERENCE_TYPES.contains(parsedResourceReference.getType())) {
111  11 parsedResourceReference = new ResourceReference(rawReference, ResourceType.URL);
112  11 parsedResourceReference.setTyped(false);
113    }
114   
115  26159 return parsedResourceReference;
116    }
117    }