1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
40 //---------------------------------------------------------------------------
42 //---------------------------------------------------------------------------
47 #include "OSGConfig.h"
56 #include "OSGXMLScanParseSkel.h"
57 #include "OSGBaseFunctions.h"
62 //---------------------------------------------------------------------------
64 //---------------------------------------------------------------------------
66 /***************************************************************************\
68 \***************************************************************************/
70 /***************************************************************************\
72 \***************************************************************************/
74 /***************************************************************************\
76 \***************************************************************************/
78 /*-------------------------------------------------------------------------*\
80 \*-------------------------------------------------------------------------*/
82 #define OSGXMLBUFSIZE 4096
84 /*-------------------------------------------------------------------------*\
86 \*-------------------------------------------------------------------------*/
88 void XMLScanParseSkel::handleChar( void *pUserData
,
92 XMLScanParseSkel
*pSkel
= static_cast<XMLScanParseSkel
*>(pUserData
);
94 if(iLength
!= 1 || szData
[0] != '\n')
98 for(int i
= 0; i
< iLength
; ++i
)
100 if(isspace(szData
[i
]) == 0)
109 pSkel
->_szDataLine
.append(szData
, iLength
);
113 if(pSkel
->_szDataLine
.size() != 0 &&
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
);
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 /*-------------------------------------------------------------------------*\
169 \*-------------------------------------------------------------------------*/
171 /***************************************************************************\
173 \***************************************************************************/
175 /*-------------------------------------------------------------------------*\
177 \*-------------------------------------------------------------------------*/
179 /*-------------------------------------------------------------------------*\
181 \*-------------------------------------------------------------------------*/
183 void XMLScanParseSkel::startElement(const std::string
&szName
)
185 if(_eErrorState
!= NoError
)
188 CallbackMapIt it
= _mCallbackMap
.find(szName
);
190 if(it
!= _mCallbackMap
.end())
192 _sCallbackStack
.push(it
->second
);
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
)
208 _eErrorState
= _sCallbackStack
.top()->_handleAttributeCB(this,
213 void XMLScanParseSkel::endElement(const std::string
&szName
)
215 if(_eErrorState
!= NoError
)
218 _eErrorState
= _sCallbackStack
.top()->_endElementCB(this, szName
);
220 _sCallbackStack
.pop();
223 void XMLScanParseSkel::handleCharBlock(const std::string
&szData
)
225 if(_eErrorState
!= NoError
)
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());
240 XMLScanParseSkel::CBResult
XMLScanParseSkel::ignoreHandleAttribute(
241 const std::string
&szName
,
242 const std::string
&szValue
)
247 XMLScanParseSkel::CBResult
XMLScanParseSkel::ignoreEndElement(
248 const std::string
&szName
)
250 fprintf(stderr
, "ignore end %s\n", szName
.c_str());
255 XMLScanParseSkel::CBResult
XMLScanParseSkel::ignoreHandleCharBlock(
256 const std::string
&szData
)
258 fprintf(stderr
, " szLine : %d -%s-\n", szData
.size(), szData
.c_str());
263 /*-------------------------------------------------------------------------*\
265 \*-------------------------------------------------------------------------*/
267 /*------------- constructors & destructors --------------------------------*/
269 /** \brief Constructor
272 XMLScanParseSkel::XMLScanParseSkel(void) :
274 _eErrorState (NoError
),
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())
306 /*------------------------------ access -----------------------------------*/
308 /*---------------------------- properties ---------------------------------*/
310 void XMLScanParseSkel::scanStream(std::istream
&is
)
312 while(_sOptionStack
.empty() == false)
317 while(_sCallbackStack
.empty() == false)
319 _sCallbackStack
.pop();
324 Char8 szBuffer
[OSGXMLBUFSIZE
];
326 XML_Parser parser
= XML_ParserCreate(NULL
);
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
)
347 "%s at line %d %d %d %d\n",
348 XML_ErrorString(XML_GetErrorCode(parser
)),
349 XML_GetCurrentLineNumber(parser
),
354 } while (!done
&& (_eErrorState
== NoError
));
356 XML_ParserFree(parser
);
360 void XMLScanParseSkel::scanFile(const Char8
*szFilename
)
362 if(szFilename
== NULL
)
365 std::ifstream
is(szFilename
);
367 while(_sOptionStack
.empty() == false)
374 fprintf(stderr
, "Loading Stream: %s\n", szFilename
);
382 /*-------------------------- your_category---------------------------------*/
384 /*-------------------------- assignment -----------------------------------*/
386 /*-------------------------- comparison -----------------------------------*/