1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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.*; 25 import java.nio.charset.Charset; 26 import java.nio.file.Files; 27 import java.nio.file.Path; 28 29 import static com.github.javaparser.utils.Utils.assertNotNull; 30 31 /** 32 * Factory for providers of source code for JavaParser. Providers that have no parameter for encoding but need it will 33 * use UTF-8. 34 */ 35 public final class Providers { 36 public static final Charset UTF8 = Charset.forName("utf-8"); 37 Providers()38 private Providers() { 39 } 40 provider(Reader reader)41 public static Provider provider(Reader reader) { 42 return new StreamProvider(assertNotNull(reader)); 43 } 44 provider(InputStream input, Charset encoding)45 public static Provider provider(InputStream input, Charset encoding) { 46 assertNotNull(input); 47 assertNotNull(encoding); 48 try { 49 return new StreamProvider(input, encoding.name()); 50 } catch (IOException e) { 51 // The only one that is thrown is UnsupportedCharacterEncodingException, 52 // and that's a fundamental problem, so runtime exception. 53 throw new RuntimeException(e); 54 } 55 } 56 provider(InputStream input)57 public static Provider provider(InputStream input) { 58 return provider(input, UTF8); 59 } 60 provider(File file, Charset encoding)61 public static Provider provider(File file, Charset encoding) throws FileNotFoundException { 62 return provider(new FileInputStream(assertNotNull(file)), assertNotNull(encoding)); 63 } 64 provider(File file)65 public static Provider provider(File file) throws FileNotFoundException { 66 return provider(assertNotNull(file), UTF8); 67 } 68 provider(Path path, Charset encoding)69 public static Provider provider(Path path, Charset encoding) throws IOException { 70 return provider(Files.newInputStream(assertNotNull(path)), assertNotNull(encoding)); 71 } 72 provider(Path path)73 public static Provider provider(Path path) throws IOException { 74 return provider(assertNotNull(path), UTF8); 75 } 76 provider(String source)77 public static Provider provider(String source) { 78 return new StringProvider(assertNotNull(source)); 79 } 80 81 82 /** 83 * Provide a Provider from the resource found in class loader with the provided encoding.<br/> As resource is 84 * accessed through a class loader, a leading "/" is not allowed in pathToResource 85 */ resourceProvider(ClassLoader classLoader, String pathToResource, Charset encoding)86 public static Provider resourceProvider(ClassLoader classLoader, String pathToResource, Charset encoding) throws IOException { 87 InputStream resourceAsStream = classLoader.getResourceAsStream(pathToResource); 88 if (resourceAsStream == null) { 89 throw new IOException("Cannot find " + pathToResource); 90 } 91 return provider(resourceAsStream, encoding); 92 } 93 94 /** 95 * Provide a Provider from the resource found in the current class loader with the provided encoding.<br/> As 96 * resource is accessed through a class loader, a leading "/" is not allowed in pathToResource 97 */ resourceProvider(String pathToResource, Charset encoding)98 public static Provider resourceProvider(String pathToResource, Charset encoding) throws IOException { 99 ClassLoader classLoader = Provider.class.getClassLoader(); 100 return resourceProvider(classLoader, pathToResource, encoding); 101 } 102 103 /** 104 * Provide a Provider from the resource found in the current class loader with UTF-8 encoding.<br/> As resource is 105 * accessed through a class loader, a leading "/" is not allowed in pathToResource 106 */ resourceProvider(String pathToResource)107 public static Provider resourceProvider(String pathToResource) throws IOException { 108 return resourceProvider(pathToResource, UTF8); 109 } 110 111 public interface PreProcessor { process(Provider innerProvider)112 Provider process(Provider innerProvider); 113 } 114 } 115