1 package com.github.javaparser; 2 3 import java.io.*; 4 import java.nio.charset.Charset; 5 import java.nio.file.Files; 6 import java.nio.file.OpenOption; 7 import java.nio.file.Path; 8 9 import static com.github.javaparser.utils.Utils.assertNotNull; 10 11 /** 12 * Factory for providers of source code for JavaParser. 13 * Providers that have no parameter for encoding but need it will use UTF-8. 14 */ 15 public final class Providers { 16 public static final Charset UTF8 = Charset.forName("utf-8"); 17 18 private Providers() { 19 } 20 21 public static Provider provider(Reader reader) { 22 return new StreamProvider(assertNotNull(reader)); 23 } 24 25 public static Provider provider(InputStream input, Charset encoding) { 26 assertNotNull(input); 27 assertNotNull(encoding); 28 try { 29 return new StreamProvider(input, encoding.name()); 30 } catch (IOException e) { 31 // The only one that is thrown is UnsupportedCharacterEncodingException, 32 // and that's a fundamental problem, so runtime exception. 33 throw new RuntimeException(e); 34 } 35 } 36 37 public static Provider provider(InputStream input) { 38 return provider(input, UTF8); 39 } 40 41 public static Provider provider(File file, Charset encoding) throws FileNotFoundException { 42 return provider(new FileInputStream(assertNotNull(file)), assertNotNull(encoding)); 43 } 44 45 public static Provider provider(File file) throws FileNotFoundException { 46 return provider(assertNotNull(file), UTF8); 47 } 48 49 public static Provider provider(Path path, Charset encoding) throws IOException { 50 return provider(Files.newInputStream(assertNotNull(path)), assertNotNull(encoding)); 51 } 52 53 public static Provider provider(Path path) throws IOException { 54 return provider(assertNotNull(path), UTF8); 55 } 56 57 public static Provider provider(String source) { 58 return new StringProvider(assertNotNull(source)); 59 } 60 61 } 62