source: icXML/icXML-devel/samples/src/EnumVal/EnumVal.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: 9.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: EnumVal.cpp 471735 2006-11-06 13:53:58Z amassari $
20 */
21
22// ---------------------------------------------------------------------------
23//  Includes
24// ---------------------------------------------------------------------------
25#include <xercesc/util/NameIdPool.hpp>
26#include <xercesc/util/PlatformUtils.hpp>
27#include <xercesc/framework/XMLValidator.hpp>
28#include <xercesc/parsers/SAXParser.hpp>
29#include <xercesc/validators/DTD/DTDValidator.hpp>
30#include <xercesc/util/OutOfMemoryException.hpp>   
31#if defined(XERCES_NEW_IOSTREAMS)
32#include <iostream>
33#else
34#include <iostream.h>
35#endif
36#include <stdlib.h>
37#include <string.h>
38
39
40XERCES_CPP_NAMESPACE_USE
41
42// ---------------------------------------------------------------------------
43//  Forward references
44// ---------------------------------------------------------------------------
45static void usage();
46
47
48
49// ---------------------------------------------------------------------------
50//  This is a simple class that lets us do easy (though not terribly efficient)
51//  trancoding of XMLCh data to local code page for display.
52// ---------------------------------------------------------------------------
53class StrX
54{
55public :
56    // -----------------------------------------------------------------------
57    //  Constructors and Destructor
58    // -----------------------------------------------------------------------
59    StrX(const XMLCh* const toTranscode)
60    {
61        // Call the private transcoding method
62        fLocalForm = XMLString::transcode(toTranscode);
63    }
64
65    ~StrX()
66    {
67        XMLString::release(&fLocalForm);
68    }
69
70
71    // -----------------------------------------------------------------------
72    //  Getter methods
73    // -----------------------------------------------------------------------
74    const char* localForm() const
75    {
76        return fLocalForm;
77    }
78
79
80private :
81    // -----------------------------------------------------------------------
82    //  Private data members
83    //
84    //  fLocalForm
85    //      This is the local code page form of the string.
86    // -----------------------------------------------------------------------
87    char*   fLocalForm;
88};
89
90inline XERCES_STD_QUALIFIER ostream& operator<<(XERCES_STD_QUALIFIER ostream& target, const StrX& toDump)
91{
92    target << toDump.localForm();
93    return target;
94}
95
96
97// ---------------------------------------------------------------------------
98//  Local helper methods
99// ---------------------------------------------------------------------------
100static void usage()
101{
102    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
103            "    EnumVal <XML file>\n\n"
104            "This program parses the specified XML file, then shows how to\n"
105            "enumerate the contents of the DTD Grammar. Essentially,\n"
106            "shows how one can access the DTD information stored in internal\n"
107            "data structures.\n"
108         << XERCES_STD_QUALIFIER endl;
109}
110
111
112// ---------------------------------------------------------------------------
113//  Program entry point
114// ---------------------------------------------------------------------------
115int main(int argC, char* argV[])
116{
117    // Initialize the XML4C system
118    try
119    {
120         XMLPlatformUtils::Initialize();
121    }
122
123    catch (const XMLException& toCatch)
124    {
125         XERCES_STD_QUALIFIER cerr   << "Error during initialization! Message:\n"
126                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
127         return 1;
128    }
129
130    // Check command line and extract arguments.
131    if (argC < 2)
132    {
133        usage();
134        XMLPlatformUtils::Terminate();
135        return 1;
136    }
137
138    // We only have one required parameter, which is the file to process
139    if ((argC != 2) || (*(argV[1]) == '-'))
140    {
141        usage();
142        XMLPlatformUtils::Terminate();
143        return 1;
144    }
145
146    const char*              xmlFile   = argV[1];
147    SAXParser::ValSchemes    valScheme = SAXParser::Val_Auto;
148
149    //
150    //  Create a DTD validator to be used for our validation work. Then create
151    //  a SAX parser object and pass it our validator. Then, according to what
152    //  we were told on the command line, set it to validate or not. He owns
153    //  the validator, so we have to allocate it.
154    //
155    int errorCount = 0;
156    DTDValidator* valToUse = new DTDValidator;
157    SAXParser* parser = new SAXParser(valToUse);
158    parser->setValidationScheme(valScheme);
159
160    //
161    //  Get the starting time and kick off the parse of the indicated
162    //  file. Catch any exceptions that might propogate out of it.
163    //
164    int errorCode = 0;
165    try
166    {
167        parser->parse(xmlFile);
168        errorCount = parser->getErrorCount();
169    }
170    catch (const OutOfMemoryException&)
171    {
172        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
173        errorCode = 5;
174    }
175    catch (const XMLException& e)
176    {
177        XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n"
178             << "Exception message is:  \n"
179             << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
180        errorCode = 4;
181    }
182    if(errorCode) {
183        XMLPlatformUtils::Terminate();
184        return errorCode;
185    }
186
187    if (!errorCount) {
188        //
189        //  Now we will get an enumerator for the element pool from the validator
190        //  and enumerate the elements, printing them as we go. For each element
191        //  we get an enumerator for its attributes and print them also.
192        //
193        DTDGrammar* grammar = (DTDGrammar*) valToUse->getGrammar();
194        NameIdPoolEnumerator<DTDElementDecl> elemEnum = grammar->getElemEnumerator();
195        if (elemEnum.hasMoreElements())
196        {
197            XERCES_STD_QUALIFIER cout << "\nELEMENTS:\n----------------------------\n";
198            while(elemEnum.hasMoreElements())
199            {
200                const DTDElementDecl& curElem = elemEnum.nextElement();
201                XERCES_STD_QUALIFIER cout << "  Name: " << StrX(curElem.getFullName()) << "\n";
202
203                XERCES_STD_QUALIFIER cout << "  Content Model: "
204                     << StrX(curElem.getFormattedContentModel())
205                     << "\n";
206
207                // Get an enumerator for this guy's attributes if any
208                if (curElem.hasAttDefs())
209                {
210                    XERCES_STD_QUALIFIER cout << "  Attributes:\n";
211                    XMLAttDefList& attList = curElem.getAttDefList();
212                    for (unsigned int i=0; i<attList.getAttDefCount(); i++)
213                    {
214                        const XMLAttDef& curAttDef = attList.getAttDef(i);
215                        XERCES_STD_QUALIFIER cout << "    Name:" << StrX(curAttDef.getFullName())
216                             << ", Type: ";
217
218                        // Get the type and display it
219                        const XMLAttDef::AttTypes type = curAttDef.getType();
220                        switch(type)
221                        {
222                            case XMLAttDef::CData :
223                                XERCES_STD_QUALIFIER cout << "CDATA";
224                                break;
225
226                            case XMLAttDef::ID :
227                                XERCES_STD_QUALIFIER cout << "ID";
228                                break;
229
230                            case XMLAttDef::IDRef :
231                            case XMLAttDef::IDRefs :
232                                XERCES_STD_QUALIFIER cout << "IDREF(S)";
233                                break;
234
235                            case XMLAttDef::Entity :
236                            case XMLAttDef::Entities :
237                                XERCES_STD_QUALIFIER cout << "ENTITY(IES)";
238                                break;
239
240                            case XMLAttDef::NmToken :
241                            case XMLAttDef::NmTokens :
242                                XERCES_STD_QUALIFIER cout << "NMTOKEN(S)";
243                                break;
244
245                            case XMLAttDef::Notation :
246                                XERCES_STD_QUALIFIER cout << "NOTATION";
247                                break;
248
249                            case XMLAttDef::Enumeration :
250                                XERCES_STD_QUALIFIER cout << "ENUMERATION";
251                                break;
252                            default:
253                                break;
254                        }
255
256                        XERCES_STD_QUALIFIER cout << "\n";
257                    }
258                }
259                XERCES_STD_QUALIFIER cout << XERCES_STD_QUALIFIER endl;
260            }
261        }
262         else
263        {
264            XERCES_STD_QUALIFIER cout << "The validator has no elements to display\n" << XERCES_STD_QUALIFIER endl;
265        }
266    }
267    else
268        XERCES_STD_QUALIFIER cout << "\nErrors occurred, no output available\n" << XERCES_STD_QUALIFIER endl;
269
270    //
271    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
272    //
273    delete parser;
274
275    // And call the termination method
276    XMLPlatformUtils::Terminate();
277
278    if (errorCount > 0)
279        return 4;
280    else
281        return 0;
282}
283
284
Note: See TracBrowser for help on using the repository browser.