0

I am working with JENA framework to scrape info about public contracts and publish it as RDF and I can't get over following problem:
When creating new RDF model, I create new Resource for each contract and then assign bunch of properties (that I scraped) using JENA's addProperty(Property, RDFNode) or addProperty(Property, String) method. The problem is, that some contracts are missing some properties so I get NullPointerException.
Using

if(contract.getProperty() != null)
{
   resource.addProperty(VOCABULARY.property, contract.getProperty());
}

for every single property is probably not the best way and since the second parameter of addProperty() method differs a lot (it can be directly the contract.getProperty(), but sometimes also model.createLiteral(contract.getProperty()) or model.createTypedLiteral(contract.getProperty(), XSDDataType.XSDInt) and so on), I can't create my own myAddProperty() method where I would check for null and call JENA's addProperty().
What would you suggest as a best solution to skip all the null properties?
Thanks a lot for any suggestions.

tom
  • 311
  • 3
  • 4
  • 13
  • 1
    What type of class is the resource variable? Can you inherit from that class and override the `addProperty()` method? – Marcelo Jun 22 '11 at 21:59
  • 3
    Why can't a "myAddProperty()" that then calls the "addProperty()" method? – JustinKSU Jun 22 '11 at 21:59
  • @Marcelo the resource variable is com.hp.hpl.jena.rdf.model.Resource type, good idea, I'll try to ingerit and override addProperty() – tom Jun 22 '11 at 22:08
  • I am curious, why can't you add a little myAddProperty method, either private in your class, or as a static method in a helper class? – Peter Tillemans Jun 22 '11 at 22:10
  • @Peter and JustinKSU you guys are probably right, I was stupid, by saying I can't create it, I meant there is so many different parameters you can pass to original `addProperty()`, but actually all the `model.createTypedLiteral()` stuff and so on returns Strings, so I CAN do it. Thanks, I was stupid, sorry – tom Jun 22 '11 at 22:19

2 Answers2

1

Refactor the null check into a separate method:

private void addProperty(final Resource resource, final String key, final String value) {
    if (value != null) {
        resource.addProperty(key, value);
    }
}

You can then use this method as follows:

addProperty(resource, VOCABULARY.property, contract.getProperty());
hoipolloi
  • 7,984
  • 2
  • 27
  • 28
  • one question, why to use `final` parameters? – tom Jun 22 '11 at 23:13
  • @tom - This is apparently quite a controversial practice! See the debate here: http://stackoverflow.com/questions/500508/why-should-i-use-the-keyword-final-on-a-method-parameter-in-java Personally, I think it increases the readability (and intent) of my code. – hoipolloi Jun 23 '11 at 08:51
0

I'm not really familiar with those frameworks but maybe you can subclass Resource and do something like this? :

@overrides
public void addProperty(String key, String value)
{
   if (value != null) super.addProperty(key,value);
}
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • well, com.hp.hpl.jena.rdf.model.Resource is actually an interface – tom Jun 22 '11 at 22:13
  • oh, sorry. Is the implementation of that interface something you can intercept at least (like an aspect for example)? – Pablo Fernandez Jun 22 '11 at 22:15
  • the interface has a lot of methods, so it's pretty long, I think I can just create `myAddProperty()` actually (as discussed above), but thanks for your idea, it really helped me a lot – tom Jun 22 '11 at 22:22