1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser; |
23 | |
24 | import java.io.File; |
25 | import java.io.FileInputStream; |
26 | import java.io.FileNotFoundException; |
27 | import java.io.IOException; |
28 | import java.io.InputStream; |
29 | import java.io.Reader; |
30 | import java.nio.charset.Charset; |
31 | import java.nio.file.Files; |
32 | import java.nio.file.Path; |
33 | |
34 | import static com.github.javaparser.utils.Utils.assertNotNull; |
35 | |
36 | /** |
37 | * Factory for providers of source code for JavaParser. Providers that have no parameter for encoding but need it will |
38 | * use UTF-8. |
39 | */ |
40 | public final class Providers { |
41 | public static final Charset UTF8 = Charset.forName("utf-8"); |
42 | |
43 | private Providers() { |
44 | } |
45 | |
46 | public static Provider provider(Reader reader) { |
47 | return new StreamProvider(assertNotNull(reader)); |
48 | } |
49 | |
50 | public static Provider provider(InputStream input, Charset encoding) { |
51 | assertNotNull(input); |
52 | assertNotNull(encoding); |
53 | try { |
54 | return new StreamProvider(input, encoding.name()); |
55 | } catch (IOException e) { |
56 | // The only one that is thrown is UnsupportedCharacterEncodingException, |
57 | // and that's a fundamental problem, so runtime exception. |
58 | throw new RuntimeException(e); |
59 | } |
60 | } |
61 | |
62 | public static Provider provider(InputStream input) { |
63 | return provider(input, UTF8); |
64 | } |
65 | |
66 | public static Provider provider(File file, Charset encoding) throws FileNotFoundException { |
67 | return provider(new FileInputStream(assertNotNull(file)), assertNotNull(encoding)); |
68 | } |
69 | |
70 | public static Provider provider(File file) throws FileNotFoundException { |
71 | return provider(assertNotNull(file), UTF8); |
72 | } |
73 | |
74 | public static Provider provider(Path path, Charset encoding) throws IOException { |
75 | return provider(Files.newInputStream(assertNotNull(path)), assertNotNull(encoding)); |
76 | } |
77 | |
78 | public static Provider provider(Path path) throws IOException { |
79 | return provider(assertNotNull(path), UTF8); |
80 | } |
81 | |
82 | public static Provider provider(String source) { |
83 | return new StringProvider(assertNotNull(source)); |
84 | } |
85 | |
86 | |
87 | /** |
88 | * Provide a Provider from the resource found in class loader with the provided encoding.<br> As resource is |
89 | * accessed through a class loader, a leading "/" is not allowed in pathToResource |
90 | */ |
91 | public static Provider resourceProvider(ClassLoader classLoader, String pathToResource, Charset encoding) throws IOException { |
92 | InputStream resourceAsStream = classLoader.getResourceAsStream(pathToResource); |
93 | if (resourceAsStream == null) { |
94 | throw new IOException("Cannot find " + pathToResource); |
95 | } |
96 | return provider(resourceAsStream, encoding); |
97 | } |
98 | |
99 | /** |
100 | * Provide a Provider from the resource found in the current class loader with the provided encoding.<br> As |
101 | * resource is accessed through a class loader, a leading "/" is not allowed in pathToResource |
102 | */ |
103 | public static Provider resourceProvider(String pathToResource, Charset encoding) throws IOException { |
104 | ClassLoader classLoader = Provider.class.getClassLoader(); |
105 | return resourceProvider(classLoader, pathToResource, encoding); |
106 | } |
107 | |
108 | /** |
109 | * Provide a Provider from the resource found in the current class loader with UTF-8 encoding.<br> As resource is |
110 | * accessed through a class loader, a leading "/" is not allowed in pathToResource |
111 | */ |
112 | public static Provider resourceProvider(String pathToResource) throws IOException { |
113 | return resourceProvider(pathToResource, UTF8); |
114 | } |
115 | |
116 | public interface PreProcessor { |
117 | Provider process(Provider innerProvider); |
118 | } |
119 | } |
120 |
Members