1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
package org.crosswire.common.xml; |
41 | |
|
42 | |
import java.util.Locale; |
43 | |
import java.util.Map; |
44 | |
import java.util.TreeMap; |
45 | |
|
46 | |
import org.xml.sax.SAXNotRecognizedException; |
47 | |
import org.xml.sax.SAXNotSupportedException; |
48 | |
import org.xml.sax.XMLReader; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public final class XMLFeatureSet { |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | public XMLFeatureSet() { |
62 | 0 | features = new TreeMap<String, XMLFeatureState>(); |
63 | 0 | states = new TreeMap<XMLFeature, String>(); |
64 | 0 | features.put("n", new XMLFeatureState(XMLFeature.NAMESPACES, true)); |
65 | 0 | features.put("np", new XMLFeatureState(XMLFeature.NAMESPACE_PREFIX)); |
66 | 0 | features.put("v", new XMLFeatureState(XMLFeature.VALIDATION)); |
67 | 0 | features.put("xd", new XMLFeatureState(XMLFeature.LOAD_EXTERNAL_DTD, true)); |
68 | 0 | features.put("s", new XMLFeatureState(XMLFeature.SCHEMA_VALIDATION)); |
69 | 0 | features.put("f", new XMLFeatureState(XMLFeature.SCHEMA_FULL_CHECKING)); |
70 | 0 | features.put("va", new XMLFeatureState(XMLFeature.VALIDATE_ANNOTATIONS)); |
71 | 0 | features.put("dv", new XMLFeatureState(XMLFeature.DYNAMIC_VALIDATION)); |
72 | 0 | features.put("xi", new XMLFeatureState(XMLFeature.XINCLUDE)); |
73 | 0 | features.put("xb", new XMLFeatureState(XMLFeature.XINCLUDE_FIXUP_BASE_URIS, true)); |
74 | 0 | features.put("xl", new XMLFeatureState(XMLFeature.XINCLUDE_FIXUP_LANGUAGE, true)); |
75 | |
|
76 | 0 | for (Map.Entry<String, XMLFeatureState> entry : features.entrySet()) { |
77 | 0 | states.put(entry.getValue().getFeature(), entry.getKey()); |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void setFeatureState(XMLFeature feature, boolean state) { |
88 | 0 | features.get(states.get(feature)).setState(state); |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void setFeatureStates(String[] argv) { |
98 | |
|
99 | 0 | for (int i = 0; i < argv.length; i++) { |
100 | 0 | String arg = argv[i]; |
101 | 0 | if (arg.charAt(0) == '-') { |
102 | 0 | String option = arg.substring(1); |
103 | 0 | String key = option.toLowerCase(Locale.ENGLISH); |
104 | 0 | XMLFeatureState feature = features.get(key); |
105 | 0 | if (feature != null) { |
106 | 0 | feature.setState(option.equals(key)); |
107 | |
} |
108 | |
} |
109 | |
} |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
@Override |
116 | |
public String toString() { |
117 | 0 | StringBuilder buf = new StringBuilder(); |
118 | 0 | buf.append('\n'); |
119 | 0 | for (XMLFeatureState state : features.values()) { |
120 | 0 | buf.append(state.getFeature().toString()).append('\n'); |
121 | |
} |
122 | 0 | return buf.toString(); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void printUsage() { |
129 | 0 | System.err.println("XML Feature Set options:"); |
130 | 0 | System.err.println(" -n | -N Turn on/off namespace processing."); |
131 | 0 | System.err.println(" -np | -NP Turn on/off namespace prefixes."); |
132 | 0 | System.err.println(" NOTE: Requires use of -n."); |
133 | 0 | System.err.println(" -v | -V Turn on/off validation."); |
134 | 0 | System.err.println(" -xd | -XD Turn on/off loading of external DTDs."); |
135 | 0 | System.err.println(" NOTE: Always on when -v in use and not supported by all parsers."); |
136 | 0 | System.err.println(" -s | -S Turn on/off Schema validation support."); |
137 | 0 | System.err.println(" NOTE: Not supported by all parsers."); |
138 | 0 | System.err.println(" -f | -F Turn on/off Schema full checking."); |
139 | 0 | System.err.println(" NOTE: Requires use of -s and not supported by all parsers."); |
140 | 0 | System.err.println(" -va | -VA Turn on/off validation of schema annotations."); |
141 | 0 | System.err.println(" NOTE: Requires use of -s and not supported by all parsers."); |
142 | 0 | System.err.println(" -dv | -DV Turn on/off dynamic validation."); |
143 | 0 | System.err.println(" NOTE: Not supported by all parsers."); |
144 | 0 | System.err.println(" -xi | -XI Turn on/off XInclude processing."); |
145 | 0 | System.err.println(" NOTE: Not supported by all parsers."); |
146 | 0 | System.err.println(" -xb | -XB Turn on/off base URI fixup during XInclude processing."); |
147 | 0 | System.err.println(" NOTE: Requires use of -xi and not supported by all parsers."); |
148 | 0 | System.err.println(" -xl | -XL Turn on/off language fixup during XInclude processing."); |
149 | 0 | System.err.println(" NOTE: Requires use of -xi and not supported by all parsers."); |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public void setFeatures(XMLReader parser) { |
158 | 0 | for (XMLFeatureState state : features.values()) { |
159 | 0 | state.setFeature(parser); |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private static class XMLFeatureState { |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | 0 | XMLFeatureState(XMLFeature feature, boolean state) { |
174 | 0 | this.feature = feature; |
175 | 0 | this.state = state; |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
XMLFeatureState(XMLFeature feature) { |
183 | 0 | this(feature, false); |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public XMLFeature getFeature() { |
190 | 0 | return feature; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public void setState(boolean newState) { |
199 | 0 | state = newState; |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public void setFeature(XMLReader parser) { |
208 | 0 | String control = feature.getControl(); |
209 | |
try { |
210 | 0 | parser.setFeature(control, state); |
211 | 0 | } catch (SAXNotRecognizedException e) { |
212 | 0 | System.err.println("warning: Parser does not recognize feature (" + control + ")"); |
213 | 0 | } catch (SAXNotSupportedException e) { |
214 | 0 | System.err.println("warning: Parser does not support feature (" + control + ")"); |
215 | 0 | } |
216 | 0 | } |
217 | |
|
218 | |
private boolean state; |
219 | |
private XMLFeature feature; |
220 | |
} |
221 | |
|
222 | |
private Map<String, XMLFeatureState> features; |
223 | |
private Map<XMLFeature, String> states; |
224 | |
} |