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

File DocumentReference.java

 

Coverage histogram

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

Code metrics

16
44
22
1
348
143
31
0.7
2
22
1.41

Classes

Class Line # Actions
DocumentReference 40 44 0% 31 1
0.987804998.8%
 

Contributing tests

This file is covered by 3296 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.model.reference;
21   
22    import java.beans.Transient;
23    import java.lang.reflect.Type;
24    import java.util.ArrayList;
25    import java.util.Collections;
26    import java.util.List;
27    import java.util.Locale;
28   
29    import javax.inject.Provider;
30   
31    import org.xwiki.component.util.DefaultParameterizedType;
32    import org.xwiki.model.EntityType;
33   
34    /**
35    * Represents a reference to a document (wiki, space and page names).
36    *
37    * @version $Id: 84557f8b4ef680a2d8bf20ddcbf8a612ffa67885 $
38    * @since 2.2M1
39    */
 
40    public class DocumentReference extends EntityReference
41    {
42    /**
43    * The {@link Type} for a {@code Provider<DocumentReference>}.
44    *
45    * @since 7.2M1
46    */
47    public static final Type TYPE_PROVIDER =
48    new DefaultParameterizedType(null, Provider.class, DocumentReference.class);
49   
50    /**
51    * Parameter key for the locale.
52    */
53    static final String LOCALE = "LOCALE";
54   
55    /**
56    * Cache the {@link LocalDocumentReference} corresponding to this {@link DocumentReference}.
57    */
58    private LocalDocumentReference localDocumentReference;
59   
60    /**
61    * Special constructor that transforms a generic entity reference into a {@link DocumentReference}. It checks the
62    * validity of the passed reference (ie correct type and correct parent).
63    *
64    * @param reference the reference to convert
65    * @exception IllegalArgumentException if the passed reference is not a valid document reference
66    */
 
67  3929973 toggle public DocumentReference(EntityReference reference)
68    {
69  3930005 super(reference);
70    }
71   
72    /**
73    * Clone an DocumentReference, but replace one of the parent in the chain by a new one.
74    *
75    * @param reference the reference that is cloned
76    * @param oldReference the old parent that will be replaced
77    * @param newReference the new parent that will replace oldReference in the chain
78    * @since 3.3M2
79    */
 
80  142 toggle protected DocumentReference(EntityReference reference, EntityReference oldReference, EntityReference newReference)
81    {
82  142 super(reference, oldReference, newReference);
83    }
84   
85    /**
86    * Clone the provided reference and change the Locale.
87    *
88    * @param reference the reference to clone
89    * @param locale the new locale for this reference, if null, locale is removed
90    * @exception IllegalArgumentException if the passed reference is not a valid document reference
91    */
 
92  779867 toggle public DocumentReference(EntityReference reference, Locale locale)
93    {
94  779887 super(reference);
95  779847 setLocale(locale);
96    }
97   
98    /**
99    * Create a new Document reference from wiki, space and page name.
100    *
101    * @param wikiName the name of the wiki containing the document, must not be null
102    * @param spaceName the name of the space containing the document, must not be null
103    * @param pageName the name of the document
104    */
 
105  141216 toggle public DocumentReference(String wikiName, String spaceName, String pageName)
106    {
107  141220 this(pageName, new SpaceReference(spaceName, new WikiReference(wikiName)));
108    }
109   
110    /**
111    * Create a new Document reference from wiki name, space name, page name and locale.
112    *
113    * @param wikiName the name of the wiki containing the document, must not be null
114    * @param spaceName the name of the space containing the document, must not be null
115    * @param pageName the name of the document
116    * @param locale the locale of the document reference, may be null
117    */
 
118  39 toggle public DocumentReference(String wikiName, String spaceName, String pageName, Locale locale)
119    {
120  39 this(pageName, new SpaceReference(spaceName, new WikiReference(wikiName)), locale);
121    }
122   
123    /**
124    * Create a new Document reference from wiki name, space name, page name and language. This is an helper function
125    * during transition from language to locale, it will be deprecated ASAP.
126    *
127    * @param wikiName the name of the wiki containing the document, must not be null
128    * @param spaceName the name of the space containing the document, must not be null
129    * @param pageName the name of the document
130    * @param language the language of the document reference, may be null
131    */
 
132  1 toggle public DocumentReference(String wikiName, String spaceName, String pageName, String language)
133    {
134  1 this(pageName, new SpaceReference(spaceName, new WikiReference(wikiName)),
135  1 (language == null) ? null : new Locale(language));
136    }
137   
138    /**
139    * Create a new Document reference from wiki name, spaces names and page name.
140    *
141    * @param wikiName the name of the wiki containing the document, must not be null
142    * @param spaceNames an ordered list of the names of the spaces containing the document from root space to last one,
143    * must not be null
144    * @param pageName the name of the document
145    */
 
146  647 toggle public DocumentReference(String wikiName, List<String> spaceNames, String pageName)
147    {
148  647 super(pageName, EntityType.DOCUMENT, new SpaceReference(wikiName, spaceNames));
149    }
150   
151    /**
152    * Create a new Document reference from wiki name, spaces names, page name and locale.
153    *
154    * @param wikiName the name of the wiki containing the document, must not be null
155    * @param spaceNames an ordered list of the names of the spaces containing the document from root space to last one,
156    * must not be null
157    * @param pageName the name of the document reference
158    * @param locale the locale of the document reference, may be null
159    */
 
160  925 toggle public DocumentReference(String wikiName, List<String> spaceNames, String pageName, Locale locale)
161    {
162  925 super(pageName, EntityType.DOCUMENT, new SpaceReference(wikiName, spaceNames));
163  925 setLocale(locale);
164    }
165   
166    /**
167    * Create a new Document reference from document name and parent space.
168    *
169    * @param pageName the name of the document
170    * @param parent the parent space for the document
171    */
 
172  318465 toggle public DocumentReference(String pageName, SpaceReference parent)
173    {
174  318481 super(pageName, EntityType.DOCUMENT, parent);
175    }
176   
177    /**
178    * Create a new Document reference from local document reference and wiki reference.
179    *
180    * @param localDocumentReference the document reference without the wiki reference
181    * @param wikiReference the wiki reference
182    * @since 5.1M1
183    */
 
184  6315 toggle public DocumentReference(LocalDocumentReference localDocumentReference, WikiReference wikiReference)
185    {
186  6315 super(localDocumentReference, null, wikiReference);
187    }
188   
189    /**
190    * Create a new Document reference from document name, parent space and locale.
191    *
192    * @param pageName the name of the document
193    * @param parent the parent space for the document
194    * @param locale the locale of the document reference, may be null
195    */
 
196  3685 toggle public DocumentReference(String pageName, SpaceReference parent, Locale locale)
197    {
198  3685 super(pageName, EntityType.DOCUMENT, parent);
199  3684 setLocale(locale);
200    }
201   
202    /**
203    * {@inheritDoc}
204    * <p>
205    * Overridden in order to verify the validity of the passed parent.
206    * </p>
207    *
208    * @see org.xwiki.model.reference.EntityReference#setParent(EntityReference)
209    * @exception IllegalArgumentException if the passed parent is not a valid document reference parent (ie a space
210    * reference)
211    */
 
212  5039930 toggle @Override
213    protected void setParent(EntityReference parent)
214    {
215  5039991 if (parent instanceof SpaceReference) {
216  4020139 super.setParent(parent);
217  4020059 return;
218    }
219   
220  1019791 if (parent == null || parent.getType() != EntityType.SPACE) {
221  2 throw new IllegalArgumentException("Invalid parent reference [" + parent + "] in a document reference");
222    }
223   
224  1019809 super.setParent(new SpaceReference(parent));
225    }
226   
227    /**
228    * {@inheritDoc}
229    * <p>
230    * Overridden in order to verify the validity of the passed type.
231    * </p>
232    *
233    * @see org.xwiki.model.reference.EntityReference#setType(org.xwiki.model.EntityType)
234    * @exception IllegalArgumentException if the passed type is not a document type
235    */
 
236  5039965 toggle @Override
237    protected void setType(EntityType type)
238    {
239  5039955 if (type != EntityType.DOCUMENT) {
240  1 throw new IllegalArgumentException("Invalid type [" + type + "] for a document reference");
241    }
242   
243  5039963 super.setType(EntityType.DOCUMENT);
244    }
245   
246    /**
247    * Set the locale of this document reference.
248    *
249    * @param locale the locale of this document reference
250    */
 
251  784447 toggle protected void setLocale(Locale locale)
252    {
253  784451 setParameter(LOCALE, locale);
254    }
255   
256    /**
257    * @return the locale of this document reference
258    */
 
259  3983043 toggle public Locale getLocale()
260    {
261  3983061 return (Locale) getParameter(LOCALE);
262    }
263   
264    /**
265    * @return the wiki reference of this document reference
266    */
 
267  1606634 toggle @Transient
268    public WikiReference getWikiReference()
269    {
270  1606617 return (WikiReference) extractReference(EntityType.WIKI);
271    }
272   
273    /**
274    * Create a new DocumentReference with passed wiki reference.
275    *
276    * @param wikiReference the wiki reference to use
277    * @return a new document reference or the same if the passed wiki is already the current wiki
278    * @since 7.2M1
279    */
 
280  111 toggle @Transient
281    public DocumentReference setWikiReference(WikiReference wikiReference)
282    {
283  111 WikiReference currentWikiReferene = getWikiReference();
284   
285  111 if (currentWikiReferene.equals(wikiReference)) {
286  20 return this;
287    }
288   
289  91 return new DocumentReference(this, currentWikiReferene, wikiReference);
290    }
291   
292    /**
293    * @return the space reference of the last space containing this document
294    */
 
295  7927491 toggle @Transient
296    public SpaceReference getLastSpaceReference()
297    {
298  7927491 return (SpaceReference) extractReference(EntityType.SPACE);
299    }
300   
301    /**
302    * @return space references of this document in an ordered list
303    */
 
304  23550 toggle @Transient
305    public List<SpaceReference> getSpaceReferences()
306    {
307  23559 List<SpaceReference> references = new ArrayList<SpaceReference>();
308   
309  23542 EntityReference reference = this;
310  96707 while (reference != null) {
311  73135 if (reference.getType() == EntityType.SPACE) {
312  26004 references.add((SpaceReference) reference);
313    }
314  73141 reference = reference.getParent();
315    }
316    // Reverse the array so that the last entry is the parent of the Document Reference
317  23571 Collections.reverse(references);
318   
319  23566 return references;
320    }
321   
 
322  51 toggle @Override
323    public DocumentReference replaceParent(EntityReference oldParent, EntityReference newParent)
324    {
325  51 return new DocumentReference(this, oldParent, newParent);
326    }
327   
328    /**
329    * @return the {@link LocalDocumentReference} corresponding to this {@link DocumentReference}
330    * @since 8.3
331    */
 
332  314 toggle public LocalDocumentReference getLocaleDocumentReference()
333    {
334  314 if (this.localDocumentReference == null) {
335  157 this.localDocumentReference = new LocalDocumentReference(this);
336    }
337   
338  314 return this.localDocumentReference;
339    }
340   
 
341  423895 toggle @Override
342    public String toString()
343    {
344    // Compared to EntityReference we don't print the type since the type is already indicated by the fact that
345    // this is a DocumentReference instance.
346  423894 return TOSTRING_SERIALIZER.serialize(this);
347    }
348    }