1:
37: package ;
38:
39: import ;
40: import ;
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47:
48:
53: public class ImplementationSource
54: implements DOMImplementationSource
55: {
56:
57: private static final String DIGITS = "1234567890";
58:
59:
62: private static final DOMImplementation[] implementations;
63:
64: static
65: {
66: List acc = new ArrayList();
67: acc.add(new gnu.xml.dom.DomImpl());
68: try
69: {
70: Class t = Class.forName("gnu.xml.libxmlj.dom.GnomeDocumentBuilder");
71: acc.add(t.newInstance());
72: }
73: catch (Exception e)
74: {
75:
76: }
77: catch (UnsatisfiedLinkError e)
78: {
79:
80: }
81: implementations = new DOMImplementation[acc.size()];
82: acc.toArray(implementations);
83: }
84:
85: public DOMImplementation getDOMImplementation(String features)
86: {
87: List available = getImplementations(features);
88: if (available.isEmpty())
89: {
90: return null;
91: }
92: return (DOMImplementation) available.get(0);
93: }
94:
95: public DOMImplementationList getDOMImplementationList(String features)
96: {
97: List available = getImplementations(features);
98: return new ImplementationList(available);
99: }
100:
101:
105: private final List getImplementations(String features)
106: {
107: List available = new ArrayList(Arrays.asList(implementations));
108: for (Iterator i = parseFeatures(features).iterator(); i.hasNext(); )
109: {
110: String feature = (String) i.next();
111: String version = null;
112: int si = feature.indexOf(' ');
113: if (si != -1)
114: {
115: version = feature.substring(si + 1);
116: feature = feature.substring(0, si);
117: }
118: for (Iterator j = available.iterator(); j.hasNext(); )
119: {
120: DOMImplementation impl = (DOMImplementation) j.next();
121: if (!impl.hasFeature(feature, version))
122: {
123: j.remove();
124: }
125: }
126: }
127: return available;
128: }
129:
130:
133: final List parseFeatures(String features)
134: {
135: List list = new ArrayList();
136: int pos = 0, start = 0;
137: int len = features.length();
138: for (; pos < len; pos++)
139: {
140: char c = features.charAt(pos);
141: if (c == ' ')
142: {
143: if (pos + 1 < len &&
144: DIGITS.indexOf(features.charAt(pos + 1)) == -1)
145: {
146: list.add(getFeature(features, start, pos));
147: start = pos + 1;
148: }
149: }
150: }
151: if (pos > start)
152: {
153: list.add(getFeature(features, start, len));
154: }
155: return list;
156: }
157:
158: final String getFeature(String features, int start, int end)
159: {
160: if (features.length() > 0 && features.charAt(start) == '+')
161: {
162: return features.substring(start + 1, end);
163: }
164: return features.substring(start, end);
165: }
166:
167: }