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

File ResourceReference.java

 

Coverage histogram

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

Code metrics

16
49
19
1
304
152
28
0.57
2.58
19
1.47

Classes

Class Line # Actions
ResourceReference 40 49 0% 28 7
0.916666791.7%
 

Contributing tests

This file is covered by 487 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.listener.reference;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.LinkedHashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import org.apache.commons.lang3.builder.EqualsBuilder;
29    import org.apache.commons.lang3.builder.HashCodeBuilder;
30    import org.apache.commons.lang3.builder.ToStringBuilder;
31    import org.xwiki.text.XWikiToStringStyle;
32   
33    /**
34    * Represents a reference to a Resource (document, image, attachment, mail, etc).
35    * Note that this representation is independent of any wiki syntax.
36    *
37    * @version $Id: c130fe7beefd5afc9b198dce9b7e6efa047dc297 $
38    * @since 2.5RC1
39    */
 
40    public class ResourceReference implements Cloneable
41    {
42    /**
43    * @see #isTyped()
44    */
45    private boolean isTyped = true;
46   
47    /**
48    * @see #getReference()
49    *
50    * Note that the reason we store the reference as a String and not as an Entity Reference is because we want
51    * the Rendering module independent of the XWiki Model so that it can be used independently of XWiki.
52    */
53    private String reference;
54   
55    /**
56    * @see #getBaseReferences()
57    *
58    * Note that the reason we store the base reference as a String and not as an Entity Reference is because we want
59    * the Rendering module independent of the XWiki Model so that it can be used independently of XWiki.
60    */
61    private List<String> baseReferences;
62   
63    /**
64    * @see #getType()
65    */
66    private ResourceType type;
67   
68    /**
69    * @see #getParameter(String).
70    */
71    private Map<String, String> parameters = new LinkedHashMap<>();
72   
73    /**
74    * @param reference see {@link #getReference()}
75    * @param type see {@link #getType()}
76    */
 
77  3288 toggle public ResourceReference(String reference, ResourceType type)
78    {
79  3288 setReference(reference);
80  3288 setType(type);
81    }
82   
83    /**
84    * @param isTyped see {@link #isTyped()}
85    */
 
86  1826 toggle public void setTyped(boolean isTyped)
87    {
88  1826 this.isTyped = isTyped;
89    }
90   
91    /**
92    * @return true if the resource type has been explicitly provided (eg in XWiki Syntax 2.1 if the reference is
93    * prefixed with the resource type followed by ":" and then the rest of the reference)
94    */
 
95  1757 toggle public boolean isTyped()
96    {
97  1757 return this.isTyped;
98    }
99   
100    /**
101    * @param reference see {@link #getReference()}
102    */
 
103  3306 toggle public void setReference(String reference)
104    {
105  3306 this.reference = reference;
106    }
107   
108    /**
109    * @return the reference pointed to by this resource. For example a reference can be a document's name (which
110    * depends on the wiki, for example for XWiki the format is "wiki:space.page"), a URI
111    * (for example: mailto:john@doe.com), a URL, an
112    * <a href="http://en.wikipedia.org/wiki/InterWiki">Inter Wiki</a> reference, etc
113    * @see #getType()
114    */
 
115  5555 toggle public String getReference()
116    {
117  5555 return this.reference;
118    }
119   
120    /**
121    * @param baseReference see {@link #getBaseReferences()}
122    */
 
123  1001 toggle public void addBaseReference(String baseReference)
124    {
125  1001 if (this.baseReferences == null) {
126  998 this.baseReferences = new ArrayList<String>();
127    }
128  1001 this.baseReferences.add(baseReference);
129    }
130   
131    /**
132    * @param baseReferences see {@link #getBaseReferences()}
133    */
 
134  1296 toggle public void addBaseReferences(List<String> baseReferences)
135    {
136  1296 for (String baseReference : baseReferences) {
137  985 addBaseReference(baseReference);
138    }
139    }
140   
141    /**
142    * @return the base references to use when we need to compute an absolute reference and {@link #getReference()}
143    * returns a non absolute reference, can be {@code null}. When resolving references the list should be
144    * evaluated from first to last (the last entries qualifying the entries earlier in the list)
145    */
 
146  3597 toggle public List<String> getBaseReferences()
147    {
148  3597 List<String> result;
149  3597 if (this.baseReferences == null) {
150  2000 result = Collections.emptyList();
151    } else {
152  1597 result = Collections.unmodifiableList(this.baseReferences);
153    }
154  3597 return result;
155    }
156   
157    /**
158    * @return the type of the resource
159    * @see ResourceType
160    */
 
161  10654 toggle public ResourceType getType()
162    {
163  10654 return this.type;
164    }
165   
166    /**
167    * @param type the type of the resource
168    * @see ResourceType
169    */
 
170  3302 toggle public void setType(ResourceType type)
171    {
172  3302 this.type = type;
173    }
174   
175    /**
176    * @param name see {@link #getParameter(String)}
177    * @param value see {@link #getParameter(String)}
178    */
 
179  367 toggle public void setParameter(String name, String value)
180    {
181  367 this.parameters.put(name, value);
182    }
183   
184    /**
185    * @param parameters see {@link #getParameters()}
186    */
 
187  79 toggle public void setParameters(Map<String, String> parameters)
188    {
189  79 this.parameters.putAll(parameters);
190    }
191   
192    /**
193    * @param name see {@link #getParameter(String)}
194    */
 
195  0 toggle public void removeParameter(String name)
196    {
197  0 this.parameters.remove(name);
198    }
199   
200    /**
201    * In order for Resource references to be extensible we allow for extra parameters in addition to the Resource
202    * reference. For example this is used in Document Resource References for storing the query string and anchor
203    * information, and in InterWiki Resource References to store the InterWiki Alias. Note that supported parameters
204    * depend on the Renderer that will be used (i.e. it depends on the target Syntax). For example the XWiki Syntax
205    * 2.1 only supports "queryString" and "anchor".
206    *
207    * @param name the name of the parameter to get
208    * @return the parameter value or null if no such parameter exist
209    */
 
210  1130 toggle public String getParameter(String name)
211    {
212  1130 return this.parameters.get(name);
213    }
214   
215    /**
216    * @return the collections of parameters, see {@link #getParameter(String)}. Returns parameters in the same order
217    * they were added.
218    */
 
219  684 toggle public Map<String, String> getParameters()
220    {
221  684 return Collections.unmodifiableMap(this.parameters);
222    }
223   
224    /**
225    * {@inheritDoc}
226    * <p>
227    * The output is syntax independent since this class is used for all syntaxes. Specific syntaxes should extend this
228    * class and override this method to perform syntax-dependent formatting.</p>
229    *
230    * @see java.lang.Object#toString()
231    */
 
232  502 toggle @Override
233    public String toString()
234    {
235    // TODO: This needs to be changed but it involves changing a lot of unit tests
236  502 XWikiToStringStyle style = new XWikiToStringStyle();
237  502 style.setSeparator("");
238  502 ToStringBuilder builder = new ToStringBuilder(this, style);
239   
240  502 builder = builder.append("Typed", isTyped()).append("Type", getType());
241   
242  502 if (getReference() != null) {
243  424 builder = builder.append("Reference", getReference());
244    }
245   
246  502 if (!getBaseReferences().isEmpty()) {
247  2 builder = builder.append("Base References", getBaseReferences());
248    }
249   
250  502 Map<String, String> params = getParameters();
251  502 if (!params.isEmpty()) {
252  136 builder = builder.append("Parameters", params);
253    }
254   
255  502 return builder.toString();
256    }
257   
 
258  568 toggle @Override
259    public ResourceReference clone()
260    {
261  568 ResourceReference clone;
262  568 try {
263  568 clone = (ResourceReference) super.clone();
264    } catch (CloneNotSupportedException e) {
265    // Should never happen
266  0 throw new RuntimeException("Failed to clone object", e);
267    }
268  568 return clone;
269    }
270   
 
271  4 toggle @Override
272    public int hashCode()
273    {
274  4 return new HashCodeBuilder(1, 9)
275    .append(getType())
276    .append(isTyped())
277    .append(getReference())
278    .append(getBaseReferences())
279    .append(getParameters())
280    .toHashCode();
281    }
282   
 
283  49 toggle @Override
284    public boolean equals(Object object)
285    {
286  49 if (object == null) {
287  0 return false;
288    }
289  49 if (object == this) {
290  0 return true;
291    }
292  49 if (object.getClass() != getClass()) {
293  1 return false;
294    }
295  48 ResourceReference rhs = (ResourceReference) object;
296  48 return new EqualsBuilder()
297    .append(getType(), rhs.getType())
298    .append(isTyped(), rhs.isTyped())
299    .append(getReference(), rhs.getReference())
300    .append(getBaseReferences(), rhs.getBaseReferences())
301    .append(getParameters(), rhs.getParameters())
302    .isEquals();
303    }
304    }