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

File InputSourceConverter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

18
26
2
1
97
62
12
0.46
13
2
6

Classes

Class Line # Actions
InputSourceConverter 46 26 0% 12 23
0.550%
 

Contributing tests

No tests hitting this source file were found.

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.internal.input;
21   
22    import java.io.File;
23    import java.io.InputStream;
24    import java.io.Reader;
25    import java.lang.reflect.Type;
26    import java.net.URL;
27   
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.filter.input.DefaultFileInputSource;
32    import org.xwiki.filter.input.DefaultInputStreamInputSource;
33    import org.xwiki.filter.input.DefaultReaderInputSource;
34    import org.xwiki.filter.input.DefaultURLInputSource;
35    import org.xwiki.filter.input.InputSource;
36    import org.xwiki.filter.input.StringInputSource;
37    import org.xwiki.properties.converter.AbstractConverter;
38    import org.xwiki.properties.converter.ConversionException;
39   
40    /**
41    * @version $Id: 4a9fd7beeb2948822b22df91aaf94aa4430e3c4e $
42    * @since 6.2M1
43    */
44    @Component
45    @Singleton
 
46    public class InputSourceConverter extends AbstractConverter<InputSource>
47    {
 
48  1 toggle @Override
49    protected <G extends InputSource> G convertToType(Type targetType, Object value)
50    {
51  1 if (value == null) {
52  0 return null;
53    }
54   
55  1 if (value instanceof InputSource) {
56  0 return (G) value;
57    }
58   
59  1 InputSource inputSource;
60   
61  1 if (value instanceof InputStream) {
62  0 inputSource = new DefaultInputStreamInputSource((InputStream) value);
63  1 } else if (value instanceof File) {
64  0 inputSource = new DefaultFileInputSource((File) value);
65  1 } else if (value instanceof Reader) {
66  0 inputSource = new DefaultReaderInputSource((Reader) value);
67  1 } else if (value instanceof URL) {
68  0 inputSource = new DefaultURLInputSource((URL) value);
69    } else {
70  1 inputSource = fromString(value.toString());
71    }
72   
73  1 return (G) inputSource;
74    }
75   
 
76  1 toggle private InputSource fromString(String source)
77    {
78  1 InputSource inputSource;
79   
80    // TODO: use some InputSourceParser instead to make it extensible
81  1 if (source.startsWith("url:")) {
82  1 try {
83  1 inputSource = new DefaultURLInputSource(new URL(source.substring("url:".length())));
84    } catch (Exception e) {
85  0 throw new ConversionException("Failed to create input source for URL [" + source + "]", e);
86    }
87  0 } else if (source.startsWith("file:")) {
88  0 inputSource = new DefaultFileInputSource(new File(source.substring("file:".length())));
89  0 } else if (source.startsWith("string:")) {
90  0 inputSource = new DefaultFileInputSource(new File(source.substring("string:".length())));
91    } else {
92  0 inputSource = new StringInputSource(source);
93    }
94   
95  1 return inputSource;
96    }
97    }