fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / ScanParseSkel / OSGXMLScanParseSkel.cpp
blobcf9ca9e5d8ff2b98ed2d648ed866e49e4dc2ddb1
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
40 //---------------------------------------------------------------------------
41 // Includes
42 //---------------------------------------------------------------------------
44 #include <cstdlib>
45 #include <cstdio>
47 #include "OSGConfig.h"
49 #ifdef OSG_WITH_EXPAT
51 #include "expat.h"
53 #include <iostream>
54 #include <string>
56 #include "OSGXMLScanParseSkel.h"
57 #include "OSGBaseFunctions.h"
58 #include "OSGLog.h"
60 OSG_USING_NAMESPACE
62 //---------------------------------------------------------------------------
63 // Class
64 //---------------------------------------------------------------------------
66 /***************************************************************************\
67 * Types *
68 \***************************************************************************/
70 /***************************************************************************\
71 * Class variables *
72 \***************************************************************************/
74 /***************************************************************************\
75 * Class methods *
76 \***************************************************************************/
78 /*-------------------------------------------------------------------------*\
79 - private -
80 \*-------------------------------------------------------------------------*/
82 #define OSGXMLBUFSIZE 4096
84 /*-------------------------------------------------------------------------*\
85 - protected -
86 \*-------------------------------------------------------------------------*/
88 void XMLScanParseSkel::handleChar( void *pUserData,
89 const Char8 *szData,
90 Int32 iLength)
92 XMLScanParseSkel *pSkel = static_cast<XMLScanParseSkel *>(pUserData);
94 if(iLength != 1 || szData[0] != '\n')
96 bool bFound = false;
98 for(int i = 0; i < iLength; ++i)
100 if(isspace(szData[i]) == 0)
102 bFound = true;
103 break;
107 if(bFound == true)
109 pSkel->_szDataLine.append(szData, iLength);
113 if(pSkel->_szDataLine.size() != 0 &&
114 iLength == 1 &&
115 szData[0] == '\n' )
117 pSkel->_szDataLine.append(" ", 1);
121 void XMLScanParseSkel::startElement( void * pUserData,
122 const Char8 * szName,
123 const Char8 **vAtts )
125 std::string szElementName(szName);
127 XMLScanParseSkel *pSkel = static_cast<XMLScanParseSkel *>(pUserData);
129 pSkel->startElement(szName);
131 if(vAtts != NULL)
133 int j = 0;
135 while(vAtts[j] != NULL)
137 pSkel->_szAttrName.assign (vAtts[j++]);
138 pSkel->_szAttrValue.assign(vAtts[j++]);
140 pSkel->handleAttribute(pSkel->_szAttrName,
141 pSkel->_szAttrValue);
145 pSkel->_szDataLine.clear();
148 void XMLScanParseSkel::endElement( void *pUserData,
149 const Char8 *szName )
151 XMLScanParseSkel *pSkel = static_cast<XMLScanParseSkel *>(pUserData);
153 if(pSkel->_szDataLine.size() != 0)
155 pSkel->_szDataLine += '\0';
157 pSkel->handleCharBlock(pSkel->_szDataLine);
159 pSkel->_szDataLine.clear();
162 std::string szElementName(szName);
164 pSkel->endElement(szName);
167 /*-------------------------------------------------------------------------*\
168 - public -
169 \*-------------------------------------------------------------------------*/
171 /***************************************************************************\
172 * Instance methods *
173 \***************************************************************************/
175 /*-------------------------------------------------------------------------*\
176 - private -
177 \*-------------------------------------------------------------------------*/
179 /*-------------------------------------------------------------------------*\
180 - protected -
181 \*-------------------------------------------------------------------------*/
183 void XMLScanParseSkel::startElement(const std::string &szName)
185 if(_eErrorState != NoError)
186 return;
188 CallbackMapIt it = _mCallbackMap.find(szName);
190 if(it != _mCallbackMap.end())
192 _sCallbackStack.push(it->second);
194 else
196 _sCallbackStack.push(&_oIgnoreCB);
199 _eErrorState = _sCallbackStack.top()->_startElementCB(this, szName);
202 void XMLScanParseSkel::handleAttribute(const std::string &szName,
203 const std::string &szValue)
205 if(_eErrorState != NoError)
206 return;
208 _eErrorState = _sCallbackStack.top()->_handleAttributeCB(this,
209 szName,
210 szValue);
213 void XMLScanParseSkel::endElement(const std::string &szName)
215 if(_eErrorState != NoError)
216 return;
218 _eErrorState = _sCallbackStack.top()->_endElementCB(this, szName);
220 _sCallbackStack.pop();
223 void XMLScanParseSkel::handleCharBlock(const std::string &szData)
225 if(_eErrorState != NoError)
226 return;
228 _eErrorState = _sCallbackStack.top()->_handleDataBlockCB(this, szData);
232 XMLScanParseSkel::CBResult XMLScanParseSkel::ignoreStartElement(
233 const std::string &szName)
235 fprintf(stderr, "ignore %s\n", szName.c_str());
237 return NoError;
240 XMLScanParseSkel::CBResult XMLScanParseSkel::ignoreHandleAttribute(
241 const std::string &szName,
242 const std::string &szValue)
244 return NoError;
247 XMLScanParseSkel::CBResult XMLScanParseSkel::ignoreEndElement(
248 const std::string &szName)
250 fprintf(stderr, "ignore end %s\n", szName.c_str());
252 return NoError;
255 XMLScanParseSkel::CBResult XMLScanParseSkel::ignoreHandleCharBlock(
256 const std::string &szData)
258 fprintf(stderr, " szLine : %d -%s-\n", szData.size(), szData.c_str());
260 return NoError;
263 /*-------------------------------------------------------------------------*\
264 - public -
265 \*-------------------------------------------------------------------------*/
267 /*------------- constructors & destructors --------------------------------*/
269 /** \brief Constructor
272 XMLScanParseSkel::XMLScanParseSkel(void) :
273 _uiLoadOptions ( 0),
274 _eErrorState (NoError),
275 _szDataLine ( ),
276 _szAttrName ( ),
277 _szAttrValue ( ),
278 _sOptionStack ( ),
279 _oIgnoreCB ( ),
280 _mCallbackMap ( ),
281 _sCallbackStack( )
283 _oIgnoreCB._startElementCB = &XMLScanParseSkel::ignoreStartElement;
284 _oIgnoreCB._handleAttributeCB = &XMLScanParseSkel::ignoreHandleAttribute;
285 _oIgnoreCB._endElementCB = &XMLScanParseSkel::ignoreEndElement;
286 _oIgnoreCB._handleDataBlockCB = &XMLScanParseSkel::ignoreHandleCharBlock;
289 /** \brief Destructor
292 XMLScanParseSkel::~XMLScanParseSkel(void)
294 CallbackMapIt it = _mCallbackMap.begin();
296 while(it != _mCallbackMap.end())
298 delete it->second;
300 it->second = NULL;
302 ++it;
306 /*------------------------------ access -----------------------------------*/
308 /*---------------------------- properties ---------------------------------*/
310 void XMLScanParseSkel::scanStream(std::istream &is)
312 while(_sOptionStack.empty() == false)
314 _sOptionStack.pop();
317 while(_sCallbackStack.empty() == false)
319 _sCallbackStack.pop();
322 if(is.good())
324 Char8 szBuffer[OSGXMLBUFSIZE];
326 XML_Parser parser = XML_ParserCreate(NULL);
328 int done;
330 XML_SetUserData (parser, this );
331 XML_SetElementHandler (parser, startElement, endElement);
332 XML_SetCharacterDataHandler(parser, handleChar );
334 _sCallbackStack.push(&_oIgnoreCB);
338 is.read(szBuffer, OSGXMLBUFSIZE);
340 std::streamsize len = is.gcount();
342 done = len < OSGXMLBUFSIZE;
344 if(XML_Parse(parser, szBuffer, len, done) == XML_STATUS_ERROR)
346 fprintf(stderr,
347 "%s at line %d %d %d %d\n",
348 XML_ErrorString(XML_GetErrorCode(parser)),
349 XML_GetCurrentLineNumber(parser),
350 done,
351 OSGXMLBUFSIZE, len);
352 break;
354 } while (!done && (_eErrorState == NoError));
356 XML_ParserFree(parser);
360 void XMLScanParseSkel::scanFile(const Char8 *szFilename)
362 if(szFilename == NULL)
363 return;
365 std::ifstream is(szFilename);
367 while(_sOptionStack.empty() == false)
369 _sOptionStack.pop();
372 if(is.good())
374 fprintf(stderr, "Loading Stream: %s\n", szFilename);
376 scanStream(is);
378 is.close();
382 /*-------------------------- your_category---------------------------------*/
384 /*-------------------------- assignment -----------------------------------*/
386 /*-------------------------- comparison -----------------------------------*/
388 #endif