1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | * $Id: SAX2Print.cpp 833057 2009-11-05 15:25:10Z borisk $ |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | // --------------------------------------------------------------------------- |
---|
24 | // Includes |
---|
25 | // --------------------------------------------------------------------------- |
---|
26 | #include <xercesc/util/PlatformUtils.hpp> |
---|
27 | #include <xercesc/util/TransService.hpp> |
---|
28 | #include <xercesc/sax2/SAX2XMLReader.hpp> |
---|
29 | #include <xercesc/sax2/XMLReaderFactory.hpp> |
---|
30 | #include "SAX2Print.hpp" |
---|
31 | #include <xercesc/util/OutOfMemoryException.hpp> |
---|
32 | #include "SAX2FilterHandlers.hpp" |
---|
33 | |
---|
34 | // --------------------------------------------------------------------------- |
---|
35 | // Local data |
---|
36 | // |
---|
37 | // encodingName |
---|
38 | // The encoding we are to output in. If not set on the command line, |
---|
39 | // then it is defaulted to LATIN1. |
---|
40 | // |
---|
41 | // xmlFile |
---|
42 | // The path to the file to parser. Set via command line. |
---|
43 | // |
---|
44 | // valScheme |
---|
45 | // Indicates what validation scheme to use. It defaults to 'auto', but |
---|
46 | // can be set via the -v= command. |
---|
47 | // |
---|
48 | // expandNamespaces |
---|
49 | // Indicates if the output should expand the namespaces Alias with |
---|
50 | // their URI's, defaults to false, can be set via the command line -e |
---|
51 | // --------------------------------------------------------------------------- |
---|
52 | static const char* encodingName = "LATIN1"; |
---|
53 | static XMLFormatter::UnRepFlags unRepFlags = XMLFormatter::UnRep_CharRef; |
---|
54 | static char* xmlFile = 0; |
---|
55 | static SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto; |
---|
56 | static bool expandNamespaces= false ; |
---|
57 | static bool doNamespaces = true; |
---|
58 | static bool doSchema = true; |
---|
59 | static bool schemaFullChecking = false; |
---|
60 | static bool namespacePrefixes = false; |
---|
61 | static bool sortAttributes = false; |
---|
62 | |
---|
63 | |
---|
64 | // --------------------------------------------------------------------------- |
---|
65 | // Local helper methods |
---|
66 | // --------------------------------------------------------------------------- |
---|
67 | static void usage() |
---|
68 | { |
---|
69 | XERCES_STD_QUALIFIER cout << "\nUsage:\n" |
---|
70 | " SAX2Print [options] <XML file>\n\n" |
---|
71 | "This program invokes the SAX2XMLReader, and then prints the\n" |
---|
72 | "data returned by the various SAX2 handlers for the specified\n" |
---|
73 | "XML file.\n\n" |
---|
74 | "Options:\n" |
---|
75 | " -u=xxx Handle unrepresentable chars [fail | rep | ref*].\n" |
---|
76 | " -v=xxx Validation scheme [always | never | auto*].\n" |
---|
77 | " -e Expand Namespace Alias with URI's. Defaults to off.\n" |
---|
78 | " -x=XXX Use a particular encoding for output (LATIN1*).\n" |
---|
79 | " -f Enable full schema constraint checking processing. Defaults to off.\n" |
---|
80 | " -p Enable namespace-prefixes feature. Defaults to off.\n" |
---|
81 | " -n Disable namespace processing. Defaults to on.\n" |
---|
82 | " NOTE: THIS IS OPPOSITE FROM OTHER SAMPLES.\n" |
---|
83 | " -s Disable schema processing. Defaults to on.\n" |
---|
84 | " NOTE: THIS IS OPPOSITE FROM OTHER SAMPLES.\n" |
---|
85 | " -sa Print the attributes in alphabetic order. Defaults to off.\n" |
---|
86 | " -? Show this help.\n\n" |
---|
87 | " * = Default if not provided explicitly.\n\n" |
---|
88 | "The parser has intrinsic support for the following encodings:\n" |
---|
89 | " UTF-8, US-ASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,\n" |
---|
90 | " WINDOWS-1252, IBM1140, IBM037, IBM1047.\n" |
---|
91 | << XERCES_STD_QUALIFIER endl; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | // --------------------------------------------------------------------------- |
---|
97 | // Program entry point |
---|
98 | // --------------------------------------------------------------------------- |
---|
99 | int main(int argC, char* argV[]) |
---|
100 | { |
---|
101 | // Initialize the XML4C2 system |
---|
102 | try |
---|
103 | { |
---|
104 | XMLPlatformUtils::Initialize(); |
---|
105 | } |
---|
106 | |
---|
107 | catch (const XMLException& toCatch) |
---|
108 | { |
---|
109 | XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n" |
---|
110 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; |
---|
111 | return 1; |
---|
112 | } |
---|
113 | |
---|
114 | // Check command line and extract arguments. |
---|
115 | if (argC < 2) |
---|
116 | { |
---|
117 | usage(); |
---|
118 | XMLPlatformUtils::Terminate(); |
---|
119 | return 1; |
---|
120 | } |
---|
121 | |
---|
122 | int parmInd; |
---|
123 | for (parmInd = 1; parmInd < argC; parmInd++) |
---|
124 | { |
---|
125 | // Break out on first parm not starting with a dash |
---|
126 | if (argV[parmInd][0] != '-') |
---|
127 | break; |
---|
128 | |
---|
129 | // Watch for special case help request |
---|
130 | if (!strcmp(argV[parmInd], "-?")) |
---|
131 | { |
---|
132 | usage(); |
---|
133 | XMLPlatformUtils::Terminate(); |
---|
134 | return 2; |
---|
135 | } |
---|
136 | else if (!strncmp(argV[parmInd], "-v=", 3) |
---|
137 | || !strncmp(argV[parmInd], "-V=", 3)) |
---|
138 | { |
---|
139 | const char* const parm = &argV[parmInd][3]; |
---|
140 | |
---|
141 | if (!strcmp(parm, "never")) |
---|
142 | valScheme = SAX2XMLReader::Val_Never; |
---|
143 | else if (!strcmp(parm, "auto")) |
---|
144 | valScheme = SAX2XMLReader::Val_Auto; |
---|
145 | else if (!strcmp(parm, "always")) |
---|
146 | valScheme = SAX2XMLReader::Val_Always; |
---|
147 | else |
---|
148 | { |
---|
149 | XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl; |
---|
150 | XMLPlatformUtils::Terminate(); |
---|
151 | return 2; |
---|
152 | } |
---|
153 | } |
---|
154 | else if (!strcmp(argV[parmInd], "-e") |
---|
155 | || !strcmp(argV[parmInd], "-E")) |
---|
156 | { |
---|
157 | expandNamespaces = true; |
---|
158 | } |
---|
159 | else if (!strncmp(argV[parmInd], "-x=", 3) |
---|
160 | || !strncmp(argV[parmInd], "-X=", 3)) |
---|
161 | { |
---|
162 | // Get out the encoding name |
---|
163 | encodingName = &argV[parmInd][3]; |
---|
164 | } |
---|
165 | else if (!strncmp(argV[parmInd], "-u=", 3) |
---|
166 | || !strncmp(argV[parmInd], "-U=", 3)) |
---|
167 | { |
---|
168 | const char* const parm = &argV[parmInd][3]; |
---|
169 | |
---|
170 | if (!strcmp(parm, "fail")) |
---|
171 | unRepFlags = XMLFormatter::UnRep_Fail; |
---|
172 | else if (!strcmp(parm, "rep")) |
---|
173 | unRepFlags = XMLFormatter::UnRep_Replace; |
---|
174 | else if (!strcmp(parm, "ref")) |
---|
175 | unRepFlags = XMLFormatter::UnRep_CharRef; |
---|
176 | else |
---|
177 | { |
---|
178 | XERCES_STD_QUALIFIER cerr << "Unknown -u= value: " << parm << XERCES_STD_QUALIFIER endl; |
---|
179 | XMLPlatformUtils::Terminate(); |
---|
180 | return 2; |
---|
181 | } |
---|
182 | } |
---|
183 | else if (!strcmp(argV[parmInd], "-n") |
---|
184 | || !strcmp(argV[parmInd], "-N")) |
---|
185 | { |
---|
186 | doNamespaces = false; |
---|
187 | } |
---|
188 | else if (!strcmp(argV[parmInd], "-s") |
---|
189 | || !strcmp(argV[parmInd], "-S")) |
---|
190 | { |
---|
191 | doSchema = false; |
---|
192 | } |
---|
193 | else if (!strcmp(argV[parmInd], "-f") |
---|
194 | || !strcmp(argV[parmInd], "-F")) |
---|
195 | { |
---|
196 | schemaFullChecking = true; |
---|
197 | } |
---|
198 | else if (!strcmp(argV[parmInd], "-p") |
---|
199 | || !strcmp(argV[parmInd], "-P")) |
---|
200 | { |
---|
201 | namespacePrefixes = true; |
---|
202 | } |
---|
203 | else if (!strcmp(argV[parmInd], "-sa")) |
---|
204 | { |
---|
205 | sortAttributes = true; |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd] |
---|
210 | << "', ignoring it\n" << XERCES_STD_QUALIFIER endl; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | // |
---|
215 | // And now we have to have only one parameter left and it must be |
---|
216 | // the file name. |
---|
217 | // |
---|
218 | if (parmInd + 1 != argC) |
---|
219 | { |
---|
220 | usage(); |
---|
221 | XMLPlatformUtils::Terminate(); |
---|
222 | return 1; |
---|
223 | } |
---|
224 | xmlFile = argV[parmInd]; |
---|
225 | |
---|
226 | // |
---|
227 | // Create a SAX parser object. Then, according to what we were told on |
---|
228 | // the command line, set it to validate or not. |
---|
229 | // |
---|
230 | SAX2XMLReader* parser; |
---|
231 | SAX2XMLReader* reader = XMLReaderFactory::createXMLReader(); |
---|
232 | SAX2XMLReader* filter = NULL; |
---|
233 | if(sortAttributes) |
---|
234 | { |
---|
235 | filter=new SAX2SortAttributesFilter(reader); |
---|
236 | parser=filter; |
---|
237 | } |
---|
238 | else |
---|
239 | parser=reader; |
---|
240 | |
---|
241 | // |
---|
242 | // Then, according to what we were told on |
---|
243 | // the command line, set it to validate or not. |
---|
244 | // |
---|
245 | if (valScheme == SAX2XMLReader::Val_Auto) |
---|
246 | { |
---|
247 | parser->setFeature(XMLUni::fgSAX2CoreValidation, true); |
---|
248 | parser->setFeature(XMLUni::fgXercesDynamic, true); |
---|
249 | } |
---|
250 | |
---|
251 | if (valScheme == SAX2XMLReader::Val_Never) |
---|
252 | { |
---|
253 | parser->setFeature(XMLUni::fgSAX2CoreValidation, false); |
---|
254 | } |
---|
255 | |
---|
256 | if (valScheme == SAX2XMLReader::Val_Always) |
---|
257 | { |
---|
258 | parser->setFeature(XMLUni::fgSAX2CoreValidation, true); |
---|
259 | parser->setFeature(XMLUni::fgXercesDynamic, false); |
---|
260 | } |
---|
261 | |
---|
262 | parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces); |
---|
263 | parser->setFeature(XMLUni::fgXercesSchema, doSchema); |
---|
264 | parser->setFeature(XMLUni::fgXercesHandleMultipleImports, true); |
---|
265 | parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking); |
---|
266 | parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes); |
---|
267 | |
---|
268 | // |
---|
269 | // Create the handler object and install it as the document and error |
---|
270 | // handler for the parser. Then parse the file and catch any exceptions |
---|
271 | // that propogate out |
---|
272 | // |
---|
273 | |
---|
274 | int errorCount = 0; |
---|
275 | int errorCode = 0; |
---|
276 | try |
---|
277 | { |
---|
278 | SAX2PrintHandlers handler(encodingName, unRepFlags, expandNamespaces); |
---|
279 | parser->setContentHandler(&handler); |
---|
280 | parser->setErrorHandler(&handler); |
---|
281 | parser->parse(xmlFile); |
---|
282 | errorCount = parser->getErrorCount(); |
---|
283 | } |
---|
284 | catch (const OutOfMemoryException&) |
---|
285 | { |
---|
286 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; |
---|
287 | errorCode = 5; |
---|
288 | } |
---|
289 | catch (const XMLException& toCatch) |
---|
290 | { |
---|
291 | XERCES_STD_QUALIFIER cerr << "\nAn error occurred\n Error: " |
---|
292 | << StrX(toCatch.getMessage()) |
---|
293 | << "\n" << XERCES_STD_QUALIFIER endl; |
---|
294 | errorCode = 4; |
---|
295 | } |
---|
296 | |
---|
297 | if(errorCode) { |
---|
298 | XMLPlatformUtils::Terminate(); |
---|
299 | return errorCode; |
---|
300 | } |
---|
301 | |
---|
302 | // |
---|
303 | // Delete the parser itself. Must be done prior to calling Terminate, below. |
---|
304 | // |
---|
305 | delete reader; |
---|
306 | if(filter) |
---|
307 | delete filter; |
---|
308 | |
---|
309 | // And call the termination method |
---|
310 | XMLPlatformUtils::Terminate(); |
---|
311 | |
---|
312 | if (errorCount > 0) |
---|
313 | return 4; |
---|
314 | else |
---|
315 | return 0; |
---|
316 | } |
---|