Skip to content

Commit

Permalink
feat: ✨ Write validator errors to System.err
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-s-betke authored and mistmist committed Aug 26, 2021
1 parent d8ffea8 commit 67f56f3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 59 deletions.
109 changes: 59 additions & 50 deletions validator/src/main/java/org/odftoolkit/odfvalidator/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public enum LogLevel {
private String m_aFileName;
private String m_aEntryName;
private PrintStream m_aOut;
private PrintStream m_aInfoStream;
private PrintStream m_aErrorStream;
private int m_nErrors;
private int m_nWarnings;
private LogLevel m_nLevel;
Expand All @@ -56,22 +58,22 @@ public enum LogLevel {
Logger(String aFileName, String aEntryName, PrintStream aOut, LogLevel nLevel) {
m_aFileName = aFileName;
m_aEntryName = aEntryName;
m_aOut = aOut;
m_nLevel = nLevel;
m_nErrors = 0;
m_nWarnings = 0;
m_aParentLogger = null;
setOutputStream(aOut);
}

/** Creates a new instance of Logger */
Logger(String aEntryName, Logger aParentLogger) {
m_aFileName = aParentLogger.m_aFileName;
m_aEntryName = aEntryName;
m_aOut = aParentLogger.m_aOut;
m_nLevel = aParentLogger.m_nLevel;
m_nErrors = 0;
m_nWarnings = 0;
m_aParentLogger = aParentLogger;
setOutputStream(aParentLogger.m_aOut);
}

static void enableHTML(boolean isHTMLEnabled) {
Expand All @@ -84,6 +86,13 @@ public PrintStream getOutputStream() {

public void setOutputStream(PrintStream aOut) {
m_aOut = aOut;
if (m_aOut != null) {
m_aInfoStream = m_aOut;
m_aErrorStream = m_aOut;
} else {
m_aInfoStream = System.out;
m_aErrorStream = System.err;
}
}

public void setInputStream(InputStream aIn) {
Expand Down Expand Up @@ -112,64 +121,64 @@ int getWarningCount() {
void logWarning(String aMsg) {
if (m_nLevel.compareTo(LogLevel.WARNING) >= 0) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='warning'>");
m_aInfoStream.print("<span class='warning'>");
}
logMessage(WARNING_PREFIX, aMsg);
logMessage(m_aInfoStream, WARNING_PREFIX, aMsg);
}
incWarnings();
}

void logFatalError(String aMsg) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='fatalError'>");
m_aErrorStream.print("<span class='fatalError'>");
}
logMessage(FATAL_PREFIX, aMsg);
logMessage(m_aErrorStream, FATAL_PREFIX, aMsg);
incErrors();
}

void logError(String aMsg) {
if (m_nLevel.compareTo(LogLevel.ERROR) >= 0) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='error'>");
m_aErrorStream.print("<span class='error'>");
}
logMessage(ERROR_PREFIX, aMsg);
logMessage(m_aErrorStream, ERROR_PREFIX, aMsg);
}
incErrors();
}

void logInfo(String aMsg, boolean bForceOutput) {
if (m_nLevel.compareTo(LogLevel.INFO) >= 0 || bForceOutput) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='info'>");
m_aInfoStream.print("<span class='info'>");
}
logMessage(INFO_PREFIX, aMsg);
logMessage(m_aInfoStream, INFO_PREFIX, aMsg);
}
}

void logWarning(SAXParseException e) {
if (m_nLevel.compareTo(LogLevel.WARNING) >= 0) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='warning'>");
m_aInfoStream.print("<span class='warning'>");
}
logMessage(WARNING_PREFIX, e);
logMessage(m_aInfoStream, WARNING_PREFIX, e);
}
incWarnings();
}

void logFatalError(SAXParseException e) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='fatalError'>");
m_aErrorStream.print("<span class='fatalError'>");
}
logMessage(FATAL_PREFIX, e);
logMessage(m_aErrorStream, FATAL_PREFIX, e);
incErrors();
}

void logError(SAXParseException e) {
if (m_isHTMLEnabled) {
m_aOut.print("<span class='error'>");
m_aErrorStream.print("<span class='error'>");
}
if (m_nLevel.compareTo(LogLevel.ERROR) >= 0) {
logMessage(ERROR_PREFIX, e);
logMessage(m_aErrorStream, ERROR_PREFIX, e);
}
incErrors();
}
Expand All @@ -183,43 +192,43 @@ void logSummaryInfo() {
false);
}

private void printFileEntryPrefix() {
m_aOut.print(m_aFileName);
private void printFileEntryPrefix(PrintStream aOut) {
aOut.print(m_aFileName);
if (m_aEntryName != null && m_aEntryName.length() > 0) {
m_aOut.print("/");
m_aOut.print(m_aEntryName);
aOut.print("/");
aOut.print(m_aEntryName);
}
}

private void logMessage(String aPrefix, SAXParseException e) {
private void logMessage(PrintStream aOut, String aPrefix, SAXParseException e) {
// filepath
if (m_isHTMLEnabled) {
m_aOut.print("<span class='filePath'>");
aOut.print("<span class='filePath'>");
}
printFileEntryPrefix();
m_aOut.print("[");
m_aOut.print(e.getLineNumber());
m_aOut.print(",");
m_aOut.print(e.getColumnNumber());
m_aOut.print("]: ");
printFileEntryPrefix(aOut);
aOut.print("[");
aOut.print(e.getLineNumber());
aOut.print(",");
aOut.print(e.getColumnNumber());
aOut.print("]: ");
if (m_isHTMLEnabled) {
m_aOut.print("</span>");
aOut.print("</span>");
}

// prefix, e.g. warning
if (m_isHTMLEnabled) {
m_aOut.print("<span class='messageType'>");
aOut.print("<span class='messageType'>");
}
m_aOut.print(aPrefix);
aOut.print(aPrefix);
if (m_isHTMLEnabled) {
m_aOut.print("</span>");
aOut.print("</span>");
}

m_aOut.print(" " + e.getMessage());
aOut.print(" " + e.getMessage());
if (m_isHTMLEnabled) {
m_aOut.print("</span></br>");
aOut.print("</span></br>");
}
m_aOut.println();
aOut.println();

if (m_aEntryContent != null) {
try {
Expand All @@ -232,44 +241,44 @@ private void logMessage(String aPrefix, SAXParseException e) {
String errorLine = reader.readLine();
int len = errorLine.length();
if (len > 80) {
m_aOut.println(
aOut.println(
errorLine.substring(
Math.max(0, e.getColumnNumber() - 40),
Math.min(len - 1, e.getColumnNumber() + 39)));
m_aOut.println("".format("%1$38s", "----^"));
aOut.println("".format("%1$38s", "----^"));
} else {
m_aOut.println(errorLine);
m_aOut.println("".format("%1$" + Math.max(0, e.getColumnNumber() - 2) + "s", "----^"));
aOut.println(errorLine);
aOut.println("".format("%1$" + Math.max(0, e.getColumnNumber() - 2) + "s", "----^"));
}
} catch (IOException x) {
}
}
}

private void logMessage(String aPrefix, String aMsg) {
private void logMessage(PrintStream aOut, String aPrefix, String aMsg) {
// filepath
if (m_isHTMLEnabled) {
m_aOut.print("<span class='filePath'>");
aOut.print("<span class='filePath'>");
}
printFileEntryPrefix();
m_aOut.print(": ");
printFileEntryPrefix(aOut);
aOut.print(": ");
if (m_isHTMLEnabled) {
m_aOut.print("</span>");
aOut.print("</span>");
}

// prefix, e.g. warning
if (m_isHTMLEnabled) {
m_aOut.print("<span class='messageType'>");
aOut.print("<span class='messageType'>");
}
m_aOut.print(aPrefix);
aOut.print(aPrefix);
if (m_isHTMLEnabled) {
m_aOut.print("</span>");
aOut.print("</span>");
}
m_aOut.print(" " + aMsg);
aOut.print(" " + aMsg);
if (m_isHTMLEnabled) {
m_aOut.print("</span></br>");
aOut.print("</span></br>");
}
m_aOut.println();
aOut.println();
}

private void incErrors() {
Expand Down
18 changes: 9 additions & 9 deletions validator/src/main/java/org/odftoolkit/odfvalidator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public static void main(String[] aArgs) {
|| aArg.equals("-1.3")) {
aVersion = OdfVersion.valueOf(aArg.substring(1), false);
} else if (aArg.startsWith("-")) {
System.out.print(aArg);
System.out.println(": unknown option, use '-h' for help");
System.err.print(aArg);
System.err.println(": unknown option, use '-h' for help");
System.exit(1);
} else if (aArg.length() > 0) {
aFileNames.add(aArg);
Expand Down Expand Up @@ -176,11 +176,11 @@ public static void main(String[] aArgs) {
aConfig = new Configuration(aConfigFile);
} catch (FileNotFoundException e) {
if (aConfigFileName != null) {
System.out.println(aConfigFile.getAbsolutePath() + ": file not found.");
System.err.println(aConfigFile.getAbsolutePath() + ": file not found.");
System.exit(1);
}
} catch (IOException e) {
System.out.println(
System.err.println(
"error reading " + aConfigFile.getAbsolutePath() + ": " + e.getLocalizedMessage());
System.exit(1);
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public static void main(String[] aArgs) {
aConfig.setProperty(Configuration.MATHML3_SCHEMA, aMathMLSchemaFileName);
}

PrintStream aOut = aOutputFileName != null ? new PrintStream(aOutputFileName) : System.out;
PrintStream aOut = aOutputFileName != null ? new PrintStream(aOutputFileName) : null;
ODFValidator aValidator = new ODFValidator(aConfig, nLogLevel, aVersion);

if (aConfigFileName != null) {
Expand All @@ -228,12 +228,12 @@ public static void main(String[] aArgs) {
aValidator.validate(aOut, aFileNames, aExcludeRegExp, eMode, bRecursive, aFilterFileName);
}
} catch (ODFValidatorException e) {
System.out.println(e.getMessage());
System.out.println("Validation aborted.");
System.err.println(e.getMessage());
System.err.println("Validation aborted.");
System.exit(1);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.out.println("Validation aborted.");
System.err.println(e.getMessage());
System.err.println("Validation aborted.");
System.exit(1);
}
}
Expand Down

0 comments on commit 67f56f3

Please sign in to comment.