1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.internal.converter

File ExtensionIdConverter.java

 

Coverage histogram

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

Code metrics

16
47
10
1
200
111
19
0.4
4.7
10
1.9

Classes

Class Line # Actions
ExtensionIdConverter 46 47 0% 19 4
0.9452054594.5%
 

Contributing tests

This file is covered by 73 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.Inject;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.extension.ExtensionId;
33    import org.xwiki.extension.internal.ExtensionFactory;
34    import org.xwiki.extension.version.Version;
35    import org.xwiki.extension.version.internal.DefaultVersion;
36    import org.xwiki.properties.converter.AbstractConverter;
37   
38    /**
39    * Convert an extension feature from a String to a {@link ExtensionId} object and the other way around.
40    *
41    * @version $Id: 22be262fbe4d7731197fa038cbd5b2c89afc2151 $
42    * @since 8.0M1
43    */
44    @Component
45    @Singleton
 
46    public class ExtensionIdConverter extends AbstractConverter<ExtensionId>
47    {
48    @Inject
49    private ExtensionFactory factory;
50   
 
51  127 toggle private static int countBackslashes(String str, int index)
52    {
53  134 for (int i = index - 1; i >= 0; --i) {
54  133 if (str.charAt(i) != '\\') {
55  126 return index - i - 1;
56    }
57    }
58   
59  1 return index;
60    }
61   
62    /**
63    * @param values the values to convert
64    * @param defaultVersion the default version to set in the {@link ExtensionId} if none can be extracted from the
65    * passed <code>value</code>
66    * @return the list of {@link ExtensionId}s created from the passed value
67    */
 
68  970 toggle public static List<ExtensionId> toExtensionIdList(Collection<?> values, Version defaultVersion)
69    {
70  970 return toExtensionIdList(values, defaultVersion, null);
71    }
72   
73    /**
74    * @param values the values to convert
75    * @param defaultVersion the default version to set in the {@link ExtensionId} if none can be extracted from the
76    * passed <code>value</code>
77    * @param factory the factory used to create the {@link Version} instance
78    * @return the list of {@link ExtensionId}s created from the passed value
79    * @since 9.6
80    */
 
81  970 toggle public static List<ExtensionId> toExtensionIdList(Collection<?> values, Version defaultVersion,
82    ExtensionFactory factory)
83    {
84  970 List<ExtensionId> list = new ArrayList<>(values.size());
85   
86  970 for (Object value : values) {
87  1150 list.add(toExtensionId(value, defaultVersion, factory));
88    }
89   
90  970 return list;
91    }
92   
93    /**
94    * @param value the value to convert
95    * @param defaultVersion the default version to set in the {@link ExtensionId} if none can be extracted from the
96    * passed <code>value</code>
97    * @return the {@link ExtensionId} created from the passed value
98    */
 
99  23 toggle public static ExtensionId toExtensionId(Object value, Version defaultVersion)
100    {
101  23 return toExtensionId(value, defaultVersion, null);
102    }
103   
104    /**
105    * @param value the value to convert
106    * @param defaultVersion the default version to set in the {@link ExtensionId} if none can be extracted from the
107    * passed <code>value</code>
108    * @param factory the factory used to create the {@link Version} instance
109    * @return the {@link ExtensionId} created from the passed value
110    * @since 9.6
111    */
 
112  1248 toggle public static ExtensionId toExtensionId(Object value, Version defaultVersion, ExtensionFactory factory)
113    {
114  1248 if (value != null) {
115  1248 String valueString = value.toString();
116  1248 return toExtensionId(valueString, valueString.length() - 1, defaultVersion, factory);
117    }
118   
119  0 return null;
120    }
121   
 
122  1251 toggle private static ExtensionId toExtensionId(String value, int end, Version defaultVersion, ExtensionFactory factory)
123    {
124  1251 String valueString = value;
125   
126  1251 int index = valueString.lastIndexOf('/');
127  1251 String id;
128  1251 Version version;
129  1251 if (index > 0 && index < end) {
130  127 int backslashes = countBackslashes(valueString, index);
131  127 if (backslashes > 0) {
132  4 StringBuilder builder = new StringBuilder();
133  4 builder.append(valueString.substring(0, index - backslashes));
134  4 builder.append(StringUtils.repeat('\\', backslashes / 2));
135  4 builder.append(valueString.substring(index));
136   
137  4 valueString = builder.toString();
138  4 index -= backslashes - (backslashes / 2);
139   
140  4 if (backslashes % 2 == 1) {
141  3 return toExtensionId(valueString, index - backslashes - 1, defaultVersion, factory);
142    }
143    }
144   
145  124 id = valueString.substring(0, index);
146  124 String versionString = valueString.substring(index + 1);
147  124 version = factory != null ? factory.getVersion(versionString) : new DefaultVersion(versionString);
148    } else {
149  1124 id = valueString;
150  1124 version = defaultVersion;
151    }
152   
153  1248 return new ExtensionId(id, version);
154    }
155   
156    /**
157    * @param value the {@link ExtensionId} to serialize
158    * @return the String version of an {@link ExtensionId}
159    */
 
160  2050057 toggle public static String toString(ExtensionId value)
161    {
162  2050057 StringBuilder builder = new StringBuilder();
163   
164  2050057 builder.append(value.getId());
165   
166  2050057 if (value.getVersion() != null) {
167  2050051 builder.append('/');
168  2050051 builder.append(value.getVersion());
169    }
170   
171  2050056 return builder.toString();
172    }
173   
174    /**
175    * @param values the list of {@link ExtensionId}s to serialize
176    * @return the String version of an {@link ExtensionId}s list
177    */
 
178  6766 toggle public static List<String> toStringList(Collection<ExtensionId> values)
179    {
180  6766 List<String> list = new ArrayList<>(values.size());
181   
182  6766 for (ExtensionId value : values) {
183  2115 list.add(toString(value));
184    }
185   
186  6766 return list;
187    }
188   
 
189  75 toggle @Override
190    protected ExtensionId convertToType(Type targetType, Object value)
191    {
192  75 return toExtensionId(value, null, this.factory);
193    }
194   
 
195  0 toggle @Override
196    protected String convertToString(ExtensionId value)
197    {
198  0 return toString(value);
199    }
200    }