Debian Codegraph @

cds/savot/stax/SavotStaxParser.java

raw symbols SCIP
1 package cds.savot.stax;
2
3 //Copyright 2002-2014 - UDS/CNRS
4 //The SAVOT library is distributed under the terms
5 //of the GNU General Public License version 3.
6 //
7 //This file is part of SAVOT.
8 //
9 //SAVOT is free software: you can redistribute it and/or modify
10 //it under the terms of the GNU General Public License as published by
11 //the Free Software Foundation, version 3 of the License.
12 //
13 //SAVOT is distributed in the hope that it will be useful,
14 //but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 //GNU General Public License for more details.
17 //
18 //The GNU General Public License is available in COPYING file
19 //along with SAVOT.
20 //
21 //SAVOT - Simple Access to VOTable - Parser
22 //
23 //Author, Co-Author: Andre Schaaff (CDS), Laurent Bourges (JMMC)
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URL;
28 import java.util.ArrayList;
29
30 // Stax parser
31 import javax.xml.stream.XMLInputFactory;
32 import javax.xml.stream.XMLStreamReader;
33 import javax.xml.stream.events.XMLEvent;
34
35 // VOTable internal data model
36 import cds.savot.common.Markups;
37 import cds.savot.common.SavotStatistics;
38 import cds.savot.common.VOTableTag;
39 import cds.savot.model.SavotBinary;
40 import cds.savot.model.SavotBinary2;
41 import cds.savot.model.SavotCoosys;
42 import cds.savot.model.SavotData;
43 import cds.savot.model.SavotDefinitions;
44 import cds.savot.model.SavotField;
45 import cds.savot.model.SavotFieldRef;
46 import cds.savot.model.SavotFits;
47 import cds.savot.model.SavotGroup;
48 import cds.savot.model.SavotInfo;
49 import cds.savot.model.SavotLink;
50 import cds.savot.model.SavotMax;
51 import cds.savot.model.SavotMin;
52 import cds.savot.model.SavotOption;
53 import cds.savot.model.SavotParam;
54 import cds.savot.model.SavotParamRef;
55 import cds.savot.model.SavotResource;
56 import cds.savot.model.SavotStream;
57 import cds.savot.model.SavotTD;
58 import cds.savot.model.SavotTR;
59 import cds.savot.model.SavotTable;
60 import cds.savot.model.SavotTableData;
61 import cds.savot.model.SavotVOTable;
62 import cds.savot.model.SavotValues;
63 import cds.savot.model.TDSet;
64 import java.io.Reader;
65 import java.util.HashMap;
66 import java.util.Map;
67 import java.util.logging.Level;
68 import java.util.zip.GZIPInputStream;
69 import javax.xml.stream.FactoryConfigurationError;
70 import javax.xml.stream.XMLStreamException;
71
72 /**
73 * <p>
74 * It has been tested with Java 1.6 Stax implementation
75 * but it is possible to use other Stax implementations
76 * </p>
77 * <p>
78 * Designed to use with JSR-173 compliant (Streaming API for XML)
79 * </p>
80 *
81 * <p> remark L. Bourgès : equalsIgnoreCase() vs() equals as XML is case sensitive and VOTable specification says that clearly</p>
82 *
83 * @author Andre Schaaff
84 */
85 @SuppressWarnings({"deprecation", "UseOfSystemOutOrSystemErr"})
86 public final class SavotStaxParser implements Markups {
87
88 /** Logger associated to SavotStaxParser classes */
89 private final static java.util.logging.Logger logger = java.util.logging.Logger.getLogger(SavotStaxParser.class.getName());
90 /** flag to enable / disable usage of String.trim() on every value (disabled by default) */
91 private static final boolean doTrimValues = false;
92 /** flag to enable / disable line numbers (TR) */
93 private static final boolean doLineInfo = false;
94 /** flag to enable / disable statistics */
95 private static final boolean doStats = false;
96
97 /* Parsing modes */
98 /** FULL parsing mode: deprecated and replaced by FULLREAD */
99 public static final int FULL = 0;
100 /** FULLREAD parsing mode: all in memory */
101 public static final int FULLREAD = 0;
102 /** SEQUENTIAL parsing mode: deprecated and replaced by RESOURCEREAD */
103 public static final int SEQUENTIAL = 1;
104 /** RESOURCEREAD parsing mode: resource per resource reading */
105 public static final int RESOURCEREAD = 1;
106 /** ROWREAD parsing mode: row per row reading */
107 public static final int ROWREAD = 2;
108 /** default stack capacity = 4 slots */
109 public final static int DEFAULT_STACK_CAPACITY = 4;
110 /** empty TD instance */
111 private final static SavotTD EMPTY_TD = new SavotTD();
112 /** shared XMLInputFactory instance */
113 private static volatile XMLInputFactory XML_INPUT_FACTORY = null;
114
115 /* members */
116 /** statistics dedicated to this parser */
117 private final SavotStatistics statistics;
118 /** xml stream parser needed for sequential parsing */
119 private XMLStreamReader xmlParser = null;
120 /** input stream used to close it anyway */
121 private InputStream inputStream = null;
122 /** reader used to close it anyway */
123 private Reader reader = null;
124 /** debug mode */
125 private boolean debugMode = false;
126
127 // data model objects
128 private SavotVOTable _currentVOTable = new SavotVOTable();
129 private SavotResource _currentResource = new SavotResource(); // RESOURCEREAD mode only
130 private SavotTR _currentTR = new SavotTR(); // ROWREAD mode only
131
132 // used for statistics
133 private int resourceCounter = 0;
134 private int tableCounter = 0;
135 private int rowCounter = 0;
136 private int dataCounter = 0;
137
138 /** used for recursive management */
139 private final ArrayList<VOTableTag> fatherTags = new ArrayList<VOTableTag>(DEFAULT_STACK_CAPACITY);
140 /** used for recursive resources, LIFO mode */
141 private final ArrayList<SavotResource> resourcestack = new ArrayList<SavotResource>(DEFAULT_STACK_CAPACITY);
142 /** used for recursive options, LIFO mode */
143 private final ArrayList<SavotOption> optionstack = new ArrayList<SavotOption>(DEFAULT_STACK_CAPACITY);
144 /** used for recursives groups, LIFO mode */
145 private final ArrayList<SavotGroup> groupstack = new ArrayList<SavotGroup>(DEFAULT_STACK_CAPACITY);
146
147 // for multi level resource
148 private int includedResource = 0;
149 // for multi level option
150 private int includedOption = 0;
151 // for multi level group
152 private int includedGroup = 0;
153
154 private SavotTable currentTable = null;
155 private SavotField currentField = null;
156 private SavotFieldRef currentFieldRef = null;
157 private SavotGroup currentGroup = null; // new since VOTable 1.1
158 private SavotParam currentParam = null;
159 private SavotParamRef currentParamRef = null;
160 private SavotData currentData = null;
161 private SavotValues currentValues = null;
162 private SavotTableData currentTableData = null;
163 private String currentDescription = null;
164 private SavotLink currentLink = null;
165 private SavotInfo currentInfo = null;
166 private SavotMin currentMin = null;
167 private SavotMax currentMax = null;
168 private SavotOption currentOption = null;
169 private SavotCoosys currentCoosys = null;
170 private SavotDefinitions currentDefinitions = null;
171 private SavotBinary currentBinary = null;
172 private SavotBinary2 currentBinary2 = null;
173 private SavotFits currentFits = null;
174 private SavotStream currentStream = null;
175
176 /**
177 * Map containing object references which have an ID
178 * So it is possible to retrieve such object reference
179 * Used to resolve ID ref
180 * TODO: move such mapping into SavotVOTable (root element)
181 */
182 private final Map<String, Object> idRefLinks = new HashMap<String, Object>(256);
183
184 /**
185 * @return XMLInputFactory instance
186 * @throws FactoryConfigurationError if the factory can not be loaded
187 * @throws IllegalArgumentException if the property "javax.xml.stream.isCoalescing" is not supported
188 */
189 private static synchronized XMLInputFactory getXMLInputFactory() throws FactoryConfigurationError, IllegalArgumentException {
190 if (XML_INPUT_FACTORY == null) {
191 final XMLInputFactory xmlif = XMLInputFactory.newInstance();
192 xmlif.setProperty("javax.xml.stream.isCoalescing", true);
193 XML_INPUT_FACTORY = xmlif;
194 }
195 return XML_INPUT_FACTORY;
196 }
197
198 /**
199 * Constructor
200 *
201 * @param file
202 * a file to parse
203 * @param mode
204 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
205 * ROWREAD (per ROW, for small memory size applications)
206 * @throws IOException
207 * @throws javax.xml.stream.XMLStreamException
208 */
209 public SavotStaxParser(final String file, final int mode) throws IOException, XMLStreamException {
210 this(file, mode, false);
211 }
212
213 /**
214 * Constructor
215 *
216 * @param file
217 * a file to parse
218 * @param mode
219 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
220 * ROWREAD (per ROW, for small memory size applications)
221 * @param debug
222 * @throws IOException
223 * @throws javax.xml.stream.XMLStreamException
224 */
225 public SavotStaxParser(final String file, final int mode, final boolean debug) throws IOException, XMLStreamException {
226 this(file, mode, debug, new SavotStatistics());
227 }
228
229 /**
230 * Constructor
231 *
232 * @param file
233 * a file to parse
234 * @param mode
235 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
236 * ROWREAD (per ROW, for small memory size applications)
237 * @param debug
238 * @param stats
239 * @throws IOException
240 * @throws javax.xml.stream.XMLStreamException
241 */
242 public SavotStaxParser(final String file, final int mode, final boolean debug, final SavotStatistics stats) throws IOException, XMLStreamException {
243
244 this.statistics = stats;
245
246 enableDebug(debug);
247
248 final boolean compressed = file.endsWith("gz");
249
250 // set the input of the parser
251 this.inputStream = getInputStream(new FileInputStream(file), compressed);
252 this.xmlParser = getXMLInputFactory().createXMLStreamReader(this.inputStream, "UTF-8"); /* specify encoding ? */
253
254 // fix parsing mode:
255 final int parsingType = parseMode(mode);
256
257 // parse the stream in the given mode
258 if (parsingType == SavotStaxParser.FULLREAD) {
259 parse(parsingType);
260 }
261 }
262
263 /**
264 * Constructor
265 *
266 * @param url
267 * url to parse
268 * @param mode
269 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
270 * ROWREAD (per ROW, for small memory size applications)
271 * @throws IOException
272 * @throws javax.xml.stream.XMLStreamException
273 */
274 public SavotStaxParser(final URL url, final int mode) throws IOException, XMLStreamException {
275 this(url, mode, false);
276 }
277
278 /**
279 * Constructor
280 *
281 * @param url
282 * url to parse
283 * @param mode
284 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
285 * ROWREAD (per ROW, for small memory size applications)
286 * @param debug
287 * @throws IOException
288 * @throws javax.xml.stream.XMLStreamException
289 */
290 public SavotStaxParser(final URL url, final int mode, final boolean debug) throws IOException, XMLStreamException {
291 this(url, mode, debug, new SavotStatistics());
292 }
293
294 /**
295 * Constructor
296 *
297 * @param url url to parse
298 * @param mode
299 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
300 * ROWREAD (per ROW, for small memory size applications)
301 * @param debug
302 * @param stats
303 * @throws IOException
304 * @throws javax.xml.stream.XMLStreamException
305 */
306 public SavotStaxParser(final URL url, final int mode, final boolean debug, final SavotStatistics stats) throws IOException, XMLStreamException {
307
308 this.statistics = stats;
309
310 enableDebug(debug);
311
312 // TODO: detect compression
313 final boolean compressed = url.getPath().endsWith("gz");
314
315 // set the input of the parser
316 this.inputStream = getInputStream(url.openStream(), compressed);
317 this.xmlParser = getXMLInputFactory().createXMLStreamReader(this.inputStream); /* specify encoding ? */
318
319 // fix parsing mode:
320
321 final int parsingType = parseMode(mode);
322
323 // parse the stream in the given mode
324 if (parsingType == SavotStaxParser.FULLREAD) {
325 parse(parsingType);
326 }
327 }
328
329 /**
330 * Constructor
331 *
332 * @param instream
333 * stream to parse
334 * @param mode
335 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
336 * ROWREAD (per ROW, for small memory size applications)
337 * @throws IOException
338 * @throws javax.xml.stream.XMLStreamException
339 */
340 public SavotStaxParser(final InputStream instream, final int mode) throws IOException, XMLStreamException {
341 this(instream, mode, false);
342 }
343
344 /**
345 * Constructor
346 *
347 * @param instream
348 * stream to parse
349 * @param mode
350 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
351 * ROWREAD (per ROW, for small memory size applications)
352 * @param debug
353 * @throws IOException
354 * @throws javax.xml.stream.XMLStreamException
355 */
356 public SavotStaxParser(final InputStream instream, final int mode, final boolean debug) throws IOException, XMLStreamException {
357
358 this(instream, mode, debug, new SavotStatistics());
359 }
360
361 /**
362 * Constructor
363 *
364 * @param instream stream to parse
365 * @param mode
366 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
367 * ROWREAD (per ROW, for small memory size applications)
368 * @param debug
369 * @param stats
370 *
371 * @throws IOException
372 * @throws javax.xml.stream.XMLStreamException
373 */
374 public SavotStaxParser(final InputStream instream, final int mode, final boolean debug, final SavotStatistics stats) throws IOException, XMLStreamException {
375
376 this.statistics = stats;
377
378 enableDebug(debug);
379
380 // set the input of the parser
381 this.inputStream = getInputStream(instream, false);
382 this.xmlParser = getXMLInputFactory().createXMLStreamReader(this.inputStream); /* specify encoding ? */
383
384 // fix parsing mode:
385
386 final int parsingType = parseMode(mode);
387
388 // parse the stream in the given mode
389 if (parsingType == SavotStaxParser.FULLREAD) {
390 parse(parsingType);
391 }
392 }
393
394 /**
395 * Constructor
396 *
397 * @param reader reader to parse
398 * @param mode
399 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
400 * ROWREAD (per ROW, for small memory size applications)
401 *
402 * @throws IOException
403 * @throws javax.xml.stream.XMLStreamException
404 */
405 public SavotStaxParser(final Reader reader, final int mode) throws IOException, XMLStreamException {
406
407 this(reader, mode, false);
408 }
409
410 /**
411 * Constructor
412 *
413 * @param reader reader to parse
414 * @param mode
415 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
416 * ROWREAD (per ROW, for small memory size applications)
417 * @param debug
418 *
419 * @throws IOException
420 * @throws javax.xml.stream.XMLStreamException
421 */
422 public SavotStaxParser(final Reader reader, final int mode, final boolean debug) throws IOException, XMLStreamException {
423
424 this(reader, mode, debug, new SavotStatistics());
425 }
426
427 /**
428 * Constructor
429 *
430 * @param reader reader to parse
431 * @param mode
432 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
433 * ROWREAD (per ROW, for small memory size applications)
434 * @param debug
435 * @param stats
436 * @throws IOException
437 * @throws javax.xml.stream.XMLStreamException
438 */
439 public SavotStaxParser(final Reader reader, final int mode,
440 final boolean debug, final SavotStatistics stats) throws IOException, XMLStreamException {
441
442 this.statistics = stats;
443
444 enableDebug(debug);
445
446 // set the input of the parser
447 this.reader = reader;
448 this.xmlParser = getXMLInputFactory().createXMLStreamReader(this.reader);
449
450 // fix parsing mode:
451 final int parsingType = parseMode(mode);
452
453 // parse the stream in the given mode
454 if (parsingType == SavotStaxParser.FULLREAD) {
455 parse(parsingType);
456 }
457 }
458
459 /**
460 * Return the parsing type (FULLREAD, RESOURCEREAD or ROWREAD)
461 * @param mode mode (FULL, SEQUENTIAL, FULLREAD, RESOURCEREAD or ROWREAD)
462 * @return parsing type
463 */
464 private int parseMode(final int mode) {
465 switch (mode) {
466 case ROWREAD:
467 return ROWREAD;
468 case SEQUENTIAL:
469 return RESOURCEREAD;
470 case FULL:
471 default:
472 return FULLREAD;
473 }
474 }
475
476 /**
477 * Get a buffered input stream or a gzip input stream
478 * @param instream stream to wrap
479 * @param compressed true to indicate to use a gzip input stream
480 * @return input stream
481 * @throws IOException useless
482 */
483 private InputStream getInputStream(final InputStream instream, final boolean compressed) throws IOException {
484 // best buffer size = 8K because kXmlParser uses also a 8K read buffer
485 final int bufferSize = 8 * 1024; // 8K read buffer
486
487 final InputStream in;
488 if (compressed) {
489 in = new GZIPInputStream(instream, bufferSize); // 512 bytes by default
490 } else {
491 // no buffer as Stax parser has a 8K read buffer too
492 in = instream;
493 }
494 return in;
495 }
496
497 /**
498 * Close the input stream if still opened and free the internal parser
499 */
500 public void close() {
501 if (this.xmlParser != null) {
502 try {
503 this.xmlParser.close();
504 } catch (XMLStreamException xse) {
505 logger.log(Level.INFO, "Exception SavotStaxParser.close: ", xse);
506 }
507 this.xmlParser = null;
508 }
509 if (this.inputStream != null) {
510 try {
511 this.inputStream.close();
512 } catch (IOException ioe) {
513 logger.log(Level.INFO, "Exception SavotStaxParser.close: ", ioe);
514 }
515 this.inputStream = null;
516 }
517 if (this.reader != null) {
518 try {
519 this.reader.close();
520 } catch (IOException ioe) {
521 logger.log(Level.INFO, "Exception SavotStaxParser.close: ", ioe);
522 }
523 this.reader = null;
524 }
525 }
526
527 /**
528 * Reset of the engine before another parsing
529 * LBO: useless methods ?
530 */
531 public void reset() {
532 // data model global classes
533 _currentVOTable = new SavotVOTable();
534 _currentResource = new SavotResource();
535 rowCounter = 0;
536 resourceCounter = 0;
537 tableCounter = 0;
538 dataCounter = 0;
539 idRefLinks.clear();
540 // used for recursive resources, LIFO mode
541 resourcestack.clear();
542 // used for recursive options, LIFO mode
543 optionstack.clear();
544 // used for recursive groups, LIFO mode
545 groupstack.clear();
546 }
547
548 /**
549 * Get the last element from the resourcestack
550 * @return SavotResource
551 */
552 private SavotResource getResourceStack() {
553 return resourcestack.remove(resourcestack.size() - 1);
554 }
555
556 /**
557 * Put a resource on the resourcestack
558 * @param resource
559 */
560 private void putResourceStack(final SavotResource resource) {
561 resourcestack.add(resource);
562 }
563
564 /**
565 * Get the last element from the optionstack
566 * @return SavotOption
567 */
568 private SavotOption getOptionStack() {
569 return optionstack.remove(optionstack.size() - 1);
570 }
571
572 /**
573 * Put an option on the optionstack
574 * @param option
575 */
576 private void putOptionStack(final SavotOption option) {
577 optionstack.add(option);
578 }
579
580 /**
581 * Get the last element from the groupstack
582 * @return SavotGroup
583 */
584 private SavotGroup getGroupStack() {
585 return groupstack.remove(groupstack.size() - 1);
586 }
587
588 /**
589 * Put a group on the groupstack
590 * @param group
591 */
592 private void putGroupStack(final SavotGroup group) {
593 groupstack.add(group);
594 }
595
596 /**
597 * Return last father tag
598 * @return tag or null
599 */
600 private VOTableTag lastFather() {
601 int size = fatherTags.size();
602 if (size == 0) {
603 return VOTableTag.UNDEFINED;
604 }
605 return fatherTags.get(size - 1);
606 }
607
608 /**
609 * Parsing engine
610 *
611 * @param parsingType
612 * FULLREAD (all in memory), RESOURCEREAD (per RESOURCE) or
613 * ROWREAD (per ROW, for small memory size applications)
614 * @throws IOException
615 * @throws javax.xml.stream.XMLStreamException
616 */
617 public void parse(final int parsingType) throws IOException, XMLStreamException {
618
619 final XMLStreamReader parser = this.xmlParser;
620
621
622 if (parser == null) {
623 return;
624 }
625
626 if (parsingType != ROWREAD && parsingType != RESOURCEREAD) {
627 // for multi level resource
628 includedResource = 0;
629 // for multi level option
630 includedOption = 0;
631 // for multi level group
632 includedGroup = 0;
633
634 currentTable = new SavotTable();
635 currentField = new SavotField();
636 currentFieldRef = new SavotFieldRef();
637 currentGroup = new SavotGroup(); // new since VOTable 1.1
638 currentParam = new SavotParam();
639 currentParamRef = new SavotParamRef();
640 currentData = new SavotData();
641 currentValues = new SavotValues();
642 currentTableData = new SavotTableData();
643 currentDescription = "";
644 currentLink = new SavotLink();
645 currentInfo = new SavotInfo();
646 currentMin = new SavotMin();
647 currentMax = new SavotMax();
648 currentOption = new SavotOption();
649 currentCoosys = new SavotCoosys();
650 currentDefinitions = new SavotDefinitions();
651 currentBinary = new SavotBinary();
652 currentBinary2 = new SavotBinary2();
653 currentFits = new SavotFits();
654 currentStream = new SavotStream();
655 }
656
657 String operation = "UNDEFINED";
658 VOTableTag tag = null;
659 int rowCount = rowCounter;
660 int dataCount = dataCounter;
661 int lineNumber = 0;
662
663 try {
664 // local copy for performance:
665 final boolean trace = debugMode;
666 final SavotStatistics stats = statistics;
667 final SavotTD emptyTD = EMPTY_TD;
668 final ArrayList<VOTableTag> father = fatherTags;
669
670 // used for RESOURCEREAD parsing
671 boolean resourceComplete = false;
672
673 // used for ROWREAD parsing
674 boolean TRComplete = false;
675
676 // local variables for performance:
677 SavotTR currentTR = _currentTR;
678 TDSet tdSet = null;
679 SavotTD currentTD = null;
680
681 // name from parser.getName and current markup
682 String name, prefix;
683 VOTableTag currentMarkup = VOTableTag.UNDEFINED;
684 String attrName, attrValue;
685 String textValue;
686 int i, counter;
687
688 if (currentTR != null) {
689 currentTR.clear(); // recycle TR instance (also TDSet)
690 tdSet = currentTR.getTDs();
691 }
692
693 // event type
694 int eventType = parser.getEventType();
695
696 // System.out.println("ENTREE DANS PARSER");
697 // while the end of the document is not reach
698 while (eventType != XMLEvent.END_DOCUMENT) {
699 /* custom line number as not provided by Stax parser */
700 lineNumber++;
701
702 // treatment depending on event type
703 switch (eventType) {
704
705 // if a start tag is reach
706 case XMLEvent.START_ELEMENT:
707 operation = "START_ELEMENT";
708
709 // the name of the current tag
710 name = parser.getLocalName();
711
712 if (name != null) {
713 if (trace) {
714 System.err.println("Name ---> " + name);
715 }
716 // avoid parsing name twice:
717 if (tag == null) {
718 tag = VOTableTag.parseTag(name);
719 }
720
721 if (trace) {
722 System.err.println(tag + " begin");
723 }
724 System.err.println("TAG -> " + tag);
725
726 // use most probable tags FIRST (performance) i.e TD / TR first :
727 switch (tag) {
728 case TD:
729 // avoid creating new SavotTD if the value is empty !!
730 currentTD = null;
731
732 if (doStats) {
733 // for statistics only
734 dataCount++;
735 }
736 break;
737
738 case TR:
739 stats.iTDLocalReset();
740
741 // recycle TR instance:
742 if (currentTR == null) {
743 // create a new row
744 currentTR = new SavotTR();
745 tdSet = currentTR.getTDs();
746 }
747 if (doLineInfo) {
748 currentTR.setLineInXMLFile(lineNumber);
749 }
750 break;
751
752 case DESCRIPTION:
753 break;
754
755 case VOTABLE:
756 // Get namespaces from Stax:
757 counter = parser.getNamespaceCount();
758 if (counter != 0) {
759 String uri;
760 for (i = 0; i < parser.getNamespaceCount(); i++) {
761 prefix = parser.getNamespacePrefix(i);
762 uri = parser.getNamespaceURI(i);
763 if (prefix == null) {
764 // xmlns attribute:
765 if (trace) {
766 System.err.println("xmlns='" + uri + "'");
767 }
768 _currentVOTable.setXmlns(uri);
769 } else {
770 if (trace) {
771 System.err.println("xmlns:" + prefix + "='" + uri + "'");
772 }
773 // TODO: handle multiple namespaces:
774 if (prefix.equalsIgnoreCase(XSI)) {
775 _currentVOTable.setXmlnsxsi(uri);
776 }
777 }
778 }
779 }
780
781 counter = parser.getAttributeCount();
782 if (counter != 0) {
783 for (i = 0; i < counter; i++) {
784 attrValue = parser.getAttributeValue(i);
785 if (attrValue.length() != 0) {
786 if (trace) {
787 System.err.println("VOTABLE attribute:" + parser.getAttributeName(i) + "='" + attrValue + "'");
788 }
789 attrName = parser.getAttributeLocalName(i);
790 if (attrName.equalsIgnoreCase(VERSION)) {
791 _currentVOTable.setVersion(attrValue);
792 } else if (attrName.equalsIgnoreCase(ID)) {
793 _currentVOTable.setId(attrValue);
794 idRefLinks.put(attrValue, _currentVOTable);
795 } else {
796 prefix = parser.getAttributePrefix(i);
797 if (trace) {
798 System.err.println("VOTABLE attribute:" + prefix + ":" + attrName);
799 }
800 /* TODO: use uri because prefix can be changed ? */
801 if (XSI.equalsIgnoreCase(prefix)) {
802 if (attrName.equalsIgnoreCase(XSI_NOSCHEMA)) {
803 _currentVOTable.setXsinoschema(attrValue);
804 } else if (attrName.equalsIgnoreCase(XSI_SCHEMA)) {
805 _currentVOTable.setXsischema(attrValue);
806 }
807 }
808 }
809 }
810 }
811 }
812 break;
813
814 case RESOURCE:
815 stats.iResourcesInc();
816
817 if (includedResource > 0) {
818 // inner case (multi level resources)
819 putResourceStack(_currentResource);
820 if (trace) {
821 System.err.println("RESOURCE - included");
822 }
823 } else if (trace) {
824 System.err.println("RESOURCE - not included");
825 }
826
827 includedResource++;
828
829 // for statistics only
830 resourceCounter++;
831
832 if (parsingType == FULL || _currentResource == null || parsingType == ROWREAD) {
833 _currentResource = new SavotResource();
834 } else {
835 _currentResource.init();
836 }
837
838 _currentResource.setType(""); // correct the "results" default value
839
840 counter = parser.getAttributeCount();
841 if (counter != 0) {
842 for (i = 0; i < counter; i++) {
843 attrValue = parser.getAttributeValue(i);
844 if (attrValue.length() != 0) {
845 attrName = parser.getAttributeLocalName(i);
846 if (attrName.equalsIgnoreCase(NAME)) {
847 _currentResource.setName(attrValue);
848 } else if (attrName.equalsIgnoreCase(TYPE)) {
849 _currentResource.setType(attrValue);
850 } else if (attrName.equalsIgnoreCase(UTYPE)) {
851 _currentResource.setUtype(attrValue);
852 } else if (attrName.equalsIgnoreCase(ID)) {
853 _currentResource.setId(attrValue);
854 if (parsingType == FULL) {
855 idRefLinks.put(attrValue, _currentResource);
856 }
857 }
858 }
859 }
860 }
861 break;
862
863 case TABLE:
864 stats.iTablesInc();
865
866 currentTable = new SavotTable();
867
868 // for statistics only
869 tableCounter++;
870
871 counter = parser.getAttributeCount();
872 if (counter != 0) {
873 for (i = 0; i < counter; i++) {
874 attrValue = parser.getAttributeValue(i);
875 if (attrValue.length() != 0) {
876 attrName = parser.getAttributeLocalName(i);
877 if (attrName.equalsIgnoreCase(NAME)) {
878 currentTable.setName(attrValue);
879 if (trace) {
880 System.err.println("TABLE name " + currentTable.getName());
881 }
882 } else if (attrName.equalsIgnoreCase(UCD)) {
883 // new since VOTable 1.1
884 currentTable.setUcd(attrValue);
885 } else if (attrName.equalsIgnoreCase(UTYPE)) {
886 // new since VOTable 1.1
887 currentTable.setUtype(attrValue);
888 } else if (attrName.equalsIgnoreCase(REF)) {
889 currentTable.setRef(attrValue);
890 } else if (attrName.equalsIgnoreCase(NROWS)) {
891 // new since VOTable 1.1
892 currentTable.setNrows(attrValue);
893 } else if (attrName.equalsIgnoreCase(ID)) {
894 currentTable.setId(attrValue);
895 if (parsingType == FULL) {
896 idRefLinks.put(attrValue, currentTable);
897 if (trace) {
898 System.err.println(attrValue);
899 }
900 }
901 }
902 }
903 }
904 }
905 break;
906
907 case FIELD:
908 currentField = new SavotField();
909
910 counter = parser.getAttributeCount();
911 if (counter != 0) {
912 for (i = 0; i < counter; i++) {
913 attrValue = parser.getAttributeValue(i);
914 if (attrValue.length() != 0) {
915 attrName = parser.getAttributeLocalName(i);
916 if (attrName.equalsIgnoreCase(UNIT)) {
917 currentField.setUnit(attrValue);
918 } else if (attrName.equalsIgnoreCase(DATATYPE)) {
919 currentField.setDataType(attrValue);
920 } else if (attrName.equalsIgnoreCase(PRECISION)) {
921 currentField.setPrecision(attrValue);
922 } else if (attrName.equalsIgnoreCase(WIDTH)) {
923 currentField.setWidth(attrValue);
924 } else if (attrName.equalsIgnoreCase(REF)) {
925 currentField.setRef(attrValue);
926 } else if (attrName.equalsIgnoreCase(NAME)) {
927 currentField.setName(attrValue);
928 } else if (attrName.equalsIgnoreCase(UCD)) {
929 currentField.setUcd(attrValue);
930 } else if (attrName.equalsIgnoreCase(ARRAYSIZE)) {
931 currentField.setArraySize(attrValue);
932 } else if (attrName.equalsIgnoreCase(TYPE)) {
933 // deprecated since VOTable 1.1
934 currentField.setType(attrValue);
935 } else if (attrName.equalsIgnoreCase(UTYPE)) {
936 currentField.setUtype(attrValue);
937 } else if (attrName.equalsIgnoreCase(ID)) {
938 currentField.setId(attrValue);
939 idRefLinks.put(attrValue, currentField);
940 }
941 }
942 }
943 }
944 if (trace) {
945 System.err.println("on vient de remplir un FIELD ---> " + currentField.getName());
946 }
947 break;
948
949 case FIELDREF:
950 currentFieldRef = new SavotFieldRef();
951
952 counter = parser.getAttributeCount();
953 if (counter != 0) {
954 for (i = 0; i < counter; i++) {
955 attrValue = parser.getAttributeValue(i);
956 if (attrValue.length() != 0) {
957 attrName = parser.getAttributeLocalName(i);
958 if (attrName.equalsIgnoreCase(REF)) {
959 currentFieldRef.setRef(attrValue);
960 } else if (attrName.equalsIgnoreCase(UCD)) {
961 currentFieldRef.setUcd(attrValue);
962 } else if (attrName.equalsIgnoreCase(UTYPE)) {
963 currentFieldRef.setUtype(attrValue);
964 }
965 }
966 }
967 }
968 break;
969
970 case VALUES:
971 currentValues = new SavotValues();
972
973 counter = parser.getAttributeCount();
974 if (counter != 0) {
975 for (i = 0; i < counter; i++) {
976 attrValue = parser.getAttributeValue(i);
977 if (attrValue.length() != 0) {
978 attrName = parser.getAttributeLocalName(i);
979 if (attrName.equalsIgnoreCase(TYPE)) {
980 currentValues.setType(attrValue);
981 } else if (attrName.equalsIgnoreCase(NULL)) {
982 currentValues.setNull(attrValue);
983 } else if (attrName.equalsIgnoreCase(INVALID)) {
984 currentValues.setInvalid(attrValue);
985 } else if (attrName.equalsIgnoreCase(REF)) {
986 currentValues.setRef(attrValue);
987 } else if (attrName.equalsIgnoreCase(ID)) {
988 currentValues.setId(attrValue);
989 idRefLinks.put(attrValue, currentValues);
990 }
991 }
992 }
993 }
994 break;
995
996 case STREAM:
997 currentStream = new SavotStream();
998
999 counter = parser.getAttributeCount();
1000 if (counter != 0) {
1001 for (i = 0; i < counter; i++) {
1002 attrValue = parser.getAttributeValue(i);
1003 if (attrValue.length() != 0) {
1004 attrName = parser.getAttributeLocalName(i);
1005 if (attrName.equalsIgnoreCase(TYPE)) {
1006 currentStream.setType(attrValue);
1007 } else if (attrName.equalsIgnoreCase(HREF)) {
1008 currentStream.setHref(attrValue);
1009 } else if (attrName.equalsIgnoreCase(ACTUATE)) {
1010 currentStream.setActuate(attrValue);
1011 } else if (attrName.equalsIgnoreCase(ENCODING)) {
1012 currentStream.setEncoding(attrValue);
1013 } else if (attrName.equalsIgnoreCase(EXPIRES)) {
1014 currentStream.setExpires(attrValue);
1015 } else if (attrName.equalsIgnoreCase(RIGHTS)) {
1016 currentStream.setRights(attrValue);
1017 }
1018 }
1019 }
1020 }
1021 break;
1022
1023 case DATA:
1024 currentData = new SavotData();
1025 break;
1026
1027 case BINARY:
1028 currentBinary = new SavotBinary();
1029 break;
1030
1031 case BINARY2:
1032 currentBinary2 = new SavotBinary2();
1033 break;
1034
1035 case FITS:
1036 currentFits = new SavotFits();
1037
1038 counter = parser.getAttributeCount();
1039 if (counter != 0) {
1040 for (i = 0; i < counter; i++) {
1041 attrValue = parser.getAttributeValue(i);
1042 if (attrValue.length() != 0) {
1043 attrName = parser.getAttributeLocalName(i);
1044 if (attrName.equalsIgnoreCase(REF)) {
1045 currentFits.setExtnum(attrValue);
1046 }
1047 }
1048 }
1049 }
1050 break;
1051
1052 case TABLEDATA:
1053 currentTableData = new SavotTableData();
1054
1055 if (parsingType == ROWREAD) {
1056 // if row sequential reading then storage of the metadata
1057 if (stats.getITablesLocal() > 1) {
1058 // not the first TABLE of the RESOURCE
1059 _currentVOTable.getResources().removeItemAt(_currentVOTable.getResources().getItemCount() - 1);
1060 }
1061 // TODO: use current table directly with a getter ?
1062 _currentResource.getTables().addItem(currentTable);
1063 _currentVOTable.getResources().addItem(_currentResource);
1064 }
1065 break;
1066
1067 case PARAM:
1068 currentParam = new SavotParam();
1069
1070 counter = parser.getAttributeCount();
1071 if (counter != 0) {
1072 for (i = 0; i < counter; i++) {
1073 attrValue = parser.getAttributeValue(i);
1074 if (attrValue.length() != 0) {
1075 attrName = parser.getAttributeLocalName(i);
1076 if (attrName.equalsIgnoreCase(UNIT)) {
1077 currentParam.setUnit(attrValue);
1078 } else if (attrName.equalsIgnoreCase(DATATYPE)) {
1079 currentParam.setDataType(attrValue);
1080 } else if (attrName.equalsIgnoreCase(PRECISION)) {
1081 currentParam.setPrecision(attrValue);
1082 } else if (attrName.equalsIgnoreCase(WIDTH)) {
1083 currentParam.setWidth(attrValue);
1084 } else if (attrName.equalsIgnoreCase(REF)) {
1085 currentParam.setRef(attrValue);
1086 } else if (attrName.equalsIgnoreCase(NAME)) {
1087 currentParam.setName(attrValue);
1088 } else if (attrName.equalsIgnoreCase(UCD)) {
1089 currentParam.setUcd(attrValue);
1090 } else if (attrName.equalsIgnoreCase(UTYPE)) {
1091 currentParam.setUtype(attrValue);
1092 } else if (attrName.equalsIgnoreCase(VALUE)) {
1093 currentParam.setValue(attrValue);
1094 } else if (attrName.equalsIgnoreCase(XTYPE)) {
1095 currentParam.setXtype(attrValue);
1096 } else if (attrName.equalsIgnoreCase(ARRAYSIZE)) {
1097 currentParam.setArraySize(attrValue);
1098 } else if (attrName.equalsIgnoreCase(XTYPE)) {
1099 currentParam.setXtype(attrValue);
1100 } else if (attrName.equalsIgnoreCase(ID)) {
1101 currentParam.setId(attrValue);
1102 idRefLinks.put(attrValue, currentParam);
1103 }
1104 }
1105 }
1106 }
1107 break;
1108
1109 case PARAMREF:
1110 currentParamRef = new SavotParamRef();
1111
1112 counter = parser.getAttributeCount();
1113 if (counter != 0) {
1114 for (i = 0; i < counter; i++) {
1115 attrValue = parser.getAttributeValue(i);
1116 if (attrValue.length() != 0) {
1117 attrName = parser.getAttributeLocalName(i);
1118 if (attrName.equalsIgnoreCase(REF)) {
1119 currentParamRef.setRef(attrValue);
1120 } else if (attrName.equalsIgnoreCase(UCD)) {
1121 currentParamRef.setUcd(attrValue);
1122 } else if (attrName.equalsIgnoreCase(UTYPE)) {
1123 currentParamRef.setUtype(attrValue);
1124 }
1125 }
1126 }
1127 }
1128 break;
1129
1130 case LINK:
1131 currentLink = new SavotLink();
1132
1133 counter = parser.getAttributeCount();
1134 if (counter != 0) {
1135 for (i = 0; i < counter; i++) {
1136 attrValue = parser.getAttributeValue(i);
1137 if (attrValue.length() != 0) {
1138 attrName = parser.getAttributeLocalName(i);
1139 if (attrName.equalsIgnoreCase(CONTENTROLE)) {
1140 currentLink.setContentRole(attrValue);
1141 } else if (attrName.equalsIgnoreCase(CONTENTTYPE)) {
1142 currentLink.setContentType(attrValue);
1143 } else if (attrName.equalsIgnoreCase(TITLE)) {
1144 currentLink.setTitle(attrValue);
1145 } else if (attrName.equalsIgnoreCase(VALUE)) {
1146 currentLink.setValue(attrValue);
1147 } else if (attrName.equalsIgnoreCase(HREF)) {
1148 currentLink.setHref(attrValue);
1149 } else if (attrName.equalsIgnoreCase(GREF)) {
1150 // deprecated since VOTable 1.1
1151 currentLink.setGref(attrValue);
1152 } else if (attrName.equalsIgnoreCase(ACTION)) {
1153 currentLink.setAction(attrValue);
1154 } else if (attrName.equalsIgnoreCase(ID)) {
1155 currentLink.setId(attrValue);
1156 idRefLinks.put(attrValue, currentLink);
1157 }
1158 }
1159 }
1160 }
1161 break;
1162
1163 case INFO:
1164 currentInfo = new SavotInfo();
1165
1166 counter = parser.getAttributeCount();
1167 if (counter != 0) {
1168 for (i = 0; i < counter; i++) {
1169 attrValue = parser.getAttributeValue(i);
1170 if (attrValue.length() != 0) {
1171 attrName = parser.getAttributeLocalName(i);
1172 if (attrName.equalsIgnoreCase(NAME)) {
1173 currentInfo.setName(attrValue);
1174 } else if (attrName.equalsIgnoreCase(VALUE)) {
1175 currentInfo.setValue(attrValue);
1176 } else if (attrName.equalsIgnoreCase(XTYPE)) {
1177 currentInfo.setXtype(attrValue);
1178 } else if (attrName.equalsIgnoreCase(UNIT)) {
1179 currentInfo.setUnit(attrValue);
1180 } else if (attrName.equalsIgnoreCase(UCD)) {
1181 currentInfo.setUcd(attrValue);
1182 } else if (attrName.equalsIgnoreCase(UTYPE)) {
1183 currentInfo.setUtype(attrValue);
1184 } else if (attrName.equalsIgnoreCase(REF)) {
1185 currentInfo.setRef(attrValue);
1186 } else if (attrName.equalsIgnoreCase(ID)) {
1187 currentInfo.setId(attrValue);
1188 idRefLinks.put(attrValue, currentInfo);
1189 }
1190 }
1191 }
1192 }
1193 break;
1194
1195 case MIN:
1196 currentMin = new SavotMin();
1197
1198 counter = parser.getAttributeCount();
1199 if (counter != 0) {
1200 for (i = 0; i < counter; i++) {
1201 attrValue = parser.getAttributeValue(i);
1202 if (attrValue.length() != 0) {
1203 attrName = parser.getAttributeLocalName(i);
1204 if (attrName.equalsIgnoreCase(VALUE)) {
1205 currentMin.setValue(attrValue);
1206 } else if (attrName.equalsIgnoreCase(INCLUSIVE)) {
1207 currentMin.setInclusive(attrValue);
1208 }
1209 }
1210 }
1211 }
1212 break;
1213
1214 case MAX:
1215 currentMax = new SavotMax();
1216
1217 counter = parser.getAttributeCount();
1218 if (counter != 0) {
1219 for (i = 0; i < counter; i++) {
1220 attrValue = parser.getAttributeValue(i);
1221 if (attrValue.length() != 0) {
1222 attrName = parser.getAttributeLocalName(i);
1223 if (attrName.equalsIgnoreCase(VALUE)) {
1224 currentMax.setValue(attrValue);
1225 } else if (attrName.equalsIgnoreCase(INCLUSIVE)) {
1226 currentMax.setInclusive(attrValue);
1227 }
1228 }
1229 }
1230 }
1231 break;
1232
1233 case OPTION:
1234 if (includedOption > 0) {
1235 // inner case (multi level options)
1236 putOptionStack(currentOption);
1237 if (trace) {
1238 System.err.println("OPTION - included");
1239 }
1240 } else if (trace) {
1241 System.err.println("OPTION - not included");
1242 }
1243
1244 includedOption++;
1245
1246 currentOption = new SavotOption();
1247
1248 counter = parser.getAttributeCount();
1249 if (counter != 0) {
1250 for (i = 0; i < counter; i++) {
1251 attrValue = parser.getAttributeValue(i);
1252 if (attrValue.length() != 0) {
1253 attrName = parser.getAttributeLocalName(i);
1254 if (attrName.equalsIgnoreCase(NAME)) {
1255 currentOption.setName(attrValue);
1256 } else if (attrName.equalsIgnoreCase(VALUE)) {
1257 currentOption.setValue(attrValue);
1258 }
1259 }
1260 }
1261 }
1262 break;
1263
1264 case GROUP:
1265 // new since VOTable 1.1
1266 stats.iGroupsGlobalInc();
1267
1268 if (includedGroup > 0) {
1269 // inner case (multi level groups)
1270 putGroupStack(currentGroup);
1271 if (trace) {
1272 System.err.println("GROUP - included");
1273 }
1274 } else if (trace) {
1275 System.err.println("GROUP - not included");
1276 }
1277 includedGroup++;
1278
1279 currentGroup = new SavotGroup();
1280
1281 counter = parser.getAttributeCount();
1282 if (counter != 0) {
1283 for (i = 0; i < counter; i++) {
1284 attrValue = parser.getAttributeValue(i);
1285 if (attrValue.length() != 0) {
1286 attrName = parser.getAttributeLocalName(i);
1287 if (attrName.equalsIgnoreCase(REF)) {
1288 currentGroup.setRef(attrValue);
1289 } else if (attrName.equalsIgnoreCase(NAME)) {
1290 currentGroup.setName(attrValue);
1291 } else if (attrName.equalsIgnoreCase(UCD)) {
1292 currentGroup.setUcd(attrValue);
1293 } else if (attrName.equalsIgnoreCase(UTYPE)) {
1294 currentGroup.setUtype(attrValue);
1295 } else if (attrName.equalsIgnoreCase(ID)) {
1296 currentGroup.setId(attrValue);
1297 idRefLinks.put(attrValue, currentGroup);
1298 }
1299 }
1300 }
1301 }
1302 break;
1303
1304 case COOSYS:
1305 // deprecated since VOTable 1.2
1306 currentCoosys = new SavotCoosys();
1307
1308 counter = parser.getAttributeCount();
1309 if (counter != 0) {
1310 for (i = 0; i < counter; i++) {
1311 attrValue = parser.getAttributeValue(i);
1312 if (attrValue.length() != 0) {
1313 attrName = parser.getAttributeLocalName(i);
1314 if (attrName.equalsIgnoreCase(EQUINOX)) {
1315 currentCoosys.setEquinox(attrValue);
1316 } else if (attrName.equalsIgnoreCase(EPOCH)) {
1317 currentCoosys.setEpoch(attrValue);
1318 } else if (attrName.equalsIgnoreCase(SYSTEM)) {
1319 currentCoosys.setSystem(attrValue);
1320 } else if (attrName.equalsIgnoreCase(ID)) {
1321 currentCoosys.setId(attrValue);
1322 idRefLinks.put(attrValue, currentCoosys);
1323 }
1324 }
1325 }
1326 }
1327 break;
1328
1329 case DEFINITIONS:
1330 currentDefinitions = new SavotDefinitions();
1331 break;
1332
1333 default:
1334 System.err.println("VOTable markup error " + operation + " : " + tag + " at line " + lineNumber);
1335 }
1336 // Anyway define currentMarkup:
1337 currentMarkup = tag;
1338 }
1339 break;
1340
1341 // if an end tag is reach
1342 case XMLEvent.END_ELEMENT:
1343 operation = "END_ELEMENT";
1344
1345 // the name of the current tag
1346 name = parser.getLocalName();
1347
1348 if (name != null) {
1349 if (trace) {
1350 System.err.println("End ---> " + name);
1351 }
1352
1353 tag = VOTableTag.parseTag(name);
1354
1355 if (trace) {
1356 System.err.println(tag + " end");
1357 }
1358
1359 // use most probable tags FIRST (performance) i.e TD / TR first :
1360 switch (tag) {
1361 case TD:
1362 stats.iTDInc();
1363
1364 // reduce SavotTD instances:
1365 tdSet.addItem((currentTD == null) ? emptyTD : currentTD);
1366 break;
1367
1368 case TR:
1369 stats.iTRInc();
1370 stats.iTDLocalReset();
1371
1372 // trim TDset (reduce memory footprint):
1373 tdSet.trim();
1374
1375 if (parsingType != ROWREAD) {
1376 // add the row to the table
1377 currentTableData.getTRs().addItem(currentTR);
1378 currentTR = null; // used so do not recycle it
1379 } else {
1380 // TR will be used without storage in the model
1381 TRComplete = true;
1382 // update reference:
1383 _currentTR = currentTR;
1384 }
1385
1386 if (doStats) {
1387 // for statistics only
1388 rowCount++;
1389 }
1390
1391 if (trace) {
1392 System.err.println("ADD row");
1393 }
1394 break;
1395
1396 case DESCRIPTION:
1397 // DESCRIPTION - several fathers are possible
1398 switch (lastFather()) {
1399 case VOTABLE:
1400 _currentVOTable.setDescription(currentDescription);
1401 break;
1402 case RESOURCE:
1403 _currentResource.setDescription(currentDescription);
1404 break;
1405 case PARAM:
1406 currentParam.setDescription(currentDescription);
1407 break;
1408 case TABLE:
1409 currentTable.setDescription(currentDescription);
1410 break;
1411 case FIELD:
1412 currentField.setDescription(currentDescription);
1413 break;
1414 case GROUP:
1415 currentGroup.setDescription(currentDescription);
1416 break;
1417
1418 default:
1419 }
1420 currentDescription = null;
1421 break;
1422
1423 case TABLE:
1424 // avoid duplicates:
1425 if (parsingType != ROWREAD) {
1426 _currentResource.getTables().addItem(currentTable);
1427
1428 if (trace) {
1429 System.err.println("TABLE " + currentTable.getName() + " added");
1430 }
1431 }
1432 break;
1433
1434 case FIELD:
1435 if (trace) {
1436 System.err.println("FIELD from father = " + lastFather());
1437 }
1438
1439 if (lastFather() == VOTableTag.TABLE) {
1440 currentTable.getFields().addItem(currentField);
1441 if (trace) {
1442 System.err.println("FIELD from TABLE father = " + father);
1443 }
1444 }
1445 break;
1446
1447 case FIELDREF:
1448 if (lastFather() == VOTableTag.GROUP) {
1449 currentGroup.getFieldsRef().addItem(currentFieldRef);
1450 if (trace) {
1451 System.err.println("FIELDRef from GROUP father = " + father);
1452 }
1453 }
1454 break;
1455
1456 case DATA:
1457 currentTable.setData(currentData);
1458 break;
1459
1460 case RESOURCE:
1461 if (includedResource > 1) {
1462 final SavotResource tmp = _currentResource;
1463 _currentResource = getResourceStack();
1464 _currentResource.getResources().addItem(tmp);
1465 } else {
1466 if (parsingType == FULL) {
1467 _currentVOTable.getResources().addItem(_currentResource);
1468 }
1469 if (trace) {
1470 System.err.println(">>>>>>>> RESOURCE COMPLETED");
1471 }
1472 resourceComplete = true;
1473 }
1474 includedResource--;
1475 break;
1476
1477 case OPTION:
1478 if (includedOption > 1) {
1479 final SavotOption tmp = currentOption;
1480 currentOption = getOptionStack();
1481 currentOption.getOptions().addItem(tmp);
1482 includedOption--;
1483 } else {
1484 if (lastFather() == VOTableTag.VALUES) {
1485 currentValues.getOptions().addItem(currentOption);
1486 if (trace) {
1487 System.err.println("OPTION from VALUES father = " + father);
1488 }
1489 includedOption--;
1490 }
1491 }
1492 break;
1493
1494 case GROUP:
1495 if (includedGroup > 1) {
1496 final SavotGroup tmp = currentGroup;
1497 currentGroup = getGroupStack();
1498 currentGroup.getGroups().addItem(tmp);
1499 includedGroup--;
1500 } else {
1501 if (lastFather() == VOTableTag.TABLE) {
1502 currentTable.getGroups().addItem(currentGroup);
1503 if (trace) {
1504 System.err.println("GROUP from TABLE father = " + father);
1505 }
1506 includedGroup--;
1507 }
1508 }
1509 break;
1510
1511 case TABLEDATA:
1512 stats.iTRLocalReset();
1513
1514 // trim TRset (reduce memory footprint):
1515 currentTableData.getTRs().trim();
1516
1517 currentData.setTableData(currentTableData);
1518 if (trace) {
1519 System.err.println("TABLEDATA " + currentTable.getName() + " added");
1520 }
1521 break;
1522
1523 case COOSYS:
1524 // COOSYS - several fathers are possible
1525 switch (lastFather()) {
1526 case DEFINITIONS:
1527 // deprecated since VOTable 1.1
1528 currentDefinitions.getCoosys().addItem(currentCoosys);
1529 if (trace) {
1530 System.err.println("COOSYS from DEFINITIONS father = " + father);
1531 }
1532 break;
1533 case RESOURCE:
1534 _currentResource.getCoosys().addItem(currentCoosys);
1535 if (trace) {
1536 System.err.println("COOSYS from RESOURCE father = " + father);
1537 }
1538 break;
1539 case VOTABLE:
1540 _currentVOTable.getCoosys().addItem(currentCoosys);
1541 if (trace) {
1542 System.err.println("COOSYS from VOTABLE father = " + father);
1543 }
1544 break;
1545 default:
1546 }
1547 break;
1548
1549 case PARAM:
1550 // PARAM - several fathers are possible
1551 switch (lastFather()) {
1552 case DEFINITIONS:
1553 // deprecated since VOTable 1.1
1554 currentDefinitions.getParams().addItem(currentParam);
1555 if (trace) {
1556 System.err.println("PARAM from DEFINITIONS father = " + father);
1557 }
1558 break;
1559 case RESOURCE:
1560 _currentResource.getParams().addItem(currentParam);
1561 if (trace) {
1562 System.err.println("PARAM from RESOURCE father = " + father);
1563 }
1564 break;
1565 case TABLE:
1566 currentTable.getParams().addItem(currentParam);
1567 if (trace) {
1568 System.err.println("PARAM from TABLE father = " + father);
1569 }
1570 break;
1571 case GROUP:
1572 currentGroup.getParams().addItem(currentParam);
1573 if (trace) {
1574 System.err.println("PARAM from GROUP father = " + father);
1575 }
1576 break;
1577 case VOTABLE:
1578 _currentVOTable.getParams().addItem(currentParam);
1579 if (trace) {
1580 System.err.println("PARAM from VOTABLE father = " + father);
1581 }
1582 break;
1583 default:
1584 }
1585 break;
1586
1587 case PARAMREF:
1588 if (lastFather() == VOTableTag.GROUP) {
1589 currentGroup.getParamsRef().addItem(currentParamRef);
1590 if (trace) {
1591 System.err.println("PARAMRef from GROUP father = " + father);
1592 }
1593 }
1594 break;
1595
1596 case LINK:
1597 // LINK - several fathers are possible
1598 switch (lastFather()) {
1599 case RESOURCE:
1600 _currentResource.getLinks().addItem(currentLink);
1601 if (trace) {
1602 System.err.println("LINK from RESOURCE father = " + father);
1603 }
1604 break;
1605 case TABLE:
1606 currentTable.getLinks().addItem(currentLink);
1607 if (trace) {
1608 System.err.println("LINK from TABLE father = " + father);
1609 }
1610 break;
1611 case FIELD:
1612 currentField.getLinks().addItem(currentLink);
1613 if (trace) {
1614 System.err.println("LINK from FIELD father = " + father);
1615 }
1616 break;
1617 case PARAM:
1618 currentParam.getLinks().addItem(currentLink);
1619 if (trace) {
1620 System.err.println("LINK from PARAM father = " + father);
1621 }
1622 break;
1623 default:
1624 }
1625 break;
1626
1627 case VALUES:
1628 // VALUES - several fathers are possible
1629 switch (lastFather()) {
1630 case PARAM:
1631 currentParam.setValues(currentValues);
1632 if (trace) {
1633 System.err.println("VALUES from PARAM father = " + father + " ID : " + currentValues.getId());
1634 }
1635 break;
1636 case FIELD:
1637 currentField.setValues(currentValues);
1638 if (trace) {
1639 System.err.println("VALUES from FIELD father = " + father + " ID : " + currentValues.getId());
1640 }
1641 break;
1642 default:
1643 }
1644 break;
1645
1646 case MIN:
1647 currentValues.setMin(currentMin);
1648 break;
1649
1650 case MAX:
1651 currentValues.setMax(currentMax);
1652 break;
1653
1654 case STREAM:
1655 // STREAM - several fathers are possible
1656 switch (lastFather()) {
1657 case BINARY:
1658 currentBinary.setStream(currentStream);
1659 if (trace) {
1660 System.err.println("STREAM from BINARY father = " + father);
1661 }
1662 break;
1663 case BINARY2:
1664 currentBinary2.setStream(currentStream);
1665 if (trace) {
1666 System.err.println("STREAM from BINARY2 father = " + father);
1667 }
1668 break;
1669 case FITS:
1670 currentFits.setStream(currentStream);
1671 if (trace) {
1672 System.err.println("STREAM from FITS father = " + father);
1673 }
1674 break;
1675 default:
1676 }
1677 break;
1678
1679 case BINARY:
1680 currentData.setBinary(currentBinary);
1681 break;
1682
1683 case BINARY2:
1684 currentData.setBinary2(currentBinary2);
1685 break;
1686
1687 case FITS:
1688 currentData.setFits(currentFits);
1689 break;
1690
1691 case INFO:
1692 if (trace) {
1693 System.err.println("INFO father = " + father);
1694 }
1695
1696 // INFO - several fathers are possible
1697 switch (lastFather()) {
1698 case VOTABLE:
1699 // since VOTable 1.2 - if RESOURCE then INFO at the end
1700 if (_currentVOTable.getResources() != null && _currentVOTable.getResources().getItemCount() != 0) {
1701 _currentVOTable.getInfosAtEnd().addItem(currentInfo);
1702 } else {
1703 _currentVOTable.getInfos().addItem(currentInfo);
1704 }
1705 if (trace) {
1706 System.err.println("INFO from VOTABLE father = " + father);
1707 }
1708 break;
1709 case RESOURCE:
1710 // since VOTable 1.2 - if RESOURCE or LINK or TABLE then INFO at the end
1711 if ((_currentResource.getResources() != null && _currentResource.getResources().getItemCount() != 0)
1712 || (_currentResource.getTables() != null && _currentResource.getTables().getItemCount() != 0)
1713 || (_currentResource.getLinks() != null && _currentResource.getLinks().getItemCount() != 0)) {
1714 _currentResource.getInfosAtEnd().addItem(currentInfo);
1715 } else {
1716 _currentResource.getInfos().addItem(currentInfo);
1717 }
1718 if (trace) {
1719 System.err.println("INFO from RESOURCE father = " + father);
1720 }
1721 break;
1722 case TABLE:
1723 // since VOTable 1.2
1724 currentTable.getInfosAtEnd().addItem(currentInfo);
1725 if (trace) {
1726 System.err.println("INFO from TABLE father = " + father);
1727 }
1728 break;
1729
1730 default:
1731 }
1732 break;
1733
1734 case DEFINITIONS:
1735 // deprecated since VOTable 1.1
1736 _currentVOTable.setDefinitions(currentDefinitions);
1737 break;
1738
1739 case VOTABLE:
1740 break;
1741
1742 default:
1743 System.err.println("VOTable markup error " + operation + " : " + tag + " at line " + lineNumber);
1744 }
1745 // Anyway reset currentMarkup:
1746 currentMarkup = VOTableTag.UNDEFINED;
1747 }
1748 break;
1749
1750 case XMLEvent.CHARACTERS:
1751 operation = "CHARACTERS";
1752
1753 // avoid parser creating empty String
1754 if (!parser.isWhiteSpace()) {
1755 // Get new String from parser:
1756 textValue = parser.getText();
1757 if (doTrimValues) {
1758 textValue = textValue.trim();
1759 }
1760
1761 // only store not empty content:
1762 if (textValue.length() != 0) {
1763 if (trace) {
1764 System.err.println(currentMarkup + " : " + textValue);
1765 }
1766
1767 // use most probable tags FIRST (performance) i.e TD / TR first :
1768 switch (currentMarkup) {
1769 case TD:
1770 // create a new data
1771 currentTD = new SavotTD(textValue);
1772 break;
1773
1774 case DESCRIPTION:
1775 currentDescription = textValue;
1776 break;
1777
1778 case INFO:
1779 currentInfo.setContent(textValue);
1780 break;
1781
1782 case LINK:
1783 currentLink.setContent(textValue);
1784 break;
1785
1786 case COOSYS:
1787 currentCoosys.setContent(textValue);
1788 break;
1789
1790 case MIN:
1791 currentMin.setContent(textValue);
1792 break;
1793
1794 case MAX:
1795 currentMax.setContent(textValue);
1796 break;
1797
1798 case STREAM:
1799 currentStream.setContent(textValue);
1800 break;
1801
1802 default:
1803 }
1804 }
1805 }
1806 break;
1807
1808 case XMLEvent.START_DOCUMENT:
1809 operation = "START_DOCUMENT";
1810
1811 if (trace) {
1812 System.err.println("Document start.");
1813 }
1814 break;
1815
1816 default:
1817 operation = "UNDEFINED";
1818 if (trace) {
1819 System.err.println("Ignoring some other (legacy) event at line : " + lineNumber);
1820 }
1821 }
1822
1823 // reset name and tag:
1824 tag = null;
1825
1826 // new values from parser:
1827 eventType = parser.next();
1828
1829 // treatment depending on event type
1830 switch (eventType) {
1831
1832 case XMLEvent.START_ELEMENT:
1833 operation = "START_ELEMENT";
1834
1835 // the name of the current tag
1836 name = parser.getLocalName();
1837 if (trace) {
1838 System.err.println("> FATHER, add : " + name);
1839 }
1840
1841 tag = VOTableTag.parseTag(name);
1842
1843 father.add(tag);
1844 break;
1845
1846 case XMLEvent.END_ELEMENT:
1847 operation = "END_ELEMENT";
1848
1849 // the name of the current tag
1850 name = parser.getLocalName();
1851
1852 if (name != null) {
1853 if (trace) {
1854 System.err.println("> FATHER, remove : " + name);
1855 }
1856
1857 father.remove(father.size() - 1);
1858
1859 } else if (trace) {
1860 // when a lf or cd is reached
1861 System.err.println("> FATHER, case null");
1862 }
1863 break;
1864
1865 // if an end document is reached:
1866 case XMLEvent.END_DOCUMENT:
1867 operation = "END_DOCUMENT";
1868
1869 if (trace) {
1870 System.err.println("Document end reached!");
1871 }
1872
1873 // Anyway:
1874 if (parsingType == ROWREAD) {
1875 // terminate now (return TR null) to indicate that the complete document is done:
1876 _currentTR = null;
1877 }
1878
1879 // Close the stream anyway:
1880 close();
1881
1882 break;
1883
1884 default:
1885 // do nothing
1886 }
1887
1888 if (TRComplete && (parsingType == ROWREAD)) {
1889 eventType = XMLEvent.END_DOCUMENT;
1890 if (trace) {
1891 System.err.println(">>>>>>>>>>>>>>> ROWREAD case : TR end");
1892 }
1893 } else if (resourceComplete && (parsingType == RESOURCEREAD)) {
1894 eventType = XMLEvent.END_DOCUMENT;
1895 if (trace) {
1896 System.err.println(">>>>>>>>>>>>>>> RESOURCEREAD case : RESOURCE end");
1897 }
1898 }
1899 }
1900 } catch (RuntimeException re) {
1901 logger.log(Level.SEVERE, "Exception " + operation + " - TAG (" + tag + ") : parse : " + re + " at line " + lineNumber, re);
1902 throw re;
1903 }
1904
1905 if (doStats) {
1906 // update statistics:
1907 rowCounter = rowCount;
1908 dataCounter = dataCount;
1909 }
1910 }
1911
1912 /**
1913 * Get the next Resource (warning : RESOURCEREAD mode only)
1914 *
1915 * @return a SavotResource (always NULL if other mode)
1916 */
1917 public SavotResource getNextResource() {
1918 _currentResource = null;
1919 try {
1920 parse(RESOURCEREAD);
1921 } catch (IOException ioe) {
1922 logger.log(Level.SEVERE, "Exception getNextResource : ", ioe);
1923 _currentResource = null;
1924 } catch (Exception e) {
1925 logger.log(Level.SEVERE, "Exception getNextResource : ", e);
1926 _currentResource = null;
1927 }
1928 return _currentResource;
1929 }
1930
1931 /**
1932 * Get the next TR (warning : ROWREAD mode only)
1933 *
1934 * @return a SavotTR (always NULL if other mode)
1935 */
1936 public SavotTR getNextTR() {
1937 // note: currentTR is not null as it can be recycled
1938 try {
1939 parse(ROWREAD);
1940 } catch (IOException ioe) {
1941 logger.log(Level.SEVERE, "Exception getNextTR : ", ioe);
1942 _currentTR = null;
1943 } catch (Exception e) {
1944 logger.log(Level.SEVERE, "Exception getNextTR : ", e);
1945 _currentTR = null;
1946 }
1947 return _currentTR;
1948 }
1949
1950 /**
1951 * Get a reference to V0TABLE object
1952 *
1953 * @return SavotVOTable
1954 */
1955 public SavotVOTable getVOTable() {
1956 return _currentVOTable;
1957 }
1958
1959 /**
1960 * Get the number of RESOURCE elements in the document (for statistics)
1961 *
1962 * @return a long value
1963 */
1964 public int getResourceCount() {
1965 return resourceCounter;
1966 }
1967
1968 /**
1969 * Get the number of TABLE elements in the document (for statistics)
1970 *
1971 * @return a long value
1972 */
1973 public int getTableCount() {
1974 return tableCounter;
1975 }
1976
1977 /**
1978 * Get the number of TR elements in the document (for statistics)
1979 *
1980 * @return a long value
1981 */
1982 public int getTRCount() {
1983 return rowCounter;
1984 }
1985
1986 /**
1987 * Get the number of DATA elements in the document (for statistics)
1988 *
1989 * @return a long value
1990 */
1991 public int getDataCount() {
1992 return dataCounter;
1993 }
1994
1995 /**
1996 * Get a reference on the Hashtable containing the link between ID and ref
1997 *
1998 * @return a refernce to the Hashtable
1999 */
2000 public Map<String, Object> getIdRefLinks() {
2001 return idRefLinks;
2002 }
2003
2004 /**
2005 * Search a RESOURCE corresponding to an ID ref
2006 *
2007 * @param ref
2008 * @return a reference to a SavotResource object
2009 */
2010 public SavotResource getResourceFromRef(final String ref) {
2011 return (SavotResource) idRefLinks.get(ref);
2012 }
2013
2014 /**
2015 * Search a FIELD corresponding to an ID ref
2016 *
2017 * @param ref
2018 * @return SavotField
2019 */
2020 public SavotField getFieldFromRef(final String ref) {
2021 return (SavotField) idRefLinks.get(ref);
2022 }
2023
2024 /**
2025 * Search a FIELDref corresponding to an ID ref
2026 *
2027 * @param ref
2028 * @return SavotFieldRef
2029 */
2030 public SavotFieldRef getFieldRefFromRef(final String ref) {
2031 return (SavotFieldRef) idRefLinks.get(ref);
2032 }
2033
2034 /**
2035 * Search a PARAM corresponding to an ID ref
2036 *
2037 * @param ref
2038 * @return SavotParam
2039 */
2040 public SavotParam getParamFromRef(final String ref) {
2041 return (SavotParam) idRefLinks.get(ref);
2042 }
2043
2044 /**
2045 * Search a PARAMref corresponding to an ID ref
2046 *
2047 * @param ref
2048 * @return SavotParamRef
2049 */
2050 public SavotParamRef getParamRefFromRef(final String ref) {
2051 return (SavotParamRef) idRefLinks.get(ref);
2052 }
2053
2054 /**
2055 * Search a TABLE corresponding to an ID ref
2056 *
2057 * @param ref
2058 * @return SavotTable
2059 */
2060 public SavotTable getTableFromRef(final String ref) {
2061 return (SavotTable) idRefLinks.get(ref);
2062 }
2063
2064 /**
2065 * Search a GROUP corresponding to an ID ref
2066 *
2067 * @param ref
2068 * @return SavotGROUP
2069 */
2070 public SavotGroup getGroupFromRef(final String ref) {
2071 return (SavotGroup) idRefLinks.get(ref);
2072 }
2073
2074 /**
2075 * Search a RESOURCE corresponding to an ID ref
2076 *
2077 * @param ref
2078 * @return SavotInfo
2079 */
2080 public SavotInfo getInfoFromRef(final String ref) {
2081 return (SavotInfo) idRefLinks.get(ref);
2082 }
2083
2084 /**
2085 * Search a VALUES corresponding to an ID ref
2086 *
2087 * @param ref
2088 * @return SavotValues
2089 */
2090 public SavotValues getValuesFromRef(final String ref) {
2091 return (SavotValues) idRefLinks.get(ref);
2092 }
2093
2094 /**
2095 * Search a LINK corresponding to an ID ref
2096 *
2097 * @param ref
2098 * @return SavotLink
2099 */
2100 public SavotLink getLinkFromRef(final String ref) {
2101 return (SavotLink) idRefLinks.get(ref);
2102 }
2103
2104 /**
2105 * Search a COOSYS corresponding to an ID ref
2106 *
2107 * @param ref
2108 * @return SavotCoosys
2109 */
2110 public SavotCoosys getCoosysFromRef(final String ref) {
2111 return (SavotCoosys) idRefLinks.get(ref);
2112 }
2113
2114 /**
2115 * Get current VOTable (all resources)
2116 *
2117 * @return SavotVOTable
2118 */
2119 public SavotVOTable getAllResources() {
2120 return _currentVOTable;
2121 }
2122
2123 /**
2124 * Enable debug mode
2125 *
2126 * @param debug
2127 * boolean
2128 */
2129 public void enableDebug(boolean debug) {
2130 debugMode = debug;
2131 }
2132
2133 /**
2134 * Returns the statistics
2135 *
2136 * @return statistics
2137 */
2138 public SavotStatistics getStatistics() {
2139 return statistics;
2140 }
2141
2142 /**
2143 * Get Parser Version
2144 *
2145 * @return String
2146 */
2147 public String getVersion() {
2148 return SavotStaxParser.SAVOTPARSER;
2149 }
2150
2151 /**
2152 * For test only
2153 *
2154 */
2155 public void sequentialTester() {
2156 SavotResource currentResource;
2157 do {
2158 currentResource = getNextResource();
2159 } while (currentResource != null);
2160 }
2161
2162 /**
2163 * Main
2164 *
2165 * @param argv
2166 * @throws Exception
2167 */
2168 public static void main(String[] argv) throws Exception {
2169 if (argv.length == 0) {
2170 System.out.println("Usage: java SavotPullParser document");
2171 } else {
2172 new SavotStaxParser(argv[0], SavotStaxParser.FULL);
2173 }
2174 }
2175 }