From 605fb0dd5287454b7464aa5bff89186a5367acde Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Mon, 2 Dec 2024 10:52:30 +0300 Subject: [PATCH] #137 skip line number of negative --- .../java/com/yegor256/xsline/StSchema.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/yegor256/xsline/StSchema.java b/src/main/java/com/yegor256/xsline/StSchema.java index 4fd3acd..ac54926 100644 --- a/src/main/java/com/yegor256/xsline/StSchema.java +++ b/src/main/java/com/yegor256/xsline/StSchema.java @@ -97,19 +97,7 @@ private static XML validate(final XML schema, final XML xml) { if (!violations.isEmpty()) { final Collection msgs = new ArrayList<>(violations.size()); for (final SAXParseException violation : violations) { - final StringBuilder msg = new StringBuilder() - .append('#') - .append(violation.getLineNumber()) - .append(':') - .append(violation.getColumnNumber()) - .append(' ') - .append(violation.getLocalizedMessage()); - if (violation.getException() != null) { - msg.append(" (") - .append(violation.getException().getClass().getSimpleName()) - .append(')'); - } - msgs.add(msg.toString()); + msgs.add(StSchema.asMessage(violation)); } if (Logger.isDebugEnabled(StSchema.class)) { Logger.debug( @@ -130,6 +118,29 @@ private static XML validate(final XML schema, final XML xml) { return xml; } + /** + * Turn violation into a message. + * @param violation The violation + * @return The message + */ + private static String asMessage(final SAXParseException violation) { + final StringBuilder msg = new StringBuilder(100); + if (violation.getLineNumber() >= 0) { + msg.append('#').append(violation.getLineNumber()); + if (violation.getColumnNumber() >= 0) { + msg.append(':').append(violation.getColumnNumber()); + } + msg.append(' '); + } + msg.append(violation.getLocalizedMessage()); + if (violation.getException() != null) { + msg.append(" (") + .append(violation.getException().getClass().getSimpleName()) + .append(')'); + } + return msg.toString(); + } + /** * Make XSD safely. * @param path Path in classpath