public static List<Resource> getResources(URI uri) throws Exception { try (XMLResourceParser parser = new XMLResourceParser(uri)) { return parser.parse(); } }
private void parseAttributesOrDirectives(CapReqBuilder capReqBuilder) throws Exception { String name = reader.getLocalName(); switch (name) { case TAG_ATTRIBUTE : parseAttribute(capReqBuilder); break; case TAG_DIRECTIVE : parseDirective(capReqBuilder); break; default : check(false, "Invalid tag, expected either <%s> or <%s>, got <%s>", TAG_ATTRIBUTE, TAG_DIRECTIVE); } next(); tagEnd(name); }
List<Resource> getResources() { if (!isOk()) return null; return resources; }
private void parseReferral() throws Exception { if (--depth < 0) error("Too deep, traversed %s", traversed); else { String depthString = reader.getAttributeValue(null, ATTR_REFERRAL_DEPTH); String urlString = reader.getAttributeValue(null, ATTR_REFERRAL_URL); if (check(urlString != null, "Expected URL in referral")) { // TODO resolve url URI url = this.url.resolve(urlString); traversed.add(url); int depth = 100; if (depthString != null) { depth = Integer.parseInt(depthString); } InputStream in = url.toURL() .openStream(); try (XMLResourceParser referralParser = new XMLResourceParser(in, urlString, depth, traversed, url)) { referralParser.parse(); resources.addAll(referralParser.resources); } } } next(); tagEnd(TAG_REFERRAL); }
private Repository readRepository(File index) throws Exception { if (index.isFile()) { try (XMLResourceParser xp = new XMLResourceParser(index.toURI())) { List<Resource> resources = xp.parse(); if (urlHash.equals(xp.name())) { return new ResourcesRepository(resources); } } } return save(readRepository()); }
public List<Resource> parse() throws Exception { if (!check(reader.hasNext(), "No content found")) return null; next(); if (!check(reader.isStartElement(), "Expected a start element at the root, is %s", reader.getEventType())) return null; if (!check(TAG_REPOSITORY.equals(localName), "Invalid tag name of top element, expected %s, got %s", TAG_REPOSITORY, localName)) return null; check(NS_URI.equals(nsUri), "Incorrect namespace. Expected %s, got %s", NS_URI, nsUri); next(); // either start resource/referral or end parseReferral(); else if (localName.equals(TAG_RESOURCE)) parseResource(resources); else { check(false, "Unexpected element %s", localName); next(); check(reader.isEndElement() && reader.getLocalName() .equals(TAG_REPOSITORY), "Expected to be at the end but are on %s", reader.getLocalName()); return getResources();
private void parseCapabilityOrRequirement(ResourceBuilder resourceBuilder) throws Exception { String name = reader.getLocalName(); check(TAG_REQUIREMENT.equals(name) || TAG_CAPABILITY.equals(name), "Expected <%s> or <%s> tag, got <%s>", TAG_REQUIREMENT, TAG_CAPABILITY, name); String namespace = reader.getAttributeValue(null, ATTR_NAMESPACE); CapReqBuilder capReqBuilder = new CapReqBuilder(namespace); next(); while (reader.isStartElement()) { parseAttributesOrDirectives(capReqBuilder); } if (TAG_REQUIREMENT.equals(name)) { resourceBuilder.addRequirement(capReqBuilder); } else { resourceBuilder.addCapability(capReqBuilder); } tagEnd(name); }
private void parseResource(List<Resource> resources) throws Exception { ResourceBuilder resourceBuilder = new ResourceBuilder(); next(); while (reader.isStartElement()) { parseCapabilityOrRequirement(resourceBuilder); } Resource resource = resourceBuilder.build(); resources.add(resource); tagEnd(TAG_RESOURCE); }
public List<Resolution> validate(Collection<Resource> toBeChecked) throws Exception { Set<Resource> allResources = new LinkedHashSet<>(); for (URI uri : repositories) { allResources.addAll(XMLResourceParser.getResources(uri)); } allResources.addAll(toBeChecked); ResourcesRepository repository = new ResourcesRepository(allResources); return validateResources(repository, toBeChecked); }
public XMLResourceParser(InputStream in, String what, int depth, Set<URI> traversed, URI url) throws Exception { this.what = what; this.depth = depth; this.traversed = traversed; this.url = url; in = GZipUtils.detectCompression(in); addClose(in); this.reader = inputFactory.createXMLStreamReader(in); }
private void parseAttribute(CapReqBuilder capReqBuilder) throws Exception { String attributeName = reader.getAttributeValue(null, ATTR_NAME); String attributeValue = reader.getAttributeValue(null, ATTR_VALUE); String attributeType = reader.getAttributeValue(null, ATTR_TYPE); if (isContent(capReqBuilder) && attributeName.equals("url")) { attributeValue = url.resolve(attributeValue) .toString(); } Object value = Attrs.convert(attributeType, attributeValue); capReqBuilder.addAttribute(attributeName, value); }
private boolean check(boolean check, String format, Object... args) { if (check) return true; String message = Strings.format(format, args); error("%s: %s", what, message); return false; }
private void parseDirective(CapReqBuilder capReqBuilder) throws XMLStreamException { String attributeName = reader.getAttributeValue(null, ATTR_NAME); String attributeValue = reader.getAttributeValue(null, ATTR_VALUE); String attributeType = reader.getAttributeValue(null, ATTR_TYPE); check(attributeType == null, "Expected a directive to have no type: %s:%s=%s", attributeName, attributeType, attributeValue); capReqBuilder.addDirective(attributeName, attributeValue); } }
private void parseReferral() throws Exception { if (--depth < 0) error("Too deep, traversed %s", traversed); else { String depthString = reader.getAttributeValue(null, ATTR_REFERRAL_DEPTH); String urlString = reader.getAttributeValue(null, ATTR_REFERRAL_URL); if (check(urlString != null, "Expected URL in referral")) { // TODO resolve url URI url = this.url.resolve(urlString); traversed.add(url); int depth = 100; if (depthString != null) { depth = Integer.parseInt(depthString); } InputStream in = url.toURL() .openStream(); try (XMLResourceParser referralParser = new XMLResourceParser(in, urlString, depth, traversed, url)) { referralParser.parse(); resources.addAll(referralParser.resources); } } } next(); tagEnd(TAG_REFERRAL); }
private Repository readRepository(File index) throws Exception { if (index.isFile()) { try (XMLResourceParser xp = new XMLResourceParser(index.toURI())) { List<Resource> resources = xp.parse(); if (urlHash.equals(xp.name())) { return new ResourcesRepository(resources); } } } return save(readRepository()); }
public List<Resource> parse() throws Exception { if (!check(reader.hasNext(), "No content found")) return null; next(); if (!check(reader.isStartElement(), "Expected a start element at the root, is %s", reader.getEventType())) return null; if (!check(TAG_REPOSITORY.equals(localName), "Invalid tag name of top element, expected %s, got %s", TAG_REPOSITORY, localName)) return null; check(NS_URI.equals(nsUri), "Incorrect namespace. Expected %s, got %s", NS_URI, nsUri); next(); // either start resource/referral or end parseReferral(); else if (localName.equals(TAG_RESOURCE)) parseResource(resources); else { check(false, "Unexpected element %s", localName); next(); check(reader.isEndElement() && reader.getLocalName() .equals(TAG_REPOSITORY), "Expected to be at the end but are on %s", reader.getLocalName()); return getResources();
private void parseCapabilityOrRequirement(ResourceBuilder resourceBuilder) throws Exception { String name = reader.getLocalName(); check(TAG_REQUIREMENT.equals(name) || TAG_CAPABILITY.equals(name), "Expected <%s> or <%s> tag, got <%s>", TAG_REQUIREMENT, TAG_CAPABILITY, name); String namespace = reader.getAttributeValue(null, ATTR_NAMESPACE); CapReqBuilder capReqBuilder = new CapReqBuilder(namespace); next(); while (reader.isStartElement()) { parseAttributesOrDirectives(capReqBuilder); } if (TAG_REQUIREMENT.equals(name)) { resourceBuilder.addRequirement(capReqBuilder); } else { resourceBuilder.addCapability(capReqBuilder); } tagEnd(name); }
private void parseResource(List<Resource> resources) throws Exception { ResourceBuilder resourceBuilder = new ResourceBuilder(); next(); while (reader.isStartElement()) { parseCapabilityOrRequirement(resourceBuilder); } Resource resource = resourceBuilder.build(); resources.add(resource); tagEnd(TAG_RESOURCE); }