StructuredDocumentHandler.C 5.94 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17
// StructuredDocumentHandler.C
//
////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
18 19

#if USE_XERCES
Francois Gygi committed
20 21 22 23 24 25 26 27 28 29 30 31

#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include "StructuredDocumentHandler.h"

#if TIMING
#include "Timer.h"
#endif

#include "StrX.h"
#include <iostream>
#include <cassert>
32
using namespace xercesc;
Francois Gygi committed
33 34 35 36
using namespace std;

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::startElement(const XMLCh* const uri,
37
  const XMLCh* const localname, const XMLCh* const qname,
Francois Gygi committed
38 39 40 41 42
  const Attributes& attributes)
{
  // cout << " StructuredDocumentHandler::startElement " << StrX(qname) << endl;
  // cout << " nestingDepth before: " << nestingDepth << endl;
  buffer = "";
43

Francois Gygi committed
44 45 46 47 48 49 50 51
  // attempt to start a subhandler for this element
  StructureHandler* next = activeHandler->startSubHandler(uri,localname,qname,
    attributes);

  // yes, this element can be processed by a subhandler
  if ( next != 0 )
  {
    contextStack.push(HandlerContext(activeHandler,contextDepth));
52

Francois Gygi committed
53 54 55 56 57 58 59 60 61
    activeHandler = next;
    contextDepth = nestingDepth;
  }
  activeHandler->startElement(uri,localname,qname,attributes);
  nestingDepth++;
  // cout << " nestingDepth after:  " << nestingDepth << endl;
}

////////////////////////////////////////////////////////////////////////////////
62 63 64 65
#ifndef XERCES_VERSION_MAJOR
#error "XERCES_VERSION_MAJOR not defined"
#endif
#if XERCES_VERSION_MAJOR > 2
66 67 68
void StructuredDocumentHandler::characters(const XMLCh* const chars,
  const XMLSize_t length)
#else
Francois Gygi committed
69 70
void StructuredDocumentHandler::characters(const XMLCh* const chars,
  const unsigned int length)
71
#endif
Francois Gygi committed
72 73 74 75 76
{
#if TIMING
  Timer tm;
  tm.start();
#endif
77 78 79 80 81

  char *str = XMLString::transcode(chars);
  buffer += str;
  XMLString::release(&str);

Francois Gygi committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#if TIMING
  tm.stop();
  cout << " StructuredDocumentHandler::characters: time: " << tm.real() << endl;
#endif
  //cout << "length=" << length << " buffer.size=" << buffer.size() << endl;
}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::endElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname)
{
  // cout << " StructuredDocumentHandler::endElement " << StrX(qname) << endl;
  // cout << " nestingDepth before: " << nestingDepth << endl;
  nestingDepth--;
  activeHandler->endElement(uri,localname,qname,buffer);
97

Francois Gygi committed
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
  // Check if this element was processed by a subhandler
  // If yes, pop the previous context from the Context stack
  if ( nestingDepth != 0 && nestingDepth == contextDepth )
  {
    // cout << " popping context stack: nestingDepth=" << nestingDepth << endl;
    HandlerContext context = contextStack.top();
    StructureHandler* last = activeHandler;
    activeHandler = context.handler;
    contextDepth = context.depth;
    contextStack.pop();

    // notify activeHandler that current structure is complete
    // This allows the activeHandler to delete the last handler it has created
    activeHandler->endSubHandler(uri,localname,qname,last);
  }
  // cout << " nestingDepth after:  " << nestingDepth << endl;
  buffer = "";
}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::startDocument() {}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::endDocument() {}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::ignorableWhitespace( const   XMLCh* const chars,
  const  unsigned int length) {}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::processingInstruction(const  XMLCh* const target,
  const XMLCh* const data) {}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::error(const SAXParseException& e)
{
    cout << "\nError at file " << StrX(e.getSystemId())
Francois Gygi committed
135 136
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
Francois Gygi committed
137
         << "\n  Message: " << StrX(e.getMessage()) << endl;
138

Francois Gygi committed
139 140 141 142 143 144 145
    throw(e);
}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::fatalError(const SAXParseException& e)
{
    cout << "\nFatal Error at file " << StrX(e.getSystemId())
Francois Gygi committed
146 147
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
Francois Gygi committed
148 149 150 151 152 153 154 155
         << "\n  Message: " << StrX(e.getMessage()) << endl;
    throw(e);
}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::warning(const SAXParseException& e)
{
    cout << "\nWarning at file " << StrX(e.getSystemId())
Francois Gygi committed
156 157
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
Francois Gygi committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::unparsedEntityDecl(const     XMLCh* const name,
  const   XMLCh* const publicId, const   XMLCh* const systemId,
  const   XMLCh* const notationName)
{
    // Not used at this time
}

////////////////////////////////////////////////////////////////////////////////
void StructuredDocumentHandler::notationDecl(const   XMLCh* const name,
  const XMLCh* const publicId, const XMLCh* const systemId)
{
    // Not used at this time
}
Francois Gygi committed
175 176

#endif