GeoPubby  Version 0.1.0.0
MetadataConfiguration Class Reference
Inheritance diagram for MetadataConfiguration:
Collaboration diagram for MetadataConfiguration:

Public Member Functions

 MetadataConfiguration (Resource dataset)
 
 MetadataConfiguration (Resource dataset, RemoteSPARQLDataSource sparqlDataSource)
 
void addCustomMetadata (Model document, Resource documentResource)
 
Resource addMetadataFromTemplate (Model document, HypermediaControls controller)
 
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

RDFNode parsePlaceholder (RDFNode phRes, HypermediaControls controller, Calendar currentTime, Resource currentDocRepr)
 
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 customTemplate
 
final String metadataTemplate
 
final RemoteSPARQLDataSource sparqlDataSource
 
final Resource resource
 

Static Private Attributes

static final String metadataPlaceholderURIPrefix = "about:metadata:"
 

Constructor & Destructor Documentation

◆ MetadataConfiguration() [1/2]

MetadataConfiguration ( Resource  dataset)
28  {
29  this(dataset, null);
30  }

◆ MetadataConfiguration() [2/2]

MetadataConfiguration ( Resource  dataset,
RemoteSPARQLDataSource  sparqlDataSource 
)
33  {
34  super(dataset);
35  this.customTemplate = getResource(CONF.rdfDocumentMetadata);
36  this.metadataTemplate = getIRI(CONF.metadataTemplate);
38  }
final Resource customTemplate
Definition: MetadataConfiguration.java:24
final RemoteSPARQLDataSource sparqlDataSource
Definition: MetadataConfiguration.java:26
final String metadataTemplate
Definition: MetadataConfiguration.java:25
String getIRI(Property p)
Definition: ResourceReader.java:71
Resource getResource(Property p)
Definition: ResourceReader.java:45

References ResourceReader.getIRI(), ResourceReader.getResource(), CONF.metadataTemplate, CONF.rdfDocumentMetadata, and MetadataConfiguration.sparqlDataSource.

Member Function Documentation

◆ addCustomMetadata()

void addCustomMetadata ( Model  document,
Resource  documentResource 
)
40  {
41  if (customTemplate == null) return;
42  StmtIterator it = customTemplate.listProperties();
43  while (it.hasNext()) {
44  Statement stmt = it.nextStatement();
45  document.add(documentResource, stmt.getPredicate(), stmt.getObject());
46  }
47  it = customTemplate.getModel().listStatements(null, null, customTemplate);
48  while (it.hasNext()) {
49  Statement stmt = it.nextStatement();
50  if (stmt.getPredicate().equals(CONF.rdfDocumentMetadata)) {
51  continue;
52  }
53  document.add(stmt.getSubject(), stmt.getPredicate(), documentResource);
54  }
55  }

References MetadataConfiguration.customTemplate, and CONF.rdfDocumentMetadata.

Referenced by BaseServlet.addDocumentMetadata().

◆ addMetadataFromTemplate()

Resource addMetadataFromTemplate ( Model  document,
HypermediaControls  controller 
)
57  {
58  if (metadataTemplate == null) {
59  return null;
60  }
61 
62  Calendar currentTime;
63  Resource currentDocRepr;
64  currentTime = Calendar.getInstance();
65 
66  // add metadata from templates
67  Model tplModel = FileManager.get().loadModel(metadataTemplate);
68 
69  // iterate over template statements to replace placeholders
70  Model metadata = ModelFactory.createDefaultModel();
71  currentDocRepr = metadata.createResource();
72  StmtIterator it = tplModel.listStatements();
73  while (it.hasNext()) {
74  Statement stmt = it.nextStatement();
75  Resource subj = stmt.getSubject();
76  Property pred = stmt.getPredicate();
77  RDFNode obj = stmt.getObject();
78 
79  try {
80  if (subj.toString().contains(metadataPlaceholderURIPrefix)){
81  subj = (Resource) parsePlaceholder(subj, controller, currentTime, currentDocRepr);
82  if (subj == null) {
83  // create a unique blank node with a fixed id.
84  subj = getModel().createResource(new AnonId(String.valueOf(stmt.getSubject().hashCode())));
85  }
86  }
87 
88  if (obj.toString().contains(metadataPlaceholderURIPrefix)){
89  obj = parsePlaceholder(obj, controller, currentTime, currentDocRepr);
90  }
91 
92  // only add statements with some objects
93  if (obj != null) {
94  stmt = metadata.createStatement(subj,pred,obj);
95  metadata.add(stmt);
96  }
97  } catch (Exception e) {
98  // something went wrong, oops - lets better remove the offending statement
99  metadata.remove(stmt);
100  e.printStackTrace();
101  }
102  }
103 
104  // remove blank nodes that don't have any properties
105  boolean changes = true;
106  while ( changes ) {
107  changes = false;
108  StmtIterator stmtIt = metadata.listStatements();
109  List<Statement> remList = new ArrayList<Statement>();
110  while (stmtIt.hasNext()) {
111  Statement s = stmtIt.nextStatement();
112  if ( s.getObject().isAnon()
113  && ! ((Resource) s.getObject().as(Resource.class)).listProperties().hasNext() ) {
114  remList.add(s);
115  changes = true;
116  }
117  }
118  metadata.remove(remList);
119  }
120 
121  if (document != null) {
122  document.add( metadata );
123  }
124 
125  return currentDocRepr;
126  }
RDFNode parsePlaceholder(RDFNode phRes, HypermediaControls controller, Calendar currentTime, Resource currentDocRepr)
Definition: MetadataConfiguration.java:128
static final String metadataPlaceholderURIPrefix
Definition: MetadataConfiguration.java:22
Model getModel()
Definition: ResourceReader.java:33

References ResourceReader.getModel(), MetadataConfiguration.metadataPlaceholderURIPrefix, MetadataConfiguration.metadataTemplate, and MetadataConfiguration.parsePlaceholder().

Referenced by BaseServlet.addDocumentMetadata(), and BaseServlet.addPageMetadata().

◆ assertHasOneValue()

void assertHasOneValue ( Property  p)
privateinherited
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
final Resource resource
Definition: ResourceReader.java:23

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

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

◆ assertIRIValue() [1/2]

void assertIRIValue ( Property  p)
privateinherited
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)
privateinherited
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)
privateinherited
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)
privateinherited
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)
privateinherited
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)
privateinherited
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)
privateinherited
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 
)
inherited
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]

◆ getIRI() [2/2]

String getIRI ( Property  p,
String  defaultValue 
)
inherited
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)
inherited
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)
inherited
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)
inherited
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)
inherited
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)
inherited
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 ( )
inherited
29  {
30  return resource;
31  }

References ResourceReader.resource.

Referenced by MetadataConfiguration.parsePlaceholder().

◆ getString() [1/2]

String getString ( Property  p)
inherited
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 
)
inherited
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)
inherited
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)
inherited
41  {
42  return resource.hasProperty(p);
43  }

References ResourceReader.resource.

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

◆ hasType()

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

References ResourceReader.resource.

Referenced by Dataset.buildDataSource().

◆ parsePlaceholder()

RDFNode parsePlaceholder ( RDFNode  phRes,
HypermediaControls  controller,
Calendar  currentTime,
Resource  currentDocRepr 
)
private
129  {
130  String phURI = phRes.asNode().getURI();
131  // get package name and placeholder name from placeholder URI
132  phURI = phURI.replace(metadataPlaceholderURIPrefix, "");
133  String phPackage = phURI.substring(0, phURI.indexOf(":")+1);
134  String phName = phURI.replace(phPackage, "");
135  phPackage = phPackage.replace(":", "");
136  Resource dataset = getSelf();
137 
138  if (phPackage.equals("runtime")) {
139  // <about:metadata:runtime:query> - the SPARQL Query used to get the RDF Graph
140  if (phName.equals("query")) {
141  if (sparqlDataSource == null) return null;
142  return getModel().createTypedLiteral(sparqlDataSource.getPreviousDescribeQuery());
143  }
144  // <about:metadata:runtime:time> - the current time
145  if (phName.equals("time")) {
146  return getModel().createTypedLiteral(currentTime);
147  }
148  // <about:metadata:runtime:graph> - URI of the graph
149  if (phName.equals("graph")) {
150  // Replaced the commented line by the following one because the
151  // RDF graph we want to talk about is a specific representation
152  // of the data identified by the getDataURL() URI.
153  // Olaf, May 28, 2010
154  // return model.createResource(describedResource.getDataURL());
155  return currentDocRepr;
156  }
157  // <about:metadata:runtime:data> - URI of the data
158  if (phName.equals("data")) {
159  return getModel().createResource(controller.getDataURL());
160  }
161  // <about:metadata:runtime:resource> - URI of the resource
162  if (phName.equals("resource")) {
163  return getModel().createResource(controller.getAbsoluteIRI());
164  }
165  }
166 
167  // <about:metadata:config:*> - The configuration parameters
168  if (phPackage.equals("config")) {
169  // look for requested property in the dataset config
170  Property p = getModel().createProperty(CONF.NS + phName);
171  if (dataset.hasProperty(p))
172  return dataset.getProperty(p).getObject();
173 
174  // find pointer to the global configuration set...
175  StmtIterator it = dataset.getModel().listStatements(null, CONF.dataset, dataset);
176  Statement ptrStmt = it.nextStatement();
177  if (ptrStmt == null) return null;
178 
179  // look in global config if nothing found so far
180  Resource globalConfig = ptrStmt.getSubject();
181  if (globalConfig.hasProperty(p))
182  return globalConfig.getProperty(p).getObject();
183  }
184 
185  // <about:metadata:metadata:*> - The metadata provided by users
186  if (phPackage.equals("metadata")) {
187  // look for requested property in the dataset config
188  Property p = getModel().createProperty(META.NS + phName);
189  if (dataset.hasProperty(p))
190  return dataset.getProperty(p).getObject();
191 
192  // find pointer to the global configuration set...
193  StmtIterator it = dataset.getModel().listStatements(null, CONF.dataset, dataset);
194  Statement ptrStmt = it.nextStatement();
195  if (ptrStmt == null) return null;
196 
197  // look in global config if nothing found so far
198  Resource globalConfig = ptrStmt.getSubject();
199  if (globalConfig.hasProperty(p))
200  return globalConfig.getProperty(p).getObject();
201  }
202 
203  return getModel().createResource(new AnonId(String.valueOf(phRes.hashCode())));
204  }
Resource getSelf()
Definition: ResourceReader.java:29
String getPreviousDescribeQuery()
Definition: RemoteSPARQLDataSource.java:273

References CONF.dataset, HypermediaControls.getAbsoluteIRI(), HypermediaControls.getDataURL(), ResourceReader.getModel(), RemoteSPARQLDataSource.getPreviousDescribeQuery(), ResourceReader.getSelf(), MetadataConfiguration.metadataPlaceholderURIPrefix, CONF.NS, META.NS, and MetadataConfiguration.sparqlDataSource.

Referenced by MetadataConfiguration.addMetadataFromTemplate().

◆ pretty() [1/2]

String pretty ( RDFNode  node)
privateinherited
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  }

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)
privateinherited
247  {
248  return pretty(stmt.getSubject()) + " " + pretty(stmt.getPredicate())
249  + " " + pretty(stmt.getObject()) + ".";
250  }

References ResourceReader.pretty().

◆ raiseMissingProperty()

void raiseMissingProperty ( Property  p)
privateinherited
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 
)
privateinherited
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)
inherited
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

◆ customTemplate

final Resource customTemplate
private

◆ metadataPlaceholderURIPrefix

final String metadataPlaceholderURIPrefix = "about:metadata:"
staticprivate

◆ metadataTemplate

final String metadataTemplate
private

◆ resource

◆ sparqlDataSource