source: icXML/icXML-devel/samples/src/Redirect/RedirectHandlers.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: 5.7 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: RedirectHandlers.cpp 673248 2008-07-02 00:58:08Z dbertoni $
20 */
21
22
23
24// ---------------------------------------------------------------------------
25//  Includes
26// ---------------------------------------------------------------------------
27#include <xercesc/util/XMLUniDefs.hpp>
28#include <xercesc/util/XMLUni.hpp>
29#include <xercesc/sax/AttributeList.hpp>
30#include <xercesc/sax/SAXParseException.hpp>
31#include <xercesc/sax/SAXException.hpp>
32#include <xercesc/framework/LocalFileInputSource.hpp>
33#include "Redirect.hpp"
34#include <string.h>
35
36
37// ---------------------------------------------------------------------------
38//  Local constant data
39//
40//  gFileToTrap
41//      This is the file that we are looking for in the entity handler, to
42//      redirect to another file.
43//
44//  gRedirectToFile
45//      This is the file that we are going to redirect the parser to.
46// ---------------------------------------------------------------------------
47static const XMLCh  gFileToTrap[] =
48{
49        chLatin_p, chLatin_e, chLatin_r, chLatin_s, chLatin_o, chLatin_n
50    ,   chLatin_a, chLatin_l, chPeriod,  chLatin_d, chLatin_t, chLatin_d, chNull
51};
52
53static const XMLCh  gRedirectToFile[] =
54{
55        chLatin_r, chLatin_e, chLatin_d, chLatin_i, chLatin_r, chLatin_e
56    ,   chLatin_c, chLatin_t, chPeriod,  chLatin_d, chLatin_t, chLatin_d, chNull
57};
58
59
60// ---------------------------------------------------------------------------
61//  RedirectHandlers: Constructors and Destructor
62// ---------------------------------------------------------------------------
63RedirectHandlers::RedirectHandlers() :
64
65    fAttrCount(0)
66    , fCharacterCount(0)
67    , fElementCount(0)
68    , fSpaceCount(0)
69{
70}
71
72RedirectHandlers::~RedirectHandlers()
73{
74}
75
76
77// ---------------------------------------------------------------------------
78//  RedirectHandlers: Implementation of the SAX DocumentHandler interface
79// ---------------------------------------------------------------------------
80void RedirectHandlers::startElement(const   XMLCh* const    /* name */
81                                    ,       AttributeList&  attributes)
82{
83    fElementCount++;
84    fAttrCount += attributes.getLength();
85}
86
87void RedirectHandlers::characters(  const   XMLCh* const    /* chars */
88                                    , const XMLSize_t    length)
89{
90    fCharacterCount += length;
91}
92
93void RedirectHandlers::ignorableWhitespace( const   XMLCh* const /* chars */
94                                            , const XMLSize_t length)
95{
96    fSpaceCount += length;
97}
98
99void RedirectHandlers::resetDocument()
100{
101    fAttrCount = 0;
102    fCharacterCount = 0;
103    fElementCount = 0;
104    fSpaceCount = 0;
105}
106
107
108
109// ---------------------------------------------------------------------------
110//  RedirectHandlers: Overrides of the SAX ErrorHandler interface
111// ---------------------------------------------------------------------------
112void RedirectHandlers::error(const SAXParseException& e)
113{
114    XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
115                 << ", line " << e.getLineNumber()
116                 << ", char " << e.getColumnNumber()
117         << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
118}
119
120void RedirectHandlers::fatalError(const SAXParseException& e)
121{
122    XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
123                 << ", line " << e.getLineNumber()
124                 << ", char " << e.getColumnNumber()
125         << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
126}
127
128void RedirectHandlers::warning(const SAXParseException& e)
129{
130    XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
131                 << ", line " << e.getLineNumber()
132                 << ", char " << e.getColumnNumber()
133         << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
134}
135
136
137// This is the SAX2 resolveEntity interface...
138// -----------------------------------------------------------------------
139//  Handlers for the SAX EntityResolver interface
140// -----------------------------------------------------------------------
141InputSource* RedirectHandlers::resolveEntity(const   XMLCh* const    /* publicId */
142                                             , const XMLCh* const    systemId)
143{
144    //
145    //  If it's our file, then create a new URL input source for the file that
146    //  we want to really be used. Otherwise, just return NULL to let the
147    //  default action occur.
148    if (XMLString::compareString(gFileToTrap, systemId) != 0)
149    {
150        return NULL;
151    }
152    else
153    {
154        // They were equal, so redirect to our other file
155        return new LocalFileInputSource(gRedirectToFile);
156    }
157}
158
159// -----------------------------------------------------------------------
160//  Handlers for the XMLEntityResolver interface
161// -----------------------------------------------------------------------
162
163InputSource* RedirectHandlers::resolveEntity(XMLResourceIdentifier* resourceIdentifier)
164{
165    // Call the SAX2 version.
166    return resolveEntity(NULL, resourceIdentifier->getSystemId());
167}
Note: See TracBrowser for help on using the repository browser.