| CorpusPersistence.java |
1 /*
2 * Copyright (c) 1998-2004, The University of Sheffield.
3 *
4 * This file is part of GATE (see http://gate.ac.uk/), and is free
5 * software, licenced under the GNU Library General Public License,
6 * Version 2, June 1991 (in the distribution as file licence.html,
7 * and also available at http://gate.ac.uk/gate/licence.html).
8 *
9 * Valentin Tablan 26/10/2001
10 *
11 * $Id: CorpusPersistence.java,v 1.4 2004/07/21 17:10:09 akshay Exp $
12 *
13 */
14
15 package gate.util.persistence;
16
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20
21 import gate.Corpus;
22 import gate.creole.ResourceInstantiationException;
23 import gate.persist.PersistenceException;
24
25
26 public class CorpusPersistence extends LRPersistence {
27 /**
28 * Populates this Persistence with the data that needs to be stored from the
29 * original source object.
30 */
31 public void extractDataFromSource(Object source)throws PersistenceException{
32 //check input
33 if(! (source instanceof Corpus)){
34 throw new UnsupportedOperationException(
35 getClass().getName() + " can only be used for " +
36 Corpus.class.getName() +
37 " objects!\n" + source.getClass().getName() +
38 " is not a " + Corpus.class.getName());
39 }
40
41 Corpus corpus = (Corpus)source;
42 super.extractDataFromSource(source);
43 if(dsData == null){
44 //transient corpus; we still need to save the docs
45 docList = new ArrayList();
46 Iterator docIter = corpus.iterator();
47 while(docIter.hasNext()){
48 docList.add(PersistenceManager.
49 getPersistentRepresentation(docIter.next()));
50 }
51 }else{
52 //persistent corpus; it takes care of documents by itself
53 //nothing to do :)
54 docList = null;
55 }
56 }
57
58
59 /**
60 * Creates a new object from the data contained. This new object is supposed
61 * to be a copy for the original object used as source for data extraction.
62 */
63 public Object createObject()throws PersistenceException,
64 ResourceInstantiationException{
65 Corpus corpus = (Corpus)super.createObject();
66 if(docList != null){
67 //transient corpus; we need to recreate the docs
68 if(!docList.isEmpty() && corpus.isEmpty()){
69 Iterator docIter = docList.iterator();
70 while(docIter.hasNext()){
71 corpus.add(PersistenceManager.
72 getTransientRepresentation(docIter.next()));
73 }
74
75 }
76 }
77 return corpus;
78 }
79
80
81 protected ArrayList docList;
82 static final long serialVersionUID = 6181534551802883626L;
83 }