source: icXML/icXML-devel/tests/src/ParserTest/ParserTest.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: 7.0 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: ParserTest.cpp 470088 2006-11-01 20:35:12Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include    <xercesc/util/PlatformUtils.hpp>
27#include    <xercesc/util/XMLString.hpp>
28#include    <xercesc/util/XMLURL.hpp>
29#include    <xercesc/internal/XMLScanner.hpp>
30#include    <xercesc/validators/DTD/DTDValidator.hpp>
31#include    "ParserTest.hpp"
32
33
34// ---------------------------------------------------------------------------
35//  Program entry point
36// ---------------------------------------------------------------------------
37int main(int argC, char** argV)
38{
39    // Init the XML platform
40    try
41    {
42        XMLPlatformUtils::Initialize();
43    }
44
45    catch(const XMLException& toCatch)
46    {
47        XERCES_STD_QUALIFIER cout << "Error during platform init! Message:\n"
48             << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
49        return 1;
50    }
51
52    //
53    //  Create our test parser object. This object implements the internal
54    //  event APIs and is plugged into the scanner.
55    //
56    TestParser parserTest;
57
58    // Figure out the parameters
59    bool doValidation = false;
60    bool doNamespaces = false;
61    bool keepGoing = false;
62    XMLCh*  urlPath = 0;
63    for (int index = 1; index < argC; index++)
64    {
65        if (!XMLString::compareIString(argV[index], "/Debug"))
66            parserTest.setOutputType(OutputType_Debug);
67        else if (!XMLString::compareIString(argV[index], "/Validate"))
68            doValidation = true;
69        else if (!XMLString::compareIString(argV[index], "/Namespaces"))
70        {
71            doNamespaces = true;
72            parserTest.setDoNamespaces(true);
73        }
74        else if (!XMLString::compareIString(argV[index], "/XML"))
75            parserTest.setOutputType(OutputType_XML);
76        else if (!XMLString::compareIString(argV[index], "/IntDTD"))
77            parserTest.setShowIntDTD(true);
78        else if (!XMLString::compareIString(argV[index], "/ShowWarnings"))
79            parserTest.setShowWarnings(true);
80        else if (!XMLString::compareIString(argV[index], "/ShowErrLoc"))
81            parserTest.setShowErrLoc(true);
82        else if (!XMLString::compareIString(argV[index], "/JCCanon"))
83            parserTest.setOutputType(OutputType_JCCanon);
84        else if (!XMLString::compareIString(argV[index], "/SunCanon"))
85            parserTest.setOutputType(OutputType_SunCanon);
86        else if (!XMLString::compareIString(argV[index], "/KeepGoing"))
87            keepGoing = true;
88        else if (!XMLString::compareNIString(argV[index], "/URL=", 5))
89            urlPath = XMLString::transcode(&argV[index][5]);
90        else
91            XERCES_STD_QUALIFIER cout << "Unknown parameter: " << argV[index] << XERCES_STD_QUALIFIER endl;
92    }
93
94    // We have to have a URL to work on
95    if (!urlPath)
96    {
97        XERCES_STD_QUALIFIER cout << "A URL must be provided, /URL=xxxx" << XERCES_STD_QUALIFIER endl;
98        return 1;
99    }
100
101    //
102    //  Create a validator of the correct type so that we can install it
103    //  on the scanner.
104    //
105    //  <TBD> Later, when Schema validators exist, we'll have a parameter
106    //  to select one or the other
107    //
108    XMLValidator* validator = 0;
109    DTDValidator* dtdVal = new DTDValidator(&parserTest);
110    validator = dtdVal;
111
112    // And now create the scanner and give it all the handlers
113    XMLScanner scanner
114    (
115        &parserTest
116        , &parserTest
117        , 0
118        , &parserTest
119        , validator
120    );
121
122    // Set the scanner flags that we were told to
123    scanner.setDoValidation(doValidation);
124    scanner.setDoNamespaces(doNamespaces);
125    scanner.setExitOnFirstFatal(!keepGoing);
126
127    // Tell the parser about the scanner
128    parserTest.setScanner(&scanner);
129
130    try
131    {
132        scanner.scanDocument(urlPath);
133    }
134
135    catch(const XMLException& toCatch)
136    {
137        XERCES_STD_QUALIFIER cout << "Exception during scan:\n    "
138             << StrX(toCatch.getMessage())
139             << XERCES_STD_QUALIFIER endl;
140    }
141
142    // And call the termination method
143    XMLPlatformUtils::Terminate();
144
145    return 0;
146}
147
148
149
150// ---------------------------------------------------------------------------
151//  StrX: Private helper methods
152// ---------------------------------------------------------------------------
153void StrX::transcode(const XMLCh* const toTranscode, const unsigned int len)
154{
155    // Short circuit if its a null pointer
156    if (!toTranscode || (!toTranscode[0]))
157    {
158        fLocalForm = new char[1];
159        fLocalForm[0] = 0;
160        return;
161        }
162
163    // See if our XMLCh and wchar_t as the same on this platform
164    const bool isSameSize = (sizeof(XMLCh) == sizeof(wchar_t));
165
166    //
167    //  Get the actual number of chars. If the passed len is zero, its null
168    //  terminated. Else we have to use the len.
169    //
170    wchar_t realLen = (wchar_t)len;
171    if (!realLen)
172    {
173        //
174        //  We cannot just assume we can use wcslen() because we don't know
175        //  if our XMLCh is the same as wchar_t on this platform.
176        //
177        const XMLCh* tmpPtr = toTranscode;
178        while (*(tmpPtr++))
179            realLen++;
180    }
181
182    //
183    //  If either the passed length was non-zero or our char sizes are not
184    //  same, we have to use a temp buffer. Since this is common in these
185    //  samples, we just do it anyway.
186    //
187    wchar_t* tmpSource = new wchar_t[realLen + 1];
188    if (isSameSize)
189    {
190        memcpy(tmpSource, toTranscode, realLen * sizeof(wchar_t));
191    }
192     else
193    {
194        for (unsigned int index = 0; index < realLen; index++)
195            tmpSource[index] = (wchar_t)toTranscode[index];
196    }
197    tmpSource[realLen] = 0;
198
199    // See now many chars we need to transcode this guy
200    const unsigned int targetLen = ::wcstombs(0, tmpSource, 0);
201
202    // Allocate out storage member
203    fLocalForm = new char[targetLen + 1];
204
205    //
206    //  And transcode our temp source buffer to the local buffer. Cap it
207    //  off since the converter won't do it (because the null is beyond
208    //  where the target will fill up.)
209    //
210    ::wcstombs(fLocalForm, tmpSource, targetLen);
211    fLocalForm[targetLen] = 0;
212
213    // Don't forget to delete our temp buffer
214    delete [] tmpSource;
215}
Note: See TracBrowser for help on using the repository browser.