1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72:
73:
93: public class SAXParser
94: extends javax.xml.parsers.SAXParser
95: implements XMLReader, Attributes2, Locator2, XMLReporter, XMLResolver
96: {
97:
98: ContentHandler contentHandler;
99: DeclHandler declHandler;
100: DTDHandler dtdHandler;
101: EntityResolver entityResolver;
102: ErrorHandler errorHandler;
103: LexicalHandler lexicalHandler;
104:
105: boolean validating = false;
106: boolean namespaceAware = true;
107: boolean xIncludeAware = false;
108: boolean stringInterning = true;
109: boolean coalescing = true;
110: boolean replaceERefs = true;
111: boolean externalEntities = true;
112: boolean supportDTD = true;
113: boolean baseAware = true;
114:
115: XMLParser parser;
116: XMLStreamReader reader;
117: String encoding;
118: String xmlVersion;
119: boolean xmlStandalone;
120: String xmlEncoding;
121: String baseURI;
122:
123: public SAXParser()
124: {
125: }
126:
127: SAXParser(boolean validating, boolean namespaceAware, boolean xIncludeAware)
128: {
129: this.validating = validating;
130: this.namespaceAware = namespaceAware;
131: this.xIncludeAware = xIncludeAware;
132: }
133:
134:
135:
136: public Parser getParser()
137: throws SAXException
138: {
139: return null;
140: }
141:
142: public XMLReader getXMLReader()
143: throws SAXException
144: {
145: return this;
146: }
147:
148: public boolean isNamespaceAware()
149: {
150: return namespaceAware;
151: }
152:
153: public boolean isValidating()
154: {
155: return validating;
156: }
157:
158: public void setProperty(String name, Object value)
159: throws SAXNotRecognizedException, SAXNotSupportedException
160: {
161: if (parser != null)
162: throw new IllegalStateException("parsing in progress");
163: final String FEATURES = "http://xml.org/sax/features/";
164: final String PROPERTIES = "http://xml.org/sax/properties/";
165: final String GNU_FEATURES = "http://gnu.org/sax/features/";
166: if ((FEATURES + "namespaces").equals(name))
167: namespaceAware = Boolean.TRUE.equals(value);
168: else if ((FEATURES + "namespace-prefixes").equals(name))
169: {
170:
171: }
172: else if ((FEATURES + "string-interning").equals(name))
173: stringInterning = Boolean.TRUE.equals(value);
174: else if ((FEATURES + "use-attributes2").equals(name))
175: {
176:
177: }
178: else if ((FEATURES + "validation").equals(name))
179: validating = Boolean.TRUE.equals(value);
180: else if ((FEATURES + "external-general-entities").equals(name))
181: externalEntities = Boolean.TRUE.equals(value);
182: else if ((FEATURES + "external-parameter-entities").equals(name))
183: externalEntities = Boolean.TRUE.equals(value);
184: else if ((PROPERTIES + "declaration-handler").equals(name))
185: declHandler = (DeclHandler) value;
186: else if ((PROPERTIES + "lexical-handler").equals(name))
187: lexicalHandler = (LexicalHandler) value;
188: else if ((GNU_FEATURES + "xml-base").equals(name))
189: baseAware = Boolean.TRUE.equals(value);
190: else
191: throw new SAXNotSupportedException(name);
192: }
193:
194: public Object getProperty(String name)
195: throws SAXNotRecognizedException, SAXNotSupportedException
196: {
197: final String FEATURES = "http://xml.org/sax/features/";
198: final String PROPERTIES = "http://xml.org/sax/properties/";
199: final String GNU_FEATURES = "http://gnu.org/sax/features/";
200: final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
201: if ((GNU_FEATURES + "base-uri").equals(name))
202: return baseURI;
203: if ((FEATURES + "is-standalone").equals(name))
204: return xmlStandalone ? Boolean.TRUE : Boolean.FALSE;
205: if ((FEATURES + "namespaces").equals(name))
206: return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
207: if ((FEATURES + "namespace-prefixes").equals(name))
208: return Boolean.TRUE;
209: if ((FEATURES + "string-interning").equals(name))
210: return stringInterning ? Boolean.TRUE : Boolean.FALSE;
211: if ((FEATURES + "use-attributes2").equals(name))
212: return Boolean.TRUE;
213: if ((FEATURES + "use-locator2").equals(name))
214: return Boolean.TRUE;
215: if ((FEATURES + "use-entity-resolver2").equals(name))
216: return Boolean.FALSE;
217: if ((FEATURES + "validation").equals(name))
218: return validating ? Boolean.TRUE : Boolean.FALSE;
219: if ((FEATURES + "external-general-entities").equals(name))
220: return externalEntities ? Boolean.TRUE : Boolean.FALSE;
221: if ((FEATURES + "external-parameter-entities").equals(name))
222: return externalEntities ? Boolean.TRUE : Boolean.FALSE;
223: if ((FEATURES + "xml-1.1").equals(name))
224: return Boolean.TRUE;
225: if ((PROPERTIES + "declaration-handler").equals(name))
226: return declHandler;
227: if ((PROPERTIES + "document-xml-version").equals(name))
228: return xmlVersion;
229: if ((PROPERTIES + "lexical-handler").equals(name))
230: return lexicalHandler;
231: if ((GNU_FEATURES + "xml-base").equals(name))
232: return baseAware ? Boolean.TRUE : Boolean.FALSE;
233: if ((GNU_PROPERTIES + "document-xml-encoding").equals(name))
234: return xmlEncoding;
235: throw new SAXNotSupportedException(name);
236: }
237:
238: public boolean isXIncludeAware()
239: {
240: return xIncludeAware;
241: }
242:
243: public void reset()
244: {
245: parser = null;
246: encoding = null;
247: xmlVersion = null;
248: xmlStandalone = false;
249: }
250:
251:
252:
253: public boolean getFeature(String name)
254: throws SAXNotRecognizedException, SAXNotSupportedException
255: {
256: Object ret = getProperty(name);
257: if (ret instanceof Boolean)
258: return ((Boolean) ret).booleanValue();
259: throw new SAXNotSupportedException(name);
260: }
261:
262: public void setFeature(String name, boolean value)
263: throws SAXNotRecognizedException, SAXNotSupportedException
264: {
265: setProperty(name, value ? Boolean.TRUE : Boolean.FALSE);
266: }
267:
268: public void setEntityResolver(EntityResolver resolver)
269: {
270: entityResolver = resolver;
271: }
272:
273: public EntityResolver getEntityResolver()
274: {
275: return entityResolver;
276: }
277:
278: public void setDTDHandler(DTDHandler handler)
279: {
280: dtdHandler = handler;
281: }
282:
283: public DTDHandler getDTDHandler()
284: {
285: return dtdHandler;
286: }
287:
288: public void setContentHandler(ContentHandler handler)
289: {
290: contentHandler = handler;
291: }
292:
293: public ContentHandler getContentHandler()
294: {
295: return contentHandler;
296: }
297:
298: public void setErrorHandler(ErrorHandler handler)
299: {
300: errorHandler = handler;
301: }
302:
303: public ErrorHandler getErrorHandler()
304: {
305: return errorHandler;
306: }
307:
308: public synchronized void parse(InputSource input)
309: throws IOException, SAXException
310: {
311: reset();
312: String systemId = input.getSystemId();
313: InputStream in = input.getByteStream();
314: boolean opened = false;
315: if (in != null)
316: parser = new XMLParser(in, systemId,
317: validating,
318: namespaceAware,
319: coalescing,
320: replaceERefs,
321: externalEntities,
322: supportDTD,
323: baseAware,
324: stringInterning,
325: true,
326: this,
327: this);
328: else
329: {
330: Reader r = input.getCharacterStream();
331: if (r != null)
332: parser = new XMLParser(r, systemId,
333: validating,
334: namespaceAware,
335: coalescing,
336: replaceERefs,
337: externalEntities,
338: supportDTD,
339: baseAware,
340: stringInterning,
341: true,
342: this,
343: this);
344: }
345: if (parser == null)
346: {
347: if (systemId == null)
348: throw new SAXException("No stream or system ID specified");
349: systemId = XMLParser.absolutize(null, systemId);
350: in = new URL(systemId).openStream();
351: opened = true;
352: parser = new XMLParser(in, systemId,
353: validating,
354: namespaceAware,
355: coalescing,
356: replaceERefs,
357: externalEntities,
358: supportDTD,
359: baseAware,
360: stringInterning,
361: true,
362: this,
363: this);
364: }
365: reader = parser;
366: baseURI = systemId;
367:
368: if (xIncludeAware)
369: reader = new XIncludeFilter(parser, systemId, namespaceAware,
370: validating, true);
371:
372: if (contentHandler != null)
373: contentHandler.setDocumentLocator(this);
374: boolean startDocumentDone = false;
375: try
376: {
377: while (parser.hasNext())
378: {
379: int event = parser.next();
380: if (baseAware)
381: baseURI = parser.getXMLBase();
382: switch (event)
383: {
384: case XMLStreamConstants.CHARACTERS:
385: if (contentHandler != null)
386: {
387: char[] b = reader.getTextCharacters();
388: contentHandler.characters(b, 0, b.length);
389: }
390: break;
391: case XMLStreamConstants.SPACE:
392: if (contentHandler != null)
393: {
394: char[] b = reader.getTextCharacters();
395: if (isIgnorableWhitespace(parser, b, false))
396: contentHandler.ignorableWhitespace(b, 0, b.length);
397: else
398: contentHandler.characters(b, 0, b.length);
399: }
400: break;
401: case XMLStreamConstants.CDATA:
402: if (lexicalHandler != null)
403: lexicalHandler.startCDATA();
404: if (contentHandler != null)
405: {
406: char[] b = reader.getTextCharacters();
407: if (isIgnorableWhitespace(parser, b, true))
408: contentHandler.ignorableWhitespace(b, 0, b.length);
409: else
410: contentHandler.characters(b, 0, b.length);
411: }
412: if (lexicalHandler != null)
413: lexicalHandler.endCDATA();
414: break;
415: case XMLStreamConstants.START_ELEMENT:
416: if (contentHandler != null)
417: {
418: QName name = reader.getName();
419: String uri = name.getNamespaceURI();
420: String localName = name.getLocalPart();
421: String prefix = name.getPrefix();
422: String qName = localName;
423: if (!"".equals(prefix))
424: qName = prefix + ":" + localName;
425: if (!namespaceAware)
426: {
427: uri = "";
428: localName = "";
429: }
430: else
431: {
432: int nc = reader.getNamespaceCount();
433: for (int i = 0; i < nc; i++)
434: {
435: String nsuri = reader.getNamespaceURI(i);
436: String nsprefix = reader.getNamespacePrefix(i);
437: if ("xml".equals(nsprefix))
438: continue;
439: contentHandler.startPrefixMapping(nsprefix, nsuri);
440: }
441: }
442: contentHandler.startElement(uri, localName, qName, this);
443: }
444: break;
445: case XMLStreamConstants.END_ELEMENT:
446: if (contentHandler != null)
447: {
448: QName name = reader.getName();
449: String uri = name.getNamespaceURI();
450: String localName = name.getLocalPart();
451: String prefix = name.getPrefix();
452: String qName = localName;
453: if (!"".equals(prefix))
454: qName = prefix + ":" + localName;
455: if (!namespaceAware)
456: {
457: uri = "";
458: localName = "";
459: }
460: contentHandler.endElement(uri, localName, qName);
461: if (namespaceAware)
462: {
463: int nc = reader.getNamespaceCount();
464: for (int i = 0; i < nc; i++)
465: {
466: String nsprefix = reader.getNamespacePrefix(i);
467: if ("xml".equals(nsprefix))
468: continue;
469: contentHandler.endPrefixMapping(nsprefix);
470: }
471: }
472: }
473: break;
474: case XMLStreamConstants.COMMENT:
475: if (lexicalHandler != null)
476: {
477: char[] b = reader.getTextCharacters();
478: lexicalHandler.comment(b, 0, b.length);
479: }
480: break;
481: case XMLStreamConstants.PROCESSING_INSTRUCTION:
482: if (contentHandler != null)
483: {
484: String target = reader.getPITarget();
485: String data = reader.getPIData();
486: if (data == null)
487: data = "";
488: contentHandler.processingInstruction(target, data);
489: }
490: break;
491: case XMLParser.START_ENTITY:
492: if (lexicalHandler != null)
493: {
494: String name = reader.getText();
495: lexicalHandler.startEntity(name);
496: }
497: break;
498: case XMLParser.END_ENTITY:
499: if (lexicalHandler != null)
500: {
501: String name = reader.getText();
502: lexicalHandler.endEntity(name);
503: }
504: break;
505: case XMLStreamConstants.START_DOCUMENT:
506: encoding = reader.getEncoding();
507: xmlVersion = reader.getVersion();
508: xmlStandalone = reader.isStandalone();
509: xmlEncoding = reader.getCharacterEncodingScheme();
510: if (contentHandler != null)
511: contentHandler.startDocument();
512: startDocumentDone = true;
513: break;
514: case XMLStreamConstants.END_DOCUMENT:
515: if (contentHandler != null)
516: contentHandler.endDocument();
517: break;
518: case XMLStreamConstants.DTD:
519: XMLParser.Doctype doctype = parser.doctype;
520: if (lexicalHandler != null)
521: {
522: String rootName = doctype.rootName;
523: String publicId = doctype.publicId;
524: String systemId2 = doctype.systemId;
525: lexicalHandler.startDTD(rootName, publicId, systemId2);
526: }
527: for (Iterator i = doctype.entryIterator(); i.hasNext(); )
528: {
529: String entry = (String) i.next();
530: char c = entry.charAt(0);
531: String name = entry.substring(1);
532: if ('E' == c)
533: {
534:
535: if (declHandler != null)
536: {
537: XMLParser.ContentModel model =
538: doctype.getElementModel(name);
539: declHandler.elementDecl(name, model.text);
540: }
541: }
542: else if ('A' == c)
543: {
544:
545: if (declHandler != null)
546: {
547: for (Iterator j = doctype.attlistIterator(name);
548: j.hasNext(); )
549: {
550: Map.Entry att = (Map.Entry) j.next();
551: String aname = (String) att.getKey();
552: XMLParser.AttributeDecl decl =
553: (XMLParser.AttributeDecl) att.getValue();
554: String type = decl.type;
555: String value = decl.value;
556: String mode = null;
557: switch (decl.valueType)
558: {
559: case XMLParser.ATTRIBUTE_DEFAULT_FIXED:
560: mode = "#FIXED";
561: break;
562: case XMLParser.ATTRIBUTE_DEFAULT_REQUIRED:
563: mode = "#REQUIRED";
564: break;
565: case XMLParser.ATTRIBUTE_DEFAULT_IMPLIED:
566: mode = "#IMPLIED";
567: break;
568: }
569: declHandler.attributeDecl(name, aname,
570: type, mode, value);
571: }
572: }
573: }
574: else if ('e' == c)
575: {
576:
577: Object entity = doctype.getEntity(name);
578: if (entity instanceof String)
579: {
580: if (declHandler != null)
581: declHandler.internalEntityDecl(name,
582: (String) entity);
583: }
584: else
585: {
586: XMLParser.ExternalIds ids =
587: (XMLParser.ExternalIds) entity;
588: if (ids.notationName != null)
589: {
590: if (dtdHandler != null)
591: {
592: String pub = ids.publicId;
593: String url = ids.systemId;
594: String not = ids.notationName;
595: dtdHandler.unparsedEntityDecl(name,
596: pub,
597: url,
598: not);
599: }
600: }
601: else
602: {
603: if (declHandler != null)
604: {
605: String pub = ids.publicId;
606: String url = ids.systemId;
607: declHandler.externalEntityDecl(name,
608: pub,
609: url);
610: }
611: }
612: }
613: }
614: else if ('n' == c)
615: {
616:
617: if (dtdHandler != null)
618: {
619: XMLParser.ExternalIds ids =
620: doctype.getNotation(name);
621: String pub = ids.publicId;
622: String url = ids.systemId;
623: dtdHandler.notationDecl(name, pub, url);
624: }
625: }
626: else if ('c' == c)
627: {
628:
629: if (lexicalHandler != null)
630: {
631: String comment = doctype.getComment(name);
632: char[] b = comment.toCharArray();
633: lexicalHandler.comment(b, 0, b.length);
634: }
635: }
636: else if ('p' == c)
637: {
638:
639: if (contentHandler != null)
640: {
641: String[] pi = doctype.getPI(name);
642: String target = pi[0];
643: String data = pi[1];
644: if (data == null)
645: data = "";
646: contentHandler.processingInstruction(target, data);
647: }
648: }
649: }
650: if (lexicalHandler != null)
651: lexicalHandler.endDTD();
652: }
653: }
654: reset();
655: if (opened)
656: in.close();
657: }
658: catch (Exception e)
659: {
660: SAXParseException e2 = new SAXParseException(e.getMessage(), this);
661: e2.initCause(e);
662: try
663: {
664: if (!startDocumentDone && contentHandler != null)
665: contentHandler.startDocument();
666: if (errorHandler != null)
667: errorHandler.fatalError(e2);
668: if (contentHandler != null)
669: contentHandler.endDocument();
670: }
671: catch (SAXException sex)
672: {
673:
674: }
675: reset();
676: if (opened)
677: in.close();
678: if (e instanceof SAXException)
679: throw (SAXException) e;
680: if (e instanceof IOException)
681: throw (IOException) e;
682: else
683: throw e2;
684: }
685: }
686:
687:
690: private boolean isIgnorableWhitespace(XMLParser reader, char[] b,
691: boolean testCharacters)
692: throws XMLStreamException
693: {
694: XMLParser.Doctype doctype = reader.doctype;
695: if (doctype == null)
696: return false;
697: String currentElement = reader.getCurrentElement();
698:
699: int ac = reader.getAttributeCount();
700: for (int i = 0; i < ac; i++)
701: {
702: QName aname = reader.getAttributeName(i);
703: if ("space".equals(aname.getLocalPart()) &&
704: XMLConstants.XML_NS_URI.equals(aname.getNamespaceURI()))
705: {
706: if ("preserve".equals(reader.getAttributeValue(i)))
707: return false;
708: }
709: }
710: XMLParser.ContentModel model = doctype.getElementModel(currentElement);
711: if (model == null || model.type != XMLParser.ContentModel.ELEMENT)
712: return false;
713: if (model.external && xmlStandalone)
714: return false;
715: boolean white = true;
716: if (testCharacters)
717: {
718: for (int i = 0; i < b.length; i++)
719: {
720: if (b[i] != ' ' && b[i] != '\t' && b[i] != '\n' && b[i] != '\r')
721: {
722: white = false;
723: break;
724: }
725: }
726: }
727: return white;
728: }
729:
730: public void parse(String systemId)
731: throws IOException, SAXException
732: {
733: parse(new InputSource(systemId));
734: }
735:
736:
737:
738: public int getIndex(String qName)
739: {
740: int len = reader.getAttributeCount();
741: for (int i = 0; i < len; i++)
742: {
743: QName q = reader.getAttributeName(i);
744: String localName = q.getLocalPart();
745: String prefix = q.getPrefix();
746: String qn = ("".equals(prefix)) ? localName : prefix + ":" + localName;
747: if (qName.equals(qn))
748: return i;
749: }
750: return -1;
751: }
752:
753: public int getIndex(String uri, String localName)
754: {
755: int len = reader.getAttributeCount();
756: for (int i = 0; i < len; i++)
757: {
758: QName q = reader.getAttributeName(i);
759: String ln = q.getLocalPart();
760: String u = q.getNamespaceURI();
761: if (u == null && uri != null)
762: continue;
763: if (u != null && !u.equals(uri))
764: continue;
765: if (ln.equals(localName))
766: return i;
767: }
768: return -1;
769: }
770:
771: public int getLength()
772: {
773: return reader.getAttributeCount();
774: }
775:
776: public String getLocalName(int index)
777: {
778: return reader.getAttributeLocalName(index);
779: }
780:
781: public String getQName(int index)
782: {
783: QName q = reader.getAttributeName(index);
784: String localName = q.getLocalPart();
785: String prefix = q.getPrefix();
786: return ("".equals(prefix)) ? localName : prefix + ":" + localName;
787: }
788:
789: public String getType(int index)
790: {
791: String ret = reader.getAttributeType(index);
792:
793: return ("ENUMERATION".equals(ret)) ? "NMTOKEN" : ret;
794: }
795:
796: public String getType(String qName)
797: {
798: int index = getIndex(qName);
799: return (index == -1) ? null : getType(index);
800: }
801:
802: public String getType(String uri, String localName)
803: {
804: int index = getIndex(uri, localName);
805: return (index == -1) ? null : getType(index);
806: }
807:
808: public String getURI(int index)
809: {
810: String ret = reader.getAttributeNamespace(index);
811: return (ret == null) ? "" : ret;
812: }
813:
814: public String getValue(int index)
815: {
816: return reader.getAttributeValue(index);
817: }
818:
819: public String getValue(String qName)
820: {
821: int index = getIndex(qName);
822: return (index == -1) ? null : getValue(index);
823: }
824:
825: public String getValue(String uri, String localName)
826: {
827: int index = getIndex(uri, localName);
828: return (index == -1) ? null : getValue(index);
829: }
830:
831: public boolean isDeclared(int index)
832: {
833: return parser.isAttributeDeclared(index);
834: }
835:
836: public boolean isDeclared(String qName)
837: {
838: int index = getIndex(qName);
839: return (index == -1) ? false : isDeclared(index);
840: }
841:
842: public boolean isDeclared(String uri, String localName)
843: {
844: int index = getIndex(uri, localName);
845: return (index == -1) ? false : isDeclared(index);
846: }
847:
848: public boolean isSpecified(int index)
849: {
850: return reader.isAttributeSpecified(index);
851: }
852:
853: public boolean isSpecified(String qName)
854: {
855: int index = getIndex(qName);
856: return (index == -1) ? false : isSpecified(index);
857: }
858:
859: public boolean isSpecified(String uri, String localName)
860: {
861: int index = getIndex(uri, localName);
862: return (index == -1) ? false : isSpecified(index);
863: }
864:
865:
866:
867: public int getColumnNumber()
868: {
869: Location l = reader.getLocation();
870: return l.getColumnNumber();
871: }
872:
873: public int getLineNumber()
874: {
875: Location l = reader.getLocation();
876: return l.getLineNumber();
877: }
878:
879: public String getPublicId()
880: {
881: Location l = reader.getLocation();
882: return l.getPublicId();
883: }
884:
885: public String getSystemId()
886: {
887: Location l = reader.getLocation();
888: return l.getSystemId();
889: }
890:
891: public String getEncoding()
892: {
893: return encoding;
894: }
895:
896: public String getXMLVersion()
897: {
898: return xmlVersion;
899: }
900:
901:
902:
903: public Object resolveEntity(String publicId, String systemId,
904: String baseURI, String namespace)
905: throws XMLStreamException
906: {
907: if (entityResolver != null)
908: {
909: try
910: {
911: InputSource input =
912: entityResolver.resolveEntity(publicId, systemId);
913: if (input != null)
914: {
915: InputStream in = input.getByteStream();
916: if (in == null)
917: {
918: String newSystemId = input.getSystemId();
919: if (newSystemId != null && !newSystemId.equals(systemId))
920: in = XMLParser.resolve(newSystemId);
921: }
922: return in;
923: }
924: }
925: catch (SAXException e)
926: {
927: XMLStreamException e2 = new XMLStreamException(e.getMessage());
928: e2.initCause(e);
929: throw e2;
930: }
931: catch (IOException e)
932: {
933: XMLStreamException e2 = new XMLStreamException(e.getMessage());
934: e2.initCause(e);
935: throw e2;
936: }
937: }
938: return null;
939: }
940:
941: public XMLEventReader resolveAsXMLEventReader(String uri)
942: throws XMLStreamException
943: {
944:
945: return null;
946: }
947:
948: public XMLStreamReader resolveAsXMLStreamReader(String uri)
949: throws XMLStreamException
950: {
951:
952: return null;
953: }
954:
955:
956:
957: public void report(String message, String errorType,
958: Object relatedInformation, Location location)
959: throws XMLStreamException
960: {
961: if (errorHandler != null)
962: {
963: try
964: {
965: errorHandler.warning(new SAXParseException(message, this));
966: }
967: catch (SAXException e)
968: {
969: XMLStreamException e2 = new XMLStreamException(e.getMessage());
970: e2.initCause(e);
971: throw e2;
972: }
973: }
974: }
975:
976: public static void main(String[] args)
977: throws Exception
978: {
979: boolean validating = false;
980: boolean namespaceAware = false;
981: boolean xIncludeAware = false;
982: boolean expectCallbackClass = false;
983: String callbackClass = null;
984: int pos = 0;
985: while (pos < args.length && (args[pos].startsWith("-") || expectCallbackClass))
986: {
987: if ("-x".equals(args[pos]))
988: xIncludeAware = true;
989: else if ("-v".equals(args[pos]))
990: validating = true;
991: else if ("-n".equals(args[pos]))
992: namespaceAware = true;
993: else if ("-c".equals(args[pos]))
994: expectCallbackClass = true;
995: else if (expectCallbackClass)
996: {
997: callbackClass = args[pos];
998: expectCallbackClass = false;
999: }
1000: pos++;
1001: }
1002: if (pos >= args.length || expectCallbackClass)
1003: {
1004: System.out.println("Syntax: SAXParser [-n] [-v] [-x] [-c <class>] <file> [<file2> [...]]");
1005: System.out.println("\t-n: use namespace aware mode");
1006: System.out.println("\t-v: use validating parser");
1007: System.out.println("\t-x: use XInclude aware mode");
1008: System.out.println("\t-c <class>: use specified class as callback handler (must have a no-arg public constructor)");
1009: System.exit(2);
1010: }
1011: while (pos < args.length)
1012: {
1013: ContentHandler handler = null;
1014: if (callbackClass != null)
1015: {
1016: Class t = Class.forName(callbackClass);
1017: handler = (ContentHandler) t.newInstance();
1018: }
1019: else
1020: handler = new org.xml.sax.helpers.DefaultHandler();
1021: SAXParser parser = new SAXParser(validating, namespaceAware,
1022: xIncludeAware);
1023: InputSource input = new InputSource(args[pos]);
1024: XMLReader reader = parser.getXMLReader();
1025: reader.setContentHandler(handler);
1026: reader.parse(input);
1027: pos++;
1028: }
1029: }
1030:
1031: }