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

File ExtensionIdConverter.java

 

Coverage histogram

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

Code metrics

14
44
8
1
167
97
16
0.36
5.5
8
2

Classes

Class Line # Actions
ExtensionIdConverter 44 44 0% 16 4
0.9393939493.9%
 

Contributing tests

This file is covered by 51 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.extension.internal.converter;
21   
22    import java.lang.reflect.Type;
23    import java.util.ArrayList;
24    import java.util.Collection;
25    import java.util.List;
26   
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.extension.ExtensionId;
32    import org.xwiki.extension.version.Version;
33    import org.xwiki.extension.version.internal.DefaultVersion;
34    import org.xwiki.properties.converter.AbstractConverter;
35   
36    /**
37    * Convert an extension feature from a String to a {@link ExtensionId} object and the other way around.
38    *
39    * @version $Id: f6bbcfa97f608ffd362d15b935a13339973219e2 $
40    * @since 8.0M1
41    */
42    @Component
43    @Singleton
 
44    public class ExtensionIdConverter extends AbstractConverter<ExtensionId>
45    {
 
46  39 toggle private static int countBackslashes(String str, int index)
47    {
48  46 for (int i = index - 1; i >= 0; --i) {
49  45 if (str.charAt(i) != '\\') {
50  38 return index - i - 1;
51    }
52    }
53   
54  1 return index;
55    }
56   
57    /**
58    * @param values the values to convert
59    * @param defaultVersion the default version to set in the {@link ExtensionId} if none can be extracted from the
60    * passed <code>value</code>
61    * @return the list of {@link ExtensionId}s created from the passed value
62    */
 
63  985 toggle public static List<ExtensionId> toExtensionIdList(Collection<?> values, Version defaultVersion)
64    {
65  985 List<ExtensionId> list = new ArrayList<>(values.size());
66   
67  985 for (Object value : values) {
68  1093 list.add(toExtensionId(value, defaultVersion));
69    }
70   
71  985 return list;
72    }
73   
74    /**
75    * @param value the value to convert
76    * @param defaultVersion the default version to set in the {@link ExtensionId} if none can be extracted from the
77    * passed <code>value</code>
78    * @return the {@link ExtensionId} created from the passed value
79    */
 
80  1112 toggle public static ExtensionId toExtensionId(Object value, Version defaultVersion)
81    {
82  1112 if (value != null) {
83  1112 String valueString = value.toString();
84  1112 return toExtensionId(valueString, valueString.length() - 1, defaultVersion);
85    }
86   
87  0 return null;
88    }
89   
 
90  1115 toggle private static ExtensionId toExtensionId(String value, int end, Version defaultVersion)
91    {
92  1115 String valueString = value;
93   
94  1115 int index = valueString.lastIndexOf('/');
95  1115 String id = valueString;
96  1115 Version version;
97  1115 if (index > 0 && index < end) {
98  39 int backslashes = countBackslashes(valueString, index);
99  39 if (backslashes > 0) {
100  4 StringBuilder builder = new StringBuilder();
101  4 builder.append(valueString.substring(0, index - backslashes));
102  4 builder.append(StringUtils.repeat('\\', backslashes / 2));
103  4 builder.append(valueString.substring(index));
104   
105  4 valueString = builder.toString();
106  4 index -= backslashes - (backslashes / 2);
107   
108  4 if (backslashes % 2 == 1) {
109  3 return toExtensionId(valueString, index - backslashes - 1, defaultVersion);
110    }
111    }
112   
113  36 id = valueString.substring(0, index);
114  36 version = new DefaultVersion(valueString.substring(index + 1));
115    } else {
116  1076 id = valueString;
117  1076 version = defaultVersion;
118    }
119   
120  1112 return new ExtensionId(id, version);
121    }
122   
123    /**
124    * @param value the {@link ExtensionId} to serialize
125    * @return the String version of an {@link ExtensionId}
126    */
 
127  459 toggle public static String toString(ExtensionId value)
128    {
129  459 StringBuilder builder = new StringBuilder();
130   
131  459 builder.append(value.getId());
132   
133  459 if (value.getVersion() != null) {
134  453 builder.append('/');
135  453 builder.append(value.getVersion());
136    }
137   
138  459 return builder.toString();
139    }
140   
141    /**
142    * @param values the list of {@link ExtensionId}s to serialize
143    * @return the String version of an {@link ExtensionId}s list
144    */
 
145  3085 toggle public static List<String> toStringList(Collection<ExtensionId> values)
146    {
147  3085 List<String> list = new ArrayList<>(values.size());
148   
149  3085 for (ExtensionId value : values) {
150  453 list.add(toString(value));
151    }
152   
153  3085 return list;
154    }
155   
 
156  6 toggle @Override
157    protected ExtensionId convertToType(Type targetType, Object value)
158    {
159  6 return toExtensionId(value, null);
160    }
161   
 
162  0 toggle @Override
163    protected String convertToString(ExtensionId value)
164    {
165  0 return toString(value);
166    }
167    }