normalizer.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 424 行 · 第 1/2 页

CPP
424
字号
/* * Copyright 2003,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include "Normalizer.hpp"#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/framework/StdOutFormatTarget.hpp>#include <xercesc/framework/XMLBuffer.hpp>#include <xercesc/parsers/XercesDOMParser.hpp>#if defined(XERCES_NEW_IOSTREAMS)#include <iostream>#else#include <iostream.h>#endif#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include "../../../src/xercesc/dom/impl/DOMConfigurationImpl.hpp"#include "../../../src/xercesc/dom/impl/DOMDocumentImpl.hpp"#include "../../../src/xercesc/dom/impl/DOMEntityImpl.hpp"#include "../../../src/xercesc/dom/impl/DOMEntityReferenceImpl.hpp"// ---------------------------------------------------------------------------//  This is a simple class that lets us do easy (though not terribly efficient)//  trancoding of char* data to XMLCh data.// ---------------------------------------------------------------------------class XStr{public :    // -----------------------------------------------------------------------    //  Constructors and Destructor    // -----------------------------------------------------------------------    XStr(const char* const toTranscode)    {        // Call the private transcoding method        fUnicodeForm = XMLString::transcode(toTranscode);    }    ~XStr()    {        XMLString::release(&fUnicodeForm);    }    // -----------------------------------------------------------------------    //  Getter methods    // -----------------------------------------------------------------------    const XMLCh* unicodeForm() const    {        return fUnicodeForm;    }private :    // -----------------------------------------------------------------------    //  Private data members    //    //  fUnicodeForm    //      This is the Unicode XMLCh format of the string.    // -----------------------------------------------------------------------    XMLCh*   fUnicodeForm;};#define X(str) XStr(str).unicodeForm()//  This is a simple class that lets us do easy (though not terribly efficient)//  trancoding of XMLCh data to local code page for display.// ---------------------------------------------------------------------------class StrX{public :    // -----------------------------------------------------------------------    //  Constructors and Destructor    // -----------------------------------------------------------------------    StrX(const XMLCh* const toTranscode)    {        // Call the private transcoding method        fLocalForm = XMLString::transcode(toTranscode);    }    ~StrX()    {        XMLString::release(&fLocalForm);    }    // -----------------------------------------------------------------------    //  Getter methods    // -----------------------------------------------------------------------    const char* localForm() const    {        return fLocalForm;    }private :    // -----------------------------------------------------------------------    //  Private data members    //    //  fLocalForm    //      This is the local code page form of the string.    // -----------------------------------------------------------------------    char*   fLocalForm;};#define StrX(str) StrX(str).localForm()Normalizer::Normalizer() {    try    {        XMLPlatformUtils::Initialize();    }    catch(const XMLException &toCatch)    {        XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"             << "  Exception message:"             << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;    }    parser = 0;}Normalizer::~Normalizer() {    XMLPlatformUtils::Terminate();}void Normalizer::printEntityRefNodes(DOMElement *ele) {        DOMNode *child = ele->getFirstChild();    while(child != 0) {        if(child->getNodeType() == DOMNode::ENTITY_REFERENCE_NODE) {            XERCES_STD_QUALIFIER cout << "start of entity ref node" << XERCES_STD_QUALIFIER endl;            DOMNode *entChild = ((DOMEntityReference*)child)->getFirstChild();            while(entChild != 0) {                serializeNode(entChild);                entChild = entChild->getNextSibling();            }            XERCES_STD_QUALIFIER cout << "\nend of entity ref node\n\n" << XERCES_STD_QUALIFIER endl;        }        if(child->getNodeType() == DOMNode::ELEMENT_NODE) {            printEntityRefNodes((DOMElement*)child);        }        child = child->getNextSibling();    }    }bool Normalizer::handleError(const DOMError& domError){    // Display whatever error message passed from the serializer    if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING)        XERCES_STD_QUALIFIER cerr << "\nWarning Message: ";    else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR)        XERCES_STD_QUALIFIER cerr << "\nError Message: ";    else        XERCES_STD_QUALIFIER cerr << "\nFatal Message: ";    char *msg = XMLString::transcode(domError.getMessage());    XERCES_STD_QUALIFIER cerr<< msg <<XERCES_STD_QUALIFIER endl;    XMLString::release(&msg);    XERCES_STD_QUALIFIER cerr << "Related data ";    msg = XMLString::transcode(((DOMNode*)domError.getRelatedData())->getNodeName());    XERCES_STD_QUALIFIER cerr << msg <<XERCES_STD_QUALIFIER endl;    XMLString::release(&msg);     // continue serialization if possible.    return true;}DOMDocument* Normalizer::createDocument() {    XMLCh coreStr[100];    XMLString::transcode("Core",coreStr,99);    DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(coreStr);    return impl->createDocument();};void Normalizer::serializeNode(const DOMNode * const node) {    XMLCh tempStr[100];    XMLString::transcode("LS", tempStr, 99);    DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempStr);    DOMWriter         *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();    theSerializer->setFeature(X("format-pretty-print"), true);    XMLFormatTarget *myFormTarget;    myFormTarget = new StdOutFormatTarget();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?