source: icXML/icXML-devel/samples/src/Redirect/Redirect.cpp @ 2726

Last change on this file since 2726 was 2726, checked in by cameron, 6 years ago

Add original Xerces tests and samples directories

File size: 5.6 KB
Line 
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/**
20 *
21 * This simplistic sample illustrates how an XML application can use
22 * the SAX entityResolver handler to provide customized handling for
23 * external entities.
24 *
25 * It registers an entity resolver with the parser. When ever the parser
26 * comes across an external entity, like a reference to an external DTD
27 * file, it calls the 'resolveEntity()' callback. This callback in this
28 * sample checks to see if the external entity to be resolved is the file
29 * 'personal.dtd'.
30 *
31 * If it is then, it redirects the input stream to the file 'redirect.dtd',
32 * which is then read instead of 'personal.dtd'.
33 *
34 * If the external entity to be resolved was not the file 'personal.dtd', the
35 * callback returns NULL indicating that do the default behaviour which is
36 * to read the contents of 'personal.dtd'.
37 *
38 * $Id: Redirect.cpp 471735 2006-11-06 13:53:58Z amassari $
39 */
40
41
42// ---------------------------------------------------------------------------
43//  Includes
44// ---------------------------------------------------------------------------
45#include <xercesc/parsers/SAXParser.hpp>
46#include "Redirect.hpp"
47#include <xercesc/util/OutOfMemoryException.hpp>
48
49// ---------------------------------------------------------------------------
50//  Local helper methods
51// ---------------------------------------------------------------------------
52void usage()
53{
54    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
55            "    Redirect <XML file>\n\n"
56            "This program installs an entity resolver, traps the call to\n"
57            "the external DTD file and redirects it to another application\n"
58            "specific file which contains the actual dtd.\n\n"
59            "The program then counts and reports the number of elements and\n"
60            "attributes in the given XML file.\n"
61         << XERCES_STD_QUALIFIER endl;
62}
63
64
65// ---------------------------------------------------------------------------
66//  Program entry point
67// ---------------------------------------------------------------------------
68int main(int argc, char* args[])
69{
70    // Initialize the XML4C system
71    try
72    {
73         XMLPlatformUtils::Initialize();
74    }
75    catch (const XMLException& toCatch)
76    {
77        XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
78             << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
79        return 1;
80    }
81
82    // We only have one parameter, which is the file to process
83    // We only have one required parameter, which is the file to process
84    if ((argc != 2) || (*(args[1]) == '-'))
85    {
86        usage();
87        XMLPlatformUtils::Terminate();
88        return 1;
89    }
90
91    const char*              xmlFile = args[1];
92
93    //
94    //  Create a SAX parser object. Then, according to what we were told on
95    //  the command line, set it to validate or not.
96    //
97    SAXParser* parser = new SAXParser;
98
99    //
100    //  Create our SAX handler object and install it on the parser, as the
101    //  document, entity and error handlers.
102    //
103    RedirectHandlers handler;
104    parser->setDocumentHandler(&handler);
105    parser->setErrorHandler(&handler);
106    //Use the new XML Entity Resolver
107    //parser->setEntityResolver(&handler);
108    parser->setXMLEntityResolver(&handler);   
109
110    //
111    //  Get the starting time and kick off the parse of the indicated file.
112    //  Catch any exceptions that might propogate out of it.
113    //
114    unsigned long duration;
115    int errorCount = 0;
116    int errorCode = 0;
117    try
118    {
119        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
120        parser->parse(xmlFile);
121        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
122        duration = endMillis - startMillis;
123        errorCount = parser->getErrorCount();
124    }
125    catch (const OutOfMemoryException&)
126    {
127        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
128        errorCode = 5;
129    }
130    catch (const XMLException& e)
131    {
132        XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n"
133                << "Exception message is:  \n"
134                << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
135        errorCode = 4;
136    }
137
138    if(errorCode) {
139        XMLPlatformUtils::Terminate();
140        return errorCode;
141    }
142
143    // Print out the stats that we collected and time taken.
144    if (!errorCount) {
145        XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms ("
146             << handler.getElementCount() << " elems, "
147             << handler.getAttrCount() << " attrs, "
148             << handler.getSpaceCount() << " spaces, "
149             << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
150    }
151
152    //
153    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
154    //
155    delete parser;
156
157    XMLPlatformUtils::Terminate();
158
159    if (errorCount > 0)
160        return 4;
161    else
162        return 0;
163}
164
Note: See TracBrowser for help on using the repository browser.