GeoPubby  Version 0.1.0.0
ResourceReader Class Referenceabstract

Convenience superclass for classes that read properties of an RDF resource, intended for making the reading of configuration files easier. More...

Inheritance diagram for ResourceReader:
Collaboration diagram for ResourceReader:

Public Member Functions

 ResourceReader (Resource resource)
 
Resource getSelf ()
 
Model getModel ()
 
boolean hasType (Resource class_)
 
boolean hasProperty (Property p)
 
Resource getResource (Property p)
 
Set< Resource > getResources (Property p)
 
Set< Property > getProperties (Property p)
 
String getIRI (Property p)
 
String getIRI (Property p, String defaultValue)
 
String getRequiredIRI (Property p)
 
Set< String > getIRIs (Property p)
 
String getString (Property p)
 
String getString (Property p, String defaultValue)
 
Set< String > getStrings (Property p)
 
boolean getBoolean (Property p, boolean defaultValue)
 
void requireExactlyOneOf (Property... properties)
 

Private Member Functions

void assertHasOneValue (Property p)
 
void assertResourceValue (Property p)
 
void assertResourceValue (Statement stmt)
 
void assertIRIValue (Property p)
 
void assertIRIValue (Statement stmt)
 
void assertLiteralValue (Property p)
 
void assertLiteralValue (Statement stmt)
 
void assertString (Statement stmt)
 
String pretty (RDFNode node)
 
String pretty (Statement stmt)
 
void raiseMissingProperty (Property p)
 
void raiseUnexpectedDatatype (String expectedDatatype, Statement stmt)
 

Private Attributes

final Resource resource
 

Detailed Description

Convenience superclass for classes that read properties of an RDF resource, intended for making the reading of configuration files easier.

Constructor & Destructor Documentation

◆ ResourceReader()

ResourceReader ( Resource  resource)
25  {
26  this.resource = resource;
27  }
final Resource resource
Definition: ResourceReader.java:23

References ResourceReader.resource.

Member Function Documentation

◆ assertHasOneValue()

void assertHasOneValue ( Property  p)
private
170  {
171  StmtIterator it = resource.listProperties(p);
172  if (!it.hasNext()) {
173  throw new ConfigurationException("Missing property " + pretty(p) +
174  " on resource " + pretty(resource));
175  }
176  it.next();
177  if (it.hasNext()) {
178  throw new ConfigurationException("Too many values for property " +
179  pretty(p) + " on resource " + pretty(resource));
180  }
181  }
String pretty(RDFNode node)
Definition: ResourceReader.java:235

References ResourceReader.pretty(), and ResourceReader.resource.

Referenced by ResourceReader.getBoolean(), ResourceReader.getIRI(), ResourceReader.getResource(), and ResourceReader.getString().

◆ assertIRIValue() [1/2]

void assertIRIValue ( Property  p)
private
194  {
195  assertIRIValue(resource.getProperty(p));
196  }
void assertIRIValue(Property p)
Definition: ResourceReader.java:194

References ResourceReader.resource.

Referenced by ResourceReader.getIRI(), and ResourceReader.getIRIs().

◆ assertIRIValue() [2/2]

void assertIRIValue ( Statement  stmt)
private
198  {
199  if (stmt.getObject().isLiteral()) {
200  throw new ConfigurationException(
201  "Expected IRI object, found literal: " + pretty(stmt));
202  }
203  if (stmt.getObject().isAnon()) {
204  throw new ConfigurationException(
205  "Expected IRI object, found blank node: " + pretty(stmt));
206  }
207  }

References ResourceReader.pretty().

◆ assertLiteralValue() [1/2]

void assertLiteralValue ( Property  p)
private
209  {
210  assertLiteralValue(resource.getProperty(p));
211  }
void assertLiteralValue(Property p)
Definition: ResourceReader.java:209

References ResourceReader.resource.

Referenced by ResourceReader.assertString(), and ResourceReader.getBoolean().

◆ assertLiteralValue() [2/2]

void assertLiteralValue ( Statement  stmt)
private
213  {
214  if (stmt.getObject().isURIResource()) {
215  throw new ConfigurationException(
216  "Expected literal object, found IRI: " + pretty(stmt));
217  }
218  if (stmt.getObject().isAnon()) {
219  throw new ConfigurationException(
220  "Expected literal object, found blank node: " + pretty(stmt));
221  }
222  }

References ResourceReader.pretty().

◆ assertResourceValue() [1/2]

void assertResourceValue ( Property  p)
private
183  {
184  assertResourceValue(resource.getProperty(p));
185  }
void assertResourceValue(Property p)
Definition: ResourceReader.java:183

References ResourceReader.resource.

Referenced by ResourceReader.getResource(), and ResourceReader.getResources().

◆ assertResourceValue() [2/2]

void assertResourceValue ( Statement  stmt)
private
187  {
188  if (stmt.getObject().isLiteral()) {
189  throw new ConfigurationException(
190  "Expected resource object, found literal: " + pretty(stmt));
191  }
192  }

References ResourceReader.pretty().

◆ assertString()

void assertString ( Statement  stmt)
private
224  {
225  assertLiteralValue(stmt);
226  Literal value = stmt.getLiteral();
227  if (value.getDatatypeURI() == null ||
228  XSD.xstring.getURI().equals(value.getDatatypeURI()) ||
229  (RDF.getURI() + "langString").equals(value.getDatatypeURI())) {
230  return;
231  }
232  raiseUnexpectedDatatype("string", stmt);
233  }
void raiseUnexpectedDatatype(String expectedDatatype, Statement stmt)
Definition: ResourceReader.java:257

References ResourceReader.assertLiteralValue(), and ResourceReader.raiseUnexpectedDatatype().

Referenced by ResourceReader.getString(), and ResourceReader.getStrings().

◆ getBoolean()

boolean getBoolean ( Property  p,
boolean  defaultValue 
)
124  {
125  if (!resource.hasProperty(p)) {
126  return defaultValue;
127  }
130  Literal value = resource.getProperty(p).getLiteral();
131  if (XSD.xboolean.getURI().equals(value.getDatatypeURI())) {
132  return value.getBoolean();
133  }
134  if (value.getDatatypeURI() == null || XSD.xstring.getURI().equals(value.getDatatypeURI())) {
135  if ("true".equals(value.getString().toLowerCase())
136  || "false".equals(value.getString().toLowerCase())) {
137  return "true".equals(value.getString().toLowerCase());
138  }
139  }
140  raiseUnexpectedDatatype("xsd:boolean", resource.getProperty(p));
141  return false;
142  }
void assertHasOneValue(Property p)
Definition: ResourceReader.java:170

References ResourceReader.assertHasOneValue(), ResourceReader.assertLiteralValue(), ResourceReader.raiseUnexpectedDatatype(), and ResourceReader.resource.

Referenced by Dataset.addSameAsStatements(), Configuration.showLabels(), Dataset.supportsIRIs(), and Dataset.supportsSPARQL11().

◆ getIRI() [1/2]

String getIRI ( Property  p)

◆ getIRI() [2/2]

String getIRI ( Property  p,
String  defaultValue 
)
75  {
76  if (!resource.hasProperty(p)) return defaultValue;
78  assertIRIValue(p);
79  return resource.getProperty(p).getResource().getURI();
80  }

References ResourceReader.assertHasOneValue(), ResourceReader.assertIRIValue(), and ResourceReader.resource.

◆ getIRIs()

Set<String> getIRIs ( Property  p)
89  {
90  Set<String> result = new HashSet<String>();
91  StmtIterator it = resource.listProperties(p);
92  while (it.hasNext()) {
93  Statement stmt = it.next();
94  assertIRIValue(stmt);
95  result.add(stmt.getResource().getURI());
96  }
97  return result;
98  }

References ResourceReader.assertIRIValue(), and ResourceReader.resource.

Referenced by Dataset.buildDataSource(), Configuration.Configuration(), Configuration.getBrowsableNamespaces(), and Dataset.getBrowsableNamespaces().

◆ getModel()

◆ getProperties()

Set<Property> getProperties ( Property  p)
63  {
64  Set<Property> result = new HashSet<Property>();
65  for (Resource r: getResources(p)) {
66  result.add(r.as(Property.class));
67  }
68  return result;
69  }
Set< Resource > getResources(Property p)
Definition: ResourceReader.java:52

References ResourceReader.getResources().

Referenced by Configuration.Configuration().

◆ getRequiredIRI()

String getRequiredIRI ( Property  p)
82  {
83  if (!resource.hasProperty(p)) {
85  }
86  return getIRI(p);
87  }
void raiseMissingProperty(Property p)
Definition: ResourceReader.java:252

References ResourceReader.getIRI(), ResourceReader.raiseMissingProperty(), and ResourceReader.resource.

Referenced by Configuration.Configuration().

◆ getResource()

Resource getResource ( Property  p)
45  {
46  if (!resource.hasProperty(p)) return null;
49  return resource.getProperty(p).getResource();
50  }

References ResourceReader.assertHasOneValue(), ResourceReader.assertResourceValue(), and ResourceReader.resource.

Referenced by MetadataConfiguration.MetadataConfiguration().

◆ getResources()

Set<Resource> getResources ( Property  p)
52  {
53  Set<Resource> result = new HashSet<Resource>();
54  StmtIterator it = resource.listProperties(p);
55  while (it.hasNext()) {
56  Statement stmt = it.next();
57  assertResourceValue(stmt);
58  result.add(stmt.getResource());
59  }
60  return result;
61  }

References ResourceReader.assertResourceValue(), and ResourceReader.resource.

Referenced by Configuration.Configuration(), and ResourceReader.getProperties().

◆ getSelf()

Resource getSelf ( )
29  {
30  return resource;
31  }

References ResourceReader.resource.

Referenced by MetadataConfiguration.parsePlaceholder().

◆ getString() [1/2]

String getString ( Property  p)
100  {
101  return getString(p, null);
102  }
String getString(Property p)
Definition: ResourceReader.java:100

Referenced by Dataset.buildDataSource(), Configuration.getDefaultLanguage(), Configuration.getProjectName(), and Configuration.getWebResourcePrefix().

◆ getString() [2/2]

String getString ( Property  p,
String  defaultValue 
)
104  {
105  if (!resource.hasProperty(p)) {
106  return defaultValue;
107  }
109  assertString(resource.getProperty(p));
110  return resource.getProperty(p).getString();
111  }
void assertString(Statement stmt)
Definition: ResourceReader.java:224

References ResourceReader.assertHasOneValue(), ResourceReader.assertString(), and ResourceReader.resource.

◆ getStrings()

Set<String> getStrings ( Property  p)
113  {
114  Set<String> result = new HashSet<String>();
115  StmtIterator it = resource.listProperties(p);
116  while (it.hasNext()) {
117  Statement stmt = it.next();
118  assertString(stmt);
119  result.add(stmt.getString());
120  }
121  return result;
122  }

References ResourceReader.assertString(), and ResourceReader.resource.

Referenced by Dataset.buildDataSource().

◆ hasProperty()

boolean hasProperty ( Property  p)
41  {
42  return resource.hasProperty(p);
43  }

References ResourceReader.resource.

Referenced by Configuration.buildDataSource(), Dataset.buildDataSource(), and Configuration.Configuration().

◆ hasType()

boolean hasType ( Resource  class_)
37  {
38  return resource.hasProperty(RDF.type, class_);
39  }

References ResourceReader.resource.

Referenced by Dataset.buildDataSource().

◆ pretty() [1/2]

String pretty ( RDFNode  node)
private
235  {
236  if (node.isAnon()) return "[]";
237  if (node.isURIResource()) {
238  Resource r = node.asResource();
239  if (getModel().qnameFor(r.getURI()) == null) {
240  return "<" + r.getURI() + ">";
241  }
242  return getModel().qnameFor(r.getURI());
243  }
244  return PrintUtil.print(node);
245  }
Model getModel()
Definition: ResourceReader.java:33

References ResourceReader.getModel().

Referenced by ResourceReader.assertHasOneValue(), ResourceReader.assertIRIValue(), ResourceReader.assertLiteralValue(), ResourceReader.assertResourceValue(), ResourceReader.pretty(), ResourceReader.raiseMissingProperty(), ResourceReader.raiseUnexpectedDatatype(), and ResourceReader.requireExactlyOneOf().

◆ pretty() [2/2]

String pretty ( Statement  stmt)
private
247  {
248  return pretty(stmt.getSubject()) + " " + pretty(stmt.getPredicate())
249  + " " + pretty(stmt.getObject()) + ".";
250  }

References ResourceReader.pretty().

◆ raiseMissingProperty()

void raiseMissingProperty ( Property  p)
private
252  {
253  throw new ConfigurationException("Missing property " +
254  pretty(p) + " on resource " + pretty(resource));
255  }

References ResourceReader.pretty(), and ResourceReader.resource.

Referenced by ResourceReader.getRequiredIRI().

◆ raiseUnexpectedDatatype()

void raiseUnexpectedDatatype ( String  expectedDatatype,
Statement  stmt 
)
private
257  {
258  throw new ConfigurationException(
259  "Expected " + expectedDatatype +
260  " object, found other datatype: " +
261  pretty(stmt));
262  }

References ResourceReader.pretty().

Referenced by ResourceReader.assertString(), and ResourceReader.getBoolean().

◆ requireExactlyOneOf()

void requireExactlyOneOf ( Property...  properties)
144  {
145  Property found = null;
146  boolean first = true;
147  StringBuilder s = new StringBuilder();
148  for (Property p: properties) {
149  if (resource.hasProperty(p)) {
150  if (found == null) {
151  found = p;
152  } else {
153  throw new ConfigurationException("Can't have both " +
154  pretty(found) + " and " + pretty(p) +
155  " on resource " + pretty(resource));
156  }
157  }
158  if (!first) {
159  s.append(", ");
160  }
161  s.append(pretty(p));
162  first = false;
163  }
164  if (found == null) {
165  throw new ConfigurationException("One of " + s.toString() +
166  " required on resource " + pretty(resource));
167  }
168  }

References ResourceReader.pretty(), and ResourceReader.resource.

Referenced by Dataset.buildDataSource().

Member Data Documentation

◆ resource