Skip to content
Snippets Groups Projects
AbstractDOMParser.cpp 50.9 KiB
Newer Older
Khaled Noaman's avatar
Khaled Noaman committed
/*
 * The Apache Software License, Version 1.1
 *
Neil Graham's avatar
Neil Graham committed
 * Copyright (c) 2002,2003 The Apache Software Foundation.  All rights
Khaled Noaman's avatar
Khaled Noaman committed
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache\@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation, and was
 * originally based on software copyright (c) 2001, International
 * Business Machines, Inc., http://www.ibm.com .  For more information
 * on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

/**
*  This file contains code to build the DOM tree. It registers a document
*  handler with the scanner. In these handler methods, appropriate DOM nodes
*  are created and added to the DOM tree.
*
* $Id$
*
*/



// ---------------------------------------------------------------------------
//  Includes
// ---------------------------------------------------------------------------
Khaled Noaman's avatar
Khaled Noaman committed
#include <xercesc/parsers/AbstractDOMParser.hpp>
#include <xercesc/internal/XMLScannerResolver.hpp>
#include <xercesc/internal/ElemStack.hpp>
Khaled Noaman's avatar
Khaled Noaman committed
#include <xercesc/sax/EntityResolver.hpp>
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/framework/XMLNotationDecl.hpp>
Khaled Noaman's avatar
Khaled Noaman committed
#include <xercesc/framework/XMLValidator.hpp>
Khaled Noaman's avatar
Khaled Noaman committed
#include <xercesc/util/IOException.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/impl/DOMAttrImpl.hpp>
#include <xercesc/dom/DOMCDATASection.hpp>
#include <xercesc/dom/DOMComment.hpp>
#include <xercesc/dom/impl/DOMTextImpl.hpp>
#include <xercesc/dom/impl/DOMDocumentImpl.hpp>
#include <xercesc/dom/impl/DOMDocumentTypeImpl.hpp>
#include <xercesc/dom/DOMDocumentType.hpp>
#include <xercesc/dom/impl/DOMElementImpl.hpp>
#include <xercesc/dom/impl/DOMEntityImpl.hpp>
#include <xercesc/dom/impl/DOMEntityReferenceImpl.hpp>
#include <xercesc/dom/impl/DOMNotationImpl.hpp>
#include <xercesc/dom/DOMNamedNodeMap.hpp>
#include <xercesc/dom/DOMProcessingInstruction.hpp>
Tinny Ng's avatar
Tinny Ng committed
#include <xercesc/dom/impl/DOMProcessingInstructionImpl.hpp>
Khaled Noaman's avatar
Khaled Noaman committed
#include <xercesc/dom/impl/DOMNodeIDMap.hpp>
#include <xercesc/validators/common/ContentSpecNode.hpp>
Khaled Noaman's avatar
Khaled Noaman committed
#include <xercesc/validators/common/GrammarResolver.hpp>
#include <xercesc/validators/schema/SchemaSymbols.hpp>
Tinny Ng's avatar
Tinny Ng committed
XERCES_CPP_NAMESPACE_BEGIN


Khaled Noaman's avatar
Khaled Noaman committed
// ---------------------------------------------------------------------------
//  AbstractDOMParser: Constructors and Destructor
// ---------------------------------------------------------------------------
AbstractDOMParser::AbstractDOMParser( XMLValidator* const valToAdopt
                                    , MemoryManager* const manager) :
Khaled Noaman's avatar
Khaled Noaman committed

  fCreateEntityReferenceNodes(true)
, fIncludeIgnorableWhitespace(true)
, fWithinElement(false)
, fParseInProgress(false)
Khaled Noaman's avatar
Khaled Noaman committed
, fCreateCommentNodes(true)
, fDocumentAdoptedByUser(false)
Khaled Noaman's avatar
Khaled Noaman committed
, fScanner(0)
, fCurrentParent(0)
, fCurrentNode(0)
Tinny Ng's avatar
Tinny Ng committed
, fCurrentEntity(0)
Khaled Noaman's avatar
Khaled Noaman committed
, fDocument(0)
, fNodeStack(0)
, fDocumentType(0)
, fDocumentVector(0)
Khaled Noaman's avatar
Khaled Noaman committed
, fGrammarResolver(0)
, fURIStringPool(0)
, fValidator(valToAdopt)
, fMemoryManager(manager)
, fBufMgr(manager)
, fInternalSubset(fBufMgr.bidOnBuffer())
Khaled Noaman's avatar
Khaled Noaman committed
{
Khaled Noaman's avatar
Khaled Noaman committed
    try
    {
        initialize();
    }
    catch(...)
    {
       cleanUp();
       throw;
    }
}


AbstractDOMParser::~AbstractDOMParser()
{
    cleanUp();
}

// ---------------------------------------------------------------------------
//  AbstractDOMParser: Initialize/CleanUp methods
// ---------------------------------------------------------------------------
void AbstractDOMParser::initialize()
{
    //  Create grammar resolver and string pool to pass to the scanner
    fGrammarResolver = new (fMemoryManager) GrammarResolver(fMemoryManager);
    fURIStringPool = new (fMemoryManager) XMLStringPool(109, fMemoryManager);
Khaled Noaman's avatar
Khaled Noaman committed
    //  Create a scanner and tell it what validator to use. Then set us
    //  as the document event handler so we can fill the DOM document.
    fScanner = XMLScannerResolver::getDefaultScanner(fValidator, fMemoryManager);
Khaled Noaman's avatar
Khaled Noaman committed
    fScanner->setDocHandler(this);
    fScanner->setDocTypeHandler(this);
Khaled Noaman's avatar
Khaled Noaman committed
    fScanner->setGrammarResolver(fGrammarResolver);
    fScanner->setURIStringPool(fURIStringPool);
    fNodeStack = new (fMemoryManager) ValueStackOf<DOMNode*>(64, fMemoryManager);
Khaled Noaman's avatar
Khaled Noaman committed
void AbstractDOMParser::cleanUp()
Khaled Noaman's avatar
Khaled Noaman committed
{
    if (fDocumentVector)
        delete fDocumentVector;

PeiYong Zhang's avatar
PeiYong Zhang committed
    if (!fDocumentAdoptedByUser && fDocument)
        fDocument->release();
Khaled Noaman's avatar
Khaled Noaman committed
    delete fNodeStack;
    delete fScanner;
Khaled Noaman's avatar
Khaled Noaman committed
    delete fGrammarResolver;
    delete fURIStringPool;
Khaled Noaman's avatar
Khaled Noaman committed
    if (fValidator)
        delete fValidator;
}
Tinny Ng's avatar
Tinny Ng committed
// ---------------------------------------------------------------------------
//  AbstractDOMParser: Utilities
Tinny Ng's avatar
Tinny Ng committed
// ---------------------------------------------------------------------------
Khaled Noaman's avatar
Khaled Noaman committed
void AbstractDOMParser::reset()
{
    // if fDocument exists already, store the old pointer in the vector for deletion later
Tinny Ng's avatar
Tinny Ng committed
    if (fDocument && !fDocumentAdoptedByUser) {
Khaled Noaman's avatar
Khaled Noaman committed
        if (!fDocumentVector) {
            // allocate the vector if not exists yet
            fDocumentVector  = new (fMemoryManager) RefVectorOf<DOMDocumentImpl>(10, true, fMemoryManager) ;
Khaled Noaman's avatar
Khaled Noaman committed
        }
        fDocumentVector->addElement(fDocument);
    }

    fDocument = 0;
    resetDocType();

Loading
Loading full blame...