Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
19   117   7   9.5
6   53   0.37   2
2     3.5  
1    
 
  DefaultResourceReferenceParser       Line # 50 19 0% 7 0 100% 1.0
 
  (38)
 
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 javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.xwiki.component.annotation.Component;
26    import org.xwiki.component.manager.ComponentLookupException;
27    import org.xwiki.component.manager.ComponentManager;
28    import org.xwiki.rendering.listener.reference.ResourceReference;
29    import org.xwiki.rendering.listener.reference.ResourceType;
30    import org.xwiki.rendering.parser.ResourceReferenceParser;
31    import org.xwiki.rendering.parser.ResourceReferenceTypeParser;
32    import org.xwiki.rendering.wiki.WikiModel;
33   
34    /**
35    * Parses the content of resource references. The format of a resource reference is the following:
36    * {@code (type):(reference)} where {@code type} represents the type (see
37    * {@link org.xwiki.rendering.listener.reference.ResourceType} of the resource pointed to (e.g. document, mailto,
38    * attachment, image, document in another wiki, etc), and {@code reference} defines the target.
39    * The syntax of {@code reference} depends on the Resource type and is documented in the javadoc of the various
40    * {@link org.xwiki.rendering.parser.ResourceReferenceTypeParser} implementations.
41    *
42    * Note that the implementation is pluggable and it's allowed plug new resource reference types by implementing
43    * {@link org.xwiki.rendering.parser.ResourceReferenceTypeParser}s and registering the implementation as a component.
44    *
45    * @version $Id: c896ee5a0dee6a50fe83b1edbcbb6370ed1ad31d $
46    * @since 2.6M1
47    */
48    @Component
49    @Singleton
 
50    public class DefaultResourceReferenceParser implements ResourceReferenceParser
51    {
52    /**
53    * Link Reference Type separator (eg "mailto:mail@address").
54    */
55    public static final String TYPE_SEPARATOR = ":";
56   
57    /**
58    * Used to verify if we're in wiki mode or not by looking up an implementation of {@link
59    * org.xwiki.rendering.wiki.WikiModel}.
60    */
61    @Inject
62    private ComponentManager componentManager;
63   
64    /**
65    * {@inheritDoc}
66    *
67    * @return the parsed resource reference or a Resource Reference with {@link ResourceType#UNKNOWN} if no reference
68    * type was specified
69    *
70    * @see org.xwiki.rendering.parser.ResourceReferenceParser#parse(String)
71    */
 
72  89 toggle @Override
73    public ResourceReference parse(String rawReference)
74    {
75    // Step 1: If we're not in wiki mode then all links are URL links.
76  89 if (!isInWikiMode()) {
77  6 ResourceReference resourceReference = new ResourceReference(rawReference, ResourceType.URL);
78  6 resourceReference.setTyped(false);
79  6 return resourceReference;
80    }
81   
82    // Step 2: Find the type parser matching the specified prefix type (if any).
83  83 int pos = rawReference.indexOf(TYPE_SEPARATOR);
84  83 if (pos > -1) {
85  56 String typePrefix = rawReference.substring(0, pos);
86  56 String reference = rawReference.substring(pos + 1);
87  56 try {
88  56 ResourceReferenceTypeParser
89    parser = this.componentManager.lookup(ResourceReferenceTypeParser.class, typePrefix);
90  48 ResourceReference parsedResourceReference = parser.parse(reference);
91  48 if (parsedResourceReference != null) {
92  44 return parsedResourceReference;
93    }
94    } catch (ComponentLookupException e) {
95    // Couldn't find a link type parser for the specified type. Will try to autodiscover the type.
96    }
97    }
98   
99    // Step 3: There's no specific type parser found.
100  39 return new ResourceReference(rawReference, ResourceType.UNKNOWN);
101    }
102   
103    /**
104    * @return true if we're in wiki mode (ie there's no implementing class for {@link
105    * org.xwiki.rendering.wiki.WikiModel})
106    */
 
107  89 toggle private boolean isInWikiMode()
108    {
109  89 boolean result = true;
110  89 try {
111  89 this.componentManager.lookup(WikiModel.class);
112    } catch (ComponentLookupException e) {
113  6 result = false;
114    }
115  89 return result;
116    }
117    }