View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.application.state.impl;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.xml.sax.InputSource;
29  
30  import com.cosylab.application.state.State;
31  import com.cosylab.application.state.StateStorage;
32  /**
33   * This class is an implementation of the <code>StateStorage</code> that uses a
34   * XML file to store the array of the <code>State</code> objects.
35   *
36   * Use the <code>csl.XMLStateStorage.useNew=true</code> system property to switch
37   * to the new implementation of the state storage (faster, with nicer output, uses SAX instead of DOM).
38   *  
39   * @author dvitas
40   * @author mkadunc
41   */
42  public class XMLStateStorage extends DefaultStateStorage {
43      private static final boolean USE_NEW="true".equalsIgnoreCase(System.getProperty("csl.XMLStateStorage.useNew"));
44      
45      /**
46       * DOCUMENT ME!
47       *
48       * @param is DOCUMENT ME!
49       *
50       * @return DOCUMENT ME!
51       *
52       * @throws IOException DOCUMENT ME!
53       */
54      public static final State[] loadStates(InputSource is) throws IOException
55      {
56          if (USE_NEW) return NewXMLStateStorage.loadStates(is);
57          else return OldXMLStateStorage.loadStates(is);
58      }
59  
60      /**
61       * DOCUMENT ME!
62       *
63       * @param outStates DOCUMENT ME!
64       * @param os DOCUMENT ME!
65       *
66       * @throws IOException DOCUMENT ME!
67       */
68      public static final void storeStates(State[] outStates, OutputStream os)
69      	throws IOException
70      {
71          if (USE_NEW) NewXMLStateStorage.storeStates(outStates, os);
72          else OldXMLStateStorage.storeStates(outStates, os);
73      }
74  
75      /**
76      		             *
77      		             */
78      private DefaultStateStorage ref=null;
79      public XMLStateStorage()
80      {
81      	super();
82      	ref=USE_NEW?new NewXMLStateStorage():new OldXMLStateStorage();
83      }
84  
85      /**
86       * Creates a new XMLStateStorage object.
87       *
88       * @param ss DOCUMENT ME!
89       */
90      public XMLStateStorage(StateStorage ss)
91      {
92      	super(ss);
93          ref=USE_NEW?new NewXMLStateStorage(ss):new OldXMLStateStorage(ss);
94      }
95  
96      public void add(State state) {
97          ref.add(state);
98      }
99  
100     public void addAll(List states) {
101         ref.addAll(states);
102     }
103 
104     public boolean contains(State st) {
105         return ref.contains(st);
106     }
107 
108     public boolean equals(Object obj) {
109         return ref.equals(obj);
110     }
111 
112     public List getStates() {
113         return ref.getStates();
114     }
115 
116     public int hashCode() {
117         return ref.hashCode();
118     }
119 
120     public Iterator iterator() {
121         return ref.iterator();
122     }
123 
124     public void load(InputStream stream) throws IOException {
125         ref.load(stream);
126     }
127 
128     public void load(String filePath, String applicationName)
129             throws IOException {
130         ref.load(filePath, applicationName);
131     }
132 
133     public void remove(State st) {
134         ref.remove(st);
135     }
136 
137     public void store(OutputStream stream) throws IOException {
138         ref.store(stream);
139     }
140 
141     public void store(String filePath, String applicationName)
142             throws IOException {
143         ref.store(filePath, applicationName);
144     }
145 
146     public String toString() {
147         return ref.toString();
148     }
149 }