@Override public boolean add(Document o) { if (o == null) return false; Document doc = o; // make it accept only docs from its own datastore if (doc.getDataStore() != null && !this.dataStore.equals(doc.getDataStore())) { Err.prln("Error: Persistent corpus can only accept documents " + "from its own datastore!"); return false; } // if // add the document with its index in the docDataList // in this case, since it's going to be added to the end // the index will be the size of the docDataList before // the addition DocumentData docData = new DocumentData(doc.getName(), doc.getLRPersistenceId(), doc.getClass().getName()); boolean result = docDataList.add(docData); documents.add(doc); documentAdded(doc); fireDocumentAdded( new CorpusEvent( SerialCorpusImpl.this, doc, docDataList.size() - 1, doc.getLRPersistenceId(), CorpusEvent.DOCUMENT_ADDED)); return result; }
@Override public void add(int index, Document o) { if (o == null) return; Document doc = o; DocumentData docData = new DocumentData(doc.getName(), doc.getLRPersistenceId(), doc.getClass().getName()); docDataList.add(index, docData); documents.add(index, doc); documentAdded(doc); fireDocumentAdded( new CorpusEvent( SerialCorpusImpl.this, doc, index, doc.getLRPersistenceId(), CorpusEvent.DOCUMENT_ADDED)); }
/** * Constructor to create a SerialCorpus from a transient one. This is called by adopt() to store * the transient corpus and re-route the methods calls to it, until the corpus is sync-ed on disk. * After that, the transientCorpus will always be null, so the new functionality will be used * instead. */ protected SerialCorpusImpl(Corpus tCorpus) { // copy the corpus name and features from the one in memory this.setName(tCorpus.getName()); this.setFeatures(tCorpus.getFeatures()); docDataList = new ArrayList<DocumentData>(); // now cache the names of all docs for future use List<String> docNames = tCorpus.getDocumentNames(); for (int i = 0; i < docNames.size(); i++) { Document doc = tCorpus.get(i); docDataList.add(new DocumentData(docNames.get(i), null, doc.getClass().getName())); } // copy all the documents from the transient corpus documents = new ArrayList<Document>(); documents.addAll(tCorpus); // make sure we fire events when docs are added/removed/etc Gate.getCreoleRegister().addCreoleListener(this); }
public int findDocument(Document doc) { boolean found = false; DocumentData docData = null; // first try finding the document in memory int index = documents.indexOf(doc); if (index > -1 && index < docDataList.size()) return index; // else try finding a document with the same name and persistent ID Iterator<DocumentData> iter = docDataList.iterator(); for (index = 0; iter.hasNext(); index++) { docData = iter.next(); if (docData.getDocumentName().equals(doc.getName()) && docData.getPersistentID().equals(doc.getLRPersistenceId()) && docData.getClassType().equals(doc.getClass().getName())) { found = true; break; } } if (found && index < docDataList.size()) return index; else return -1; } // findDocument
public void setTransientSource(Object source) { if (!(source instanceof Corpus)) return; // the following initialisation is only valid when we're // constructing // this object from a transient one. If it has already been stored // in // a datastore, then the initialisation is done in readObject() // since // this method is the one called by serialisation, when objects // are restored. if (this.dataStore != null && this.lrPersistentId != null) return; Corpus tCorpus = (Corpus) source; // copy the corpus name and features from the one in memory this.setName(tCorpus.getName()); this.setFeatures(tCorpus.getFeatures()); docDataList = new ArrayList<DocumentData>(); // now cache the names of all docs for future use List<String> docNames = tCorpus.getDocumentNames(); for (int i = 0; i < docNames.size(); i++) { Document aDoc = tCorpus.get(i); docDataList.add(new DocumentData(docNames.get(i), null, aDoc.getClass().getName())); } // copy all the documents from the transient corpus documents = new ArrayList<Document>(); documents.addAll(tCorpus); this.addedDocs = new Vector<Document>(); this.removedDocIDs = new Vector<String>(); this.changedDocs = new Vector<Document>(); // make sure we fire events when docs are added/removed/etc Gate.getCreoleRegister().addCreoleListener(this); }