VIVO-705 Trim ontology URI before using. Check for URISyntaxException before using.
This commit is contained in:
parent
63140fa91b
commit
83dd4a57e7
2 changed files with 34 additions and 8 deletions
|
@ -62,4 +62,13 @@ public class Ontology implements Comparable<Ontology>
|
||||||
return collator.compare(this.getName(), o2.getName());
|
return collator.compare(this.getName(), o2.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Ontology[myID=" + myID + ", myName=" + myName + ", myType="
|
||||||
|
+ myType + ", myPrefix=" + myPrefix + ", myNamespaceID="
|
||||||
|
+ myNamespaceID + ", myURI=" + myURI + ", myVClassesList="
|
||||||
|
+ myVClassesList + ", myPropsList=" + myPropsList
|
||||||
|
+ ", myEntitiesList=" + myEntitiesList + "]";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -28,10 +30,12 @@ public class OntologyDaoJena extends JenaBaseDao implements OntologyDao {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized String adjustOntologyURI(String ontologyURI) {
|
public static synchronized String adjustOntologyURI(String ontologyURI) {
|
||||||
if ( (ontologyURI.length()>1) && (ontologyURI.charAt(ontologyURI.length()-1)=='#') ) {
|
String uri = ontologyURI.trim();
|
||||||
return ontologyURI.substring(0,ontologyURI.length()-1);
|
int length = uri.length();
|
||||||
|
if ( (length>1) && (uri.charAt(length-1)=='#') ) {
|
||||||
|
return uri.substring(0,length-1);
|
||||||
} else {
|
} else {
|
||||||
return ontologyURI;
|
return uri;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,10 +128,14 @@ public class OntologyDaoJena extends JenaBaseDao implements OntologyDao {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String insertNewOntology(Ontology ontology, OntModel ontModel) {
|
public String insertNewOntology(Ontology ontology, OntModel ontModel) {
|
||||||
if (ontology != null && ontology.getURI() != null && ontology.getURI().length()>0) {
|
if (ontology == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String ontologyURI = adjustAndValidateOntologyURI(ontology.getURI());
|
||||||
ontModel.enterCriticalSection(Lock.WRITE);
|
ontModel.enterCriticalSection(Lock.WRITE);
|
||||||
try {
|
try {
|
||||||
com.hp.hpl.jena.ontology.Ontology o = ontModel.createOntology(adjustOntologyURI(ontology.getURI()));
|
com.hp.hpl.jena.ontology.Ontology o = ontModel.createOntology(ontologyURI);
|
||||||
if (ontology.getName() != null && ontology.getName().length()>0) {
|
if (ontology.getName() != null && ontology.getName().length()>0) {
|
||||||
o.setLabel(ontology.getName(), getDefaultLanguage());
|
o.setLabel(ontology.getName(), getDefaultLanguage());
|
||||||
}
|
}
|
||||||
|
@ -138,12 +146,21 @@ public class OntologyDaoJena extends JenaBaseDao implements OntologyDao {
|
||||||
} finally {
|
} finally {
|
||||||
ontModel.leaveCriticalSection();
|
ontModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
} else {
|
} catch (URISyntaxException e) {
|
||||||
return null;
|
log.warn("Failed to insert new ontology: " + ontology, e);
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateOntology(Ontology ontology) {
|
private String adjustAndValidateOntologyURI(String uri) throws URISyntaxException {
|
||||||
|
if (uri == null || uri.isEmpty()) {
|
||||||
|
throw new URISyntaxException(uri, "URI is empty");
|
||||||
|
}
|
||||||
|
String adjusted = adjustOntologyURI(uri);
|
||||||
|
return new URI(adjusted).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateOntology(Ontology ontology) {
|
||||||
updateOntology(ontology,getOntModel());
|
updateOntology(ontology,getOntModel());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue