domwriterimpl.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,852 行 · 第 1/5 页
CPP
1,852 行
if(!bPrefixDeclared) { if(namespaceMap==NULL) { namespaceMap=new (fMemoryManager) RefHashTableOf<XMLCh>(12, false, fMemoryManager); fNamespaceStack->addElement(namespaceMap); } namespaceMap->put((void*)prefix,(XMLCh*)attribute->getNamespaceURI()); *fFormatter << XMLFormatter::NoEscapes << chSpace << XMLUni::fgXMLNSString << chColon << prefix << chEqual << chDoubleQuote << XMLFormatter::AttrEscapes << attribute->getNamespaceURI() << XMLFormatter::NoEscapes << chDoubleQuote; } } } } *fFormatter << XMLFormatter::NoEscapes << chSpace << attribute->getNodeName() << chEqual << chDoubleQuote << XMLFormatter::AttrEscapes << attribute->getNodeValue() << XMLFormatter::NoEscapes << chDoubleQuote; } // end of for } // end of FILTER_ACCEPT level++; // FILTER_SKIP may start from here // // Test for the presence of children, which includes both // text content and nested elements. // DOMNodeSPtr child = nodeToWrite->getFirstChild(); if (child != 0) { // There are children. Close start-tag, and output children. // No escapes are legal here if (filterAction == DOMNodeFilter::FILTER_ACCEPT) *fFormatter << XMLFormatter::NoEscapes << chCloseAngle; while( child != 0) { processNode(child, level); child = child->getNextSibling(); } level--; if (filterAction == DOMNodeFilter::FILTER_ACCEPT) { //if we are not on the same line as when we started //this node then print a new line and indent if(nodeLine != fCurrentLine) { if (!lineFeedInTextNodePrinted) { printNewLine(); } else { lineFeedInTextNodePrinted = false; } if(nodeLine != fCurrentLine && level == 0) printNewLine(); printIndent(level); } TRY_CATCH_THROW ( *fFormatter << XMLFormatter::NoEscapes << gEndElement << nodeName << chCloseAngle; ,true ) } } else { level--; // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // if (filterAction == DOMNodeFilter::FILTER_ACCEPT) { TRY_CATCH_THROW ( *fFormatter << XMLFormatter::NoEscapes << chForwardSlash << chCloseAngle; , true ) } } // remove the namespace map at this level if(namespaceMap!=NULL) fNamespaceStack->removeLastElement(); break; } case DOMNode::ATTRIBUTE_NODE: { if (checkFilter(nodeToWrite) != DOMNodeFilter::FILTER_ACCEPT) break; const XMLCh* localName = nodeToWrite->getLocalName(); // check if this is a DOM Level 1 Node if(localName == 0) { *fFormatter << XMLFormatter::NoEscapes << nodeToWrite->getNodeName() << chEqual << chDoubleQuote << XMLFormatter::AttrEscapes << nodeToWrite->getNodeValue() << XMLFormatter::NoEscapes << chDoubleQuote; } else { *fFormatter << XMLFormatter::NoEscapes << chOpenCurly << nodeToWrite->getNamespaceURI() << chCloseCurly << localName << chEqual << chDoubleQuote << XMLFormatter::AttrEscapes << nodeToWrite->getNodeValue() << XMLFormatter::NoEscapes << chDoubleQuote; } break; } case DOMNode::ENTITY_REFERENCE_NODE: { //"entities" //true //[required] (default) //Keep EntityReference and Entity nodes in the document. //false //[optional] //Remove all EntityReference and Entity nodes from the document, // putting the entity expansions directly in their place. // Text nodes are into "normal" form. //Only EntityReference nodes to non-defined entities are kept in the document. if (checkFilter(nodeToWrite) != DOMNodeFilter::FILTER_ACCEPT) break; if (getFeature(ENTITIES_ID)) { TRY_CATCH_THROW ( *fFormatter << XMLFormatter::NoEscapes << chAmpersand << nodeName << chSemiColon; , true ) } else { // check if the referenced entity is defined or not if (nodeToWrite->getOwnerDocument()->getDoctype()->getEntities()->getNamedItem(nodeName)) { DOMNodeSPtr child; for (child = nodeToWrite->getFirstChild(); child != 0; child = child->getNextSibling()) { processNode(child, level); } } else { TRY_CATCH_THROW ( *fFormatter<<XMLFormatter::NoEscapes<<chAmpersand<<nodeName<<chSemiColon; , true ) } } break; } // // feature:split_cdata_sections occurence of ]]> unrep-char // =============================================================== // true split split // false fails fails // case DOMNode::CDATA_SECTION_NODE: { if (checkFilter(nodeToWrite) != DOMNodeFilter::FILTER_ACCEPT) break; if(level == 1) printNewLine(); printNewLine(); printIndent(level); if (getFeature(SPLIT_CDATA_SECTIONS_ID)) { // it is fairly complicated and we process this // in a separate function. procCdataSection(nodeValue, nodeToWrite, level); } else { // search for "]]>", the node value is not supposed to have this if (XMLString::patternMatch((XMLCh* const) nodeValue, gEndCDATA) != -1) { reportError(nodeToWrite, DOMError::DOM_SEVERITY_FATAL_ERROR, XMLDOMMsg::Writer_NestedCDATA); } TRY_CATCH_THROW ( // transcoder throws exception for unrep chars *fFormatter << XMLFormatter::NoEscapes << gStartCDATA << nodeValue << gEndCDATA; , true ) } break; } case DOMNode::COMMENT_NODE: { if (checkFilter(nodeToWrite) != DOMNodeFilter::FILTER_ACCEPT) break; if(level == 1) printNewLine(); printNewLine(); printIndent(level); TRY_CATCH_THROW ( *fFormatter << XMLFormatter::NoEscapes << gStartComment << nodeValue << gEndComment; , true ) break; } case DOMNode::DOCUMENT_TYPE_NODE: // Not to be shown to Filter { const DOMDocumentType *doctype = (const DOMDocumentType *)nodeToWrite; fFormatter->setEscapeFlags(XMLFormatter::NoEscapes); printNewLine(); printIndent(level); TRY_CATCH_THROW ( *fFormatter << gStartDoctype << nodeName; const XMLCh *id = doctype->getPublicId(); if (id && *id) { *fFormatter << chSpace << gPublic << id << chDoubleQuote; id = doctype->getSystemId(); if (id && *id) { *fFormatter << chSpace << chDoubleQuote << id << chDoubleQuote; } else { // // 4.2.2 External Entities // [Definition: If the entity is not internal, // it is an external entity, declared as follows:] // External Entity Declaration // [75] ExternalID ::= 'SYSTEM' S SystemLiteral // | 'PUBLIC' S PubidLiteral S SystemLiteral // reportError(nodeToWrite, DOMError::DOM_SEVERITY_FATAL_ERROR, XMLDOMMsg::Writer_NotRecognizedType); // systemLiteral not found } } else { id = doctype->getSystemId(); if (id && *id) { *fFormatter << chSpace << gSystem << id << chDoubleQuote; } } id = doctype->getInternalSubset(); if (id && *id) { *fFormatter << chSpace << chOpenSquare << id << chCloseSquare; } *fFormatter << chCloseAngle; , true ) // end of TRY_CATCH_THROW break; } case DOMNode::ENTITY_NODE: // Not to be shown to Filter { // // REVISIT: how does the feature "entities" impact // entity node? // printNewLine(); printIndent(level); fFormatter->setEscapeFlags(XMLFormatter::NoEscapes); *fFormatter << gStartEntity << nodeName; const XMLCh * id = ((const DOMEntity*)nodeToWrite)->getPublicId(); if (id) *fFormatter << gPublic << id << chDoubleQuote; id = ((const DOMEntity*)nodeToWrite)->getSystemId(); if (id) *fFormatter << gSystem << id << chDoubleQuote; id = ((const DOMEntity*)nodeToWrite)->getNotationName(); if (id) *fFormatter << gNotation << id << chDoubleQuote; *fFormatter << chCloseAngle; break; } default: /*** This is an implementation specific behaviour, we abort if a user derived class has not dealt with this node type. ***/ { if(!customNodeSerialize(nodeToWrite, level)) { reportError(nodeToWrite, DOMError::DOM_SEVERITY_FATAL_ERROR, XMLDOMMsg::Writer_NotRecognizedType); // UnreognizedNodeType; } } break; }}bool DOMWriterImpl::customNodeSerialize(const DOMNode* const, int) { return false;}////DOMNodeFilter::FilterAction DOMWriterImpl::checkFilter(const DOMNode* const node) const{ if (!fFilter || ((fFilter->getWhatToShow() & (1 << (node->getNodeType() - 1))) == 0)) return DOMNodeFilter::FILTER_ACCEPT; // // if and only if there is a filter, and it is interested // in the node type, then we pass the node to the filter // for examination //
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?