1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.listener.reference

File ResourceReference.java

 

Coverage histogram

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

Code metrics

14
47
19
1
301
149
27
0.57
2.47
19
1.42

Classes

Class Line # Actions
ResourceReference 40 47 0% 27 3
0.962596.2%
 

Contributing tests

This file is covered by 483 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: 3c0ee468a628138395bb29f9017689510138ff8c $
38    * @since 2.5RC1
39    */
 
40    public class ResourceReference implements Cloneable
41    {
42    /**
43    * @see #isTyped()
44    */
45    private boolean typed = 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  54597 toggle public ResourceReference(String reference, ResourceType type)
78    {
79  54598 setReference(reference);
80  54601 setType(type);
81    }
82   
83    /**
84    * @param typed see {@link #isTyped()}
85    */
 
86  33032 toggle public void setTyped(boolean typed)
87    {
88  33034 this.typed = typed;
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  22295 toggle public boolean isTyped()
96    {
97  22297 return this.typed;
98    }
99   
100    /**
101    * @param reference see {@link #getReference()}
102    */
 
103  54617 toggle public void setReference(String reference)
104    {
105  54621 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  122180 toggle public String getReference()
116    {
117  122186 return this.reference;
118    }
119   
120    /**
121    * @param baseReference see {@link #getBaseReferences()}
122    */
 
123  25112 toggle public void addBaseReference(String baseReference)
124    {
125  25115 if (this.baseReferences == null) {
126  25102 this.baseReferences = new ArrayList<String>();
127    }
128  25115 this.baseReferences.add(baseReference);
129    }
130   
131    /**
132    * @param baseReferences see {@link #getBaseReferences()}
133    */
 
134  35454 toggle public void addBaseReferences(List<String> baseReferences)
135    {
136  35458 for (String baseReference : baseReferences) {
137  25098 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  98920 toggle public List<String> getBaseReferences()
147    {
148  98929 List<String> result;
149  98924 if (this.baseReferences == null) {
150  37416 result = Collections.emptyList();
151    } else {
152  61513 result = Collections.unmodifiableList(this.baseReferences);
153    }
154  98927 return result;
155    }
156   
157    /**
158    * @return the type of the resource
159    * @see ResourceType
160    */
 
161  288999 toggle public ResourceType getType()
162    {
163  289006 return this.type;
164    }
165   
166    /**
167    * @param type the type of the resource
168    * @see ResourceType
169    */
 
170  54614 toggle public void setType(ResourceType type)
171    {
172  54615 this.type = type;
173    }
174   
175    /**
176    * @param name see {@link #getParameter(String)}
177    * @param value see {@link #getParameter(String)}
178    */
 
179  5859 toggle public void setParameter(String name, String value)
180    {
181  5859 this.parameters.put(name, value);
182    }
183   
184    /**
185    * @param parameters see {@link #getParameters()}
186    */
 
187  74 toggle public void setParameters(Map<String, String> parameters)
188    {
189  74 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  31526 toggle public String getParameter(String name)
211    {
212  31527 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  727 toggle public Map<String, String> getParameters()
220    {
221  727 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  486 toggle @Override
233    public String toString()
234    {
235    // TODO: This needs to be changed but it involves changing a lot of unit tests
236  486 XWikiToStringStyle style = new XWikiToStringStyle();
237  486 style.setSeparator("");
238  486 ToStringBuilder builder = new ToStringBuilder(this, style);
239   
240  486 builder = builder.append("Typed", isTyped()).append("Type", getType());
241   
242  486 if (getReference() != null) {
243  408 builder = builder.append("Reference", getReference());
244    }
245   
246  486 if (!getBaseReferences().isEmpty()) {
247  2 builder = builder.append("Base References", getBaseReferences());
248    }
249   
250  486 Map<String, String> params = getParameters();
251  486 if (!params.isEmpty()) {
252  128 builder = builder.append("Parameters", params);
253    }
254   
255  486 return builder.toString();
256    }
257   
 
258  213060 toggle @Override
259    public ResourceReference clone()
260    {
261  213065 ResourceReference clone;
262  213069 try {
263  213070 clone = (ResourceReference) super.clone();
264    } catch (CloneNotSupportedException e) {
265    // Should never happen
266  0 throw new RuntimeException("Failed to clone object", e);
267    }
268  213063 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  60 toggle @Override
284    public boolean equals(Object object)
285    {
286  60 if (!(object instanceof ResourceReference)) {
287  2 return false;
288    }
289  58 if (object == this) {
290  1 return true;
291    }
292  57 ResourceReference rhs = (ResourceReference) object;
293  57 return new EqualsBuilder()
294    .append(getType(), rhs.getType())
295    .append(isTyped(), rhs.isTyped())
296    .append(getReference(), rhs.getReference())
297    .append(getBaseReferences(), rhs.getBaseReferences())
298    .append(getParameters(), rhs.getParameters())
299    .isEquals();
300    }
301    }