Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../img/srcFileCovDistChart8.png 68% of files have more coverage
67   334   32   3.53
24   176   0.48   19
19     1.68  
1    
 
  ResourceReference       Line # 39 67 0% 32 25 77.3% 0.77272725
 
  (230)
 
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.listener.reference;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.Iterator;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.apache.commons.lang3.builder.EqualsBuilder;
30    import org.apache.commons.lang3.builder.HashCodeBuilder;
31   
32    /**
33    * Represents a reference to a Resource (document, image, attachment, mail, etc).
34    * Note that this representation is independent of any wiki syntax.
35    *
36    * @version $Id: 25e867ca65ee7aaf173b0866077d4efc3076b65a $
37    * @since 2.5RC1
38    */
 
39    public class ResourceReference implements Cloneable
40    {
41    /**
42    * Bracket start char.
43    */
44    private static final char BRACKET_START = '[';
45   
46    /**
47    * Bracket dtop char.
48    */
49    private static final char BRACKET_STOP = ']';
50   
51    /**
52    * Comma followed by space.
53    */
54    private static final String COMMA_SPACE = ", ";
55   
56    /**
57    * @see #isTyped()
58    */
59    private boolean isTyped = true;
60   
61    /**
62    * @see #getReference()
63    *
64    * Note that the reason we store the reference as a String and not as an Entity Reference is because we want
65    * the Rendering module independent of the XWiki Model so that it can be used independently of XWiki.
66    */
67    private String reference;
68   
69    /**
70    * @see #getBaseReferences()
71    *
72    * Note that the reason we store the base reference as a String and not as an Entity Reference is because we want
73    * the Rendering module independent of the XWiki Model so that it can be used independently of XWiki.
74    */
75    private List<String> baseReferences;
76   
77    /**
78    * @see #getType()
79    */
80    private ResourceType type;
81   
82    /**
83    * @see #getParameter(String)
84    */
85    private Map<String, String> parameters = new HashMap<String, String>();
86   
87    /**
88    * @param reference see {@link #getReference()}
89    * @param type see {@link #getType()}
90    */
 
91  600 toggle public ResourceReference(String reference, ResourceType type)
92    {
93  600 setReference(reference);
94  600 setType(type);
95    }
96   
97    /**
98    * @param isTyped see {@link #isTyped()}
99    */
 
100  365 toggle public void setTyped(boolean isTyped)
101    {
102  365 this.isTyped = isTyped;
103    }
104   
105    /**
106    * @return true if the resource type has been explicitly provided (eg in XWiki Syntax 2.1 if the reference is
107    * prefixed with the resource type followed by ":" and then the rest of the reference)
108    */
 
109  596 toggle public boolean isTyped()
110    {
111  596 return this.isTyped;
112    }
113   
114    /**
115    * @param reference see {@link #getReference()}
116    */
 
117  600 toggle public void setReference(String reference)
118    {
119  600 this.reference = reference;
120    }
121   
122    /**
123    * @return the reference pointed to by this resource. For example a reference can be a document's name (which
124    * depends on the wiki, for example for XWiki the format is "wiki:space.page"), a URI
125    * (for example: mailto:john@doe.com), a URL, an
126    * <a href="http://en.wikipedia.org/wiki/InterWiki">Inter Wiki</a> reference, etc
127    * @see #getType()
128    */
 
129  1246 toggle public String getReference()
130    {
131  1246 return this.reference;
132    }
133   
134    /**
135    * @param baseReference see {@link #getBaseReferences()}
136    */
 
137  14 toggle public void addBaseReference(String baseReference)
138    {
139  14 if (this.baseReferences == null) {
140  12 this.baseReferences = new ArrayList<String>();
141    }
142  14 this.baseReferences.add(baseReference);
143    }
144   
145    /**
146    * @param baseReferences see {@link #getBaseReferences()}
147    */
 
148  127 toggle public void addBaseReferences(List<String> baseReferences)
149    {
150  127 for (String baseReference : baseReferences) {
151  3 addBaseReference(baseReference);
152    }
153    }
154   
155    /**
156    * @return the base references to use when we need to compute an absolute reference and {@link #getReference()}
157    * returns a non absolute reference, can be {@code null}. When resolving references the list should be
158    * evaluated from first to last (the last entries qualifying the entries earlier in the list)
159    */
 
160  537 toggle public List<String> getBaseReferences()
161    {
162  537 List<String> result;
163  537 if (this.baseReferences == null) {
164  518 result = Collections.emptyList();
165    } else {
166  19 result = Collections.unmodifiableList(this.baseReferences);
167    }
168  537 return result;
169    }
170   
171    /**
172    * @return the type of the resource
173    * @see ResourceType
174    */
 
175  1172 toggle public ResourceType getType()
176    {
177  1172 return this.type;
178    }
179   
180    /**
181    * @param type the type of the resource
182    * @see ResourceType
183    */
 
184  600 toggle public void setType(ResourceType type)
185    {
186  600 this.type = type;
187    }
188   
189    /**
190    * @param name see {@link #getParameter(String)}
191    * @param value see {@link #getParameter(String)}
192    */
 
193  116 toggle public void setParameter(String name, String value)
194    {
195  116 this.parameters.put(name, value);
196    }
197   
198    /**
199    * @param parameters see {@link #getParameters()}
200    */
 
201  0 toggle public void setParameters(Map<String, String> parameters)
202    {
203  0 this.parameters.putAll(parameters);
204    }
205   
206    /**
207    * @param name see {@link #getParameter(String)}
208    */
 
209  0 toggle public void removeParameter(String name)
210    {
211  0 this.parameters.remove(name);
212    }
213   
214    /**
215    * In order for Resource references to be extensible we allow for extra parameters in addition to the Resource
216    * reference. For example this is used in Document Resource References for storing the query string and anchor
217    * information, and in InterWiki Resource References to store the InterWiki Alias. Note that supported parameters
218    * depend on the Renderer that will be used (i.e. it depends on the target Syntax). For example the XWiki Syntax
219    * 2.1 only supports "queryString" and "anchor".
220    *
221    * @param name the name of the parameter to get
222    * @return the parameter value or null if no such parameter exist
223    */
 
224  152 toggle public String getParameter(String name)
225    {
226  152 return this.parameters.get(name);
227    }
228   
229    /**
230    * @return the collections of parameters, see {@link #getParameter(String)}
231    */
 
232  468 toggle public Map<String, String> getParameters()
233    {
234  468 return Collections.unmodifiableMap(this.parameters);
235    }
236   
237    /**
238    * {@inheritDoc}
239    * <p>
240    * The output is syntax independent since this class is used for all syntaxes. Specific syntaxes should extend this
241    * class and override this method to perform syntax-dependent formatting.
242    *
243    * @see java.lang.Object#toString()
244    */
 
245  382 toggle @Override
246    public String toString()
247    {
248  382 StringBuffer sb = new StringBuffer();
249  382 sb.append("Typed = [").append(isTyped()).append("]");
250  382 sb.append(" ");
251  382 sb.append("Type = [").append(getType().getScheme()).append("]");
252  382 if (getReference() != null) {
253  308 sb.append(" ");
254  308 sb.append("Reference = [").append(getReference()).append("]");
255    }
256  382 if (!getBaseReferences().isEmpty()) {
257  0 sb.append(" ");
258  0 sb.append("Base References = [");
259  0 Iterator<String> it = getBaseReferences().listIterator();
260  0 while (it.hasNext()) {
261  0 String baseReference = it.next();
262  0 sb.append(BRACKET_START).append(baseReference).append(BRACKET_STOP);
263  0 if (it.hasNext()) {
264  0 sb.append(COMMA_SPACE);
265    }
266    }
267  0 sb.append(BRACKET_STOP);
268    }
269  382 Map<String, String> params = getParameters();
270  382 if (!params.isEmpty()) {
271  116 sb.append(" ");
272  116 sb.append("Parameters = [");
273  116 Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
274  240 while (it.hasNext()) {
275  124 Map.Entry<String, String> entry = it.next();
276  124 sb.append(BRACKET_START).append(entry.getKey()).append("] = [").append(entry.getValue());
277  124 sb.append(BRACKET_STOP);
278  124 if (it.hasNext()) {
279  8 sb.append(COMMA_SPACE);
280    }
281    }
282  116 sb.append(BRACKET_STOP);
283    }
284   
285  382 return sb.toString();
286    }
287   
 
288  21 toggle @Override
289    public ResourceReference clone()
290    {
291  21 ResourceReference clone;
292  21 try {
293  21 clone = (ResourceReference) super.clone();
294    } catch (CloneNotSupportedException e) {
295    // Should never happen
296  0 throw new RuntimeException("Failed to clone object", e);
297    }
298  21 return clone;
299    }
300   
 
301  4 toggle @Override
302    public int hashCode()
303    {
304  4 return new HashCodeBuilder(1, 9)
305    .append(getType())
306    .append(isTyped())
307    .append(getReference())
308    .append(getBaseReferences())
309    .append(getParameters())
310    .toHashCode();
311    }
312   
 
313  11 toggle @Override
314    public boolean equals(Object object)
315    {
316  11 if (object == null) {
317  0 return false;
318    }
319  11 if (object == this) {
320  0 return true;
321    }
322  11 if (object.getClass() != getClass()) {
323  0 return false;
324    }
325  11 ResourceReference rhs = (ResourceReference) object;
326  11 return new EqualsBuilder()
327    .append(getType(), rhs.getType())
328    .append(isTyped(), rhs.isTyped())
329    .append(getReference(), rhs.getReference())
330    .append(getBaseReferences(), rhs.getBaseReferences())
331    .append(getParameters(), rhs.getParameters())
332    .isEquals();
333    }
334    }