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

File CompactStringEntityReferenceSerializer.java

 

Coverage histogram

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

Code metrics

28
43
5
1
167
97
27
0.63
8.6
5
5.4

Classes

Class Line # Actions
CompactStringEntityReferenceSerializer 45 43 0% 27 4
0.9473684494.7%
 

Contributing tests

This file is covered by 55 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.internal.model.reference;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.EntityType;
30    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.EntityReferenceProvider;
33   
34    /**
35    * Generate an entity reference string that doesn't contain reference parts that are the same as either the current
36    * entity in the execution context or as the passed entity reference (if any). Note that the terminal part is always
37    * kept (eg the document's page for a document reference or the attachment's filename for an attachment reference).
38    *
39    * @version $Id: d99a8ab7139584e51c46d94d72ac8b8524a72388 $
40    * @since 2.2M1
41    */
42    @Component
43    @Singleton
44    @Named("compact")
 
45    public class CompactStringEntityReferenceSerializer extends DefaultStringEntityReferenceSerializer
46    {
47    @Inject
48    @Named("current")
49    private EntityReferenceProvider provider;
50   
 
51  178437 toggle @Override
52    public String serialize(EntityReference reference, Object... parameters)
53    {
54  178435 if (reference == null) {
55  0 return null;
56    }
57   
58  178434 StringBuilder representation = new StringBuilder();
59   
60  178434 List<EntityReference> references = reference.getReversedReferenceChain();
61  709859 for (int i = 0; i < references.size();) {
62  531414 EntityReference currentReference = references.get(i);
63  531415 EntityType currentType = currentReference.getType();
64   
65    // Move to last element of the same type
66  531639 while (++i < references.size() && references.get(i).getType() == currentType) {
67  221 currentReference = references.get(i);
68    }
69   
70  531439 if (shouldSerialize(currentReference, representation, currentReference == reference, parameters)) {
71  358159 serializeEntityReferenceType(currentReference, representation, currentReference == reference);
72    }
73    }
74   
75  178459 return representation.toString();
76    }
77   
78    /**
79    * @since 7.2M2
80    */
 
81  531413 toggle protected boolean shouldSerialize(EntityReference currentReference, StringBuilder representation,
82    boolean isLastReference, Object... parameters)
83    {
84  531400 boolean shouldPrint = false;
85   
86    // Only serialize if:
87    // - the current entity reference has a different value and type than the passed reference
88    // - the entity type being serialized is not the last type of the chain
89    // In addition an entity reference isn't printed only if all parent references are not printed either,
90    // otherwise print it. For example "wiki:page" isn't allowed for a Document Reference.
91   
92  531433 if (isLastReference || representation.length() > 0) {
93  181182 shouldPrint = true;
94    } else {
95  350252 EntityReference defaultReference = resolveDefaultReference(currentReference.getType(), parameters);
96  350266 if (defaultReference == null || !equal(defaultReference, currentReference)) {
97  176962 shouldPrint = true;
98    }
99    }
100   
101  531429 return shouldPrint;
102    }
103   
104    /**
105    * Serialize the last part of the reference (all the ending elements having the same entity type).
106    *
107    * @param reference the reference to serialize
108    * @since 7.2M2
109    */
 
110  358270 toggle protected void serializeEntityReferenceType(EntityReference reference, StringBuilder representation,
111    boolean isLastReference)
112    {
113  358274 EntityReference parent = reference.getParent();
114  358275 if (parent != null && parent.getType() == reference.getType()) {
115  135 serializeEntityReferenceType(parent, representation, false);
116    }
117   
118  358283 super.serializeEntityReference(reference, representation, isLastReference);
119    }
120   
 
121  176165 toggle protected boolean equal(EntityReference defaultReference, EntityReference currentReference)
122    {
123  176165 EntityReference defaultReferenceIt = defaultReference;
124  176163 EntityReference currentReferenceIt = currentReference;
125   
126  349546 for (; defaultReferenceIt != null; defaultReferenceIt = defaultReferenceIt.getParent(), currentReferenceIt =
127    currentReferenceIt.getParent()) {
128  176262 if (currentReferenceIt == null || defaultReferenceIt.getType() != currentReferenceIt.getType()
129    || !defaultReferenceIt.getName().equals(currentReferenceIt.getName())) {
130  2877 return false;
131    }
132    }
133   
134  173289 return true;
135    }
136   
137    /**
138    * @since 7.2M1
139    */
 
140  176151 toggle protected EntityReference resolveDefaultReference(EntityType type, Object... parameters)
141    {
142  176157 EntityReference resolvedDefaultReference = null;
143  176162 if (parameters.length > 0 && parameters[0] instanceof EntityReference) {
144    // Try to extract the type from the passed parameter.
145  164273 EntityReference referenceParameter = (EntityReference) parameters[0];
146  164275 EntityReference extractedReference = referenceParameter.extractReference(type);
147  164277 if (extractedReference != null) {
148  164274 resolvedDefaultReference = extractedReference;
149   
150    // Remove parent if any
151  164272 EntityReference parent = resolvedDefaultReference.getParent();
152  164272 while (parent != null && parent.getType() == resolvedDefaultReference.getType()) {
153  0 parent = parent.getParent();
154    }
155  164258 if (parent != null) {
156  1043 resolvedDefaultReference = resolvedDefaultReference.removeParent(parent);
157    }
158    }
159    }
160   
161  176143 if (resolvedDefaultReference == null) {
162  11883 resolvedDefaultReference = this.provider.getDefaultReference(type);
163    }
164   
165  176184 return resolvedDefaultReference;
166    }
167    }