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

File AbstractReferenceEntityReferenceResolver.java

 

Coverage histogram

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

Code metrics

16
29
2
1
111
58
15
0.52
14.5
2
7.5

Classes

Class Line # Actions
AbstractReferenceEntityReferenceResolver 37 29 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 243 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: c0e49cac5b0e9488081feb486e30a4473c65c51f $
35    * @since 2.2.3
36    */
 
37    public abstract class AbstractReferenceEntityReferenceResolver extends AbstractEntityReferenceResolver implements
38    EntityReferenceResolver<EntityReference>
39    {
 
40  364484 toggle @Override
41    public EntityReference resolve(EntityReference referenceToResolve, EntityType type, Object... parameters)
42    {
43  364469 EntityReference normalizedReference;
44   
45  364444 if (referenceToResolve == null) {
46  44 normalizedReference = resolveDefaultReference(type, parameters);
47    } else {
48    // If the passed type is a supertype of the reference to resolve's type then we need to insert a top level
49    // reference.
50  364401 if (type.ordinal() > referenceToResolve.getType().ordinal()) {
51  3313 normalizedReference = resolveDefaultReference(type, parameters).appendParent(referenceToResolve);
52    } else {
53  361093 normalizedReference = referenceToResolve;
54    }
55    }
56   
57    // Check all references and parent references which have a NULL name and replace them with default values.
58    // In addition insert references where needed.
59  364440 try {
60  364454 normalizedReference = normalizeReference(normalizedReference, parameters);
61    } catch (InvalidEntityReferenceException e) {
62  1 throw new InvalidEntityReferenceException("Invalid reference [" + referenceToResolve + "]");
63    }
64   
65  364484 if (referenceToResolve != null) {
66    // If the passed type is a subtype of the reference to resolve's type then we extract the reference.
67  364429 if (type.ordinal() < referenceToResolve.getType().ordinal()) {
68  6 normalizedReference = normalizedReference.extractReference(type);
69    }
70    }
71   
72  364459 return normalizedReference;
73    }
74   
75    /**
76    * Normalize the provided reference, filling missing names, and gaps in the parent chain.
77    *
78    * @param referenceToResolve the reference to normalize, if the first parameter is an entity reference, it is used
79    * to compute default names.
80    * @param parameters optional parameters,
81    * @return a normalized reference chain
82    */
 
83  364445 toggle private EntityReference normalizeReference(EntityReference referenceToResolve, Object[] parameters)
84    {
85  364433 EntityReference normalizedReference = referenceToResolve;
86  364443 EntityReference reference = normalizedReference;
87  1460035 while (reference != null) {
88  1095584 List<EntityType> types = EntityReferenceConstants.PARENT_TYPES.get(reference.getType());
89  1095625 if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) {
90    // The parent reference isn't the allowed parent: insert an allowed reference
91  252 EntityReference newReference =
92    resolveDefaultReference(types.get(0), parameters).appendParent(reference.getParent());
93  252 normalizedReference = normalizedReference.replaceParent(reference.getParent(), newReference);
94  252 reference = newReference;
95  1095365 } else if (reference.getParent() == null && !types.isEmpty()) {
96    // The top reference isn't the allowed top level reference, add a parent reference
97  221738 EntityReference newReference = resolveDefaultReference(types.get(0), parameters);
98  221737 normalizedReference = normalizedReference.appendParent(newReference);
99  221730 reference = newReference;
100  873620 } else if (reference.getParent() != null && types.isEmpty()) {
101    // There's a parent but no one is allowed
102  1 throw new InvalidEntityReferenceException();
103    } else {
104    // Parent is ok, check next
105  873628 reference = reference.getParent();
106    }
107    }
108   
109  364475 return normalizedReference;
110    }
111    }