Created
September 12, 2011 21:47
-
-
Save msalvadores/1212562 to your computer and use it in GitHub Desktop.
Java class to export Protege 3 database backend.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ncbo.proto; | |
import java.io.FileInputStream; | |
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Properties; | |
import edu.stanford.smi.protegex.owl.database.creator.OwlDatabaseCreator; | |
import edu.stanford.smi.protegex.owl.model.OWLModel; | |
import edu.stanford.smi.protegex.owl.writer.rdfxml.rdfwriter.OWLModelAllTripleStoresWriter; | |
/** | |
* | |
* @author [email protected] | |
* | |
* Sample code to export a protege 3 database backend programatically. | |
* References: | |
* http://protegewiki.stanford.edu/wiki/LoadOWLOntologyFromDB (method 2) | |
* http://smi-protege.stanford.edu/svn/owl/trunk/src/edu/stanford/smi/protegex/owl/jena/export/JenaExportPlugin.java?view=markup | |
* | |
*/ | |
public class MetadataExport { | |
public static void main(String[] args) throws Exception { | |
if (args.length < 1) { | |
System.out.println("use: java ncbo.proto.MetadataExport <file_to_export>"); | |
System.out.println("make sure that MetadataExport.properties contains the database configuration"); | |
return; | |
} | |
URI fileURI = new URI("file://"+(new File(args[0])).getAbsolutePath()); | |
Properties properties = new Properties(); | |
properties.load(new FileInputStream("MetadataExport.properties")); | |
Collection<String> errors = new ArrayList<String>(); | |
//the "false" argument means that it won't overide the existing table <--- IMPORTANT otherwise will destroy existing database. | |
OwlDatabaseCreator creator = new OwlDatabaseCreator(false); | |
creator.setDriver(properties.getProperty("jdbcDriver")); | |
creator.setURL(properties.getProperty("protegeJdbcUrl")); | |
creator.setUsername(properties.getProperty("protegeJdbcUsername")); | |
creator.setPassword(properties.getProperty("protegeJdbcPassword")); | |
creator.setTable(properties.getProperty("tableName")); | |
creator.create(errors); | |
if (errors.size() > 0) { | |
System.out.println("Errors opening database ...\n" + StringUtils.join(errors,"\n")); | |
} | |
OWLModel owlModel = creator.getOwlModel(); | |
try { | |
OWLModelAllTripleStoresWriter writer = new OWLModelAllTripleStoresWriter(owlModel, fileURI, true); | |
writer.write(); | |
} | |
catch (Exception ex) { | |
String message = "Failed to save file " + fileURI; | |
System.out.println(message); | |
throw ex; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment