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
15   99   5   7.5
4   51   0.33   2
2     2.5  
1    
 
  GenericImageReferenceParser       Line # 45 15 0% 5 0 100% 1.0
 
  (45)
 
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.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.listener.reference.ResourceType;
31    import org.xwiki.rendering.parser.ResourceReferenceParser;
32    import org.xwiki.rendering.parser.ResourceReferenceTypeParser;
33    import org.xwiki.rendering.wiki.WikiModel;
34   
35    /**
36    * Each syntax should have its own resource reference parser. However while we wait for syntax specific parser to be
37    * implemented this generic parser should provide a good approximation.
38    *
39    * @version $Id: 1eda056b48a0e91d5ecc04cbd239820c2a9b12a2 $
40    * @since 2.5RC1
41    */
42    @Component
43    @Named("default/image")
44    @Singleton
 
45    public class GenericImageReferenceParser implements ResourceReferenceParser
46    {
47    /**
48    * Used to verify if we're in wiki mode or not by looking up an implementation of
49    * {@link org.xwiki.rendering.wiki.WikiModel}. In non wiki mode all image references are considered as URLs.
50    */
51    @Inject
52    private ComponentManager componentManager;
53   
54    /**
55    * Parser to parse image references pointing to URLs.
56    */
57    @Inject
58    @Named("url")
59    private ResourceReferenceTypeParser urlResourceReferenceTypeParser;
60   
 
61  66 toggle @Override
62    public ResourceReference parse(String rawReference)
63    {
64  66 ResourceType type;
65   
66  66 if (!isInWikiMode()) {
67  21 type = ResourceType.URL;
68    } else {
69    // Try to guess the image type. It can be either:
70    // - a URL
71    // - a reference to an attachment
72  45 ResourceReference reference = this.urlResourceReferenceTypeParser.parse(rawReference);
73  45 if (reference == null) {
74  40 type = ResourceType.ATTACHMENT;
75    } else {
76  5 type = ResourceType.URL;
77    }
78    }
79   
80  66 ResourceReference result = new ResourceReference(rawReference, type);
81  66 result.setTyped(false);
82   
83  66 return result;
84    }
85   
86    /**
87    * @return true if we're in wiki mode (ie there's no implementing class for {@link WikiModel})
88    */
 
89  66 toggle private boolean isInWikiMode()
90    {
91  66 boolean result = true;
92  66 try {
93  66 this.componentManager.lookup(WikiModel.class);
94    } catch (ComponentLookupException e) {
95  21 result = false;
96    }
97  66 return result;
98    }
99    }