1
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
31 public void extractDataFromSource(Object source)throws PersistenceException{
32 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 docList = new ArrayList();
46 Iterator docIter = corpus.iterator();
47 while(docIter.hasNext()){
48 docList.add(PersistenceManager.
49 getPersistentRepresentation(docIter.next()));
50 }
51 }else{
52 docList = null;
55 }
56 }
57
58
59
63 public Object createObject()throws PersistenceException,
64 ResourceInstantiationException{
65 Corpus corpus = (Corpus)super.createObject();
66 if(docList != null){
67 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 }