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

File AbstractReferenceEntityReferenceResolver.java

 

Coverage histogram

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

Code metrics

18
31
2
1
115
59
18
0.58
15.5
2
9

Classes

Class Line # Actions
AbstractReferenceEntityReferenceResolver 37 31 0% 18 0
1.0100%
 

Contributing tests

This file is covered by 272 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.internal.reference;
21   
22    import java.util.List;
23   
24    import org.xwiki.model.EntityType;
25    import org.xwiki.model.reference.EntityReference;
26    import org.xwiki.model.reference.EntityReferenceResolver;
27    import org.xwiki.model.reference.InvalidEntityReferenceException;
28   
29    /**
30    * Resolve an {@link EntityReference} into a valid and absolute reference (with all required parents filled in). Generic
31    * implementation deferring default values for unspecified reference parts to extending classes.
32    *
33    * @see AbstractEntityReferenceResolver
34    * @version $Id: c62aa22c9052d872714a30a0fd0586db08da9d0f $
35    * @since 2.2.3
36    */
 
37    public abstract class AbstractReferenceEntityReferenceResolver extends AbstractEntityReferenceResolver
38    implements EntityReferenceResolver<EntityReference>
39    {
 
40  8515433 toggle @Override
41    public EntityReference resolve(EntityReference referenceToResolve, EntityType type, Object... parameters)
42    {
43  8518803 EntityReference normalizedReference = referenceToResolve;
44   
45  8519110 if (normalizedReference == null) {
46  1040 normalizedReference = resolveDefaultReference(type, parameters);
47  8519052 } else if (normalizedReference.getType() != type) {
48    // If the passed type is not compatible with the reference to resolve's type then we need to convert it
49  137478 if (!type.isAllowedAncestor(normalizedReference.getType())
50    && !normalizedReference.getType().isAllowedAncestor(type)) {
51  5104 normalizedReference = normalizeReference(normalizedReference, parameters);
52   
53  5104 normalizedReference = toCompatibleEntityReference(normalizedReference, type);
54    }
55   
56    // If the passed type is a supertype of the reference to resolve's type then we need to insert a top
57    // level reference.
58  137481 if (type != normalizedReference.getType() && type.isAllowedAncestor(normalizedReference.getType())) {
59  132082 normalizedReference = resolveDefaultReference(type, parameters).appendParent(normalizedReference);
60    }
61    }
62   
63    // Check all references and parent references which have a NULL name and replace them with default values.
64    // In addition insert references where needed.
65  8520363 try {
66  8520956 normalizedReference = normalizeReference(normalizedReference, parameters);
67    } catch (InvalidEntityReferenceException e) {
68  1 throw new InvalidEntityReferenceException("Invalid reference [" + referenceToResolve + "]");
69    }
70   
71  8520313 if (type != normalizedReference.getType()) {
72    // If the passed type is a subtype of the reference to resolve's type then we extract the reference.
73  294 normalizedReference = normalizedReference.extractReference(type);
74    }
75   
76  8520393 return normalizedReference;
77    }
78   
79    /**
80    * Normalize the provided reference, filling missing names, and gaps in the parent chain.
81    *
82    * @param referenceToResolve the reference to normalize, if the first parameter is an entity reference, it is used
83    * to compute default names.
84    * @param parameters optional parameters,
85    * @return a normalized reference chain
86    */
 
87  8523067 toggle private EntityReference normalizeReference(EntityReference referenceToResolve, Object[] parameters)
88    {
89  8524086 EntityReference normalizedReference = referenceToResolve;
90  8523327 EntityReference reference = normalizedReference;
91  35028002 while (reference != null) {
92  26507359 List<EntityType> types = reference.getType().getAllowedParents();
93  26506604 if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) {
94    // The parent reference isn't the allowed parent: insert an allowed reference
95  96068 EntityReference newReference =
96    resolveDefaultReference(types.get(0), parameters).appendParent(reference.getParent());
97  96068 normalizedReference = normalizedReference.replaceParent(reference.getParent(), newReference);
98  96068 reference = newReference;
99  26408124 } else if (reference.getParent() == null && !types.isEmpty()) {
100    // The top reference isn't the allowed top level reference, add a parent reference
101  4999967 EntityReference newReference = resolveDefaultReference(types.get(0), parameters);
102  4999305 normalizedReference = normalizedReference.appendParent(newReference);
103  4999907 reference = newReference;
104  21407637 } else if (reference.getParent() != null && types.isEmpty()) {
105    // There's a parent but no one is allowed
106  1 throw new InvalidEntityReferenceException();
107    } else {
108    // Parent is ok, check next
109  21410394 reference = reference.getParent();
110    }
111    }
112   
113  8525460 return normalizedReference;
114    }
115    }