source: icXML/icXML-devel/tests/src/XSTSHarness/XMLHarnessHandlers.cpp @ 2726

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

Add original Xerces tests and samples directories

File size: 7.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: XMLHarnessHandlers.cpp 781475 2009-06-03 17:00:27Z amassari $
20 */
21
22// ---------------------------------------------------------------------------
23//  Includes
24// ---------------------------------------------------------------------------
25#include "XMLHarnessHandlers.hpp"
26#include <xercesc/sax2/Attributes.hpp>
27#include <xercesc/sax/SAXParseException.hpp>
28#include <xercesc/sax/SAXException.hpp>
29#include <xercesc/validators/common/Grammar.hpp>
30#include <xercesc/util/OutOfMemoryException.hpp>
31#include <xercesc/util/BinInputStream.hpp>
32
33// ---------------------------------------------------------------------------
34//  XMLHarnessHandlers: Constructors and Destructor
35// ---------------------------------------------------------------------------
36XMLHarnessHandlers::XMLHarnessHandlers(const XMLCh* baseURL) : BaseHarnessHandlers(baseURL)
37, fTestBaseURL(5)
38{
39    fParser = XMLReaderFactory::createXMLReader();
40    fParser->setFeature(XMLUni::fgSAX2CoreValidation, true);
41    fParser->setFeature(XMLUni::fgXercesDynamic, false);
42    fParser->setErrorHandler(&fErrorHandler);
43   
44    fTestBaseURL.push(new XMLURL(fBaseURL));
45}
46
47XMLHarnessHandlers::~XMLHarnessHandlers()
48{
49    delete fParser;
50}
51
52static XMLCh szTest[]={ chLatin_T, chLatin_E, chLatin_S, chLatin_T, chNull };
53static XMLCh szTestCases[]={ chLatin_T, chLatin_E, chLatin_S, chLatin_T, chLatin_C, chLatin_A, chLatin_S, chLatin_E, chLatin_S, chNull };
54static XMLCh szID[]={ chLatin_I, chLatin_D, chNull };
55static XMLCh szURI[]={ chLatin_U, chLatin_R, chLatin_I, chNull };
56static XMLCh szType[]={ chLatin_T, chLatin_Y, chLatin_P, chLatin_E, chNull };
57static XMLCh szValid[]={ chLatin_v, chLatin_a, chLatin_l, chLatin_i, chLatin_d, chNull };
58static XMLCh szInvalid[]={ chLatin_i, chLatin_n, chLatin_v, chLatin_a, chLatin_l, chLatin_i, chLatin_d, chNull };
59static XMLCh szNotWellFormed[]={ chLatin_n, chLatin_o, chLatin_t, chDash, chLatin_w, chLatin_f, chNull };
60static XMLCh szError[]={ chLatin_e, chLatin_r, chLatin_r, chLatin_o, chLatin_r, chNull };
61static XMLCh szBase[]={ chLatin_x, chLatin_m, chLatin_l, chColon, chLatin_b, chLatin_a, chLatin_s, chLatin_e, chNull };
62static XMLCh szNamespace[]={ chLatin_N, chLatin_A, chLatin_M, chLatin_E, chLatin_S, chLatin_P, chLatin_A, chLatin_C, chLatin_E, chNull };
63static XMLCh szNO[]={ chLatin_n, chLatin_o, chNull };
64
65// ---------------------------------------------------------------------------
66//  XMLHarnessHandlers: Implementation of the SAX DocumentHandler interface
67// ---------------------------------------------------------------------------
68void XMLHarnessHandlers::startElement(const XMLCh* const uri
69                                   , const XMLCh* const localname
70                                   , const XMLCh* const /* qname */
71                                   , const Attributes& attrs)
72{
73    if(XMLString::equals(localname, szTestCases))
74    {
75        const XMLCh* baseUrl=attrs.getValue(szBase);
76        XMLURL newBase;
77        XMLURL prevBase=*fTestBaseURL.peek();
78        if(baseUrl!=NULL)
79            newBase.setURL(prevBase, baseUrl);
80        else
81            newBase=prevBase;
82        fTestBaseURL.push(new XMLURL(newBase));
83    }
84    else if(XMLString::equals(localname, szTest))
85    {
86        const XMLCh* useNS=attrs.getValue(szNamespace);
87        const XMLCh* testName=attrs.getValue(szID);
88        XMLURL testSet;
89        testSet.setURL(*fTestBaseURL.peek(), attrs.getValue(szURI));
90        bool success=true, fatalFailure=false;
91        try
92        {
93            if(useNS!=NULL && XMLString::equals(useNS, szNO))
94            {
95                fParser->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
96                fParser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, false);
97            }
98            else
99            {
100                fParser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
101                fParser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, true);
102            }
103            fErrorHandler.resetErrors();
104            fParser->parse(testSet.getURLText());
105        }
106        catch (const OutOfMemoryException&)
107        {
108            fatalFailure=true;
109            XERCES_STD_QUALIFIER cout << "Test " << StrX(testName) << " ran out of memory" << XERCES_STD_QUALIFIER endl;
110            success=false;
111        }
112        catch(const XMLException& exc)
113        {
114            XERCES_STD_QUALIFIER cout << "Test " << StrX(testName) << " threw " << StrX(exc.getMessage()) << XERCES_STD_QUALIFIER endl;
115            success=false;
116        }
117        catch (...)
118        {
119            fatalFailure=true;
120            XERCES_STD_QUALIFIER cout << "Test " << StrX(testName) << " crashed" << XERCES_STD_QUALIFIER endl;
121            success=false;
122            exit(1);
123        }
124        fTests++;
125        if(fatalFailure)
126        {
127            fFailures++;
128            printFile(testSet);
129        }
130        else
131        {
132            ValidityOutcome expResult=unknown;
133            const XMLCh* validity=attrs.getValue(szType);
134            if(XMLString::equals(validity, szValid))
135                expResult=valid;
136            else if(XMLString::equals(validity, szInvalid) || XMLString::equals(validity, szNotWellFormed) || XMLString::equals(validity, szError) )
137                expResult=invalid;
138            else
139                XERCES_STD_QUALIFIER cerr << "Unknown result type " << StrX(validity) << XERCES_STD_QUALIFIER endl;
140            if(success && !fErrorHandler.getSawErrors())
141            {
142                if(expResult!=valid)
143                {
144                    fFailures++;
145                    XERCES_STD_QUALIFIER cout << "Test " << StrX(testName) << " succeeded but was expected to fail" << XERCES_STD_QUALIFIER endl;
146                    printFile(testSet);
147                }
148            }
149            else
150            {
151                if(expResult!=invalid)
152                {
153                    fFailures++;
154                    XERCES_STD_QUALIFIER cout << "Test " << StrX(testName) << " failed but was expected to pass" << XERCES_STD_QUALIFIER endl;
155                    XERCES_STD_QUALIFIER cout << "Reported error: " << StrX(fErrorHandler.getErrorText()) << XERCES_STD_QUALIFIER endl;
156                    printFile(testSet);
157                }
158            }
159        }
160    }
161}
162
163void XMLHarnessHandlers::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname)
164{
165    if(XMLString::equals(localname, szTestCases))
166    {
167        fTestBaseURL.pop();
168    }
169}
Note: See TracBrowser for help on using the repository browser.