Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions src/main/java/org/editorconfig/core/EditorConfig.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -83,25 +108,26 @@ public List<OutPair> getProperties(String filePath, Set<String> explicitRootDirs
Map<String, String> options = new LinkedHashMap<String, String>();
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<String, String>();
root |= explicitRootDirs.contains(dir);
dir = new File(dir).getParent();
dir = provider.getParent(dir);
}
} catch (IOException e) {
throw new EditorConfigException(null, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/editorconfig/core/provider/StreamProvider.java
Original file line number Diff line number Diff line change
@@ -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);
}