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

File AbstractReader.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

22
22
2
1
84
51
14
0.64
11
2
7

Classes

Class Line # Actions
AbstractReader 39 22 0% 14 7
0.8478260684.8%
 

Contributing tests

This file is covered by 32 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   
21    package org.xwiki.filter.xar.internal.input;
22   
23    import java.util.Date;
24    import java.util.Locale;
25   
26    import javax.inject.Inject;
27   
28    import org.apache.commons.lang3.LocaleUtils;
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.rendering.parser.ParseException;
31    import org.xwiki.rendering.syntax.Syntax;
32    import org.xwiki.rendering.syntax.SyntaxFactory;
33    import org.xwiki.filter.FilterException;
34   
35    /**
36    * @version $Id: 0016badaa814c8ddd540d37cb4c9b9aecfe64416 $
37    * @since 6.2M1
38    */
 
39    public abstract class AbstractReader
40    {
41    @Inject
42    protected SyntaxFactory syntaxFactory;
43   
 
44  84508 toggle protected <T> T convert(Class< ? > type, String source) throws FilterException
45    {
46  84511 Object value = source;
47   
48  84511 if (type == Locale.class) {
49  2518 value = toLocale(source);
50  81989 } else if (type == Date.class) {
51  7994 value = StringUtils.isNotEmpty(source) ? new Date(Long.parseLong(source)) : null;
52  73997 } else if (type == Boolean.class) {
53  5028 value = StringUtils.isNotEmpty(source) ? Boolean.valueOf(source).booleanValue() : null;
54  68972 } else if (type == Syntax.class) {
55  2544 if (StringUtils.isNotEmpty(source)) {
56  2544 try {
57  2544 value = this.syntaxFactory.createSyntaxFromIdString(source);
58    } catch (ParseException e) {
59  0 throw new FilterException(String.format("Failed to create Syntax istance for [%s]", source), e);
60    }
61    } else {
62  0 value = null;
63    }
64  66434 } else if (type == Integer.class) {
65  2958 value = StringUtils.isNotEmpty(source) ? Integer.parseInt(source) : null;
66    }
67   
68  84515 return (T) value;
69    }
70   
 
71  5065 toggle protected Locale toLocale(String value)
72    {
73  5065 Locale locale = null;
74  5065 if (value != null) {
75  5065 if (value.length() == 0) {
76  3572 locale = Locale.ROOT;
77    } else {
78  1493 locale = LocaleUtils.toLocale(value);
79    }
80    }
81   
82  5065 return locale;
83    }
84    }