package sheffield.examples;
/*
* DataStoreApplication.java
*
* Copyright (c) 1998-2003, The University of Sheffield.
*
* This file is part of GATE (see http://gate.ac.uk/), and is free
* software, licenced under the GNU Library General Public License,
* Version 2, June 1991 (in the distribution as file licence.html,
* and also available at http://gate.ac.uk/gate/licence.html).
*
* Marin Dimitrov, 18/Jan/2003
*
* $Id: DataStoreApplication.java,v 1.3 2004/12/14 14:36:24 niraj Exp $
*/
import java.net.*;
import gate.*;
import gate.persist.SerialDataStore;
import gate.util.Err;
import gate.util.Out;
import gate.creole.ResourceInstantiationException;
import gate.util.GateException;
public class DataStoreApplication {
//the directory must EXIST and be EMPTY
//private static final String dsDir = "/var/tmp/gate001";
private static final String DS_DIR = "file:///c:/temp/gate001";
public DataStoreApplication() {
}
public static void main(String[] args) {
DataStoreApplication dsApp = new DataStoreApplication();
//init GATE
// this is the first thing to be done
try {
Gate.init();
}
catch (GateException gex) {
Err.prln("cannot initialise GATE...");
gex.printStackTrace();
return;
}
try {
//create&open a new Serial Data Store
// pass the datastore class and path as parameteres
SerialDataStore sds = (SerialDataStore)Factory.createDataStore("gate.persist.SerialDataStore",
DS_DIR);
sds.open();
Out.prln("serial datastore created...");
//create test corpus
Corpus corp = dsApp.createTestCorpus();
Corpus persistCorp = null;
//save corpus in datastore
// SecurityInfo is ingored for SerialDataStore - just pass null
// a new persisent corpus is returned
persistCorp = (Corpus)sds.adopt(corp,null);
sds.sync(persistCorp);
Out.prln("corpus saved in datastore...");
//change corpus and sync it with the datastore
persistCorp.setName("new name");
persistCorp.sync();
//load corpus from datastore using its persistent ID
Object corpusID = persistCorp.getLRPersistenceId();
persistCorp = null;
FeatureMap corpFeatures = Factory.newFeatureMap();
corpFeatures.put(DataStore.LR_ID_FEATURE_NAME, corpusID);
corpFeatures.put(DataStore.DATASTORE_FEATURE_NAME, sds);
//tell the factory to load the Serial Corpus with the specified ID from the specified datastore
persistCorp = (Corpus)Factory.createResource("gate.corpora.SerialCorpusImpl", corpFeatures);
Out.prln("corpus loaded from datastore...");
//remove corpus from datastore
sds.delete("gate.corpora.SerialCorpusImpl", corpusID);
Out.prln("corpus deleted from datastore...");
persistCorp = null;
//close data store
sds.close();
sds = null;
//reopen it
sds = new SerialDataStore(DS_DIR);
sds.open();
//save, load and delete a document
Document doc = dsApp.createTestDocument();
Document persistDoc = null;
//save document in datastore
// SecurityInfo is ingored for SerialDataStore - just pass null
persistDoc = (Document)sds.adopt(doc,null);
sds.sync(persistDoc);
Out.prln("document saved in datastore...");
//change document and sync it with the datastore
persistDoc.setName("new name");
persistDoc.sync();
//load document from datastore
Object docID = persistDoc.getLRPersistenceId();
persistDoc = null;
FeatureMap docFeatures = Factory.newFeatureMap();
docFeatures.put(DataStore.LR_ID_FEATURE_NAME, docID);
docFeatures.put(DataStore.DATASTORE_FEATURE_NAME, sds);
persistDoc = (Document)Factory.createResource("gate.corpora.DocumentImpl", docFeatures);
Out.prln("document loaded from datastore...");
//remove document from datastore
sds.delete("gate.corpora.DocumentImpl", docID);
Out.prln("document deleted from datastore...");
persistDoc = null;
//close data store
sds.close();
//delete datastore
sds.delete();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public Corpus createTestCorpus()
throws MalformedURLException, ResourceInstantiationException {
Document doc1 = Factory.newDocument(new URL("http://gate.ac.uk/index.html"));
//Document doc1 = Factory.newDocument(new URL("file:///c:/temp/test.txt"));
Document doc2 = Factory.newDocument(new URL("http://gate.ac.uk/documentation.html"));
assert doc1!=null && doc2!=null;
// create a corpus with the above documents
Corpus result = Factory.newCorpus("test corpus");
assert result != null;
result.add(doc1);
result.add(doc2);
return result;
}
public Document createTestDocument()
throws MalformedURLException, ResourceInstantiationException {
Document result = Factory.newDocument(new URL("http://gate.ac.uk/index.html"));
assert result!=null;
return result;
}
}