1 | /*
|
---|
2 | * Copyright 1999-2000,2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Id: RedirectHandlers.cpp,v 1.10 2004/09/08 13:55:33 peiyongz Exp $
|
---|
19 | */
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 | // ---------------------------------------------------------------------------
|
---|
24 | // Includes
|
---|
25 | // ---------------------------------------------------------------------------
|
---|
26 | #include <xercesc/util/XMLUniDefs.hpp>
|
---|
27 | #include <xercesc/util/XMLUni.hpp>
|
---|
28 | #include <xercesc/sax/AttributeList.hpp>
|
---|
29 | #include <xercesc/sax/SAXParseException.hpp>
|
---|
30 | #include <xercesc/sax/SAXException.hpp>
|
---|
31 | #include <xercesc/framework/LocalFileInputSource.hpp>
|
---|
32 | #include "Redirect.hpp"
|
---|
33 | #include <string.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | // ---------------------------------------------------------------------------
|
---|
37 | // Local constant data
|
---|
38 | //
|
---|
39 | // gFileToTrap
|
---|
40 | // This is the file that we are looking for in the entity handler, to
|
---|
41 | // redirect to another file.
|
---|
42 | //
|
---|
43 | // gRedirectToFile
|
---|
44 | // This is the file that we are going to redirect the parser to.
|
---|
45 | // ---------------------------------------------------------------------------
|
---|
46 | static const XMLCh gFileToTrap[] =
|
---|
47 | {
|
---|
48 | chLatin_p, chLatin_e, chLatin_r, chLatin_s, chLatin_o, chLatin_n
|
---|
49 | , chLatin_a, chLatin_l, chPeriod, chLatin_d, chLatin_t, chLatin_d, chNull
|
---|
50 | };
|
---|
51 |
|
---|
52 | static const XMLCh gRedirectToFile[] =
|
---|
53 | {
|
---|
54 | chLatin_r, chLatin_e, chLatin_d, chLatin_i, chLatin_r, chLatin_e
|
---|
55 | , chLatin_c, chLatin_t, chPeriod, chLatin_d, chLatin_t, chLatin_d, chNull
|
---|
56 | };
|
---|
57 |
|
---|
58 |
|
---|
59 | // ---------------------------------------------------------------------------
|
---|
60 | // RedirectHandlers: Constructors and Destructor
|
---|
61 | // ---------------------------------------------------------------------------
|
---|
62 | RedirectHandlers::RedirectHandlers() :
|
---|
63 |
|
---|
64 | fElementCount(0)
|
---|
65 | , fAttrCount(0)
|
---|
66 | , fCharacterCount(0)
|
---|
67 | , fSpaceCount(0)
|
---|
68 | {
|
---|
69 | }
|
---|
70 |
|
---|
71 | RedirectHandlers::~RedirectHandlers()
|
---|
72 | {
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | // ---------------------------------------------------------------------------
|
---|
77 | // RedirectHandlers: Implementation of the SAX DocumentHandler interface
|
---|
78 | // ---------------------------------------------------------------------------
|
---|
79 | void RedirectHandlers::startElement(const XMLCh* const name
|
---|
80 | , AttributeList& attributes)
|
---|
81 | {
|
---|
82 | fElementCount++;
|
---|
83 | fAttrCount += attributes.getLength();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void RedirectHandlers::characters( const XMLCh* const chars
|
---|
87 | , const unsigned int length)
|
---|
88 | {
|
---|
89 | fCharacterCount += length;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void RedirectHandlers::ignorableWhitespace( const XMLCh* const chars
|
---|
93 | , const unsigned int length)
|
---|
94 | {
|
---|
95 | fSpaceCount += length;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void RedirectHandlers::resetDocument()
|
---|
99 | {
|
---|
100 | fAttrCount = 0;
|
---|
101 | fCharacterCount = 0;
|
---|
102 | fElementCount = 0;
|
---|
103 | fSpaceCount = 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 | // ---------------------------------------------------------------------------
|
---|
109 | // RedirectHandlers: Overrides of the SAX ErrorHandler interface
|
---|
110 | // ---------------------------------------------------------------------------
|
---|
111 | void RedirectHandlers::error(const SAXParseException& e)
|
---|
112 | {
|
---|
113 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
114 | << ", line " << e.getLineNumber()
|
---|
115 | << ", char " << e.getColumnNumber()
|
---|
116 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void RedirectHandlers::fatalError(const SAXParseException& e)
|
---|
120 | {
|
---|
121 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
122 | << ", line " << e.getLineNumber()
|
---|
123 | << ", char " << e.getColumnNumber()
|
---|
124 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void RedirectHandlers::warning(const SAXParseException& e)
|
---|
128 | {
|
---|
129 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
130 | << ", line " << e.getLineNumber()
|
---|
131 | << ", char " << e.getColumnNumber()
|
---|
132 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | // -----------------------------------------------------------------------
|
---|
137 | // Handlers for the SAX EntityResolver interface
|
---|
138 | // -----------------------------------------------------------------------
|
---|
139 | InputSource* RedirectHandlers::resolveEntity(const XMLCh* const publicId
|
---|
140 | , const XMLCh* const systemId)
|
---|
141 | {
|
---|
142 | //
|
---|
143 | // If its our file, then create a new URL input source for the file that
|
---|
144 | // we want to really be used. Otherwise, just return zero to let the
|
---|
145 | // default action occur.
|
---|
146 | //
|
---|
147 | // We cannot assume that the XMLCh type is ok to pass to wcscmp(), so
|
---|
148 | // just do a comparison ourselves.
|
---|
149 | //
|
---|
150 | const XMLCh* s1 = gFileToTrap;
|
---|
151 | const XMLCh* s2 = systemId;
|
---|
152 | while (true)
|
---|
153 | {
|
---|
154 | // Break out on any difference
|
---|
155 | if (*s1 != *s2)
|
---|
156 | return 0;
|
---|
157 |
|
---|
158 | // If one is null, then both were null, so they are equal
|
---|
159 | if (!*s1)
|
---|
160 | break;
|
---|
161 |
|
---|
162 | // Else get the next char
|
---|
163 | s1++;
|
---|
164 | s2++;
|
---|
165 | }
|
---|
166 |
|
---|
167 | // They were equal, so redirect to our other file
|
---|
168 | return new LocalFileInputSource(gRedirectToFile);
|
---|
169 | }
|
---|