1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/******************************************************************************
* $Id: ili2handler.cpp 24408 2012-05-11 21:31:45Z pka $
*
* Project: Interlis 2 Reader
* Purpose: Implementation of ILI2Handler class.
* Author: Markus Schnider, Sourcepole AG
*
******************************************************************************
* Copyright (c) 2004, Pirmin Kalberer, Sourcepole AG
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "ogr_ili2.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ili2readerp.h"
#include <xercesc/sax2/Attributes.hpp>
CPL_CVSID("$Id: ili2handler.cpp 24408 2012-05-11 21:31:45Z pka $");
//
// constants
//
static const char* ILI2_DATASECTION = "DATASECTION";
//
// ILI2Handler
//
ILI2Handler::ILI2Handler( ILI2Reader *poReader ) {
m_poReader = poReader;
XMLCh *tmpCh = XMLString::transcode("CORE");
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tmpCh);
XMLString::release(&tmpCh);
// the root element
tmpCh = XMLString::transcode("ROOT");
dom_doc = impl->createDocument(0,tmpCh,0);
XMLString::release(&tmpCh);
// the first element is root
dom_elem = dom_doc->getDocumentElement();
}
ILI2Handler::~ILI2Handler() {
// remove all elements
DOMNode *tmpNode = dom_doc->getFirstChild();
while (tmpNode != NULL) {
tmpNode = dom_doc->removeChild(tmpNode);
tmpNode = dom_doc->getFirstChild();
}
// release the dom tree
dom_doc->release();
}
void ILI2Handler::startDocument() {
// the level counter starts with DATASECTION
level = -1;
m_nEntityCounter = 0;
}
void ILI2Handler::endDocument() {
// nothing to do
}
void ILI2Handler::startElement(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes& attrs
) {
// start to add the layers, features with the DATASECTION
char *tmpC = NULL;
m_nEntityCounter = 0;
if ((level >= 0) || (cmpStr(ILI2_DATASECTION, tmpC = XMLString::transcode(qname)) == 0)) {
level++;
if (level >= 2) {
// create the dom tree
DOMElement *elem = (DOMElement*)dom_doc->createElement(qname);
// add all attributes
unsigned int len = attrs.getLength();
for (unsigned int index = 0; index < len; index++)
elem->setAttribute(attrs.getQName(index), attrs.getValue(index));
dom_elem->appendChild(elem);
dom_elem = elem;
}
}
XMLString::release(&tmpC);
}
void ILI2Handler::endElement(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname
) {
m_nEntityCounter = 0;
if (level >= 0) {
if (level == 2) {
// go to the parent element and parse the child element
DOMElement* childElem = dom_elem;
dom_elem = (DOMElement*)dom_elem->getParentNode();
m_poReader->AddFeature(childElem);
// remove the child element
childElem = (DOMElement*)dom_elem->removeChild(childElem);
} else if (level >= 3) {
// go to the parent element
dom_elem = (DOMElement*)dom_elem->getParentNode();
}
level--;
}
}
#if XERCES_VERSION_MAJOR >= 3
/************************************************************************/
/* characters() (xerces 3 version) */
/************************************************************************/
void ILI2Handler::characters( const XMLCh *const chars,
const XMLSize_t length ) {
// add the text element
if (level >= 3) {
char *tmpC = XMLString::transcode(chars);
// only add the text if it is not empty
if (trim(tmpC) != "")
dom_elem->appendChild(dom_doc->createTextNode(chars));
XMLString::release(&tmpC);
}
}
#else
/************************************************************************/
/* characters() (xerces 2 version) */
/************************************************************************/
void ILI2Handler::characters( const XMLCh *const chars,
const unsigned int length ) {
// add the text element
if (level >= 3) {
char *tmpC = XMLString::transcode(chars);
// only add the text if it is not empty
if (trim(tmpC) != "")
dom_elem->appendChild(dom_doc->createTextNode(chars));
XMLString::release(&tmpC);
}
}
#endif
void ILI2Handler::startEntity (const XMLCh *const name)
{
m_nEntityCounter++;
if (m_nEntityCounter > 1000)
{
throw SAXNotSupportedException ("File probably corrupted (million laugh pattern)");
}
}
void ILI2Handler::fatalError(const SAXParseException&) {
// FIXME Error handling
}
|