1. Project Clover database Sat Feb 2 2019 06:45:20 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 0
1.0100%
 

Contributing tests

This file is covered by 120 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  1189478 toggle @Override
52    public String serialize(EntityReference reference, Object... parameters)
53    {
54  1190166 if (reference == null) {
55  47 return null;
56    }
57   
58  1190476 StringBuilder representation = new StringBuilder();
59   
60  1189987 List<EntityReference> references = reference.getReversedReferenceChain();
61  4748894 for (int i = 0; i < references.size();) {
62  3558538 EntityReference currentReference = references.get(i);
63  3558789 EntityType currentType = currentReference.getType();
64   
65    // Move to last element of the same type
66  3589493 while (++i < references.size() && references.get(i).getType() == currentType) {
67  30688 currentReference = references.get(i);
68    }
69   
70  3558594 if (shouldSerialize(currentReference, representation, currentReference == reference, parameters)) {
71  2349885 serializeEntityReferenceType(currentReference, representation, currentReference == reference);
72    }
73    }
74   
75  1191028 return representation.toString();
76    }
77   
78    /**
79    * @since 7.2M2
80    */
 
81  3559156 toggle protected boolean shouldSerialize(EntityReference currentReference, StringBuilder representation,
82    boolean isLastReference, Object... parameters)
83    {
84  3559074 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  3558902 if (isLastReference || representation.length() > 0) {
93  1191472 shouldPrint = true;
94    } else {
95  2367191 EntityReference defaultReference = resolveDefaultReference(currentReference.getType(), parameters);
96  2366801 if (defaultReference == null || !equal(defaultReference, currentReference)) {
97  1158382 shouldPrint = true;
98    }
99    }
100   
101  3558719 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  2370722 toggle protected void serializeEntityReferenceType(EntityReference reference, StringBuilder representation,
111    boolean isLastReference)
112    {
113  2370659 EntityReference parent = reference.getParent();
114  2369563 if (parent != null && parent.getType() == reference.getType()) {
115  20961 serializeEntityReferenceType(parent, representation, false);
116    }
117   
118  2370562 super.serializeEntityReference(reference, representation, isLastReference);
119    }
120   
 
121  1216569 toggle protected boolean equal(EntityReference defaultReference, EntityReference currentReference)
122    {
123  1216833 EntityReference defaultReferenceIt = defaultReference;
124  1217133 EntityReference currentReferenceIt = currentReference;
125   
126  2434993 for (; defaultReferenceIt != null; defaultReferenceIt = defaultReferenceIt.getParent(), currentReferenceIt =
127    currentReferenceIt.getParent()) {
128  1225982 if (currentReferenceIt == null || defaultReferenceIt.getType() != currentReferenceIt.getType()
129    || !defaultReferenceIt.getName().equals(currentReferenceIt.getName())) {
130  8230 return false;
131    }
132    }
133   
134  1208634 return true;
135    }
136   
137    /**
138    * @since 7.2M1
139    */
 
140  1215470 toggle protected EntityReference resolveDefaultReference(EntityType type, Object... parameters)
141    {
142  1216378 EntityReference resolvedDefaultReference = null;
143  1217072 if (parameters.length > 0 && parameters[0] instanceof EntityReference) {
144    // Try to extract the type from the passed parameter.
145  1113897 EntityReference referenceParameter = (EntityReference) parameters[0];
146  1114103 EntityReference extractedReference = referenceParameter.extractReference(type);
147  1113140 if (extractedReference != null) {
148  1113167 resolvedDefaultReference = extractedReference;
149   
150    // Remove parent if any
151  1113301 EntityReference parent = resolvedDefaultReference.getParent();
152  1113407 while (parent != null && parent.getType() == resolvedDefaultReference.getType()) {
153  26 parent = parent.getParent();
154    }
155  1112969 if (parent != null) {
156  249 resolvedDefaultReference = resolvedDefaultReference.removeParent(parent);
157    }
158    }
159    }
160   
161  1216972 if (resolvedDefaultReference == null) {
162  103600 resolvedDefaultReference = this.provider.getDefaultReference(type);
163    }
164   
165  1217175 return resolvedDefaultReference;
166    }
167    }