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

File AbstractEntityReferenceResolver.java

 

Coverage histogram

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

Code metrics

22
50
8
1
195
107
25
0.5
6.25
8
3.12

Classes

Class Line # Actions
AbstractEntityReferenceResolver 41 50 0% 25 4
0.9595%
 

Contributing tests

This file is covered by 477 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   
21    package org.xwiki.model.internal.reference;
22   
23    import java.util.EnumMap;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27   
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.reference.EntityReference;
30    import org.xwiki.model.reference.EntityReferenceProvider;
31    import org.xwiki.model.reference.InvalidEntityReferenceException;
32   
33    /**
34    * Generic entity reference resolver deferring resolution and default values to extending classes but resolving default
35    * value from the first optional parameter when provided and is an instance of a entity reference. This is use by most
36    * resolver to provide relative resolution to a provided reference.
37    *
38    * @version $Id: 631874355ae1d61f03ba3bf38f6b4dd77797970b $
39    * @since 3.3M2
40    */
 
41    public abstract class AbstractEntityReferenceResolver
42    {
43    protected static final Map<EntityType, EntityType> TYPE_CONVERT_MAPPING = new EnumMap<>(EntityType.class);
44   
 
45  121 toggle static {
46  121 TYPE_CONVERT_MAPPING.put(EntityType.WIKI, EntityType.WIKI);
47   
48  121 TYPE_CONVERT_MAPPING.put(EntityType.PAGE, EntityType.SPACE);
49  121 TYPE_CONVERT_MAPPING.put(EntityType.PAGE_ATTACHMENT, EntityType.ATTACHMENT);
50  121 TYPE_CONVERT_MAPPING.put(EntityType.PAGE_CLASS_PROPERTY, EntityType.CLASS_PROPERTY);
51  121 TYPE_CONVERT_MAPPING.put(EntityType.PAGE_OBJECT, EntityType.OBJECT);
52  121 TYPE_CONVERT_MAPPING.put(EntityType.PAGE_OBJECT_PROPERTY, EntityType.OBJECT_PROPERTY);
53   
54  121 TYPE_CONVERT_MAPPING.put(EntityType.SPACE, EntityType.PAGE);
55  121 TYPE_CONVERT_MAPPING.put(EntityType.ATTACHMENT, EntityType.PAGE_ATTACHMENT);
56  121 TYPE_CONVERT_MAPPING.put(EntityType.CLASS_PROPERTY, EntityType.PAGE_CLASS_PROPERTY);
57  121 TYPE_CONVERT_MAPPING.put(EntityType.OBJECT, EntityType.PAGE_OBJECT);
58  121 TYPE_CONVERT_MAPPING.put(EntityType.OBJECT_PROPERTY, EntityType.PAGE_OBJECT_PROPERTY);
59    }
60   
61    @Inject
62    private EntityReferenceProvider defaultProvider;
63   
 
64  5102 toggle private String getDefaultDocumentName()
65    {
66  5102 return this.defaultProvider.getDefaultReference(EntityType.DOCUMENT).getName();
67    }
68   
69    /**
70    * @param type the entity type for which to return the default value to use (since the use has not specified it)
71    * @param parameters optional parameters. Their meaning depends on the resolver implementation
72    * @return the default value to use
73    * @since 7.2M1
74    */
75    protected abstract EntityReference getDefaultReference(EntityType type, Object... parameters);
76   
77    /**
78    * Resolve default reference for a given reference type.
79    *
80    * @param type the type for which a default name is requested
81    * @param parameters optional parameters, if the first parameter is an entity reference which is of the given type
82    * or contains the given types in its parent chain, use the name of the reference having the requested
83    * type in place of the default value
84    * @return the reference for the given type
85    * @since 7.2M1
86    */
 
87  23942132 toggle protected EntityReference resolveDefaultReference(EntityType type, Object... parameters)
88    {
89  23947016 EntityReference resolvedDefaultValue = null;
90   
91  23952132 if (parameters.length > 0 && parameters[0] instanceof EntityReference) {
92    // Try to extract the required type from the passed parameter.
93  8905009 EntityReference referenceParameter = (EntityReference) parameters[0];
94    // Make sure to use a compatible reference
95  8906024 referenceParameter = toCompatibleEntityReference(referenceParameter, type);
96   
97  8905038 EntityReference extractedReference = referenceParameter.extractReference(type);
98  8902024 if (extractedReference != null) {
99  7811614 resolvedDefaultValue = extractedReference;
100   
101    // Get rid of parent if any
102  7813789 EntityReference parent = extractedReference.getParent();
103  7876975 while (parent != null && parent.getType() == type) {
104  63345 parent = parent.getParent();
105    }
106  7813945 if (parent != null) {
107  336895 resolvedDefaultValue = resolvedDefaultValue.removeParent(parent);
108    }
109    }
110    }
111   
112  23947158 if (resolvedDefaultValue == null) {
113  16139162 resolvedDefaultValue = getDefaultReference(type, parameters);
114    }
115   
116  23955441 return resolvedDefaultValue;
117    }
118   
119    /**
120    * @param referenceToConvert the reference to convert (if needed)
121    * @param toType the required type
122    * @return the converted reference if conversion is needed, the passed one otherwise
123    */
 
124  8909914 toggle protected EntityReference toCompatibleEntityReference(EntityReference referenceToConvert, EntityType toType)
125    {
126  8910778 return toCompatibleEntityReference(referenceToConvert, toType, true);
127    }
128   
129    /**
130    * Check if the passed types are compatible. This means that they are the same of that one can be the parent of the
131    * other.
132    *
133    * @param type1 the first type of test
134    * @param type2 the second type of test
135    * @return true of the passed types are compatible
136    */
 
137  8921809 toggle protected boolean isCompatible(EntityType type1, EntityType type2)
138    {
139  8922388 return type1 == type2 || type1.isAllowedAncestor(type2) || type2.isAllowedAncestor(type1);
140    }
141   
 
142  8922189 toggle private EntityReference toCompatibleEntityReference(EntityReference referenceToConvert, EntityType toType,
143    boolean firstPage)
144    {
145  8922761 if (referenceToConvert == null || isCompatible(referenceToConvert.getType(), toType)) {
146  8910296 return referenceToConvert;
147    }
148   
149    // Convert parent
150  13552 EntityReference convertedParent = toCompatibleEntityReference(referenceToConvert.getParent(), toType,
151    firstPage && referenceToConvert.getType() != EntityType.PAGE);
152   
153    // Convert reference
154  13552 EntityReference convertedReference;
155  13552 if (referenceToConvert.getType() == EntityType.DOCUMENT) {
156  5072 if (getDefaultDocumentName().equals(referenceToConvert.getName())) {
157  3302 convertedReference = new EntityReference(convertedParent, referenceToConvert.getParameters());
158    } else {
159  1770 convertedReference = new EntityReference(referenceToConvert.getName(), EntityType.PAGE, convertedParent,
160    referenceToConvert.getParameters());
161    }
162  8480 } else if (referenceToConvert.getType() == EntityType.PAGE && firstPage
163    && (toType == EntityType.DOCUMENT || toType.isAllowedAncestor(EntityType.DOCUMENT))) {
164  30 convertedReference = toDOCUMENT(referenceToConvert, convertedParent);
165    } else {
166  8450 convertedReference = convert(referenceToConvert, convertedParent);
167    }
168   
169  13552 return convertedReference;
170    }
171   
 
172  30 toggle private EntityReference toDOCUMENT(EntityReference referenceToConvert, EntityReference convertedParent)
173    {
174  30 return new EntityReference(getDefaultDocumentName(), EntityType.DOCUMENT,
175    new EntityReference(referenceToConvert.getName(), EntityType.SPACE, convertedParent),
176    referenceToConvert.getParameters());
177    }
178   
 
179  8450 toggle private EntityReference convert(EntityReference referenceToConvert, EntityReference convertedParent)
180    {
181  8450 EntityType convertedType = TYPE_CONVERT_MAPPING.get(referenceToConvert.getType());
182   
183  8450 EntityReference convertedReference;
184  8450 if (convertedType == referenceToConvert.getType()) {
185  0 convertedReference = referenceToConvert;
186  8450 } else if (convertedType != null) {
187  8450 convertedReference = new EntityReference(referenceToConvert.getName(), convertedType, convertedParent,
188    referenceToConvert.getParameters());
189    } else {
190  0 throw new InvalidEntityReferenceException("Reference [" + referenceToConvert + "] cannot be converted");
191    }
192   
193  8450 return convertedReference;
194    }
195    }