/** Gets a given resource (including the #this reference) from a model. **/ public static Resource getResource(String url, Model model) { List<Resource> matchingSubjects = model.listSubjects() .filterKeep(s -> s.getURI() != null) .filterKeep(s -> s.getURI().equalsIgnoreCase(url) || s.getURI().equalsIgnoreCase(url + "#this")).toList(); if (matchingSubjects.isEmpty()) { return null; } checkState(matchingSubjects.size() == 1, "Model %s didn't contain %s", model, url); return matchingSubjects.get(0); }
private List<List<VCard>> explore(String url, SolidUtilities utilities) throws IOException { logger.debug("Exploring: {}", url); List<List<VCard>> results = new ArrayList<>(); utilities.explore(url, r -> { try { List<VCard> cards = parseAddressBookIfApplicable(r, utilities); if (cards != null) { results.add(cards); } } catch (IOException e) { throw new IllegalStateException("Problem parsing " + r.getURI(), e); } }); return results; }
private List<VCard> parseAddressBookIfApplicable(Resource resource, SolidUtilities utilities) throws IOException { if (SolidUtilities.isType(resource, "http://www.w3.org/2006/vcard/ns#AddressBook")) { logger.debug("Got Address book at {}", resource.getURI()); return parseAddressBook(resource, utilities); } return null; }
/** Checks if a {@link Resource} is a given type. **/ public static boolean isType(Resource resource, String type) { return resource.listProperties(RDF.type) .toList() .stream() .anyMatch(s -> s.getResource().getURI().equalsIgnoreCase(type)); }
/** Recursively deletes all sub resources starting at the given url. **/ public void recursiveDelete(String url) throws IOException{ explore(url, r-> delete(r.getURI())); }
private List<VCard> parseAddressBook(Resource selfResource, SolidUtilities utilities) throws IOException { String peopleUri = selfResource.getProperty(NAME_EMAIL_INDEX_PROPERTY).getResource().getURI(); Model peopleModel = utilities.getModel(peopleUri); List<VCard> vcards = new ArrayList<>(); ResIterator subjects = peopleModel.listSubjects(); while (subjects.hasNext()) { Resource subject = subjects.nextResource(); Model personModel = utilities.getModel(subject.getURI()); Resource personResource = SolidUtilities.getResource(subject.getURI(), personModel); if (personResource == null) { throw new IllegalStateException(subject.getURI() + " not found in " + subject.toString()); } vcards.add(parsePerson(personResource)); } return vcards; }
/** * Does a depth first traversal of a RDF graph, passing each {@link Resource} into the * provided {@link Consumer} */ public void explore(String url, Consumer<Resource> resourceConsumer) throws IOException { logger.debug("Exploring: {}", url); Model model = getModel(url); Resource selfResource = getResource(url, model); if (selfResource == null) { resourceConsumer.accept(model.createResource(url)); return; } if (isType(selfResource, "http://www.w3.org/ns/ldp#Container")) { for (Resource r : getContainedResource(model, url)) { explore(r.getURI(), resourceConsumer); } } resourceConsumer.accept(selfResource); }
String mailTo = emailResource.getURI(); Matcher matcher = MAIL_TO_PATTERN.matcher(mailTo); checkState(matcher.matches(), "%s mail to address doesn't match", mailTo); String telephoneNumber = telephoneResource.getURI(); Telephone telephoneObject = new Telephone(telephoneNumber); vcard.addTelephoneNumber(telephoneObject);
private String getLanguage( Assembler a, Resource root ) { Resource lang = getUniqueResource( root, JA.ontLanguage ); return lang == null ? null : lang.getURI(); }
@Override void wTypeEnd(Resource u) { print(prettyWriter.endElementTag(u.getURI())); } };
@Override protected void blockRule( Resource r ) { if (r.equals( RDFSyntax.parseTypeLiteralPropertyElt )) { // System.err.println("Blocking"); blockLiterals = true; } else logger.warn("Cannot block rule <"+r.getURI()+">"); }
static private void addExternalRules( Resource root, List<Rule> result ) { StmtIterator it = root.listProperties( JA.rulesFrom ); while (it.hasNext()) { Resource s = getResource( it.nextStatement() ); result.addAll( Rule.rulesFromURL( s.getURI() ) ); } }
/** * <p>Answer a new empty list. This method overrides the list create method in ModelCom, * to allow both DAML and RDFS lists to be created.</p> * @return An RDF-encoded list of no elements, using the current language profile */ @Override public RDFList createList() { Resource list = getResource( getProfile().NIL().getURI() ); return list.as( RDFList.class ); }
protected void writeResourceId( Resource r, PrintWriter writer ) { if (r.isAnon()) { writer.print(rdfAt("nodeID") + "=" + attributeQuoted(anonId(r))); } else { writer.print( rdfAt("about") + "=" + substitutedAttribute(relativize(r.getURI()))); } }
public void testCreateNamedResource() { final String uri = "http://aldabaran.hpl.hp.com/foo"; Assert.assertEquals(uri, model.createResource(uri).getURI()); }
void initExpectedFromModel() { StmtIterator si = testID.listProperties(errorCodes); if (si.hasNext()) { expected = new HashSet<>(); while (si.hasNext()) { String uri = si.nextStatement().getResource().getURI(); String fieldName = uri.substring(uri.lastIndexOf('#') + 1); expected.add(new Integer(ParseException.errorCode(fieldName))); } } } void initExpected() {
String create(Property p) { Resource file = testID.getRequiredProperty(p).getResource(); Resource t = file.getRequiredProperty(RDF.type).getResource(); if (ntriple.equals(t)) { return "\"" + file.getURI() + "\",false"; } else if (rdfxml.equals(t)) { return "\"" + file.getURI() + "\",true"; } else { return "Unrecognized file type: " + t; } } Model read(Property p) throws IOException {
public void testCreateTypedNamedresource() { final String uri = "http://aldabaran.hpl.hp.com/foo"; final Resource r = model.createResource(uri, RDF.Property); Assert.assertEquals(uri, r.getURI()); Assert.assertTrue(model.contains(r, RDF.type, RDF.Property)); }
public void testCreateResource() { Resource r1 = ResourceFactory.createResource(); Assert.assertTrue(r1.isAnon()); final Resource r2 = ResourceFactory.createResource(); Assert.assertTrue(r2.isAnon()); Assert.assertTrue(!r1.equals(r2)); r1 = ResourceFactory.createResource(TestResourceFactory.uri1); Assert.assertTrue(r1.getURI().equals(TestResourceFactory.uri1)); }
public void testCreateAnonResourceWithNull() { final Resource r = model.createResource((String) null); Assert.assertTrue(r.isAnon()); Assert.assertNull(r.getURI()); Assert.assertNull(r.getNameSpace()); Assert.assertNull(r.getLocalName()); }