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

File DefaultSolrFieldNameEncoder.java

 

Coverage histogram

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

Code metrics

16
32
4
1
138
73
14
0.44
8
4
3.5

Classes

Class Line # Actions
DefaultSolrFieldNameEncoder 44 32 0% 14 2
0.9615384396.2%
 

Contributing tests

This file is covered by 2 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.search.solr.internal;
21   
22    import java.io.UnsupportedEncodingException;
23    import java.net.URLDecoder;
24   
25    import javax.inject.Singleton;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.search.solr.internal.api.SolrFieldNameEncoder;
29   
30    /**
31    * Default {@link SolrFieldNameEncoder} implementation. It uses an encoding scheme similar to the URL encoding, with '$'
32    * (dollar) as the escape character instead of '%'. We couldn't use directly an URL encoding because some characters
33    * that are URL valid are not allowed in a field name and the other way around. Also the '%' (percent) character is not
34    * allowed in the name of a Solr field. We chose to use '$' instead because it was the least used from the few
35    * non-alphanumeric characters that are allowed in the field name. Other options would have been '.' (but it is used to
36    * separate the space and page name in a class/property reference), '_' (often used in field names), '-' (which doesn't
37    * look natural as an escape character and it's also used more often).
38    *
39    * @version $Id: e6301a6d28920fcb7f088696d6d85439fe92b61b $
40    * @since 5.3RC1
41    */
42    @Component
43    @Singleton
 
44    public class DefaultSolrFieldNameEncoder implements SolrFieldNameEncoder
45    {
46    /**
47    * The character used to escape/encode special characters.
48    */
49    private static final char ESCAPE = '$';
50   
51    /**
52    * The code point difference between upper case and lower case.
53    */
54    private static final int CASE_DIFF = 'a' - 'A';
55   
56    /**
57    * The UTF-8 character set.
58    */
59    private static final String UTF8 = "UTF-8";
60   
 
61  8694 toggle @Override
62    public String encode(final String fieldName)
63    {
64  8694 if (fieldName == null) {
65  1 return null;
66    }
67   
68  8693 int offset = 0;
69  8693 final int length = fieldName.length();
70  8693 final StringBuilder output = new StringBuilder(length);
71  8693 boolean dirty = false;
72  271132 while (offset < length) {
73  262439 final int codePoint = fieldName.codePointAt(offset);
74  262439 final char[] chars = Character.toChars(codePoint);
75  262439 if (needsEncoding(codePoint)) {
76  5 encode(chars, output);
77  5 dirty = true;
78    } else {
79  262434 output.append(chars);
80    }
81  262439 offset += chars.length;
82    }
83  8693 return dirty ? output.toString() : fieldName;
84    }
85   
 
86  4 toggle @Override
87    public String decode(String fieldName)
88    {
89  4 try {
90  4 if (fieldName != null) {
91  3 return URLDecoder.decode(fieldName.replace('$', '%'), UTF8);
92    }
93    } catch (UnsupportedEncodingException e) {
94    // Should never happen.
95    }
96   
97  1 return null;
98    }
99   
100    /**
101    * @param codePoint a code point, as returned by {@link String#codePointAt(int)}
102    * @return {@code true} if the specified code point needs to be encoded, {@code false} otherwise
103    */
 
104  262439 toggle protected boolean needsEncoding(int codePoint)
105    {
106  262439 return codePoint == ESCAPE
107    || !(Character.isJavaIdentifierPart(codePoint) || codePoint == '-' || codePoint == '.');
108    }
109   
110    /**
111    * Encodes the given characters.
112    *
113    * @param chars the characters to encode
114    * @param output where to write the encoded characters
115    */
 
116  5 toggle protected void encode(char[] chars, StringBuilder output)
117    {
118  5 try {
119  5 byte[] bytes = new String(chars).getBytes(UTF8);
120  10 for (int i = 0, length = bytes.length; i < length; i++) {
121  5 output.append(ESCAPE);
122  5 char ch = Character.forDigit((bytes[i] >> 4) & 0xF, 16);
123    // Use upper case letters in the hex value.
124  5 if (Character.isLetter(ch)) {
125  0 ch -= CASE_DIFF;
126    }
127  5 output.append(ch);
128  5 ch = Character.forDigit(bytes[i] & 0xF, 16);
129  5 if (Character.isLetter(ch)) {
130  3 ch -= CASE_DIFF;
131    }
132  5 output.append(ch);
133    }
134    } catch (UnsupportedEncodingException e) {
135    // Shouldn't never happen.
136    }
137    }
138    }