moving utility methods to a different class.

This commit is contained in:
stellamit 2011-05-26 14:03:38 +00:00
parent a23ff48374
commit 42c8fe1b39
3 changed files with 109 additions and 68 deletions

View file

@ -4,10 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -58,7 +56,7 @@ import edu.cornell.mannlib.vitro.webapp.filestorage.model.FileInfo;
import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasoner;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils;
import edu.cornell.mannlib.vitro.webapp.utils.jena.ExtendedLinkedDataUtils;
import edu.cornell.mannlib.vitro.webapp.web.ContentType;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ListedIndividualTemplateModel;
@ -674,7 +672,7 @@ public class IndividualController extends FreemarkerHttpServlet {
rootDir = RICH_EXPORT_ROOT + include + "/";
}
Model extendedModel = InitialJenaModelUtils.createModelFromQueries(getServletContext(), rootDir, contextModel, entity.getURI());
Model extendedModel = ExtendedLinkedDataUtils.createModelFromQueries(getServletContext(), rootDir, contextModel, entity.getURI());
newModel.add(extendedModel);
}
}

View file

@ -0,0 +1,107 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils.jena;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.util.ResourceUtils;
import com.hp.hpl.jena.util.iterator.ClosableIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
public class ExtendedLinkedDataUtils {
private static final Log log = LogFactory.getLog(ExtendedLinkedDataUtils.class.getName());
public static Model createModelFromQueries(ServletContext sc, String rootDir, OntModel sourceModel, String subject) {
Model model = ModelFactory.createDefaultModel();
Set<String> pathSet = sc.getResourcePaths(rootDir);
if (pathSet == null) {
log.warn(rootDir + " not found.");
return model;
}
for ( String path : pathSet ) {
File file = new File(sc.getRealPath(path));
if (file.isDirectory()) {
model.add(createModelFromQueries(sc, path, sourceModel, subject));
} else if (file.isFile()) {
if (!path.endsWith(".sparql")) {
log.warn("Ignoring file " + path + " because the file extension is not sparql.");
continue;
}
model.add(createModelFromQuery(file, sourceModel, subject));
} else {
log.warn("path is neither a directory nor a file " + path);
}
} // end - for
return model;
}
public static Model createModelFromQuery(File sparqlFile, OntModel sourceModel, String subject) {
Model model = ModelFactory.createDefaultModel();
BufferedReader reader = null;
try {
try {
reader = new BufferedReader(new FileReader(sparqlFile));
StringBuffer fileContents = new StringBuffer();
String ln;
while ( (ln = reader.readLine()) != null) {
fileContents.append(ln).append('\n');
}
String query = fileContents.toString();
String subjectString = "<" + subject + ">";
query = query.replaceAll("PERSON_URI", subjectString);
Query q = QueryFactory.create(query, Syntax.syntaxARQ);
QueryExecution qe = QueryExecutionFactory.create(q, sourceModel);
qe.execConstruct(model);
} catch (Exception e) {
log.error("Unable to process file " + sparqlFile.getAbsolutePath(), e);
} finally {
reader.close();
}
} catch (IOException ioe) {
// this is for the reader.close above
log.warn("Exception while trying to close file: " + sparqlFile.getAbsolutePath(), ioe);
}
return model;
}
}

View file

@ -106,68 +106,4 @@ public class InitialJenaModelUtils {
return m;
}
public static Model createModelFromQueries(ServletContext sc, String rootDir, OntModel sourceModel, String subject) {
Model model = ModelFactory.createDefaultModel();
Set<String> pathSet = sc.getResourcePaths(rootDir);
if (pathSet == null) {
log.warn(rootDir + " not found.");
return model;
}
for ( String path : pathSet ) {
File file = new File(sc.getRealPath(path));
if (file.isDirectory()) {
model.add(createModelFromQueries(sc, path, sourceModel, subject));
} else if (file.isFile()) {
if (!path.endsWith(".sparql")) {
log.warn("Ignoring file " + path + " because the file extension is not sparql.");
continue;
}
model.add(createModelFromQuery(file, sourceModel, subject));
} else {
log.warn("path is neither a directory nor a file " + path);
}
} // end - for
return model;
}
public static Model createModelFromQuery(File sparqlFile, OntModel sourceModel, String subject) {
Model model = ModelFactory.createDefaultModel();
BufferedReader reader = null;
try {
try {
reader = new BufferedReader(new FileReader(sparqlFile));
StringBuffer fileContents = new StringBuffer();
String ln;
while ( (ln = reader.readLine()) != null) {
fileContents.append(ln).append('\n');
}
String query = fileContents.toString();
String subjectString = "<" + subject + ">";
query = query.replaceAll("PERSON_URI", subjectString);
Query q = QueryFactory.create(query, Syntax.syntaxARQ);
QueryExecution qe = QueryExecutionFactory.create(q, sourceModel);
qe.execConstruct(model);
} catch (Exception e) {
log.error("Unable to process file " + sparqlFile.getAbsolutePath(), e);
} finally {
reader.close();
}
} catch (IOException ioe) {
// this is for the reader.close above
log.warn("Exception while trying to close file: " + sparqlFile.getAbsolutePath(), ioe);
}
return model;
}
}