diff --git a/src/main/java/org/editorconfig/core/EditorConfig.java b/src/main/java/org/editorconfig/core/EditorConfig.java index 9b5305b..60ab58d 100644 --- a/src/main/java/org/editorconfig/core/EditorConfig.java +++ b/src/main/java/org/editorconfig/core/EditorConfig.java @@ -1,5 +1,8 @@ package org.editorconfig.core; +import org.editorconfig.core.provider.FileStreamProvider; +import org.editorconfig.core.provider.StreamProvider; + import java.io.*; import java.util.*; import java.util.regex.Matcher; @@ -29,6 +32,7 @@ public class EditorConfig { private final String configFilename; private final String version; + private final StreamProvider provider; /** * Creates EditorConfig handler with default configuration filename (.editorconfig) and @@ -38,6 +42,13 @@ public EditorConfig() { this(".editorconfig", VERSION); } + /** + * Creates EditorConfig handler with specified StreamProvider + */ + public EditorConfig(StreamProvider provider){ + this(".editorconfig", VERSION, provider); + } + /** * Creates EditorConfig handler with specified configuration filename and version. * Used mostly for debugging/testing. @@ -47,6 +58,20 @@ public EditorConfig() { public EditorConfig(String configFilename, String version) { this.configFilename = configFilename; this.version = version; + this.provider = new FileStreamProvider(); + } + + /** + * Creates EditorConfig handler with specified configuration filename and version. + * Used mostly for debugging/testing. + * @param configFilename configuration file name to be searched for instead of .editorconfig + * @param version required version + * @param provider stream provider + */ + public EditorConfig(String configFilename, String version, StreamProvider provider) { + this.configFilename = configFilename; + this.version = version; + this.provider = provider; } /** @@ -83,25 +108,26 @@ public List getProperties(String filePath, Set explicitRootDirs Map options = new LinkedHashMap(); try { boolean root = false; - String dir = new File(filePath).getParent(); + String dir = provider.getParent(filePath); while (dir != null && !root) { - File configFile = new File(dir, configFilename); - if (configFile.exists()) { - BufferedReader bufferedReader = null; - try { - bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), "UTF-8")); + String configFilePath = provider.combinePath(dir, configFilename); + BufferedReader bufferedReader = null; + try { + InputStream inputStream = provider.openStream(configFilePath); + if(inputStream != null){ + bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); root = parseFile(bufferedReader, dir + "/", filePath, options); - } finally { - if (bufferedReader != null) { - bufferedReader.close(); - } + } + } finally { + if (bufferedReader != null) { + bufferedReader.close(); } } options.putAll(oldOptions); oldOptions = options; options = new LinkedHashMap(); root |= explicitRootDirs.contains(dir); - dir = new File(dir).getParent(); + dir = provider.getParent(dir); } } catch (IOException e) { throw new EditorConfigException(null, e); diff --git a/src/main/java/org/editorconfig/core/provider/FileStreamProvider.java b/src/main/java/org/editorconfig/core/provider/FileStreamProvider.java new file mode 100644 index 0000000..56f8c4d --- /dev/null +++ b/src/main/java/org/editorconfig/core/provider/FileStreamProvider.java @@ -0,0 +1,33 @@ +package org.editorconfig.core.provider; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +public class FileStreamProvider implements StreamProvider { + @Override + public String getParent(String filePath) { + return new File(filePath).getParent(); + } + + @Override + public String combinePath(String dirPath, String filePath) { + return new File(dirPath, filePath).getPath(); + } + + @Override + public InputStream openStream(String filePath) { + try{ + if (new File(filePath).exists()){ + return new FileInputStream(filePath); + } + else { + return null; + } + } + catch (FileNotFoundException ex){ + return null; + } + } +} \ No newline at end of file diff --git a/src/main/java/org/editorconfig/core/provider/StreamProvider.java b/src/main/java/org/editorconfig/core/provider/StreamProvider.java new file mode 100644 index 0000000..6b56829 --- /dev/null +++ b/src/main/java/org/editorconfig/core/provider/StreamProvider.java @@ -0,0 +1,27 @@ +package org.editorconfig.core.provider; + +import java.io.InputStream; + +public interface StreamProvider { + /** + * get Parent directory + * @param filePath + * @return parent direcoty path by String + */ + String getParent(String filePath); + + /** + * combine Path + * @param dirPath directory path + * @param filePath file path + * @return dirPath + filePath + */ + String combinePath(String dirPath, String filePath); + + /** + * open InputStream for specified path + * @param filePath file path to open file. + * @return opened InputStream + */ + InputStream openStream(String filePath); +}