root/trunk/src/java/org/jcoderz/phoenix/chart2d/Chart2DParser.java

Revision 1011, 14.9 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.phoenix.chart2d;
34
35
36import java.io.IOException;
37import java.io.InputStream;
38import java.net.URL;
39import java.util.Stack;
40
41import javax.xml.parsers.ParserConfigurationException;
42import javax.xml.parsers.SAXParserFactory;
43
44import org.xml.sax.Attributes;
45import org.xml.sax.ContentHandler;
46import org.xml.sax.EntityResolver;
47import org.xml.sax.ErrorHandler;
48import org.xml.sax.InputSource;
49import org.xml.sax.Locator;
50import org.xml.sax.SAXException;
51import org.xml.sax.SAXParseException;
52import org.xml.sax.XMLReader;
53import org.xml.sax.helpers.AttributesImpl;
54
55
56/**
57 * The class reads XML documents according to specified DTD and
58 * translates all related events into Chart2DHandler events.
59 * <p>
60 * Usage sample:
61 *
62 * <pre>
63 *         Chart2DParser parser = new Chart2DParser(...);
64 *         parser.parse(new InputSource(&quot;...&quot;));
65 * </pre>
66 *
67 * <p>
68 * <b>Warning:</b> the class is machine generated. DO NOT MODIFY
69 * </p>
70 */
71public class Chart2DParser implements ContentHandler
72{
73   private static final String UNEXPECTED_CHARACTERS_EVENT
74       = "Unexpected characters() event! (Missing DTD?)";
75   private final StringBuffer mBuffer;
76   private final Chart2DHandler mHandler;
77   private final Stack mContext;
78   private final EntityResolver mResolver;
79   private final EntityResolver mRootResolver;
80
81   /**
82    * Creates a parser instance.
83    * @param hdlr handler interface implementation (never
84    *        <code>null</code>
85    * @param rslvr SAX entity resolver implementation or
86    *        <code>null</code>. It is recommended that it could be
87    *        able to resolve at least the DTD.
88    */
89   public Chart2DParser (final Chart2DHandler hdlr, final EntityResolver rslvr)
90   {
91      mRootResolver = rslvr;
92
93      mResolver = new MyResolver();
94      mHandler = hdlr;
95      mBuffer = new StringBuffer();
96      mContext = new Stack();
97   }
98
99   /** {@inheritDoc} */
100   public final void setDocumentLocator (Locator locator)
101   {
102       // NOOP
103   }
104
105   /** {@inheritDoc} */
106   public final void startDocument () 
107         throws SAXException
108   {
109      // NOOP
110   }
111
112   /** {@inheritDoc} */
113   public final void endDocument () 
114         throws SAXException
115   {
116      // NOOP
117   }
118
119   /** {@inheritDoc} */
120   public final void startElement (String ns, String name, String qname, 
121         Attributes attrs) 
122         throws SAXException
123   {
124      dispatch(true);
125      mContext.push(new Object[] {qname, new AttributesImpl(attrs)});
126      if ("GraphLabelsLinesStyle".equals(name))
127      {
128         mHandler.handleGraphLabelsLinesStyle(attrs);
129      }
130      else if ("Category".equals(name))
131      {
132         mHandler.startCategory(attrs);
133      }
134      else if ("MultiColorsProperties".equals(name))
135      {
136         mHandler.startMultiColorsProperties(attrs);
137      }
138      else if ("LBChart2D".equals(name))
139      {
140         mHandler.startLBChart2D(attrs);
141      }
142      else if ("Dataset".equals(name))
143      {
144         mHandler.startDataset(attrs);
145      }
146      else if ("PieChart2DProperties".equals(name))
147      {
148         mHandler.handlePieChart2DProperties(attrs);
149      }
150      else if ("GraphChart2DProperties".equals(name))
151      {
152         mHandler.startGraphChart2DProperties(attrs);
153      }
154      else if ("Chart2D".equals(name))
155      {
156         mHandler.startChart2D(attrs);
157      }
158      else if ("LLChart2D".equals(name))
159      {
160         mHandler.startLLChart2D(attrs);
161      }
162      else if ("GraphNumbersLinesStyle".equals(name))
163      {
164         mHandler.handleGraphNumbersLinesStyle(attrs);
165      }
166      else if ("PieChart2D".equals(name))
167      {
168         mHandler.startPieChart2D(attrs);
169      }
170      else if ("Object2DProperties".equals(name))
171      {
172         mHandler.handleObject2DProperties(attrs);
173      }
174      else if ("WarningRegionProperties".equals(name))
175      {
176         mHandler.handleWarningRegionProperties(attrs);
177      }
178      else if ("Chart2DProperties".equals(name))
179      {
180         mHandler.handleChart2DProperties(attrs);
181      }
182      else if ("Set".equals(name))
183      {
184         mHandler.startSet(attrs);
185      }
186      else if ("GraphProperties".equals(name))
187      {
188         mHandler.startGraphProperties(attrs);
189      }
190      else if ("LegendProperties".equals(name))
191      {
192         mHandler.startLegendProperties(attrs);
193      }
194   }
195
196   /** {@inheritDoc} */
197   public final void endElement (String ns, String name, String qname) 
198         throws SAXException
199   {
200      dispatch(false);
201      mContext.pop();
202      if ("Category".equals(name))
203      {
204         mHandler.endCategory();
205      }
206      else if ("MultiColorsProperties".equals(name))
207      {
208         mHandler.endMultiColorsProperties();
209      }
210      else if ("LBChart2D".equals(name))
211      {
212         mHandler.endLBChart2D();
213      }
214      else if ("Dataset".equals(name))
215      {
216         mHandler.endDataset();
217      }
218      else if ("GraphChart2DProperties".equals(name))
219      {
220         mHandler.endGraphChart2DProperties();
221      }
222      else if ("Chart2D".equals(name))
223      {
224         mHandler.endChart2D();
225      }
226      else if ("LLChart2D".equals(name))
227      {
228         mHandler.endLLChart2D();
229      }
230      else if ("PieChart2D".equals(name))
231      {
232         mHandler.endPieChart2D();
233      }
234      else if ("Set".equals(name))
235      {
236         mHandler.endSet();
237      }
238      else if ("GraphProperties".equals(name))
239      {
240         mHandler.endGraphProperties();
241      }
242      else if ("LegendProperties".equals(name))
243      {
244         mHandler.endLegendProperties();
245      }
246   }
247
248   /** {@inheritDoc} */
249   public final void characters (char[] chars, int start, int len)
250         throws SAXException
251   {
252      mBuffer.append(chars, start, len);
253   }
254
255   /** {@inheritDoc} */
256   public final void ignorableWhitespace (char[] chars, int start, int len)
257         throws SAXException
258   {
259        // NOOP
260   }
261
262   /** {@inheritDoc} */
263   public final void processingInstruction (String target, String data) 
264         throws SAXException
265   {
266       // NOOP
267   }
268
269   /** {@inheritDoc} */
270   public final void startPrefixMapping (final String prefix, final String uri) 
271         throws SAXException
272   {
273       // NOOP
274   }
275
276   /** {@inheritDoc} */
277   public final void endPrefixMapping (final String prefix)
278         throws SAXException
279   {
280       // NOOP
281   }
282
283   /** {@inheritDoc} */
284   public final void skippedEntity (String name) 
285         throws SAXException
286   {
287       // NOOP
288   }
289
290   private void dispatch (final boolean fireOnlyIfMixed) 
291         throws SAXException
292   {
293      if (fireOnlyIfMixed && (mBuffer.length() == 0))
294      {
295         return; // skip it
296      }
297
298      final Object[] ctx = (Object[]) mContext.peek();
299      final String here = (String) ctx[0];
300      final Attributes attrs = (Attributes) ctx[1];
301      if ("LegendLabelsTexts".equals(here))
302      {
303         if (fireOnlyIfMixed)
304         {
305            throw new IllegalStateException(
306                  UNEXPECTED_CHARACTERS_EVENT);
307         }
308         mHandler.handleLegendLabelsTexts((mBuffer.length() == 0) 
309               ? null : mBuffer.toString(), attrs);
310      }
311      else if ("AxisLabelText".equals(here))
312      {
313         if (fireOnlyIfMixed)
314         {
315            throw new IllegalStateException(
316                  UNEXPECTED_CHARACTERS_EVENT);
317         }
318         mHandler.handleAxisLabelText((mBuffer.length() == 0) 
319               ? null : mBuffer.toString(), attrs);
320      }
321      else if ("Data".equals(here))
322      {
323         if (fireOnlyIfMixed)
324         {
325            throw new IllegalStateException(
326                  UNEXPECTED_CHARACTERS_EVENT);
327         }
328         mHandler.handleData((mBuffer.length() == 0) 
329               ? null : mBuffer.toString(), attrs);
330      }
331      else if ("ColorsCustom".equals(here))
332      {
333         if (fireOnlyIfMixed)
334         {
335            throw new IllegalStateException(
336                  UNEXPECTED_CHARACTERS_EVENT);
337         }
338         mHandler.handleColorsCustom((mBuffer.length() == 0) 
339               ? null : mBuffer.toString(), attrs);
340      }
341      else
342      {
343         // do not care
344      }
345      mBuffer.delete(0, mBuffer.length());
346   }
347
348   /**
349    * The recognizer entry method taking an InputSource.
350    * @param input InputSource to be parsed.
351    * @throws java.io.IOException on I/O error.
352    * @throws SAXException propagated exception thrown by a
353    *         DocumentHandler.
354    * @throws javax.xml.parsers.ParserConfigurationException a parser
355    *         satisfining requested configuration can not be created.
356    * @throws javax.xml.parsers.FactoryConfigurationError if the
357    *         implementation can not be instantiated.
358    */
359   public void parse (final InputSource input)
360         throws SAXException, ParserConfigurationException, IOException
361   {
362      parse(input, this);
363   }
364
365   /**
366    * The recognizer entry method taking a URL.
367    * @param url URL source to be parsed.
368    * @throws java.io.IOException on I/O error.
369    * @throws SAXException propagated exception thrown by a
370    *         DocumentHandler.
371    * @throws javax.xml.parsers.ParserConfigurationException a parser
372    *         satisfining requested configuration can not be created.
373    * @throws javax.xml.parsers.FactoryConfigurationError if the
374    *         implementation can not be instantiated.
375    */
376   public void parse (final java.net.URL url)
377         throws SAXException, ParserConfigurationException, IOException
378   {
379      parse(new InputSource(url.toExternalForm()), this);
380   }
381
382   /**
383    * The recognizer entry method taking an Inputsource.
384    * @param input InputSource to be parsed.
385    * @throws java.io.IOException on I/O error.
386    * @throws SAXException propagated exception thrown by a
387    *         DocumentHandler.
388    * @throws javax.xml.parsers.ParserConfigurationException a parser
389    *         satisfining requested configuration can not be created.
390    * @throws javax.xml.parsers.FactoryConfigurationError if the
391    *         implementation can not be instantiated.
392    */
393   public static void parse (final InputSource input,
394         final Chart2DHandler handler)
395         throws SAXException, ParserConfigurationException, IOException
396   {
397      parse(input, new Chart2DParser(handler, null));
398   }
399
400   /**
401    * The recognizer entry method taking a URL.
402    * @param url URL source to be parsed.
403    * @throws java.io.IOException on I/O error.
404    * @throws SAXException propagated exception thrown by a
405    *         DocumentHandler.
406    * @throws javax.xml.parsers.ParserConfigurationException a parser
407    *         satisfining requested configuration can not be created.
408    * @throws javax.xml.parsers.FactoryConfigurationError if the
409    *         implementation can not be instantiated.
410    */
411   public static void parse (final URL url, final Chart2DHandler handler)
412         throws SAXException, ParserConfigurationException, IOException
413   {
414      parse(new InputSource(url.toExternalForm()), handler);
415   }
416
417   private static void parse (final InputSource input,
418         final Chart2DParser recognizer)
419         throws SAXException, ParserConfigurationException, IOException
420   {
421      final SAXParserFactory factory = SAXParserFactory.newInstance();
422      factory.setValidating(true); // the code was generated according
423      // DTD
424      factory.setNamespaceAware(true); // the code was generated
425      // according DTD
426      final XMLReader parser = factory.newSAXParser().getXMLReader();
427      parser.setContentHandler(recognizer);
428      parser.setErrorHandler(recognizer.getDefaultErrorHandler());
429      if (recognizer.mResolver != null)
430      {
431         parser.setEntityResolver(recognizer.mResolver);
432      }
433      parser.parse(input);
434   }
435
436   /**
437    * Creates default error handler used by this parser.
438    * @return org.xml.sax.ErrorHandler implementation
439    */
440   protected ErrorHandler getDefaultErrorHandler ()
441   {
442      return new ErrorHandler()
443      {
444         public void error (SAXParseException ex) 
445               throws SAXException
446         {
447            if (mContext.isEmpty())
448            {
449               System.err.println("Missing DOCTYPE.");
450            }
451            throw ex;
452         }
453
454         public void fatalError (SAXParseException ex) 
455               throws SAXException
456         {
457            throw ex;
458         }
459
460         public void warning (SAXParseException ex) 
461               throws SAXException
462         {
463            // ignore
464         }
465      };
466   }
467
468
469   class MyResolver implements EntityResolver
470   {
471      public InputSource resolveEntity (String publicId, String systemId)
472            throws SAXException, IOException
473      {
474         InputSource result = null;
475         if ((systemId != null && systemId.equals("chart2d.dtd"))
476               || (publicId != null && publicId.equals("chart2d.dtd"))
477               || (systemId != null && systemId
478                     .equals("-//The jCoderZ Project//Chart2D//EN"))
479               || (publicId != null && publicId
480                     .equals("-//The jCoderZ Project//Chart2D//EN")))
481         {
482            // return a special input source
483            final InputStream s
484                = getClass().getResourceAsStream("chart2d.dtd");
485            if (s != null)
486            {
487               result = new InputSource(s);
488            }
489         }
490         else
491         {
492            // use the default behaviour
493            if (mRootResolver != null)
494            {
495               result = mRootResolver.resolveEntity(publicId, systemId);
496            }
497         }
498         return result;
499      }
500   }
501}
Note: See TracBrowser for help on using the browser.