source: icXML/icXML-devel/samples/src/XInclude/XInclude.cpp @ 2733

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

More path fixes.

File size: 8.1 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 * $Id: XInclude.cpp 833057 2009-11-05 15:25:10Z borisk $
20 */
21
22// ---------------------------------------------------------------------------
23//  Includes
24// ---------------------------------------------------------------------------
25#include <icxercesc/util/PlatformUtils.hpp>
26#include <icxercesc/parsers/AbstractDOMParser.hpp>
27#include <xercesc/framework/LocalFileFormatTarget.hpp>
28#include <xercesc/dom/DOMDocument.hpp>
29#include <xercesc/dom/DOM.hpp>
30#include <icxercesc/util/XMLString.hpp>
31
32#include "XInclude.hpp"
33
34// ---------------------------------------------------------------------------
35//  This is a simple program which tests the different XInclude mechanisms prototyped
36//  for my dissertation. Based largely on the DOMCount example provided with the
37//  Xerces C++ project.
38//  Simon Rowland 2006
39// ---------------------------------------------------------------------------
40static void usage()
41{
42    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
43                        "    XInclude [-h] InputFile OutputFile\n"
44                        "  -h :             Prints this help message and exits.\n"
45         << XERCES_STD_QUALIFIER endl;
46}
47
48// ---------------------------------------------------------------------------
49//
50//   main
51//
52// ---------------------------------------------------------------------------
53int main(int argC, char* argV[])
54{
55        char                                            *testFileName;
56        char                                            *outputFileName;
57
58    for (int argInd = 1; argInd < argC; argInd++)
59        {
60        if (!strcmp(argV[argInd], "-?") || !strcmp(argV[argInd], "-h"))
61        {
62                        /* print help and exit */
63            usage();
64            return 2;
65        }
66    }
67
68        if (argC < 3){
69                usage();
70                return 2;
71        }
72
73        testFileName = argV[argC-2];
74        outputFileName = argV[argC-1];
75
76    // Initialize the XML4C system
77    try
78    {
79        XMLPlatformUtils::Initialize();
80    }
81
82    catch (const XMLException& toCatch)
83    {
84         XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
85              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
86         return 1;
87    }
88
89        //============================================================================
90        // Instantiate the DOM parser to use for the source documents
91        //============================================================================
92    static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
93    DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
94    DOMLSParser       *parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
95    DOMConfiguration  *config = parser->getDomConfig();
96
97    config->setParameter(XMLUni::fgDOMNamespaces, true);
98    config->setParameter(XMLUni::fgXercesSchema, true);
99    config->setParameter(XMLUni::fgXercesHandleMultipleImports, true);
100    config->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
101
102    if(config->canSetParameter(XMLUni::fgXercesDoXInclude, true)){
103      config->setParameter(XMLUni::fgXercesDoXInclude, true);
104    }
105
106    // enable datatype normalization - default is off
107    //config->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
108
109    // And create our error handler and install it
110    XIncludeErrorHandler errorHandler;
111    config->setParameter(XMLUni::fgDOMErrorHandler, &errorHandler);
112
113    XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
114
115    try
116    {
117        // load up the test source document
118                XERCES_STD_QUALIFIER cerr << "Parse " << testFileName << " in progress ...";
119        parser->resetDocumentPool();
120                doc = parser->parseURI(testFileName);
121                XERCES_STD_QUALIFIER cerr << " finished." << XERCES_STD_QUALIFIER endl;
122    }
123    catch (const XMLException& toCatch)
124    {
125        XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << testFileName << "'\n"
126                << "Exception message is:  \n"
127                << StrX(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
128    }
129    catch (const DOMException& toCatch)
130    {
131        const unsigned int maxChars = 2047;
132        XMLCh errText[maxChars + 1];
133
134        XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '" << testFileName << "'\n"
135                << "DOMException code is:  " << toCatch.code << XERCES_STD_QUALIFIER endl;
136
137        if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars))
138                XERCES_STD_QUALIFIER cerr << "Message is: " << StrX(errText) << XERCES_STD_QUALIFIER endl;
139
140    }
141    catch (...)
142    {
143        XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" << testFileName << "'\n";
144    }
145
146    if (!errorHandler.getSawErrors() && doc) {
147            DOMLSSerializer     *writer = ((DOMImplementationLS*)impl)->createLSSerializer();
148            DOMLSOutput     *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
149
150                try {
151                        // write out the results
152                        XERCES_STD_QUALIFIER cerr << "Writing result to: " << outputFileName << XERCES_STD_QUALIFIER endl;
153
154                        XMLFormatTarget *myFormTarget = new LocalFileFormatTarget(outputFileName);
155                        theOutputDesc->setByteStream(myFormTarget);
156                        writer->write(doc, theOutputDesc);
157            delete myFormTarget;
158                }
159                catch (const XMLException& toCatch)
160                {
161                    XERCES_STD_QUALIFIER cerr << "\nXMLException during writing: '" << testFileName << "'\n"
162                                << "Exception message is:  \n"
163                                << StrX(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
164                }
165                catch (const DOMException& toCatch)
166                {
167                        const unsigned int maxChars = 2047;
168                        XMLCh errText[maxChars + 1];
169
170                        XERCES_STD_QUALIFIER cerr << "\nDOM Error during writing: '" << testFileName << "'\n"
171                                << "DOMException code is:  " << toCatch.code << XERCES_STD_QUALIFIER endl;
172
173                        if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars))
174                                XERCES_STD_QUALIFIER cerr << "Message is: " << StrX(errText) << XERCES_STD_QUALIFIER endl;
175                }
176                catch (...)
177                {
178                        XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during writing: '" << testFileName << "'\n";
179                }
180        writer->release();
181        theOutputDesc->release();
182    }
183
184    //
185    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
186    //
187    parser->release();
188        // And call the termination method
189    XMLPlatformUtils::Terminate();
190
191    return 0;
192}
193
194XIncludeErrorHandler::XIncludeErrorHandler() :
195    fSawErrors(false)
196{
197}
198
199XIncludeErrorHandler::~XIncludeErrorHandler()
200{
201}
202
203
204// ---------------------------------------------------------------------------
205//  XIncludeHandlers: Overrides of the DOM ErrorHandler interface
206// ---------------------------------------------------------------------------
207bool XIncludeErrorHandler::handleError(const DOMError& domError)
208{
209        bool continueParsing = true;
210    if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING)
211        XERCES_STD_QUALIFIER cerr << "\nWarning at file ";
212    else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR)
213    {
214        XERCES_STD_QUALIFIER cerr << "\nError at file ";
215        fSawErrors = true;
216    }
217        else {
218        XERCES_STD_QUALIFIER cerr << "\nFatal Error at file ";
219                continueParsing = false;
220        fSawErrors = true;
221        }
222
223    XERCES_STD_QUALIFIER cerr << StrX(domError.getLocation()->getURI())
224         << ", line " << domError.getLocation()->getLineNumber()
225         << ", char " << domError.getLocation()->getColumnNumber()
226         << "\n  Message: " << StrX(domError.getMessage()) << XERCES_STD_QUALIFIER endl;
227
228    return continueParsing;
229}
230
231void XIncludeErrorHandler::resetErrors()
232{
233    fSawErrors = false;
234}
Note: See TracBrowser for help on using the repository browser.