1 | /*
|
---|
2 | * Copyright 2003,2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | //REVISIT
|
---|
18 | /*
|
---|
19 | * $Log: PSVIWriter.cpp,v $
|
---|
20 | * Revision 1.5 2004/09/08 13:55:32 peiyongz
|
---|
21 | * Apache License Version 2.0
|
---|
22 | *
|
---|
23 | * Revision 1.4 2004/09/02 14:59:29 cargilld
|
---|
24 | * Add OutOfMemoryException block to samples.
|
---|
25 | *
|
---|
26 | * Revision 1.3 2004/02/10 17:36:38 cargilld
|
---|
27 | * Fix build problems.
|
---|
28 | *
|
---|
29 | * Revision 1.2 2004/02/09 13:23:45 cargilld
|
---|
30 | * Fix build problems and update usage.
|
---|
31 | *
|
---|
32 | * Revision 1.1 2004/02/04 20:11:54 peiyongz
|
---|
33 | * PSVIWriter
|
---|
34 | *
|
---|
35 | *
|
---|
36 | * $Id: PSVIWriter
|
---|
37 | *
|
---|
38 | */
|
---|
39 |
|
---|
40 |
|
---|
41 | // ---------------------------------------------------------------------------
|
---|
42 | // Includes
|
---|
43 | // ---------------------------------------------------------------------------
|
---|
44 | #include "PSVIWriter.hpp"
|
---|
45 | #include <xercesc/framework/StdOutFormatTarget.hpp>
|
---|
46 | #include <xercesc/framework/LocalFileFormatTarget.hpp>
|
---|
47 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
48 | #include <xercesc/util/TransService.hpp>
|
---|
49 | #include <xercesc/util/XMLString.hpp>
|
---|
50 | #include <xercesc/sax2/SAX2XMLReader.hpp>
|
---|
51 | #include <xercesc/sax2/XMLReaderFactory.hpp>
|
---|
52 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
53 |
|
---|
54 | #if defined(XERCES_NEW_IOSTREAMS)
|
---|
55 | #include <fstream>
|
---|
56 | #else
|
---|
57 | #include <fstream.h>
|
---|
58 | #endif
|
---|
59 | // ---------------------------------------------------------------------------
|
---|
60 | // Local helper methods
|
---|
61 | // ---------------------------------------------------------------------------
|
---|
62 | void usage()
|
---|
63 | {
|
---|
64 | XERCES_STD_QUALIFIER cout << "\nUsage:\n"
|
---|
65 | " PSVIWriter [options] <XML file | List file>\n\n"
|
---|
66 | "This program invokes the SAX2XMLReaderImpl, and then exposes the\n"
|
---|
67 | "underlying PSVI of each parsed XML file, using SAX2 API.\n\n"
|
---|
68 | "Options:\n"
|
---|
69 | " -f Enable full schema constraint checking processing. Defaults to off.\n"
|
---|
70 | " -o=xxx Output PSVI to file xxx (default is stdout)\n"
|
---|
71 | " -e=xxx Output errors to file xxx (default is stdout)\n"
|
---|
72 | " -u=xxx Handle unrepresentable chars [fail | rep | ref*].\n"
|
---|
73 | " -x=XXX Use a particular encoding for output (UTF8*).\n"
|
---|
74 | " -l Indicate the input file is a List File that has a list of xml files.\n"
|
---|
75 | " Default to off (Input file is an XML file).\n"
|
---|
76 | " -? Show this help.\n\n"
|
---|
77 | " * = Default if not provided explicitly.\n"
|
---|
78 | << XERCES_STD_QUALIFIER endl;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | // ---------------------------------------------------------------------------
|
---|
83 | // Program entry point
|
---|
84 | // ---------------------------------------------------------------------------
|
---|
85 | int main(int argC, char* argV[])
|
---|
86 | {
|
---|
87 |
|
---|
88 | // Check command line and extract arguments.
|
---|
89 | if (argC < 2)
|
---|
90 | {
|
---|
91 | usage();
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static const char* encodingName = "UTF8";
|
---|
96 | static XMLFormatter::UnRepFlags unRepFlags = XMLFormatter::UnRep_CharRef;
|
---|
97 | const char* xmlFile = 0;
|
---|
98 | SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto;
|
---|
99 | bool doList = false; //REVISIT
|
---|
100 | bool schemaFullChecking = false;
|
---|
101 | bool errorOccurred = false;
|
---|
102 | const char* psviOut = 0;
|
---|
103 | const char* errorOut = 0;
|
---|
104 | const char* output = 0;
|
---|
105 | XMLFormatTarget* psviTarget = 0;
|
---|
106 | XMLFormatTarget* errorTarget = 0;
|
---|
107 | XMLFormatter* psviFormatter = 0;
|
---|
108 | XMLFormatter* errorFormatter = 0;
|
---|
109 | char fileName[80] ="";
|
---|
110 |
|
---|
111 |
|
---|
112 | int argInd;
|
---|
113 | for (argInd = 1; argInd < argC; argInd++)
|
---|
114 | {
|
---|
115 | // Break out on first parm not starting with a dash
|
---|
116 | if (argV[argInd][0] != '-')
|
---|
117 | break;
|
---|
118 |
|
---|
119 | // Watch for special case help request
|
---|
120 | if (!strcmp(argV[argInd], "-?"))
|
---|
121 | {
|
---|
122 | usage();
|
---|
123 | return 2;
|
---|
124 | }
|
---|
125 | else if (!strcmp(argV[argInd], "-l")
|
---|
126 | || !strcmp(argV[argInd], "-L"))
|
---|
127 | {
|
---|
128 | doList = true;
|
---|
129 | }
|
---|
130 | else if (!strcmp(argV[argInd], "-f")
|
---|
131 | || !strcmp(argV[argInd], "-F"))
|
---|
132 | {
|
---|
133 | schemaFullChecking = true;
|
---|
134 | }
|
---|
135 | else if (!strncmp(argV[argInd], "-o=", 3)
|
---|
136 | || !strncmp(argV[argInd], "-O=", 3))
|
---|
137 | {
|
---|
138 | psviOut = &argV[argInd][3];
|
---|
139 | }
|
---|
140 | else if (!strncmp(argV[argInd], "-e=", 3)
|
---|
141 | || !strncmp(argV[argInd], "-E=", 3))
|
---|
142 | {
|
---|
143 | errorOut = &argV[argInd][3];
|
---|
144 | }
|
---|
145 | else if (!strncmp(argV[argInd], "-x=", 3)
|
---|
146 | || !strncmp(argV[argInd], "-X=", 3))
|
---|
147 | {
|
---|
148 | // Get out the encoding name
|
---|
149 | encodingName = &argV[argInd][3];
|
---|
150 | }
|
---|
151 | else if (!strncmp(argV[argInd], "-u=", 3)
|
---|
152 | || !strncmp(argV[argInd], "-U=", 3))
|
---|
153 | {
|
---|
154 | const char* const parm = &argV[argInd][3];
|
---|
155 |
|
---|
156 | if (!strcmp(parm, "fail"))
|
---|
157 | unRepFlags = XMLFormatter::UnRep_Fail;
|
---|
158 | else if (!strcmp(parm, "rep"))
|
---|
159 | unRepFlags = XMLFormatter::UnRep_Replace;
|
---|
160 | else if (!strcmp(parm, "ref"))
|
---|
161 | unRepFlags = XMLFormatter::UnRep_CharRef;
|
---|
162 | else
|
---|
163 | {
|
---|
164 | XERCES_STD_QUALIFIER cerr << "Unknown -u= value: " << parm << XERCES_STD_QUALIFIER endl;
|
---|
165 | XMLPlatformUtils::Terminate();
|
---|
166 | return 2;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | else
|
---|
170 | {
|
---|
171 | XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[argInd]
|
---|
172 | << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | //
|
---|
177 | // There should be only one and only one parameter left, and that
|
---|
178 | // should be the file name.
|
---|
179 | //
|
---|
180 | if (argInd != argC - 1)
|
---|
181 | {
|
---|
182 | usage();
|
---|
183 | return 1;
|
---|
184 | }
|
---|
185 |
|
---|
186 | // Initialize the XML4C2 system
|
---|
187 | try
|
---|
188 | {
|
---|
189 | XMLPlatformUtils::Initialize();
|
---|
190 | } catch (const XMLException& toCatch) {
|
---|
191 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
192 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
193 | return 1;
|
---|
194 | }
|
---|
195 |
|
---|
196 | //
|
---|
197 | // Create a SAX parser object, then set it to validate or not.
|
---|
198 | //
|
---|
199 | SAX2XMLReaderImpl* parser = new SAX2XMLReaderImpl();
|
---|
200 | parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
|
---|
201 | parser->setFeature(XMLUni::fgXercesSchema, true);
|
---|
202 | parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
|
---|
203 | parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, false);
|
---|
204 | parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
|
---|
205 | parser->setFeature(XMLUni::fgXercesDynamic, true);
|
---|
206 |
|
---|
207 | //
|
---|
208 | // Based on commandline arguments, create XMLFormatters for PSVI output and errors
|
---|
209 | //
|
---|
210 | if (!doList) {
|
---|
211 | if (psviOut==0) {
|
---|
212 | psviTarget = new StdOutFormatTarget();
|
---|
213 | } else {
|
---|
214 | psviTarget = new LocalFileFormatTarget(psviOut);
|
---|
215 | }
|
---|
216 | psviFormatter = new XMLFormatter(encodingName, psviTarget, XMLFormatter::NoEscapes, unRepFlags);
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (errorOut==0) {
|
---|
220 | errorTarget = new StdOutFormatTarget();
|
---|
221 | } else {
|
---|
222 | errorTarget = new LocalFileFormatTarget(errorOut);
|
---|
223 | }
|
---|
224 | errorFormatter = new XMLFormatter(encodingName, errorTarget, XMLFormatter::NoEscapes, unRepFlags);
|
---|
225 |
|
---|
226 | //
|
---|
227 | // Create our SAX handler object and install it as the handlers
|
---|
228 | //
|
---|
229 |
|
---|
230 | PSVIWriterHandlers* handler;
|
---|
231 | if (doList)
|
---|
232 | handler = new PSVIWriterHandlers(0, errorFormatter);
|
---|
233 | else
|
---|
234 | handler = new PSVIWriterHandlers(psviFormatter, errorFormatter);
|
---|
235 |
|
---|
236 | parser->setPSVIHandler(handler);
|
---|
237 | parser->setContentHandler(handler);
|
---|
238 | parser->setLexicalHandler(handler);
|
---|
239 | parser->setXMLEntityResolver(handler);
|
---|
240 | parser->setErrorHandler(handler);
|
---|
241 |
|
---|
242 | //
|
---|
243 | // Get the starting time and kick off the parse of the indicated
|
---|
244 | // file. Catch any exceptions that might propogate out of it.
|
---|
245 | //
|
---|
246 | unsigned long duration;
|
---|
247 |
|
---|
248 | bool more = true;
|
---|
249 | XERCES_STD_QUALIFIER ifstream fin;
|
---|
250 |
|
---|
251 | // the input is a list file
|
---|
252 | if (doList) //REVISIT
|
---|
253 | fin.open(argV[argInd]);
|
---|
254 |
|
---|
255 | if (fin.fail()) {
|
---|
256 | XERCES_STD_QUALIFIER cerr <<"Cannot open the list file: " << argV[argInd] << XERCES_STD_QUALIFIER endl;
|
---|
257 | return 2;
|
---|
258 | }
|
---|
259 |
|
---|
260 | while (more) //REVISIT
|
---|
261 | {
|
---|
262 | char fURI[1000];
|
---|
263 | //initialize the array to zeros
|
---|
264 | memset(fURI,0,sizeof(fURI));
|
---|
265 |
|
---|
266 | if (doList) {
|
---|
267 | if (! fin.eof() ) {
|
---|
268 | fin.getline (fURI, sizeof(fURI));
|
---|
269 | if (!*fURI)
|
---|
270 | continue;
|
---|
271 | else {
|
---|
272 | xmlFile =fURI;
|
---|
273 | XMLString::trim((char*)xmlFile);
|
---|
274 | XERCES_STD_QUALIFIER cerr << "==Parsing== \"" << xmlFile << "\"" << XERCES_STD_QUALIFIER endl;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if (psviOut==0) {
|
---|
278 | if (psviTarget==0 && psviFormatter==0) {
|
---|
279 | psviTarget = new StdOutFormatTarget();
|
---|
280 | psviFormatter = new XMLFormatter(encodingName, psviTarget, XMLFormatter::NoEscapes, unRepFlags);
|
---|
281 | handler->resetPSVIFormatter(psviFormatter);
|
---|
282 | }
|
---|
283 | } else {
|
---|
284 | strcpy(fileName, psviOut);
|
---|
285 | if (strrchr(xmlFile, '\\')>strrchr(xmlFile, '/')) {
|
---|
286 | strcat(fileName, strrchr(xmlFile, '\\'));
|
---|
287 | } else {
|
---|
288 | strcat(fileName, strrchr(xmlFile, '/'));
|
---|
289 | }
|
---|
290 | if (psviFormatter)
|
---|
291 | delete psviFormatter;
|
---|
292 | if (psviTarget)
|
---|
293 | delete psviTarget;
|
---|
294 | psviTarget = new LocalFileFormatTarget(fileName);
|
---|
295 | psviFormatter = new XMLFormatter(encodingName, psviTarget, XMLFormatter::NoEscapes, unRepFlags);
|
---|
296 | handler->resetPSVIFormatter(psviFormatter);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | else
|
---|
300 | break;
|
---|
301 | }
|
---|
302 | else {
|
---|
303 | xmlFile = argV[argInd];
|
---|
304 | more = false; //REVISIT
|
---|
305 | }
|
---|
306 |
|
---|
307 | //reset error count first
|
---|
308 | handler->resetErrors();
|
---|
309 |
|
---|
310 | try
|
---|
311 | {
|
---|
312 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
313 | parser->parse(xmlFile);
|
---|
314 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
315 | duration = endMillis - startMillis;
|
---|
316 | }
|
---|
317 | catch (const OutOfMemoryException&)
|
---|
318 | {
|
---|
319 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
320 | errorOccurred = true;
|
---|
321 | continue;
|
---|
322 | }
|
---|
323 | catch (const XMLException& e)
|
---|
324 | {
|
---|
325 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n"
|
---|
326 | << "Exception message is: \n"
|
---|
327 | << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
328 | errorOccurred = true;
|
---|
329 | continue;
|
---|
330 | }
|
---|
331 |
|
---|
332 | catch (...)
|
---|
333 | {
|
---|
334 | XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n" << XERCES_STD_QUALIFIER endl;;
|
---|
335 | errorOccurred = true;
|
---|
336 | continue;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (doList) //REVISIT
|
---|
341 | fin.close();
|
---|
342 |
|
---|
343 | //
|
---|
344 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
345 | //
|
---|
346 | delete parser;
|
---|
347 | delete handler;
|
---|
348 | delete psviFormatter;
|
---|
349 | delete errorFormatter;
|
---|
350 | delete psviTarget;
|
---|
351 | delete errorTarget;
|
---|
352 |
|
---|
353 | // And call the termination method
|
---|
354 | XMLPlatformUtils::Terminate();
|
---|
355 |
|
---|
356 | if (errorOccurred)
|
---|
357 | return 4;
|
---|
358 | else
|
---|
359 | return 0;
|
---|
360 |
|
---|
361 | }
|
---|
362 |
|
---|