1 | /*
|
---|
2 | * Copyright 1999-2000,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 |
|
---|
18 | /**
|
---|
19 | *
|
---|
20 | * This simplistic sample illustrates how an XML application can use
|
---|
21 | * the SAX entityResolver handler to provide customized handling for
|
---|
22 | * external entities.
|
---|
23 | *
|
---|
24 | * It registers an entity resolver with the parser. When ever the parser
|
---|
25 | * comes across an external entity, like a reference to an external DTD
|
---|
26 | * file, it calls the 'resolveEntity()' callback. This callback in this
|
---|
27 | * sample checks to see if the external entity to be resolved is the file
|
---|
28 | * 'personal.dtd'.
|
---|
29 | *
|
---|
30 | * If it is then, it redirects the input stream to the file 'redirect.dtd',
|
---|
31 | * which is then read instead of 'personal.dtd'.
|
---|
32 | *
|
---|
33 | * If the external entity to be resolved was not the file 'personal.dtd', the
|
---|
34 | * callback returns NULL indicating that do the default behaviour which is
|
---|
35 | * to read the contents of 'personal.dtd'.
|
---|
36 | *
|
---|
37 | * $Log: Redirect.cpp,v $
|
---|
38 | * Revision 1.11 2004/09/08 13:55:33 peiyongz
|
---|
39 | * Apache License Version 2.0
|
---|
40 | *
|
---|
41 | * Revision 1.10 2004/09/02 14:59:29 cargilld
|
---|
42 | * Add OutOfMemoryException block to samples.
|
---|
43 | *
|
---|
44 | * Revision 1.9 2003/08/07 21:21:38 neilg
|
---|
45 | * fix segmentation faults that may arise when the parser throws exceptions during document parsing. In general, XMLPlatformUtils::Terminate() should not be called from within a catch statement.
|
---|
46 | *
|
---|
47 | * Revision 1.8 2003/05/30 09:36:35 gareth
|
---|
48 | * Use new macros for iostream.h and std:: issues.
|
---|
49 | *
|
---|
50 | * Revision 1.7 2002/02/01 22:38:26 peiyongz
|
---|
51 | * sane_include
|
---|
52 | *
|
---|
53 | * Revision 1.6 2001/10/25 15:18:33 tng
|
---|
54 | * delete the parser before XMLPlatformUtils::Terminate.
|
---|
55 | *
|
---|
56 | * Revision 1.5 2001/10/19 19:02:42 tng
|
---|
57 | * [Bug 3909] return non-zero an exit code when error was encounted.
|
---|
58 | * And other modification for consistent help display and return code across samples.
|
---|
59 | *
|
---|
60 | * Revision 1.4 2000/05/31 18:53:15 rahulj
|
---|
61 | * Removed extraneous command line arguments.
|
---|
62 | *
|
---|
63 | * Revision 1.3 2000/02/11 02:38:28 abagchi
|
---|
64 | * Removed StrX::transcode
|
---|
65 | *
|
---|
66 | * Revision 1.2 2000/02/06 07:47:21 rahulj
|
---|
67 | * Year 2K copyright swat.
|
---|
68 | *
|
---|
69 | * Revision 1.1.1.1 1999/11/09 01:09:37 twl
|
---|
70 | * Initial checkin
|
---|
71 | *
|
---|
72 | * Revision 1.6 1999/11/08 20:43:39 rahul
|
---|
73 | * Swat for adding in Product name and CVS comment log variable.
|
---|
74 | *
|
---|
75 | */
|
---|
76 |
|
---|
77 |
|
---|
78 | // ---------------------------------------------------------------------------
|
---|
79 | // Includes
|
---|
80 | // ---------------------------------------------------------------------------
|
---|
81 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
82 | #include "Redirect.hpp"
|
---|
83 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
84 |
|
---|
85 | // ---------------------------------------------------------------------------
|
---|
86 | // Local helper methods
|
---|
87 | // ---------------------------------------------------------------------------
|
---|
88 | void usage()
|
---|
89 | {
|
---|
90 | XERCES_STD_QUALIFIER cout << "\nUsage:\n"
|
---|
91 | " Redirect <XML file>\n\n"
|
---|
92 | "This program installs an entity resolver, traps the call to\n"
|
---|
93 | "the external DTD file and redirects it to another application\n"
|
---|
94 | "specific file which contains the actual dtd.\n\n"
|
---|
95 | "The program then counts and reports the number of elements and\n"
|
---|
96 | "attributes in the given XML file.\n"
|
---|
97 | << XERCES_STD_QUALIFIER endl;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | // ---------------------------------------------------------------------------
|
---|
102 | // Program entry point
|
---|
103 | // ---------------------------------------------------------------------------
|
---|
104 | int main(int argc, char* args[])
|
---|
105 | {
|
---|
106 | // Initialize the XML4C system
|
---|
107 | try
|
---|
108 | {
|
---|
109 | XMLPlatformUtils::Initialize();
|
---|
110 | }
|
---|
111 | catch (const XMLException& toCatch)
|
---|
112 | {
|
---|
113 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
114 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
115 | return 1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // We only have one parameter, which is the file to process
|
---|
119 | // We only have one required parameter, which is the file to process
|
---|
120 | if ((argc != 2) || (*(args[1]) == '-'))
|
---|
121 | {
|
---|
122 | usage();
|
---|
123 | XMLPlatformUtils::Terminate();
|
---|
124 | return 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | const char* xmlFile = args[1];
|
---|
128 |
|
---|
129 | //
|
---|
130 | // Create a SAX parser object. Then, according to what we were told on
|
---|
131 | // the command line, set it to validate or not.
|
---|
132 | //
|
---|
133 | SAXParser* parser = new SAXParser;
|
---|
134 |
|
---|
135 | //
|
---|
136 | // Create our SAX handler object and install it on the parser, as the
|
---|
137 | // document, entity and error handlers.
|
---|
138 | //
|
---|
139 | RedirectHandlers handler;
|
---|
140 | parser->setDocumentHandler(&handler);
|
---|
141 | parser->setErrorHandler(&handler);
|
---|
142 | parser->setEntityResolver(&handler);
|
---|
143 |
|
---|
144 | //
|
---|
145 | // Get the starting time and kick off the parse of the indicated file.
|
---|
146 | // Catch any exceptions that might propogate out of it.
|
---|
147 | //
|
---|
148 | unsigned long duration;
|
---|
149 | int errorCount = 0;
|
---|
150 | int errorCode = 0;
|
---|
151 | try
|
---|
152 | {
|
---|
153 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
154 | parser->parse(xmlFile);
|
---|
155 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
156 | duration = endMillis - startMillis;
|
---|
157 | errorCount = parser->getErrorCount();
|
---|
158 | }
|
---|
159 | catch (const OutOfMemoryException&)
|
---|
160 | {
|
---|
161 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
162 | errorCode = 5;
|
---|
163 | }
|
---|
164 | catch (const XMLException& e)
|
---|
165 | {
|
---|
166 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n"
|
---|
167 | << "Exception message is: \n"
|
---|
168 | << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
169 | errorCode = 4;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if(errorCode) {
|
---|
173 | XMLPlatformUtils::Terminate();
|
---|
174 | return errorCode;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // Print out the stats that we collected and time taken.
|
---|
178 | if (!errorCount) {
|
---|
179 | XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms ("
|
---|
180 | << handler.getElementCount() << " elems, "
|
---|
181 | << handler.getAttrCount() << " attrs, "
|
---|
182 | << handler.getSpaceCount() << " spaces, "
|
---|
183 | << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
184 | }
|
---|
185 |
|
---|
186 | //
|
---|
187 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
188 | //
|
---|
189 | delete parser;
|
---|
190 |
|
---|
191 | XMLPlatformUtils::Terminate();
|
---|
192 |
|
---|
193 | if (errorCount > 0)
|
---|
194 | return 4;
|
---|
195 | else
|
---|
196 | return 0;
|
---|
197 | }
|
---|
198 |
|
---|