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

File LocalUidStringEntityReferenceSerializer.java

 

Coverage histogram

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

Code metrics

16
23
2
1
126
63
11
0.48
11.5
2
5.5

Classes

Class Line # Actions
LocalUidStringEntityReferenceSerializer 55 23 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 34 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.List;
24    import java.util.Locale;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35   
36    /**
37    * <p>
38    * Serialize a reference into a unique identifier string within a wiki. Its similar to the
39    * {@link UidStringEntityReferenceSerializer}, but is made appropriate for a wiki independent storage.
40    * </p>
41    * <p>
42    * The string created looks like {@code 5:space3:doc} for the {@code wiki:space.doc} document reference. and
43    * {@code 5:space3:doc15:xspace.class[0]} for the wiki:space.doc^wiki:xspace.class[0] object. (with {@code 5} being the
44    * length of the space name, i.e the length of {@code space} and {@code 3} being the length of the page name, i.e. the
45    * length of {@code doc}).
46    * </p>
47    *
48    * @version $Id: 2db3e238086a32b8dc6bf43a2e1b3869df59b093 $
49    * @since 4.0M1
50    * @see UidStringEntityReferenceSerializer
51    */
52    @Component
53    @Named("local/uid")
54    @Singleton
 
55    public class LocalUidStringEntityReferenceSerializer implements EntityReferenceSerializer<String>
56    {
57    /**
58    * Unique instance of the uid serializer.
59    */
60    public static final LocalUidStringEntityReferenceSerializer INSTANCE =
61    new LocalUidStringEntityReferenceSerializer();
62   
63    @Inject
64    private SymbolScheme symbolScheme;
65   
 
66  312806 toggle @Override
67    public String serialize(EntityReference reference, Object... parameters)
68    {
69  312798 if (reference == null) {
70  3 return null;
71    }
72   
73  312794 StringBuilder representation = new StringBuilder();
74  312797 List<EntityReference> references = reference.getReversedReferenceChain();
75  312797 EntityReference wikiReference = references.get(0);
76   
77  312801 int index;
78  312800 if (wikiReference.getType() == EntityType.WIKI) {
79  312799 index = 1;
80    } else {
81  2 index = 0;
82    }
83   
84  1125767 for (; index < reference.size(); ++index) {
85  812971 serializeEntityReference(references.get(index), representation, wikiReference, parameters);
86    }
87   
88  312808 return representation.toString();
89    }
90   
91    /**
92    * Serialize a single reference element into the representation string builder.
93    *
94    * @param currentReference the reference to serialize
95    * @param representation the builder where to happen the serialized member
96    * @param wikiReference the wiki reference of this entity reference
97    * @param parameters optional parameters
98    */
 
99  812963 toggle protected void serializeEntityReference(EntityReference currentReference, StringBuilder representation,
100    EntityReference wikiReference, Object... parameters)
101    {
102  812959 String name = currentReference.getName();
103   
104    // FIXME: Not really nice to parse here the serialized XClass reference to remove its wiki name when local.
105    // This also why this is not a simple derived class of UidStringEntityReferenceSerializer.
106  812966 if (wikiReference != null && currentReference.getType() == EntityType.OBJECT) {
107  176218 if (name.startsWith(wikiReference.getName()
108    + this.symbolScheme.getSeparatorSymbols().get(EntityType.SPACE).get(EntityType.WIKI)))
109    {
110  176216 name = name.substring(wikiReference.getName().length() + 1);
111    }
112    }
113  812966 representation.append(name.length()).append(':').append(name);
114   
115    // Append Locale
116  812973 if (currentReference instanceof DocumentReference) {
117  308871 Locale locale = ((DocumentReference) currentReference).getLocale();
118  308866 if (locale != null) {
119  15106 String localeString = locale.toString();
120  15102 if (!localeString.isEmpty()) {
121  2871 representation.append(localeString.length()).append(':').append(localeString);
122    }
123    }
124    }
125    }
126    }