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

File ExtensionUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

32
58
8
1
241
130
27
0.47
7.25
8
3.38

Classes

Class Line # Actions
ExtensionUtils 55 58 0% 27 28
0.7142857371.4%
 

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;
21   
22    import java.io.StreamTokenizer;
23    import java.io.StringReader;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.Collection;
27    import java.util.Collections;
28    import java.util.HashMap;
29    import java.util.List;
30    import java.util.Map;
31   
32    import org.apache.commons.lang3.StringUtils;
33    import org.xwiki.extension.CoreExtension;
34    import org.xwiki.extension.Extension;
35    import org.xwiki.extension.ExtensionDependency;
36    import org.xwiki.extension.InstalledExtension;
37    import org.xwiki.extension.LocalExtension;
38    import org.xwiki.extension.MutableExtension;
39    import org.xwiki.extension.RemoteExtension;
40    import org.xwiki.extension.rating.RatingExtension;
41    import org.xwiki.extension.wrap.WrappingCoreExtension;
42    import org.xwiki.extension.wrap.WrappingExtension;
43    import org.xwiki.extension.wrap.WrappingInstalledExtension;
44    import org.xwiki.extension.wrap.WrappingLocalExtension;
45    import org.xwiki.extension.wrap.WrappingRatingExtension;
46    import org.xwiki.extension.wrap.WrappingRemoteExtension;
47    import org.xwiki.properties.converter.ConversionException;
48   
49    /**
50    * Various extension related utilities.
51    *
52    * @version $Id: d9a77f14b3fcb68bd8748f4207abda0ce3e0608c $
53    * @since 8.2RC1
54    */
 
55    public final class ExtensionUtils
56    {
 
57  0 toggle private ExtensionUtils()
58    {
59    // Utility class
60    }
61   
62    /**
63    * @param dependency the initial dependency
64    * @param managedDependencies the managed dependencies
65    * @param extension the extension with the passed dependency
66    * @return the actual dependency to resolve
67    */
 
68  1107 toggle public static ExtensionDependency getDependency(ExtensionDependency dependency,
69    Map<String, ExtensionDependency> managedDependencies, Extension extension)
70    {
71  1107 ExtensionDependency managedDependency = managedDependencies.get(dependency.getId());
72   
73    // If the dependency does not have any version try to find it in extension managed dependencies
74  1107 if (managedDependency == null && dependency.getVersionConstraint() == null) {
75  1 for (ExtensionDependency extensionManagedDependency : extension.getManagedDependencies()) {
76  1 if (extensionManagedDependency.getId().equals(dependency.getId())) {
77  1 managedDependency = extensionManagedDependency;
78    }
79    }
80    }
81   
82  1107 return managedDependency != null ? managedDependency : dependency;
83    }
84   
85    /**
86    * @param managedDependencies the managed dependencies
87    * @param extension the extension for which to append managed dependencies
88    * @return the new map of managed dependencies
89    */
 
90  1107 toggle public static Map<String, ExtensionDependency> append(Map<String, ExtensionDependency> managedDependencies,
91    Extension extension)
92    {
93  1107 Map<String, ExtensionDependency> newManagedDependencies =
94  1107 managedDependencies != null ? new HashMap<>(managedDependencies) : new HashMap<>();
95   
96  1107 for (ExtensionDependency dependency : extension.getManagedDependencies()) {
97  1567 newManagedDependencies.put(dependency.getId(), dependency);
98    }
99   
100  1107 return newManagedDependencies;
101    }
102   
103    /**
104    * Delete and return the value of the passed special property.
105    *
106    * @param <T> type of the property value
107    * @param extension the extension from which to extract custom property
108    * @param propertySuffix the property suffix
109    * @return the value
110    * @ @since 8.3M1
111    */
 
112  267 toggle public static <T> T importProperty(MutableExtension extension, String propertySuffix)
113    {
114  267 return extension.removeProperty(Extension.IKEYPREFIX + propertySuffix);
115    }
116   
117    /**
118    * Delete and return the value of the passed special property.
119    *
120    * @param extension the extension from which to extract custom property
121    * @param propertySuffix the property suffix
122    * @param def the default value
123    * @return the value or {@code def} if none was found
124    * @since 8.3M1
125    */
 
126  199 toggle public static String importProperty(MutableExtension extension, String propertySuffix, String def)
127    {
128  199 return StringUtils.defaultString(importProperty(extension, propertySuffix), def);
129    }
130   
131    /**
132    * Delete and return the value of the passed special property as a Collection of Strings.
133    *
134    * @param extension the extension from which to extract custom property
135    * @param propertySuffix the property suffix
136    * @param def the default value
137    * @return the value or {@code def} if none was found
138    * @since 8.3M1
139    */
 
140  66 toggle public static Collection<String> importProperty(MutableExtension extension, String propertySuffix,
141    Collection<String> def)
142    {
143  66 Object obj = importProperty(extension, propertySuffix);
144   
145  66 if (obj == null) {
146  67 return def;
147  0 } else if (obj instanceof Collection) {
148  0 return (Collection) obj;
149  0 } else if (obj instanceof String[]) {
150  0 return Arrays.asList((String[]) obj);
151    } else {
152  0 return importPropertyStringList(obj.toString(), true);
153    }
154    }
155   
156    /**
157    * @param str the String to parse
158    * @param trim true if the passed String should be trimmed
159    * @return the collection of Strings extracted from the passed String
160    * @since 8.3M1
161    */
 
162  995 toggle public static List<String> importPropertyStringList(String str, boolean trim)
163    {
164  995 try {
165  995 String cleanedString = str;
166   
167    // Trim
168  995 if (trim) {
169  995 cleanedString = cleanedString.trim();
170    }
171   
172    // Set up a StreamTokenizer on the characters in this String
173  995 StreamTokenizer st = new StreamTokenizer(new StringReader(cleanedString));
174   
175    // Everything is word
176  995 st.ordinaryChars(0, 255);
177  995 st.wordChars(0, 255);
178   
179    // Except quote chars
180  995 st.quoteChar('"');
181  995 st.quoteChar('\'');
182   
183    // And delimiters
184  995 st.whitespaceChars(',', ',');
185  995 st.whitespaceChars(' ', ' ');
186  995 st.whitespaceChars('\t', '\t');
187  995 st.whitespaceChars('\n', '\n');
188  995 st.whitespaceChars('\r', '\r');
189   
190    // Split comma-delimited tokens into a List
191  995 List<String> collection = new ArrayList<>();
192  995 while (true) {
193  2109 int ttype = st.nextToken();
194  2109 if (ttype == StreamTokenizer.TT_WORD || ttype > 0) {
195  1114 if (st.sval != null) {
196  1114 collection.add(st.sval);
197    }
198  995 } else if (ttype == StreamTokenizer.TT_EOF) {
199  995 break;
200    } else {
201  0 throw new ConversionException("Encountered token of type " + ttype + " parsing elements.");
202    }
203    }
204   
205    // Return the completed list
206  995 return collection;
207    } catch (Exception e) {
208    // Log ?
209    }
210   
211  0 return Collections.emptyList();
212    }
213   
214    /**
215    * @param <T> the type
216    * @param extension the extension to wrap
217    * @return the wrapped version
218    * @since 8.4.2
219    * @since 9.0RC1
220    */
 
221  1 toggle public static <T extends Extension> WrappingExtension<T> wrap(Extension extension)
222    {
223  1 WrappingExtension<?> wrapper;
224   
225  1 if (extension instanceof CoreExtension) {
226  0 wrapper = new WrappingCoreExtension<>((CoreExtension) extension);
227  1 } else if (extension instanceof InstalledExtension) {
228  0 wrapper = new WrappingInstalledExtension<>((InstalledExtension) extension);
229  1 } else if (extension instanceof LocalExtension) {
230  0 wrapper = new WrappingLocalExtension<>((LocalExtension) extension);
231  1 } else if (extension instanceof RatingExtension) {
232  0 wrapper = new WrappingRatingExtension<>((RatingExtension) extension);
233  1 } else if (extension instanceof RemoteExtension) {
234  0 wrapper = new WrappingRemoteExtension<>((RemoteExtension) extension);
235    } else {
236  1 wrapper = new WrappingExtension<>(extension);
237    }
238   
239  1 return (WrappingExtension<T>) wrapper;
240    }
241    }