Skip to content

Commit

Permalink
Testing build after PR80
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Oct 29, 2015
1 parent a9fc86a commit 3ea8d9a
Showing 1 changed file with 62 additions and 1 deletion.
Expand Up @@ -30,4 +30,65 @@
/**
* @author marvin.froeder
*/
public abstract class AbstractCacheableFormatter { protected Log log; protected Charset encoding; public AbstractCacheableFormatter() { super(); } protected abstract void init(Map<String, String> options, ConfigurationSource cfg); protected void initCfg(ConfigurationSource cfg) { this.log = cfg.getLog(); this.encoding = cfg.getEncoding(); } public Result formatFile(File file, LineEnding ending, boolean dryRun) { try { this.log.debug("Processing file: " + file); String code = FileUtils.fileRead(file, this.encoding.name()); String formattedCode = doFormat(code, ending); if (formattedCode == null) { formattedCode = fixLineEnding(code, ending); } if (formattedCode == null) { this.log.debug("Equal code. Not writing result to file."); return Result.SKIPPED; } if (!dryRun) { FileUtils.fileWrite(file, this.encoding.name(), formattedCode); } return Result.SUCCESS; } catch (IOException e) { this.log.warn(e); return Result.FAIL; } catch (MalformedTreeException e) { this.log.warn(e); return Result.FAIL; } catch (BadLocationException e) { this.log.warn(e); return Result.FAIL; } } private String fixLineEnding(String code, LineEnding ending) { if (ending == LineEnding.KEEP) { return null; } LineEnding current = LineEnding.determineLineEnding(code); if (current == LineEnding.UNKNOW) { return null; } if (current == ending) { return null; } if (ending == LineEnding.AUTO && Objects.equals(current.getChars(), ending.getChars())) { return null; } return code.replace(current.getChars(), ending.getChars()); } protected abstract String doFormat(String code, LineEnding ending) throws IOException, BadLocationException;}
public abstract class AbstractCacheableFormatter {
protected Log log;
protected Charset encoding;

public AbstractCacheableFormatter() {
super();
}

protected abstract void init(Map<String, String> options, ConfigurationSource cfg);

protected void initCfg(ConfigurationSource cfg) {
this.log = cfg.getLog();
this.encoding = cfg.getEncoding();
}

public Result formatFile(File file, LineEnding ending, boolean dryRun) {
try {
this.log.debug("Processing file: " + file);
String code = FileUtils.fileRead(file, this.encoding.name());
String formattedCode = doFormat(code, ending);
if (formattedCode == null) {
formattedCode = fixLineEnding(code, ending);
}
if (formattedCode == null) {
this.log.debug("Equal code. Not writing result to file.");
return Result.SKIPPED;
}
if (!dryRun) {
FileUtils.fileWrite(file, this.encoding.name(), formattedCode);
}
return Result.SUCCESS;
} catch (IOException e) {
this.log.warn(e);
return Result.FAIL;
} catch (MalformedTreeException e) {
this.log.warn(e);
return Result.FAIL;
} catch (BadLocationException e) {
this.log.warn(e);
return Result.FAIL;
}
}

private String fixLineEnding(String code, LineEnding ending) {
if (ending == LineEnding.KEEP) {
return null;
}
LineEnding current = LineEnding.determineLineEnding(code);
if (current == LineEnding.UNKNOW) {
return null;
}
if (current == ending) {
return null;
}
if (ending == LineEnding.AUTO && Objects.equals(current.getChars(), ending.getChars())) {
return null;
}
return code.replace(current.getChars(), ending.getChars());
}

protected abstract String doFormat(String code, LineEnding ending) throws IOException, BadLocationException;
}

0 comments on commit 3ea8d9a

Please sign in to comment.