01: /*
02: * <copyright>
03: *
04: * Copyright 2007 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26: package org.cougaar.bootstrap;
27:
28: import org.xml.sax.XMLReader;
29: import org.xml.sax.helpers.XMLReaderFactory;
30:
31: /**
32: * Create an XMLReader that matches the JDK version's supported parser.
33: */
34: public final class XMLReaderUtils {
35:
36: // preferred XML readers
37: private static final String[] XML_READERS = new String[] {
38: "-Dorg.xml.sax.driver", // optional system property
39: "com.sun.org.apache.xerces.internal.parsers.SAXParser", // JDK 1.5
40: "org.apache.crimson.parser.XMLReaderImpl", // JDK 1.4
41: null, // use ParserFactory, which uses -Dorg.xml.sax.parser
42: };
43:
44: private XMLReaderUtils() {
45: }
46:
47: public static XMLReader createXMLReader() {
48: for (int i = 0; i < XML_READERS.length; i++) {
49: String classname = XML_READERS[i];
50: if (classname != null && classname.startsWith("-D")) {
51: classname = SystemProperties.getProperty(classname
52: .substring(2));
53: if (classname == null) {
54: continue;
55: }
56: }
57: XMLReader ret;
58: try {
59: ret = (classname == null ? XMLReaderFactory
60: .createXMLReader() : XMLReaderFactory
61: .createXMLReader(classname));
62: } catch (Exception e) {
63: ret = null;
64: }
65: if (ret != null) {
66: // success!
67: return ret;
68: }
69: }
70: // failed
71: StringBuffer buf = new StringBuffer();
72: buf.append("Unable to create XMLReader, attempted:");
73: for (int i = 0; i < XML_READERS.length; i++) {
74: String s = XML_READERS[i];
75: if (s == null) {
76: s = "(default XMLFactory.createXMLReader())";
77: }
78: buf.append("\n ");
79: if (s.startsWith("-D")) {
80: buf.append(SystemProperties.getProperty(s)).append(
81: " (from \"").append(s).append("\")");
82: } else {
83: buf.append(s);
84: }
85: }
86: throw new RuntimeException(buf.toString());
87: }
88: }
|