1. Project Clover database Sat Feb 2 2019 06:45:20 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
26
4
1
125
66
7
0.27
6.5
4
1.75

Classes

Class Line # Actions
XMLUtils 44 26 0% 7 1
0.970588297.1%
 

Contributing tests

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