public boolean documentHasContent() { return this.context.documentLines().stream() .map(String::trim) .filter(s -> !s.isEmpty()) .count() > 0; }
public ErrorPrinter(final URL document, final List<URL> schemas) throws IOException { this.context = new ValidationContext(document, schemas); this.docURL = document; }
private static Charset detectBom(final URL document) { Charset fromBom = null; try (final InputStream in = document.openStream()) { byte[] maybeBom = new byte[3]; in.read(maybeBom); fromBom = checkBom(maybeBom); } catch (IOException ignored) {} return fromBom; }
public static Charset detectCharset(final URL document) { Charset charset = detectBom(document); if (charset == null) { try (final BufferedReader reader = new BufferedReader(new InputStreamReader(document.openStream(), StandardCharsets.UTF_8))) { final String firstLine = reader.readLine(); charset = detectCharsetFromDecl(firstLine); } catch (IOException ignored) {} } // default to UTF-8 return charset != null ? charset : StandardCharsets.UTF_8; }
final QName el = error.element(); final String elName = el.getLocalPart(); final List<List<SchemaElement>> altElements = ctx.alternateElementsForElement(el); ctx.pathToDocElement(e -> e.qname().equals(el) && e.encloses(error.position())); if (!ctx.docElementSiblings(path, e -> e.qname().equals(el)).isEmpty()) { List<DocElement> pathToDocElement = ctx.pathToDocElement(el, error.position()); ctx.mapDocPathToSchemaPath(pathToDocElement.subList(0, pathToDocElement.size() - 1)); if (!schemaPath.isEmpty()) { otherElements = Util.asSortedList(ctx.elementsForElement(schemaPath).stream() .map(SchemaElement::name) .collect(Collectors.toList()));
final String attr = error.attribute().getLocalPart(); final String el = error.element().getLocalPart(); final Position pos = ctx.searchForward(loc.getLineNumber() - 1, loc.getColumnNumber(), Pattern.compile(attr + "\\s*=")); final List<List<SchemaElement>> altElements = ctx.alternateElementsForAttribute(attr); final HandledResult result = HandledResult.from(error) .addPrimaryMessage(I18N.Key.ATTRIBUTE_NOT_ALLOWED, attr, el); final List<SchemaElement> schemaPath = ctx.mapDocLocationToSchemaPath(error.element(), error.position()); if (schemaPath.isEmpty()) { result.possiblyMalformed(true); otherAttributes = Util.asSortedList(ctx.attributesForElement(schemaPath)); } else { otherAttributes = Util.asSortedList(error.alternatives());
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final String el = error.element().getLocalPart(); final Set<String> alts = error.alternatives(); final HandledResult result = HandledResult.from(error) .addPrimaryMessage(I18N.Key.ATTRIBUTE_REQUIRED_MISSING, el); if (!alts.isEmpty()) { final List<SchemaElement> path = ctx.mapDocLocationToSchemaPath(error.element(), error.position()); if (path.isEmpty()) { result.possiblyMalformed(true); } final Set<String> attributesForElement = ctx.attributesForElement(path); result.addPrimaryMessage(I18N.Key.ATTRIBUTE_REQUIRED_MISSING_LIST, Util.asSortedList(alts).stream() .map(String::toLowerCase) .map(Util.possiblyUnderscoredName(attributesForElement)) .collect(Collectors.toList())); } return result; } }
public List<List<SchemaElement>> alternateElementsForAttribute(final String attribute) { return alternateElements(true, el -> el.attributes().contains(attribute)); }
public ValidationContext(final URL document, final List<URL> schemas) throws IOException { this.document = document; this.docWalker = new DocWalker(document); try (final BufferedReader reader = new BufferedReader(new InputStreamReader(document.openStream(), detectCharset(document)))) { this.lines = reader.lines().collect(Collectors.toList()); } final Set<String> xmlnses = Util.extractXMLNS(this.lines); for (URL url : schemas) { if (Util.providesXMLNS(xmlnses, url)) { this.schemas.add(url); } } }
public List<List<SchemaElement>> alternateElementsForElement(final QName element) { return alternateElements(false, el -> el.qname().equals(element)); }
private void init(final ValidationContext ctx) { if (prefix.isEmpty()) { this.rootElement = Util.extractFirstElement(ctx.documentLines()); prefix.add(this.rootElement); if (this.rootElement.getLocalPart().equals("domain")) { prefix.add(new QName(this.rootElement.getNamespaceURI(), "profiles")); } prefix.add(new QName(this.rootElement.getNamespaceURI(), "profile")); final String uri = this.rootElement.getNamespaceURI(); if (this.rootElement.getLocalPart().equals("server")) { excludedRoots.add(new QName(uri, "domain")); excludedRoots.add(new QName(uri, "host")); } else if (this.rootElement.getLocalPart().equals("domain")) { excludedRoots.add(new QName(uri, "server")); excludedRoots.add(new QName(uri, "host")); } else { excludedRoots.add(new QName(uri, "domain")); excludedRoots.add(new QName(uri, "server")); } } }
private static Charset checkBom(final byte[] maybeBom) { for (Map.Entry<Charset, byte[]> each: BOMS.entrySet()) { if (checkBom(each.getValue(), maybeBom)) { return each.getKey(); } } return null; }