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

File DefaultSymbolScheme.java

 

Coverage histogram

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

Code metrics

0
32
7
1
162
85
7
0.22
4.57
7
1

Classes

Class Line # Actions
DefaultSymbolScheme 41 32 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3230 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.ArrayList;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.model.EntityType;
32   
33    /**
34    * Default Symbols used for representing {@link org.xwiki.model.reference.EntityReference} as strings.
35    *
36    * @version $Id: 2ef28ab905bb8126e012732b754a64d9d481821f $
37    * @since 8.1M2
38    */
39    @Component
40    @Singleton
 
41    public class DefaultSymbolScheme implements SymbolScheme
42    {
43    /**
44    * A backslash string.
45    */
46    private static final char CESCAPE = '\\';
47   
48    /**
49    * A colon string. Colon is used to separate wiki name.
50    */
51    private static final char CWIKISEP = ':';
52   
53    /**
54    * A dot string. Dot is used to separate space names and document name.
55    */
56    private static final char CSPACESEP = '.';
57   
58    /**
59    * An at-sign string. At sign is used to separate attachment name.
60    */
61    private static final char CATTACHMENTSEP = '@';
62   
63    /**
64    * An hat sign string. Hat sign is used to separate object name.
65    */
66    private static final char COBJECTSEP = '^';
67   
68    /**
69    * An dot is used to separate object property name.
70    */
71    private static final char CPROPERTYSEP = CSPACESEP;
72   
73    /**
74    * An hat sign is used to separate class name.
75    */
76    private static final char CCLASSPROPSEP = COBJECTSEP;
77   
78    private static final Map<EntityType, Map<EntityType, Character>> SEPARATORS =
79    new HashMap<EntityType, Map<EntityType, Character>>()
80    {
 
81  160 toggle {
82  160 put(EntityType.WIKI, Collections.emptyMap());
83   
84  160 Map<EntityType, Character> spaceSeparators = new HashMap<>();
85  160 spaceSeparators.put(EntityType.WIKI, CWIKISEP);
86  160 spaceSeparators.put(EntityType.SPACE, CSPACESEP);
87  160 put(EntityType.SPACE, spaceSeparators);
88   
89  160 put(EntityType.DOCUMENT, Collections.singletonMap(EntityType.SPACE, CSPACESEP));
90  160 put(EntityType.ATTACHMENT, Collections.singletonMap(EntityType.DOCUMENT, CATTACHMENTSEP));
91  160 put(EntityType.OBJECT, Collections.singletonMap(EntityType.DOCUMENT, COBJECTSEP));
92  160 put(EntityType.OBJECT_PROPERTY, Collections.singletonMap(EntityType.OBJECT, CPROPERTYSEP));
93  160 put(EntityType.CLASS_PROPERTY, Collections.singletonMap(EntityType.DOCUMENT, CCLASSPROPSEP));
94    }
95    };
96   
97    private Map<EntityType, String[]> escapes;
98   
99    private Map<EntityType, String[]> replacements;
100   
101    /**
102    * Initialize internal data structures.
103    */
 
104  1283 toggle public DefaultSymbolScheme()
105    {
106  1283 initialize();
107    }
108   
109    /**
110    * Initialize internal data structures.
111    */
 
112  1283 toggle public void initialize()
113    {
114    // Dynamically create the escape/replacement maps.
115    // The characters to escape are all the characters that are separators between the current type and its parent
116    // type + the escape symbol itself.
117  1283 this.escapes = new HashMap<>();
118  1283 this.replacements = new HashMap<>();
119   
120  1283 String escape = Character.toString(getEscapeSymbol());
121  1283 for (Map.Entry<EntityType, Map<EntityType, Character>> entry : SEPARATORS.entrySet()) {
122  8981 EntityType type = entry.getKey();
123  8981 Map<EntityType, Character> separators = entry.getValue();
124  8981 List<String> charactersToEscape = new ArrayList<>();
125  8981 List<String> replacementCharacters = new ArrayList<>();
126  8981 for (Character characterToEscape : separators.values()) {
127  8981 charactersToEscape.add(Character.toString(characterToEscape));
128  8981 replacementCharacters.add(escape + Character.toString(characterToEscape));
129    }
130  8981 charactersToEscape.add(escape);
131  8981 replacementCharacters.add(escape + escape);
132  8981 String[] escapesArray = new String[charactersToEscape.size()];
133  8981 this.escapes.put(type, charactersToEscape.toArray(escapesArray));
134  8981 String[] replacementsArray = new String[replacementCharacters.size()];
135  8981 this.replacements.put(type, replacementCharacters.toArray(replacementsArray));
136    }
137    }
138   
 
139  13391238 toggle @Override
140    public Character getEscapeSymbol()
141    {
142  13391175 return CESCAPE;
143    }
144   
 
145  2741870 toggle @Override
146    public Map<EntityType, Map<EntityType, Character>> getSeparatorSymbols()
147    {
148  2741867 return SEPARATORS;
149    }
150   
 
151  4306136 toggle @Override
152    public String[] getSymbolsRequiringEscapes(EntityType type)
153    {
154  4306145 return this.escapes.get(type);
155    }
156   
 
157  4306129 toggle @Override
158    public String[] getReplacementSymbols(EntityType type)
159    {
160  4306126 return this.replacements.get(type);
161    }
162    }