01 /*
02 * Copyright (c) 1995-2010, The University of Sheffield. See the file
03 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04 *
05 * This file is part of GATE (see http://gate.ac.uk/), and is free
06 * software, licenced under the GNU Library General Public License,
07 * Version 2, June 1991 (in the distribution as file licence.html,
08 * and also available at http://gate.ac.uk/gate/licence.html).
09 *
10 * Valentin Tablan 26/10/2001
11 *
12 * $Id: ControllerPersistence.java 12006 2009-12-01 17:24:28Z thomas_heitz $
13 *
14 */
15 package gate.util.persistence;
16
17 import java.util.*;
18
19 import gate.Controller;
20 import gate.creole.ResourceInstantiationException;
21 import gate.persist.PersistenceException;
22
23 public class ControllerPersistence extends ResourcePersistence {
24 /**
25 * Populates this Persistence with the data that needs to be stored from the
26 * original source object.
27 */
28 public void extractDataFromSource(Object source)throws PersistenceException{
29 if(! (source instanceof Controller)){
30 throw new UnsupportedOperationException(
31 getClass().getName() + " can only be used for " +
32 Controller.class.getName() +
33 " objects!\n" + source.getClass().getName() +
34 " is not a " + Controller.class.getName());
35 }
36 Controller controller = (Controller)source;
37
38 super.extractDataFromSource(source);
39 prList = new ArrayList(controller.getPRs().size());
40 Iterator prIter = controller.getPRs().iterator();
41
42 while(prIter.hasNext()){
43 ((List)prList).add(prIter.next());
44 }
45 prList = PersistenceManager.getPersistentRepresentation(prList);
46 }
47
48 /**
49 * Creates a new object from the data contained. This new object is supposed
50 * to be a copy for the original object used as source for data extraction.
51 */
52 public Object createObject()throws PersistenceException,
53 ResourceInstantiationException{
54
55 Controller controller = (Controller)super.createObject();
56
57 if(controller.getPRs().isEmpty()){
58 prList = PersistenceManager.getTransientRepresentation(prList);
59 controller.setPRs((Collection)prList);
60 }
61
62 return controller;
63 }
64
65 protected Object prList;
66 static final long serialVersionUID = 727852357092819439L;
67 }
|