1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
42 |
|
@since |
43 |
|
|
44 |
|
@Component |
45 |
|
@Singleton |
|
|
| 50% |
Uncovered Elements: 23 (46) |
Complexity: 12 |
Complexity Density: 0.46 |
|
46 |
|
public class InputSourceConverter extends AbstractConverter<InputSource> |
47 |
|
{ |
|
|
| 55.6% |
Uncovered Elements: 12 (27) |
Complexity: 7 |
Complexity Density: 0.47 |
|
48 |
1 |
@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 |
|
|
|
|
| 35.3% |
Uncovered Elements: 11 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
76 |
1 |
private InputSource fromString(String source)... |
77 |
|
{ |
78 |
1 |
InputSource inputSource; |
79 |
|
|
80 |
|
|
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 |
|
} |