| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.chart2d; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.io.IOException; |
| 37 | | | import java.io.InputStream; |
| 38 | | | import java.net.URL; |
| 39 | | | import java.util.Stack; |
| 40 | | | |
| 41 | | | import javax.xml.parsers.ParserConfigurationException; |
| 42 | | | import javax.xml.parsers.SAXParserFactory; |
| 43 | | | |
| 44 | | | import org.xml.sax.Attributes; |
| 45 | | | import org.xml.sax.ContentHandler; |
| 46 | | | import org.xml.sax.EntityResolver; |
| 47 | | | import org.xml.sax.ErrorHandler; |
| 48 | | | import org.xml.sax.InputSource; |
| 49 | | | import org.xml.sax.Locator; |
| 50 | | | import org.xml.sax.SAXException; |
| 51 | | | import org.xml.sax.SAXParseException; |
| 52 | | | import org.xml.sax.XMLReader; |
| 53 | | | import org.xml.sax.helpers.AttributesImpl; |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | | <p> |
| 60 | | | |
| 61 | | | |
| 62 | | | <pre> |
| 63 | | | |
| 64 | | | |
| 65 | | | </pre> |
| 66 | | | |
| 67 | | | <p> |
| 68 | | | <b></b> |
| 69 | | | </p> |
| 70 | | | |
| 71 | | (1) | public 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 | | | |
| 83 | | | @param |
| 84 | | | <code></code> |
| 85 | | | @param |
| 86 | | | <code></code> |
| 87 | | | |
| 88 | | | |
| 89 | | | public Chart2DParser (final Chart2DHandler hdlr, final EntityResolver rslvr) |
| 90 | 0 | | { |
| 91 | 0 | | mRootResolver = rslvr; |
| 92 | | | |
| 93 | 0 | | mResolver = new MyResolver(); |
| 94 | 0 | | mHandler = hdlr; |
| 95 | 0 | | mBuffer = new StringBuffer(); |
| 96 | 0 | | mContext = new Stack(); |
| 97 | 0 | | } |
| 98 | | | |
| 99 | | | {@inheritDoc} |
| 100 | | | public final void setDocumentLocator (Locator locator) |
| 101 | | | { |
| 102 | | | |
| 103 | 0 | | } |
| 104 | | | |
| 105 | | | {@inheritDoc} |
| 106 | | | public final void startDocument () |
| 107 | | | throws SAXException |
| 108 | | | { |
| 109 | | | |
| 110 | 0 | | } |
| 111 | | | |
| 112 | | | {@inheritDoc} |
| 113 | | | public final void endDocument () |
| 114 | | | throws SAXException |
| 115 | | | { |
| 116 | | | |
| 117 | 0 | | } |
| 118 | | | |
| 119 | | | {@inheritDoc} |
| 120 | | (2) | public final void startElement (String ns, String name, String qname, |
| 121 | | | Attributes attrs) |
| 122 | | | throws SAXException |
| 123 | | | { |
| 124 | 0 | | dispatch(true); |
| 125 | 0 | | mContext.push(new Object[] {qname, new AttributesImpl(attrs)}); |
| 126 | 0 | | if ("GraphLabelsLinesStyle".equals(name)) |
| 127 | | | { |
| 128 | 0 | | mHandler.handleGraphLabelsLinesStyle(attrs); |
| 129 | | | } |
| 130 | 0 | | else if ("Category".equals(name)) |
| 131 | | | { |
| 132 | 0 | | mHandler.startCategory(attrs); |
| 133 | | | } |
| 134 | 0 | | else if ("MultiColorsProperties".equals(name)) |
| 135 | | | { |
| 136 | 0 | | mHandler.startMultiColorsProperties(attrs); |
| 137 | | | } |
| 138 | 0 | | else if ("LBChart2D".equals(name)) |
| 139 | | | { |
| 140 | 0 | | mHandler.startLBChart2D(attrs); |
| 141 | | | } |
| 142 | 0 | | else if ("Dataset".equals(name)) |
| 143 | | | { |
| 144 | 0 | | mHandler.startDataset(attrs); |
| 145 | | | } |
| 146 | 0 | | else if ("PieChart2DProperties".equals(name)) |
| 147 | | | { |
| 148 | 0 | | mHandler.handlePieChart2DProperties(attrs); |
| 149 | | | } |
| 150 | 0 | | else if ("GraphChart2DProperties".equals(name)) |
| 151 | | | { |
| 152 | 0 | | mHandler.startGraphChart2DProperties(attrs); |
| 153 | | | } |
| 154 | 0 | | else if ("Chart2D".equals(name)) |
| 155 | | | { |
| 156 | 0 | | mHandler.startChart2D(attrs); |
| 157 | | | } |
| 158 | 0 | | else if ("LLChart2D".equals(name)) |
| 159 | | | { |
| 160 | 0 | | mHandler.startLLChart2D(attrs); |
| 161 | | | } |
| 162 | 0 | | else if ("GraphNumbersLinesStyle".equals(name)) |
| 163 | | | { |
| 164 | 0 | | mHandler.handleGraphNumbersLinesStyle(attrs); |
| 165 | | | } |
| 166 | 0 | | else if ("PieChart2D".equals(name)) |
| 167 | | | { |
| 168 | 0 | | mHandler.startPieChart2D(attrs); |
| 169 | | | } |
| 170 | 0 | | else if ("Object2DProperties".equals(name)) |
| 171 | | | { |
| 172 | 0 | | mHandler.handleObject2DProperties(attrs); |
| 173 | | | } |
| 174 | 0 | | else if ("WarningRegionProperties".equals(name)) |
| 175 | | | { |
| 176 | 0 | | mHandler.handleWarningRegionProperties(attrs); |
| 177 | | | } |
| 178 | 0 | | else if ("Chart2DProperties".equals(name)) |
| 179 | | | { |
| 180 | 0 | | mHandler.handleChart2DProperties(attrs); |
| 181 | | | } |
| 182 | 0 | | else if ("Set".equals(name)) |
| 183 | | | { |
| 184 | 0 | | mHandler.startSet(attrs); |
| 185 | | | } |
| 186 | 0 | | else if ("GraphProperties".equals(name)) |
| 187 | | | { |
| 188 | 0 | | mHandler.startGraphProperties(attrs); |
| 189 | | | } |
| 190 | 0 | | else if ("LegendProperties".equals(name)) |
| 191 | | | { |
| 192 | 0 | | mHandler.startLegendProperties(attrs); |
| 193 | | | } |
| 194 | 0 | | } |
| 195 | | | |
| 196 | | | {@inheritDoc} |
| 197 | | | public final void endElement (String ns, String name, String qname) |
| 198 | | | throws SAXException |
| 199 | | | { |
| 200 | 0 | | dispatch(false); |
| 201 | 0 | | mContext.pop(); |
| 202 | 0 | | if ("Category".equals(name)) |
| 203 | | | { |
| 204 | 0 | | mHandler.endCategory(); |
| 205 | | | } |
| 206 | 0 | | else if ("MultiColorsProperties".equals(name)) |
| 207 | | | { |
| 208 | 0 | | mHandler.endMultiColorsProperties(); |
| 209 | | | } |
| 210 | 0 | | else if ("LBChart2D".equals(name)) |
| 211 | | | { |
| 212 | 0 | | mHandler.endLBChart2D(); |
| 213 | | | } |
| 214 | 0 | | else if ("Dataset".equals(name)) |
| 215 | | | { |
| 216 | 0 | | mHandler.endDataset(); |
| 217 | | | } |
| 218 | 0 | | else if ("GraphChart2DProperties".equals(name)) |
| 219 | | | { |
| 220 | 0 | | mHandler.endGraphChart2DProperties(); |
| 221 | | | } |
| 222 | 0 | | else if ("Chart2D".equals(name)) |
| 223 | | | { |
| 224 | 0 | | mHandler.endChart2D(); |
| 225 | | | } |
| 226 | 0 | | else if ("LLChart2D".equals(name)) |
| 227 | | | { |
| 228 | 0 | | mHandler.endLLChart2D(); |
| 229 | | | } |
| 230 | 0 | | else if ("PieChart2D".equals(name)) |
| 231 | | | { |
| 232 | 0 | | mHandler.endPieChart2D(); |
| 233 | | | } |
| 234 | 0 | | else if ("Set".equals(name)) |
| 235 | | | { |
| 236 | 0 | | mHandler.endSet(); |
| 237 | | | } |
| 238 | 0 | | else if ("GraphProperties".equals(name)) |
| 239 | | | { |
| 240 | 0 | | mHandler.endGraphProperties(); |
| 241 | | | } |
| 242 | 0 | | else if ("LegendProperties".equals(name)) |
| 243 | | | { |
| 244 | 0 | | mHandler.endLegendProperties(); |
| 245 | | | } |
| 246 | 0 | | } |
| 247 | | | |
| 248 | | | {@inheritDoc} |
| 249 | | | public final void characters (char[] chars, int start, int len) |
| 250 | | | throws SAXException |
| 251 | | | { |
| 252 | 0 | | mBuffer.append(chars, start, len); |
| 253 | 0 | | } |
| 254 | | | |
| 255 | | | {@inheritDoc} |
| 256 | | | public final void ignorableWhitespace (char[] chars, int start, int len) |
| 257 | | | throws SAXException |
| 258 | | | { |
| 259 | | | |
| 260 | 0 | | } |
| 261 | | | |
| 262 | | | {@inheritDoc} |
| 263 | | | public final void processingInstruction (String target, String data) |
| 264 | | | throws SAXException |
| 265 | | | { |
| 266 | | | |
| 267 | 0 | | } |
| 268 | | | |
| 269 | | | {@inheritDoc} |
| 270 | | | public final void startPrefixMapping (final String prefix, final String uri) |
| 271 | | | throws SAXException |
| 272 | | | { |
| 273 | | | |
| 274 | 0 | | } |
| 275 | | | |
| 276 | | | {@inheritDoc} |
| 277 | | | public final void endPrefixMapping (final String prefix) |
| 278 | | | throws SAXException |
| 279 | | | { |
| 280 | | | |
| 281 | 0 | | } |
| 282 | | | |
| 283 | | | {@inheritDoc} |
| 284 | | | public final void skippedEntity (String name) |
| 285 | | | throws SAXException |
| 286 | | | { |
| 287 | | | |
| 288 | 0 | | } |
| 289 | | | |
| 290 | | (3) | private void dispatch (final boolean fireOnlyIfMixed) |
| 291 | | | throws SAXException |
| 292 | | | { |
| 293 | 0 | | if (fireOnlyIfMixed && (mBuffer.length() == 0)) |
| 294 | | | { |
| 295 | 0 | | return; |
| 296 | | | } |
| 297 | | | |
| 298 | 0 | | final Object[] ctx = (Object[]) mContext.peek(); |
| 299 | 0 | | final String here = (String) ctx[0]; |
| 300 | 0 | (4) | final Attributes attrs = (Attributes) ctx[1]; |
| 301 | 0 | | if ("LegendLabelsTexts".equals(here)) |
| 302 | | | { |
| 303 | 0 | | if (fireOnlyIfMixed) |
| 304 | | | { |
| 305 | 0 | (5) | throw new IllegalStateException( |
| 306 | | | UNEXPECTED_CHARACTERS_EVENT); |
| 307 | | | } |
| 308 | 0 | | mHandler.handleLegendLabelsTexts((mBuffer.length() == 0) |
| 309 | | | ? null : mBuffer.toString(), attrs); |
| 310 | | | } |
| 311 | 0 | | else if ("AxisLabelText".equals(here)) |
| 312 | | | { |
| 313 | 0 | | if (fireOnlyIfMixed) |
| 314 | | | { |
| 315 | 0 | (6) | throw new IllegalStateException( |
| 316 | | | UNEXPECTED_CHARACTERS_EVENT); |
| 317 | | | } |
| 318 | 0 | | mHandler.handleAxisLabelText((mBuffer.length() == 0) |
| 319 | | | ? null : mBuffer.toString(), attrs); |
| 320 | | | } |
| 321 | 0 | | else if ("Data".equals(here)) |
| 322 | | | { |
| 323 | 0 | | if (fireOnlyIfMixed) |
| 324 | | | { |
| 325 | 0 | (7) | throw new IllegalStateException( |
| 326 | | | UNEXPECTED_CHARACTERS_EVENT); |
| 327 | | | } |
| 328 | 0 | | mHandler.handleData((mBuffer.length() == 0) |
| 329 | | | ? null : mBuffer.toString(), attrs); |
| 330 | | | } |
| 331 | 0 | | else if ("ColorsCustom".equals(here)) |
| 332 | | | { |
| 333 | 0 | | if (fireOnlyIfMixed) |
| 334 | | | { |
| 335 | 0 | (8) | throw new IllegalStateException( |
| 336 | | | UNEXPECTED_CHARACTERS_EVENT); |
| 337 | | | } |
| 338 | 0 | | mHandler.handleColorsCustom((mBuffer.length() == 0) |
| 339 | | | ? null : mBuffer.toString(), attrs); |
| 340 | | | } |
| 341 | | | else |
| 342 | | | { |
| 343 | | | |
| 344 | | | } |
| 345 | 0 | | mBuffer.delete(0, mBuffer.length()); |
| 346 | 0 | | } |
| 347 | | | |
| 348 | | | |
| 349 | | | |
| 350 | | | @param |
| 351 | | | @throws |
| 352 | | | @throws |
| 353 | | | |
| 354 | | | @throws |
| 355 | | | |
| 356 | | | @throws |
| 357 | | | |
| 358 | | | |
| 359 | | | public void parse (final InputSource input) |
| 360 | | | throws SAXException, ParserConfigurationException, IOException |
| 361 | | | { |
| 362 | 0 | | parse(input, this); |
| 363 | 0 | | } |
| 364 | | | |
| 365 | | | |
| 366 | | | |
| 367 | | | @param |
| 368 | | | @throws |
| 369 | | | @throws |
| 370 | | | |
| 371 | | | @throws |
| 372 | | | |
| 373 | | | @throws |
| 374 | | | |
| 375 | | | |
| 376 | | | public void parse (final java.net.URL url) |
| 377 | | | throws SAXException, ParserConfigurationException, IOException |
| 378 | | | { |
| 379 | 0 | | parse(new InputSource(url.toExternalForm()), this); |
| 380 | 0 | | } |
| 381 | | | |
| 382 | | | |
| 383 | | | |
| 384 | | | @param |
| 385 | | | @throws |
| 386 | | | @throws |
| 387 | | | |
| 388 | | | @throws |
| 389 | | | |
| 390 | | | @throws |
| 391 | | | |
| 392 | | | |
| 393 | | | public static void parse (final InputSource input, |
| 394 | | (9) | final Chart2DHandler handler) |
| 395 | | | throws SAXException, ParserConfigurationException, IOException |
| 396 | | | { |
| 397 | 0 | (10) | parse(input, new Chart2DParser(handler, null)); |
| 398 | 0 | | } |
| 399 | | | |
| 400 | | | |
| 401 | | | |
| 402 | | | @param |
| 403 | | | @throws |
| 404 | | | @throws |
| 405 | | | |
| 406 | | | @throws |
| 407 | | | |
| 408 | | | @throws |
| 409 | | | |
| 410 | | | |
| 411 | | (11) | public static void parse (final URL url, final Chart2DHandler handler) |
| 412 | | | throws SAXException, ParserConfigurationException, IOException |
| 413 | | | { |
| 414 | 0 | | parse(new InputSource(url.toExternalForm()), handler); |
| 415 | 0 | | } |
| 416 | | | |
| 417 | | | private static void parse (final InputSource input, |
| 418 | | | final Chart2DParser recognizer) |
| 419 | | | throws SAXException, ParserConfigurationException, IOException |
| 420 | | | { |
| 421 | 0 | | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 422 | 0 | | factory.setValidating(true); |
| 423 | | | |
| 424 | 0 | | factory.setNamespaceAware(true); |
| 425 | | | |
| 426 | 0 | | final XMLReader parser = factory.newSAXParser().getXMLReader(); |
| 427 | 0 | | parser.setContentHandler(recognizer); |
| 428 | 0 | | parser.setErrorHandler(recognizer.getDefaultErrorHandler()); |
| 429 | 0 | | if (recognizer.mResolver != null) |
| 430 | | | { |
| 431 | 0 | | parser.setEntityResolver(recognizer.mResolver); |
| 432 | | | } |
| 433 | 0 | | parser.parse(input); |
| 434 | 0 | | } |
| 435 | | | |
| 436 | | | |
| 437 | | | |
| 438 | | | @return |
| 439 | | | |
| 440 | | | protected ErrorHandler getDefaultErrorHandler () |
| 441 | | | { |
| 442 | 0 | (12) | return new ErrorHandler() |
| 443 | 0 | | { |
| 444 | | | public void error (SAXParseException ex) |
| 445 | | | throws SAXException |
| 446 | | | { |
| 447 | 0 | | if (mContext.isEmpty()) |
| 448 | | | { |
| 449 | 0 | (13) | System.err.println("Missing DOCTYPE."); |
| 450 | | | } |
| 451 | 0 | | throw ex; |
| 452 | | | } |
| 453 | | | |
| 454 | | | public void fatalError (SAXParseException ex) |
| 455 | | | throws SAXException |
| 456 | | | { |
| 457 | 0 | | throw ex; |
| 458 | | | } |
| 459 | | | |
| 460 | | | public void warning (SAXParseException ex) |
| 461 | | | throws SAXException |
| 462 | | | { |
| 463 | | | |
| 464 | 0 | | } |
| 465 | | | }; |
| 466 | | | } |
| 467 | | | |
| 468 | | | |
| 469 | 0 | | class MyResolver implements EntityResolver |
| 470 | | | { |
| 471 | | | public InputSource resolveEntity (String publicId, String systemId) |
| 472 | | | throws SAXException, IOException |
| 473 | | | { |
| 474 | 0 | | InputSource result = null; |
| 475 | 0 | (14) | 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 | | | |
| 483 | 0 | | final InputStream s |
| 484 | | | = getClass().getResourceAsStream("chart2d.dtd"); |
| 485 | 0 | | if (s != null) |
| 486 | | | { |
| 487 | 0 | | result = new InputSource(s); |
| 488 | | | } |
| 489 | 0 | | } |
| 490 | | | else |
| 491 | | | { |
| 492 | | | |
| 493 | 0 | | if (mRootResolver != null) |
| 494 | | | { |
| 495 | 0 | | result = mRootResolver.resolveEntity(publicId, systemId); |
| 496 | | | } |
| 497 | | | } |
| 498 | 0 | | return result; |
| 499 | | | } |
| 500 | | | } |
| 501 | | | } |