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

File XMLUtils.java

 

Coverage histogram

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

Code metrics

4
23
4
1
120
61
7
0.3
5.75
4
1.75

Classes

Class Line # Actions
XMLUtils 41 23 0% 7 1
0.967741996.8%
 

Contributing tests

This file is covered by 361 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.filter.xml.internal;
21   
22    import java.lang.reflect.Type;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.Collection;
26    import java.util.HashMap;
27    import java.util.HashSet;
28    import java.util.LinkedHashMap;
29    import java.util.LinkedHashSet;
30    import java.util.List;
31    import java.util.Map;
32    import java.util.Set;
33    import java.util.regex.Pattern;
34   
35    /**
36    * Various tools.
37    *
38    * @version $Id: 0caec7ccc645673b88278d0c1a53032069229d74 $
39    * @since 5.2M1
40    */
 
41    public final class XMLUtils
42    {
43    /**
44    * An index based parameter.
45    */
46    public static final Pattern INDEX_PATTERN = Pattern.compile("_(\\d+)");
47   
48    /**
49    * The default mapping between interface and instance.
50    */
51    private static final Map<Class<?>, Object> DEFAULTS = new HashMap<Class<?>, Object>();
52   
 
53  25 toggle static {
54  25 DEFAULTS.put(boolean.class, false);
55  25 DEFAULTS.put(char.class, '\0');
56  25 DEFAULTS.put(byte.class, (byte) 0);
57  25 DEFAULTS.put(short.class, (short) 0);
58  25 DEFAULTS.put(int.class, 0);
59  25 DEFAULTS.put(long.class, 0L);
60  25 DEFAULTS.put(float.class, 0f);
61  25 DEFAULTS.put(double.class, 0d);
62  25 DEFAULTS.put(Map.class, new LinkedHashMap<Object, Object>());
63  25 DEFAULTS.put(Set.class, new LinkedHashSet<Object>());
64  25 DEFAULTS.put(List.class, new ArrayList<Object>());
65  25 DEFAULTS.put(Collection.class, new ArrayList<Object>());
66    }
67   
68    /**
69    * The classes of object that can easily be converted to simple String.
70    */
71    private static final Set<Class<?>> SIMPLECLASSES = new HashSet<Class<?>>(Arrays.<Class<?>>asList(
72    String.class, Character.class, Boolean.class, byte[].class));
73   
74    /**
75    * Utility class.
76    */
 
77  0 toggle private XMLUtils()
78    {
79    }
80   
81    /**
82    * @param type the type can be converted to simple String
83    * @return true of the type is simple
84    */
 
85  7933 toggle public static boolean isSimpleType(Type type)
86    {
87  7933 boolean simpleType = false;
88   
89  7933 if (type instanceof Class) {
90  7890 Class<?> typeClass = (Class<?>) type;
91   
92  7890 simpleType =
93    SIMPLECLASSES.contains(typeClass) || Number.class.isAssignableFrom(typeClass)
94    || typeClass.isPrimitive() || typeClass.isEnum();
95    }
96   
97  7933 return simpleType;
98    }
99   
100    /**
101    * @param type the type
102    * @return the default value of the provided type
103    */
 
104  4385 toggle public static Object emptyValue(Class<?> type)
105    {
106  4385 Object defaultValue = null;
107   
108  4385 if (DEFAULTS.containsKey(type)) {
109  2138 defaultValue = DEFAULTS.get(type);
110    } else {
111  2247 try {
112  2247 defaultValue = type.newInstance();
113    } catch (Exception e) {
114    // Ignore
115    }
116    }
117   
118  4385 return defaultValue;
119    }
120    }