1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.objects

File BaseObjectReference.java

 

Coverage histogram

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

Code metrics

12
33
6
1
189
81
12
0.36
5.5
6
2

Classes

Class Line # Actions
BaseObjectReference 51 33 0% 12 1
0.9803921698%
 

Contributing tests

This file is covered by 58 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 com.xpn.xwiki.objects;
21   
22    import java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import org.xwiki.model.reference.DocumentReference;
26    import org.xwiki.model.reference.DocumentReferenceResolver;
27    import org.xwiki.model.reference.EntityReference;
28    import org.xwiki.model.reference.EntityReferenceSerializer;
29    import org.xwiki.model.reference.ObjectReference;
30   
31    import com.xpn.xwiki.web.Utils;
32   
33    /**
34    * Object reference implementation for object names generated in {@code className[objectNumber]} format. It provides
35    * helper functions to extract the class name as specified by the caller, and object number.
36    * <p>
37    * Accepted formats for the object name are:
38    * <dl>
39    * <dt>className[objectNumber]
40    * <dd>interpreted as the object of class className with number objectNumber. refers the object returned by
41    * XWikiDocument.getObject(String className, int objectNumber). In this case, className is obtained by calling
42    * {@link #getXClassReference()} and object index by calling {@link #getObjectNumber()}.
43    * <dt>className
44    * <dd>interpreted as the first object of class className. refers the object returned by XWikiDocument.getObject(String
45    * className). In this case, {@link #getObjectNumber()} will return {@code null} and {@code className} is obtained by
46    * calling {@link #getXClassReference()}.
47    * </dl>
48    *
49    * @version $Id: a856b9937b791bc71c7c96f9fd383d6249888b87 $
50    */
 
51    public class BaseObjectReference extends ObjectReference
52    {
53    /**
54    * The version identifier for this Serializable class. Increment only if the <i>serialized</i> form of the class
55    * changes.
56    */
57    private static final long serialVersionUID = 1L;
58   
59    /**
60    * Used to match number part of the object reference name.
61    */
62    private static final Pattern NUMBERPATTERN = Pattern.compile("(\\\\*)\\[(\\d*)\\]$");
63   
64    /**
65    * The class reference of this object.
66    */
67    protected DocumentReference xclassReference;
68   
69    /**
70    * The number of this object.
71    */
72    protected Integer objectNumber;
73   
74    /**
75    * Constructor which would raise exceptions if the source entity reference does not have the appropriate type or
76    * parent, etc.
77    *
78    * @param reference the raw reference to build this object reference from
79    */
 
80  9814 toggle public BaseObjectReference(EntityReference reference)
81    {
82  9814 super(reference);
83    }
84   
85    /**
86    * Builds an indexed object reference for the object of class {@code className} with index {@code objectNumber} in
87    * the document referenced by {@code parent}.
88    *
89    * @param classReference the name of the class of the object
90    * @param objectNumber the number of the object in the document, or {@code null} if the default object should be
91    * referenced
92    * @param parent reference to the parent document where the object is
93    */
 
94  21142 toggle public BaseObjectReference(DocumentReference classReference, Integer objectNumber, DocumentReference parent)
95    {
96    // FIXME: Not very nice having to serialize classReference/objectNumber and then parse it in #setName
97  21140 super(toName(classReference, objectNumber), parent);
98    }
99   
100    /**
101    * Convert provided class reference and object number into object reference name.
102    *
103    * @param classReference the class reference
104    * @param objectNumber the object number
105    * @return the object reference name
106    */
 
107  21140 toggle private static String toName(DocumentReference classReference, Integer objectNumber)
108    {
109  21141 String name =
110    Utils.<EntityReferenceSerializer<String>>getComponent(EntityReferenceSerializer.TYPE_STRING).serialize(
111    classReference);
112   
113  21142 if (objectNumber != null) {
114  21137 StringBuilder builder = new StringBuilder(name);
115  21138 builder.append('[');
116  21138 builder.append(objectNumber);
117  21136 builder.append(']');
118  21137 name = builder.toString();
119    } else {
120  3 Matcher matcher = NUMBERPATTERN.matcher(name);
121  3 if (matcher.find()) {
122  2 if (matcher.group(1).length() % 2 == 0) {
123  2 StringBuilder builder = new StringBuilder(name);
124  2 builder.insert(matcher.start(), '\\');
125  2 name = builder.toString();
126    }
127    }
128    }
129   
130  21139 return name;
131    }
132   
133    /**
134    * @return the reference of the class of this object.
135    */
 
136  12949 toggle public DocumentReference getXClassReference()
137    {
138  12949 return this.xclassReference;
139    }
140   
141    /**
142    * @return the number of this object among the objects of the same class in the document, as set by the caller in
143    * [objectNumber] format after the class name (i.e. no resolving is done, existence of this object is not
144    * guaranteed). If no number can be parsed (i.e. [number] cannot be parsed) this function returns
145    * {@code null} and object should be interpreted as the first object of this class in the document.
146    */
 
147  22797 toggle public Integer getObjectNumber()
148    {
149  22797 return this.objectNumber;
150    }
151   
152    /**
153    * {@inheritDoc}
154    * <p>
155    * Overridden to always compute the class name and the object number.
156    * </p>
157    *
158    * @see org.xwiki.model.reference.EntityReference#setName(java.lang.String)
159    */
 
160  30947 toggle @Override
161    protected void setName(String name)
162    {
163  30949 super.setName(name);
164   
165  30950 String classReferenceStr;
166  30951 String objectNumberStr;
167   
168  30951 Matcher matcher = NUMBERPATTERN.matcher(name);
169  30951 if (matcher.find()) {
170  30949 if (matcher.group(1).length() % 2 == 0) {
171  30942 classReferenceStr = name.substring(0, matcher.end(1));
172  30943 objectNumberStr = matcher.group(2);
173    } else {
174  4 classReferenceStr = name;
175  4 objectNumberStr = null;
176    }
177    } else {
178  6 classReferenceStr = name;
179  6 objectNumberStr = null;
180    }
181   
182  30954 this.xclassReference =
183    Utils.<DocumentReferenceResolver<String>>getComponent(DocumentReferenceResolver.TYPE_STRING).resolve(
184    classReferenceStr);
185  30955 if (objectNumberStr != null) {
186  30945 this.objectNumber = Integer.valueOf(objectNumberStr);
187    }
188    }
189    }