/** * This converts a bibtex file into Dublin Core RDFXML. This * example treats all of the authors as string literals. */ //Import every class and interface by name. import java.io.PrintWriter; import java.io.Writer; import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.OutputStream; import java.util.StringTokenizer; import java.util.Vector; //The Dublin Core import com.hp.hpl.mesa.rdf.jena.vocabulary.DC; //Use the memory model implementation import com.hp.hpl.mesa.rdf.jena.mem.ModelMem; //Model package imports import com.hp.hpl.mesa.rdf.jena.model.Model; import com.hp.hpl.mesa.rdf.jena.model.Resource; import com.hp.hpl.mesa.rdf.jena.model.Property; import com.hp.hpl.mesa.rdf.jena.model.Statement; import com.hp.hpl.mesa.rdf.jena.model.StmtIterator; import com.hp.hpl.mesa.rdf.jena.model.RDFNode; import com.hp.hpl.mesa.rdf.jena.model.RDFException; public class DublinCoreEx4 { //Some bibtex constants final String AUTHOR="AUTHOR"; final String TITLE="TITLE"; final String INSTITUTION="INSTITUTION"; final String YEAR="YEAR"; final String JOURNAL="JOURNAL"; //Base URI for the papers. final String baseUri="http://cgl.indiana.edu/publications/"; //Some classwide variables. Model model=null; public DublinCoreEx4() { model=new ModelMem(); } //Reads public String readBibtexFile(String filename) throws IOException { String accumulate=""; BufferedReader buf=new BufferedReader(new FileReader(filename)); String line=buf.readLine(); while(line!=null) { accumulate+=line+"\n"; line=buf.readLine(); } return accumulate; } public void parseEntries(String acc) throws RDFException { StringTokenizer st=new StringTokenizer(acc,"@"); int index=0; while(st.hasMoreTokens()) { //First, create a resource. String resourceUri=baseUri+"Publication"+index; index++; Resource publication=model.createResource(resourceUri); //Next, parse the input file and fill out the resource. String test=st.nextToken(); String type=acc.substring(0,acc.indexOf("{")); String token="\n"; StringTokenizer st2=new StringTokenizer(test,token); while(st2.hasMoreTokens()) { String toparse=st2.nextToken(); if(toparse.indexOf(AUTHOR)>-1) { Vector authors=parseAuthors(toparse); for(int i=0;i-1) { String title=parseTitle(toparse); publication.addProperty(DC.title,title); } if(toparse.indexOf(JOURNAL)>-1) { String journal=parseJournal(toparse); } if(toparse.indexOf(INSTITUTION)>-1) { String journal=parseInstitution(toparse); } if(toparse.indexOf(YEAR)>-1) { String year=parseYear(toparse); publication.addProperty(DC.date,year); } } } } private Vector csvToVector(String csvString) { StringTokenizer st=new StringTokenizer(csvString,","); Vector retvec=new Vector(); while(st.hasMoreTokens()) { retvec.add(st.nextToken()); } return retvec; } private Vector parseAuthors(String toparse) { Vector authors=null; String csvauthor=toparse.substring(toparse.indexOf("\"")+1, toparse.lastIndexOf("\"")); authors=csvToVector(csvauthor); for(int i=0;i