Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/iicm/xpg/statemachine/XMLHandler.java


1   /***********************************************************************
2    * @(#)$RCSfile: XMLHandler.java,v $   $Revision: 1.9 $ $Date: 2002/05/21 16:59:30 $
3    *
4    * Copyright (c) 2002 stefan thalauer
5    * 
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License (LGPL)
8    * as published by the Free Software Foundation; either version 2.1 of
9    * the License, or (at your option) any later version.
10   * 
11   * This program 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 Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public 
17   * License along with this program; if not, write to the
18   * Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20   ***********************************************************************/
21  
22  package edu.iicm.xpg.statemachine;
23  
24  import org.xml.sax.Attributes;
25  import org.xml.sax.SAXParseException;
26  import org.xml.sax.SAXException;
27  import org.xml.sax.helpers.DefaultHandler;
28  
29  import edu.iicm.xpg.util.Debug;
30  
31  //----------------------------------------------------------------------
32  /**
33   * <p> 
34   * This class extends the DefaultHandler of the SAX2.0 API.
35   * It sends the XMLInputs to the statemachine. 
36   *
37   * @author Stefan Thalauer
38   * @version $revision$
39   */
40  
41  public class XMLHandler extends DefaultHandler
42  {
43    protected PrimitiveStateMachine state_machine_;
44    protected DataObject data_;
45  
46  
47  //----------------------------------------------------------------------
48  /**
49   * @param state_machine the statemachine which handles the parsed states
50   * @param data the DataObject for storing diverse data
51   * @exception IllegalArgumentException thrown if state_machine or data is null
52   */
53  
54    public XMLHandler(PrimitiveStateMachine state_machine,DataObject data)  
55      throws IllegalArgumentException
56    {
57      super();
58  
59      if (state_machine == null || data == null)
60      {
61        throw (new IllegalArgumentException
62          ("initialize XMLHandler with statemachine or data equal null!"));
63      }
64      state_machine_=state_machine;
65      data_=data;
66    }
67  
68  
69  //----------------------------------------------------------------------
70  /**
71   * @return
72   */
73  
74    public void startDocument()
75    {
76      state_machine_.initializeMachine();
77    }
78  
79  
80  //----------------------------------------------------------------------
81  /**
82   * @return
83   */
84  
85    public void endDocument()
86      throws SAXException
87    {
88      try
89      {
90        state_machine_.input(new XMLInput(Const.XML_END_DOCUMENT,null));
91      }
92      catch(TransitionException exc)
93      {
94        Exception exception = (Exception)exc.getException();
95        if ( exception != null)
96          throw new SAXException(exception);
97        else 
98          throw new SAXException(exc.getMessage());
99      }
100   }
101 
102 
103 //----------------------------------------------------------------------
104 /**
105  * @param chars the character array
106  * @param start start index of the char array to work with
107  * @param length number of chars in the char array to work with
108  * @return
109  */
110 
111   public void characters(char[] chars,int start,int length)
112     throws SAXException
113   {
114     try
115     {
116       if (length > 0)
117       {
118         data_.getXMLElement().setValue(new String(chars, start, length));
119       }
120       state_machine_.input(new XMLInput(Const.XML_CHARS,chars,start,length));
121     }
122     catch(TransitionException exc)
123     {
124       Exception exception = (Exception)exc.getException();
125       if ( exception != null)
126         throw new SAXException(exception);
127       else 
128         throw new SAXException(exc.getMessage());
129     }
130   }
131 
132 
133 //----------------------------------------------------------------------
134 /**
135  * @param uri element uri
136  * @param name element name
137  * @param qname ???
138  * @param attributes element attributes
139  * @return
140  */
141 
142   public void startElement(String uri,String name,String qname,Attributes attributes)
143     throws SAXException
144   {
145     try
146     {
147       XMLElement xml_element = new XMLElement();
148       xml_element.setName(name);
149       if (attributes != null)
150       {
151         xml_element.setAttributes(attributes);
152       }
153       data_.getXMLElementStack().push(xml_element);
154 
155       state_machine_.input(new XMLInput(Const.XML_START_TAG,name));
156     }
157     catch(TransitionException exc)
158     {
159       Exception exception = (Exception)exc.getException();
160       if ( exception != null)
161         throw new SAXException(exception);
162       else 
163         throw new SAXException(exc.getMessage());
164     }
165   }
166 
167 
168 //----------------------------------------------------------------------
169 /**
170  * @param uri element uri
171  * @param name element name
172  * @param qname ???
173  * @return
174  */
175 
176   public void endElement(String uri,String name,String qname)
177     throws SAXException
178   {
179     try
180     {
181       state_machine_.input(new XMLInput(Const.XML_END_TAG,name));
182       data_.getXMLElementStack().pop();
183     }
184     catch(TransitionException exc)
185     {
186       Exception exception = (Exception)exc.getException();
187       if ( exception != null)
188         throw new SAXException(exception);
189       else 
190         throw new SAXException(exc.getMessage());
191     }
192   }
193 
194 
195 //----------------------------------------------------------------------
196 /**
197  * @param ch The whitespace characters.
198  * @param start The start position in the character array.
199  * @param length The number of characters tcharacter array.
200  */
201 
202   public void ignorableWhitespace(char[] ch,int start,int length)
203   {
204   }
205 
206 
207 //----------------------------------------------------------------------
208 /**
209  * @param exc the error that occured
210  * @return
211  */
212 
213   public void error(SAXParseException exc)
214            throws SAXException
215 
216   {
217        fatalError(exc);
218   }
219 
220   
221 //----------------------------------------------------------------------
222 /**
223  * @param exc the error that occured
224  * @return
225  */
226 
227   public void fatalError(SAXParseException exc)
228            throws SAXException
229   {
230     if (Debug.DEBUG)
231       Debug.println(Debug.PARSER_LEVEL,"SAX Error: " 
232                     + ", line " + exc.getLineNumber()
233                     + "\n,   uri " + exc.getSystemId());
234     
235        System.err.println(" Sax Error:" + exc.getMessage());
236        throw ( new SAXException(exc));
237   }
238 
239 
240 //----------------------------------------------------------------------
241 /**
242  * @param exc the warning that occured
243  * @return
244  */
245 
246   public void warning(SAXParseException exc)
247   {
248     if (Debug.DEBUG)
249       Debug.println(Debug.PARSER_LEVEL,"SAX Warning: " 
250                     + ", line " + exc.getLineNumber()
251                     + "\n,   uri " + exc.getSystemId());
252     exc.printStackTrace();
253   }
254 }
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271