/** * This converts a bibtex file into Dublin Core RDFXML. This * version uses VCARD representations for selected authors. */ //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; //The VCARD class import com.hp.hpl.mesa.rdf.jena.vocabulary.VCARD; //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; import com.hp.hpl.mesa.rdf.jena.model.ResIterator; public class DublinCoreEx5 { //Some URIs for people with VCARDS. String[] nameUris={"http://cgl.indiana.edu/people/GCF", "http://cgl.indiana.edu/people/MEP", "http://cgl.indiana.edu/people/DBC"}; Model model=null; public DublinCoreEx5() throws Exception { model=new ModelMem(); } public DublinCoreEx5(Model model) { this.model=model; } public Model getModel() { return model; } public void init() throws RDFException, IOException { //Set up people CGLPeople people=new CGLPeople(model,nameUris); people.createEntry(nameUris[0],"Geoffrey","Fox","gcf@indiana.edu"); people.createEntry(nameUris[1],"Marlon","Pierce","marpierc@indaiana.edu"); people.createEntry(nameUris[2],"Bryan","Carpenter","dbcarpen@indiana.edu"); //Set up the publications CGLPublications dcpub=new CGLPublications(model,people); dcpub.parseEntries(dcpub.readBibtexFile("cgl-pubs.txt")); } /** * Prints the model out as RDF XML */ public void printRDFXML(Model model,OutputStream out) throws RDFException { model.write(new PrintWriter(out),"RDF/XML-ABBREV"); } public static void main(String[] args) throws Exception{ Model model=new ModelMem(); DublinCoreEx5 dc5=new DublinCoreEx5(model); dc5.init(); dc5.printRDFXML(model,System.out); } } /** * A simple class to represent people with VCARDS. */ class CGLPeople { Model model; String[] nameUris; public CGLPeople() { model=new ModelMem(); nameUris=null; } public CGLPeople(Model model,String[] nameUris) { this.model=model; this.nameUris=nameUris; } public void createEntry(String uri, String first_name, String last_name, String email) throws RDFException{ String fullname=first_name+" "+last_name; Resource person=model.createResource(uri); person.addProperty(VCARD.FN,fullname); person.addProperty(VCARD.EMAIL,email); //Create name as an anonymous resource. Resource name=model.createResource(); name.addProperty(VCARD.Given,first_name); name.addProperty(VCARD.Family,last_name); person.addProperty(VCARD.N,name); } //See if the named person has a VCARD. public boolean hasVCARD(String name) throws RDFException{ boolean retval=false; ResIterator iter=model.listSubjectsWithProperty(VCARD.FN); while(iter.hasNext()) { String entryFN=iter.next().getProperty(VCARD.FN).getString(); if(entryFN.equals(name)) { retval=true; break; } } return retval; } //Return the requested VCARD entry, null if no match } /** * A simple class to create publications. */ class CGLPublications { //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/"; Model model; CGLPeople people; public CGLPublications() { model=new ModelMem(); people=new CGLPeople(); } public CGLPublications(Model model,CGLPeople people) { this.model=model; this.people=people; } //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