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

File IndexedObjectReference.java

 

Coverage histogram

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

Code metrics

8
18
5
1
141
47
11
0.61
3.6
5
2.2

Classes

Class Line # Actions
IndexedObjectReference 44 18 0% 11 2
0.935483993.5%
 

Contributing tests

This file is covered by 7 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.annotation.reference;
21   
22    import org.xwiki.model.reference.DocumentReference;
23    import org.xwiki.model.reference.EntityReference;
24    import org.xwiki.model.reference.ObjectReference;
25   
26    /**
27    * Object reference implementation for object names generated in {@code className[objectNumber]} format. It provides
28    * helper functions to extract the class name as specified by the caller, and object number. <br>
29    * Accepted formats for the object name are:
30    * <dl>
31    * <dt>className[objectNumber]
32    * <dd>interpreted as the object of class className with number objectNumber. refers the object returned by
33    * XWikiDocument.getObject(String className, int objectNumber). In this case, className is obtained by calling
34    * {@link #getClassName()} and object index by calling {@link #getObjectNumber()}.
35    * <dt>className
36    * <dd>interpreted as the first object of class className. refers the object returned by XWikiDocument.getObject(String
37    * className). In this case, {@link #getObjectNumber()} will return {@code null} and {@code className} is obtained by
38    * calling {@link #getClassName()}.
39    * </dl>
40    *
41    * @version $Id: d32aa710eba15aa28a84fd05907c2757cb40d03b $
42    * @since 2.3M1
43    */
 
44    public class IndexedObjectReference extends ObjectReference
45    {
46    /**
47    * The class name of this object, as set by the caller.
48    */
49    protected String className;
50   
51    /**
52    * The number of this object, as set by the caller.
53    */
54    protected Integer objectNumber;
55   
56    /**
57    * Constructor which would raise exceptions if the source entity reference does not have the appropriate type or
58    * parent, etc.
59    *
60    * @param reference the raw reference to build this object reference from
61    */
 
62  7 toggle public IndexedObjectReference(EntityReference reference)
63    {
64  7 super(reference);
65    }
66   
67    /**
68    * Builds an indexed object reference for the object of class {@code className} with index {@code objectNumber} in
69    * the document referenced by {@code parent}.
70    *
71    * @param className the name of the class of the object
72    * @param objectNumber the number of the object in the document, or {@code null} if the default object should be
73    * referenced
74    * @param parent reference to the parent document where the object is
75    */
 
76  1 toggle public IndexedObjectReference(String className, Integer objectNumber, EntityReference parent)
77    {
78    // crap, i'm building the string here to only parse it back in setName. But it shouldn't be that much processing
79  1 super(objectNumber != null ? className + "[" + objectNumber + "]" : className, new DocumentReference(parent));
80    }
81   
82    /**
83    * @return the name of the class of this object, as it was set by caller. I.e. no resolving is done from the value
84    * set in the name of the object.
85    */
 
86  8 toggle public String getClassName()
87    {
88  8 return className != null ? className : getName();
89    }
90   
91    /**
92    * @return the number of this object among the objects of the same class in the document, as set by the caller in
93    * [objectNumber] format after the class name (i.e. no resolving is done, existence of this object is not
94    * guaranteed). If no number can be parsed (i.e. [number] cannot be parsed) this function returns {@code
95    * null} and object should be interpreted as the first object of this class in the document.
96    */
 
97  8 toggle public Integer getObjectNumber()
98    {
99  8 return objectNumber;
100    }
101   
102    /**
103    * {@inheritDoc}
104    * <p>
105    * Overridden to always compute the class name and the object number.
106    * </p>
107    *
108    * @see org.xwiki.model.reference.EntityReference#setName(java.lang.String)
109    */
 
110  8 toggle @Override
111    protected void setName(String name)
112    {
113  8 super.setName(name);
114   
115    // set fields to default values
116  8 className = name;
117  8 objectNumber = null;
118   
119    // find last encounter of ] (which should be the last character), then last encounter of [, parse in between as
120    // number. If anything in this fails, then everything is className
121  8 int closePosition = name.lastIndexOf(']');
122    // if there is no ] or is not on the last position
123  8 if (closePosition < 0 || closePosition != name.length() - 1) {
124  2 return;
125    }
126    // if there is no [
127  6 int openPosition = name.lastIndexOf('[');
128  6 if (openPosition < 0) {
129  1 return;
130    }
131    // parse the string between the two as object number
132  5 String numberString = name.substring(openPosition + 1, closePosition);
133  5 try {
134  5 objectNumber = Integer.parseInt(numberString);
135  4 className = name.substring(0, openPosition);
136    } catch (NumberFormatException e) {
137    // number could not be parsed, which means className stays name, and object stays null
138  1 return;
139    }
140    }
141    }