forked from roghughe/captaindebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roger Hughes
committed
Mar 8, 2014
1 parent
7041962
commit 2785462
Showing
1 changed file
with
62 additions
and
3 deletions.
There are no files selected for viewing
65 changes: 62 additions & 3 deletions
65
error-track/src/main/java/com/captaindebug/errortrack/format/TextFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,76 @@ | ||
package com.captaindebug.errortrack.format; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.captaindebug.errortrack.Formatter; | ||
import com.captaindebug.errortrack.Results; | ||
import com.captaindebug.errortrack.Results.ErrorResult; | ||
|
||
@Service | ||
public class TextFormatter implements Formatter { | ||
|
||
private static final String RULE = "\n==================================================================================================================\n"; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> T format(Results report) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
public <T> T format(Results results) { | ||
|
||
StringBuilder sb = new StringBuilder(dateFormat()); | ||
sb.append(RULE); | ||
|
||
Set<Entry<String, List<ErrorResult>>> entries = results.getRawResults().entrySet(); | ||
for (Entry<String, List<ErrorResult>> entry : entries) { | ||
appendFileName(sb, entry.getKey()); | ||
appendErrors(sb, entry.getValue()); | ||
} | ||
|
||
sb.append(RULE); | ||
return (T) sb.toString(); | ||
} | ||
|
||
private String dateFormat() { | ||
|
||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | ||
return df.format(Calendar.getInstance()); | ||
} | ||
|
||
private void appendFileName(StringBuilder sb, String fileName) { | ||
sb.append("File: "); | ||
sb.append(fileName); | ||
sb.append("\n"); | ||
} | ||
|
||
private void appendErrors(StringBuilder sb, List<ErrorResult> errorResults) { | ||
|
||
for (ErrorResult errorResult : errorResults) { | ||
appendErrorResult(sb, errorResult); | ||
} | ||
|
||
} | ||
|
||
private void appendErrorResult(StringBuilder sb, ErrorResult errorResult) { | ||
addLineNumber(sb, errorResult.getLineNumber()); | ||
addDetails(sb, errorResult.getLines()); | ||
sb.append(RULE); | ||
} | ||
|
||
private void addLineNumber(StringBuilder sb, int lineNumber) { | ||
sb.append("Error found at line: "); | ||
sb.append(lineNumber); | ||
sb.append("\n"); | ||
} | ||
|
||
private void addDetails(StringBuilder sb, List<String> lines) { | ||
|
||
for (String line : lines) { | ||
sb.append(line); | ||
sb.append("\n"); | ||
} | ||
} | ||
} |