source: NonGTP/Xerces/xerces/samples/EnumVal/EnumVal.cpp @ 358

Revision 358, 12.4 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2001,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 * $Log: EnumVal.cpp,v $
19 * Revision 1.23  2004/09/08 13:55:31  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.22  2004/09/02 14:59:28  cargilld
23 * Add OutOfMemoryException block to samples.
24 *
25 * Revision 1.21  2003/08/07 21:21:38  neilg
26 * fix segmentation faults that may arise when the parser throws exceptions during document parsing.  In general, XMLPlatformUtils::Terminate() should not be called from within a catch statement.
27 *
28 * Revision 1.20  2003/05/30 09:36:35  gareth
29 * Use new macros for iostream.h and std:: issues.
30 *
31 * Revision 1.19  2003/02/05 18:53:23  tng
32 * [Bug 11915] Utility for freeing memory.
33 *
34 * Revision 1.18  2002/11/05 21:46:19  tng
35 * Explicit code using namespace in application.
36 *
37 * Revision 1.17  2002/04/17 20:18:08  tng
38 * [Bug 7493] The word "occured" is misspelled and it is a global error.
39 *
40 * Revision 1.16  2002/02/01 22:35:51  peiyongz
41 * sane_include
42 *
43 * Revision 1.15  2001/11/22 14:47:54  tng
44 * Use the phrase "Grammar" instead of "Validator" in EnumVal and SEnumVal Description.
45 *
46 * Revision 1.14  2001/10/25 15:18:33  tng
47 * delete the parser before XMLPlatformUtils::Terminate.
48 *
49 * Revision 1.13  2001/10/19 19:02:42  tng
50 * [Bug 3909] return non-zero an exit code when error was encounted.
51 * And other modification for consistent help display and return code across samples.
52 *
53 * Revision 1.12  2001/05/11 13:24:54  tng
54 * Copyright update.
55 *
56 * Revision 1.11  2001/04/19 18:16:46  tng
57 * Schema: SchemaValidator update, and use QName in Content Model
58 *
59 * Revision 1.10  2001/03/21 21:55:57  tng
60 * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
61 *
62 * Revision 1.9  2000/06/12 22:07:22  rahulj
63 * Correctly rejects unsupported command line options.
64 *
65 * Revision 1.8  2000/05/31 18:50:42  rahulj
66 * Removed extraneous command line arguments.
67 *
68 * Revision 1.7  2000/03/03 01:29:30  roddey
69 * Added a scanReset()/parseReset() method to the scanner and
70 * parsers, to allow for reset after early exit from a progressive parse.
71 * Added calls to new Terminate() call to all of the samples. Improved
72 * documentation in SAX and DOM parsers.
73 *
74 * Revision 1.6  2000/03/02 19:53:40  roddey
75 * This checkin includes many changes done while waiting for the
76 * 1.1.0 code to be finished. I can't list them all here, but a list is
77 * available elsewhere.
78 *
79 * Revision 1.5  2000/02/11 02:49:47  abagchi
80 * Removed StrX::transcode
81 *
82 * Revision 1.4  2000/02/11 02:26:40  abagchi
83 * Removed StrX::transcode
84 *
85 * Revision 1.3  2000/02/06 07:47:19  rahulj
86 * Year 2K copyright swat.
87 *
88 * Revision 1.2  1999/12/21 19:54:44  rahulj
89 * Added Apache license and version tags.
90 *
91 */
92
93// ---------------------------------------------------------------------------
94//  Includes
95// ---------------------------------------------------------------------------
96#include <xercesc/util/NameIdPool.hpp>
97#include <xercesc/util/PlatformUtils.hpp>
98#include <xercesc/framework/XMLValidator.hpp>
99#include <xercesc/parsers/SAXParser.hpp>
100#include <xercesc/validators/DTD/DTDValidator.hpp>
101#include <xercesc/util/OutOfMemoryException.hpp>   
102#if defined(XERCES_NEW_IOSTREAMS)
103#include <iostream>
104#else
105#include <iostream.h>
106#endif
107#include <stdlib.h>
108#include <string.h>
109
110
111XERCES_CPP_NAMESPACE_USE
112
113// ---------------------------------------------------------------------------
114//  Forward references
115// ---------------------------------------------------------------------------
116static void usage();
117
118
119
120// ---------------------------------------------------------------------------
121//  This is a simple class that lets us do easy (though not terribly efficient)
122//  trancoding of XMLCh data to local code page for display.
123// ---------------------------------------------------------------------------
124class StrX
125{
126public :
127    // -----------------------------------------------------------------------
128    //  Constructors and Destructor
129    // -----------------------------------------------------------------------
130    StrX(const XMLCh* const toTranscode)
131    {
132        // Call the private transcoding method
133        fLocalForm = XMLString::transcode(toTranscode);
134    }
135
136    ~StrX()
137    {
138        XMLString::release(&fLocalForm);
139    }
140
141
142    // -----------------------------------------------------------------------
143    //  Getter methods
144    // -----------------------------------------------------------------------
145    const char* localForm() const
146    {
147        return fLocalForm;
148    }
149
150
151private :
152    // -----------------------------------------------------------------------
153    //  Private data members
154    //
155    //  fLocalForm
156    //      This is the local code page form of the string.
157    // -----------------------------------------------------------------------
158    char*   fLocalForm;
159};
160
161inline XERCES_STD_QUALIFIER ostream& operator<<(XERCES_STD_QUALIFIER ostream& target, const StrX& toDump)
162{
163    target << toDump.localForm();
164    return target;
165}
166
167
168// ---------------------------------------------------------------------------
169//  Local helper methods
170// ---------------------------------------------------------------------------
171static void usage()
172{
173    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
174            "    EnumVal <XML file>\n\n"
175            "This program parses the specified XML file, then shows how to\n"
176            "enumerate the contents of the DTD Grammar. Essentially,\n"
177            "shows how one can access the DTD information stored in internal\n"
178            "data structures.\n"
179         << XERCES_STD_QUALIFIER endl;
180}
181
182
183// ---------------------------------------------------------------------------
184//  Program entry point
185// ---------------------------------------------------------------------------
186int main(int argC, char* argV[])
187{
188    // Initialize the XML4C system
189    try
190    {
191         XMLPlatformUtils::Initialize();
192    }
193
194    catch (const XMLException& toCatch)
195    {
196         XERCES_STD_QUALIFIER cerr   << "Error during initialization! Message:\n"
197                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
198         return 1;
199    }
200
201    // Check command line and extract arguments.
202    if (argC < 2)
203    {
204        usage();
205        XMLPlatformUtils::Terminate();
206        return 1;
207    }
208
209    // We only have one required parameter, which is the file to process
210    if ((argC != 2) || (*(argV[1]) == '-'))
211    {
212        usage();
213        XMLPlatformUtils::Terminate();
214        return 1;
215    }
216
217    const char*              xmlFile   = argV[1];
218    SAXParser::ValSchemes    valScheme = SAXParser::Val_Auto;
219
220    //
221    //  Create a DTD validator to be used for our validation work. Then create
222    //  a SAX parser object and pass it our validator. Then, according to what
223    //  we were told on the command line, set it to validate or not. He owns
224    //  the validator, so we have to allocate it.
225    //
226    int errorCount = 0;
227    DTDValidator* valToUse = new DTDValidator;
228    SAXParser* parser = new SAXParser(valToUse);
229    parser->setValidationScheme(valScheme);
230
231    //
232    //  Get the starting time and kick off the parse of the indicated
233    //  file. Catch any exceptions that might propogate out of it.
234    //
235    int errorCode = 0;
236    try
237    {
238        parser->parse(xmlFile);
239        errorCount = parser->getErrorCount();
240    }
241    catch (const OutOfMemoryException&)
242    {
243        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
244        errorCode = 5;
245    }
246    catch (const XMLException& e)
247    {
248        XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n"
249             << "Exception message is:  \n"
250             << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
251        errorCode = 4;
252    }
253    if(errorCode) {
254        XMLPlatformUtils::Terminate();
255        return errorCode;
256    }
257
258    if (!errorCount) {
259        //
260        //  Now we will get an enumerator for the element pool from the validator
261        //  and enumerate the elements, printing them as we go. For each element
262        //  we get an enumerator for its attributes and print them also.
263        //
264        DTDGrammar* grammar = (DTDGrammar*) valToUse->getGrammar();
265        NameIdPoolEnumerator<DTDElementDecl> elemEnum = grammar->getElemEnumerator();
266        if (elemEnum.hasMoreElements())
267        {
268            XERCES_STD_QUALIFIER cout << "\nELEMENTS:\n----------------------------\n";
269            while(elemEnum.hasMoreElements())
270            {
271                const DTDElementDecl& curElem = elemEnum.nextElement();
272                XERCES_STD_QUALIFIER cout << "  Name: " << StrX(curElem.getFullName()) << "\n";
273
274                XERCES_STD_QUALIFIER cout << "  Content Model: "
275                     << StrX(curElem.getFormattedContentModel())
276                     << "\n";
277
278                // Get an enumerator for this guy's attributes if any
279                if (curElem.hasAttDefs())
280                {
281                    XERCES_STD_QUALIFIER cout << "  Attributes:\n";
282                    XMLAttDefList& attList = curElem.getAttDefList();
283                    while (attList.hasMoreElements())
284                    {
285                        const XMLAttDef& curAttDef = attList.nextElement();
286                        XERCES_STD_QUALIFIER cout << "    Name:" << StrX(curAttDef.getFullName())
287                             << ", Type: ";
288
289                        // Get the type and display it
290                        const XMLAttDef::AttTypes type = curAttDef.getType();
291                        switch(type)
292                        {
293                            case XMLAttDef::CData :
294                                XERCES_STD_QUALIFIER cout << "CDATA";
295                                break;
296
297                            case XMLAttDef::ID :
298                                XERCES_STD_QUALIFIER cout << "ID";
299                                break;
300
301                            case XMLAttDef::IDRef :
302                            case XMLAttDef::IDRefs :
303                                XERCES_STD_QUALIFIER cout << "IDREF(S)";
304                                break;
305
306                            case XMLAttDef::Entity :
307                            case XMLAttDef::Entities :
308                                XERCES_STD_QUALIFIER cout << "ENTITY(IES)";
309                                break;
310
311                            case XMLAttDef::NmToken :
312                            case XMLAttDef::NmTokens :
313                                XERCES_STD_QUALIFIER cout << "NMTOKEN(S)";
314                                break;
315
316                            case XMLAttDef::Notation :
317                                XERCES_STD_QUALIFIER cout << "NOTATION";
318                                break;
319
320                            case XMLAttDef::Enumeration :
321                                XERCES_STD_QUALIFIER cout << "ENUMERATION";
322                                break;
323                        }
324
325                        XERCES_STD_QUALIFIER cout << "\n";
326                    }
327                }
328                XERCES_STD_QUALIFIER cout << XERCES_STD_QUALIFIER endl;
329            }
330        }
331         else
332        {
333            XERCES_STD_QUALIFIER cout << "The validator has no elements to display\n" << XERCES_STD_QUALIFIER endl;
334        }
335    }
336    else
337        XERCES_STD_QUALIFIER cout << "\nErrors occurred, no output available\n" << XERCES_STD_QUALIFIER endl;
338
339    //
340    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
341    //
342    delete parser;
343
344    // And call the termination method
345    XMLPlatformUtils::Terminate();
346
347    if (errorCount > 0)
348        return 4;
349    else
350        return 0;
351}
352
353
Note: See TracBrowser for help on using the repository browser.