001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 26/10/2001
011 *
012 * $Id: CollectionPersistence.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 *
014 */
015 package gate.util.persistence;
016
017 import java.util.*;
018
019 import gate.creole.ResourceInstantiationException;
020 import gate.persist.PersistenceException;
021 import gate.util.Err;
022
023
024 public class CollectionPersistence implements Persistence {
025
026 /**
027 * Populates this Persistence with the data that needs to be stored from the
028 * original source object.
029 */
030 public void extractDataFromSource(Object source)throws PersistenceException{
031 if(! (source instanceof Collection)){
032 throw new UnsupportedOperationException(
033 getClass().getName() + " can only be used for " +
034 Collection.class.getName() +
035 " objects!\n" + source.getClass().getName() +
036 " is not a " + Collection.class.getName());
037 }
038 collectionType = source.getClass();
039
040 Collection coll = (Collection)source;
041
042 //get the values in the iterator's order
043 localList = new ArrayList(coll.size());
044 Iterator elemIter = coll.iterator();
045 while(elemIter.hasNext()){
046 localList.add(PersistenceManager.
047 getPersistentRepresentation(elemIter.next()));
048 }
049 }
050
051 /**
052 * Creates a new object from the data contained. This new object is supposed
053 * to be a copy for the original object used as source for data extraction.
054 */
055 public Object createObject()throws PersistenceException,
056 ResourceInstantiationException{
057 List<String> exceptionsOccurred = new ArrayList<String>();
058 //let's try to create a collection of the same type as the original
059 Collection result = null;
060 try{
061 result = (Collection)collectionType.newInstance();
062 }catch(Exception e){
063 // ignore - if we can't create a collection of the original type
064 // for any reason, just create an ArrayList as a fallback. The
065 // main use for this class is to persist parameter values for
066 // GATE resources, and GATE can convert an ArrayList to any type
067 // required by a resource parameter.
068 }
069 if(result == null) result = new ArrayList(localList.size());
070
071 //now we have the collection let's populate it
072 for(Object local : localList) {
073 try {
074 result.add(PersistenceManager.getTransientRepresentation(local));
075 }
076 catch(PersistenceException pe) {
077 exceptionsOccurred.add(pe.getMessage());
078 pe.printStackTrace(Err.getPrintWriter());
079 }
080 catch(ResourceInstantiationException rie) {
081 exceptionsOccurred.add(rie.getMessage());
082 rie.printStackTrace(Err.getPrintWriter());
083 }
084 }
085
086 if(exceptionsOccurred.size() > 0) {
087 throw new PersistenceException("Some resources cannot be restored:\n" +
088 Arrays.toString(exceptionsOccurred
089 .toArray(new String[exceptionsOccurred.size()]))
090 .replaceAll("[\\]\\[]", "").replaceAll(", ", "\n"));
091 }
092
093 return result;
094 }
095
096
097 protected List localList;
098 protected Class collectionType;
099 static final long serialVersionUID = 7908364068699089834L;
100 }
|