bool DOMDocumentTypeImpl::isEqualNode(const DOMNode* arg) const { if (isSameNode(arg)) { return true; } if (!fNode.isEqualNode(arg)) { return false; } DOMDocumentType* argDT = (DOMDocumentType*) arg; // check the string values if (!getPublicId()) { if (argDT->getPublicId()) { return false; } } else if (!XMLString::equals(getPublicId(), argDT->getPublicId())) { return false; } if (!getSystemId()) { if (argDT->getSystemId()) { return false; } } else if (!XMLString::equals(getSystemId(), argDT->getSystemId())) { return false; } if (!getInternalSubset()) { if (argDT->getInternalSubset()) { return false; } } else if (!XMLString::equals(getInternalSubset(), argDT->getInternalSubset())) { return false; } // check the notations if (getNotations()) { if (!argDT->getNotations()) return false; DOMNamedNodeMap* map1 = getNotations(); DOMNamedNodeMap* map2 = argDT->getNotations(); XMLSize_t len = map1->getLength(); if (len != map2->getLength()) { return false; } for (XMLSize_t i = 0; i < len; i++) { DOMNode* n1 = map1->item(i); DOMNode* n2 = map2->getNamedItem(n1->getNodeName()); if (!n2 || !n1->isEqualNode(n2)) { return false; } } } else { if (argDT->getNotations()) return false; } // check the entities if (getEntities()) { if (!argDT->getEntities()) return false; DOMNamedNodeMap* map1 = getEntities(); DOMNamedNodeMap* map2 = argDT->getEntities(); XMLSize_t len = map1->getLength(); if (len != map2->getLength()) { return false; } for (XMLSize_t i = 0; i < len; i++) { DOMNode* n1 = map1->item(i); DOMNode* n2 = map2->getNamedItem(n1->getNodeName()); if (!n2 || !n1->isEqualNode(n2)) { return false; } } } else { if (argDT->getEntities()) return false; } return fParent.isEqualNode(arg); }
xmlstream& operator<<(xmlstream& target, const DOMNode* toWrite) { // Get the name and value out for convenience const XMLCh* nodeName = toWrite->getNodeName(); const XMLCh* nodeValue = toWrite->getNodeValue(); switch (toWrite->getNodeType()) { case DOMNode::TEXT_NODE: { outputContent(target, nodeValue); break; } case DOMNode::PROCESSING_INSTRUCTION_NODE : { target << XMLStrL("<?") << nodeName << XMLStrL(' ') << nodeValue << XMLStrL("?>"); break; } case DOMNode::DOCUMENT_NODE : { // // Bug here: we need to find a way to get the encoding name // for the default code page on the system where the program // is running, and plug that in for the encoding name. // XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* document=(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument*)toWrite; target << XMLStrL("<?xml version=\"") << document->getVersion() << XMLStrL("\""); const XMLCh* str = document->getEncoding(); if (str != 0) target << XMLStrL(" encoding=\"") << str << XMLStrL("\""); if(document->getStandalone()) target << XMLStrL(" standalone=\"yes\""); target << XMLStrL("?>"); DOMNode* child = toWrite->getFirstChild(); while( child != 0) { target << child; child = child->getNextSibling(); } break; } case DOMNode::ELEMENT_NODE : { // Output the element start tag. target << XMLStrL('<') << nodeName; // Output any attributes on this element DOMNamedNodeMap* attributes = toWrite->getAttributes(); int attrCount = attributes->getLength(); for (int i = 0; i < attrCount; i++) { DOMNode* attribute = attributes->item(i); target << XMLStrL(' ') << attribute->getNodeName() << XMLStrL(" = \""); // Note that "<" must be escaped in attribute values. outputContent(target, attribute->getNodeValue()); target << XMLStrL('"'); } // // Test for the presence of children, which includes both // text content and nested elements. // DOMNode* child = toWrite->getFirstChild(); if (child != 0) { // There are children. Close start-tag, and output children. target << XMLStrL(">"); while( child != 0) { target << child; child = child->getNextSibling(); } // Done with children. Output the end tag. target << XMLStrL("</") << nodeName << XMLStrL(">"); } else { // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // target << XMLStrL("/>"); } break; } case DOMNode::ENTITY_REFERENCE_NODE: { DOMNode* child; for (child = toWrite->getFirstChild(); child != 0; child = child->getNextSibling()) target << child; break; } case DOMNode::CDATA_SECTION_NODE: { target << XMLStrL("<![CDATA[") << nodeValue << XMLStrL("]]>"); break; } case DOMNode::COMMENT_NODE: { target << XMLStrL("<!--") << nodeValue << XMLStrL("-->"); break; } case DOMNode::DOCUMENT_TYPE_NODE: { DOMDocumentType* doctype = (DOMDocumentType*)toWrite;; target << XMLStrL("<!DOCTYPE ") << nodeName ; const XMLCh* id = doctype->getPublicId(); if (id != 0) target << XMLStrL(" PUBLIC \"") << id << XMLStrL("\""); id = doctype->getSystemId(); if (id != 0) target << XMLStrL(" SYSTEM \"") << id << XMLStrL("\""); id = doctype->getInternalSubset(); if (id !=0) target << XMLStrL(" [ ") << id << XMLStrL("]"); target << XMLStrL(">"); break; } case DOMNode::ENTITY_NODE: { DOMEntity* entity = (DOMEntity*)toWrite;; target << XMLStrL("<!ENTITY ") << nodeName; const XMLCh* id = entity->getPublicId(); if (id != 0) target << XMLStrL("PUBLIC \"") << id << XMLStrL("\""); id = entity->getSystemId(); if (id != 0) target << XMLStrL("SYSTEM \"") << id << XMLStrL("\""); id = entity->getNotationName(); if (id != 0) target << XMLStrL("NDATA \"") << id << XMLStrL("\""); break; } default: target << XMLStrL("<!-- Unrecognized node type -->"); } return target; }
// --------------------------------------------------------------------------- // ostream << DOMNode // // Stream out a DOM node, and, recursively, all of its children. This // function is the heart of writing a DOM tree out as XML source. Give it // a document node and it will do the whole thing. // --------------------------------------------------------------------------- ostream& operator<<(ostream& target, DOMNode* toWrite) { // Get the name and value out for convenience const XMLCh* nodeName = toWrite->getNodeName(); const XMLCh* nodeValue = toWrite->getNodeValue(); unsigned long lent = XMLString::stringLen(nodeValue); switch (toWrite->getNodeType()) { case DOMNode::TEXT_NODE: { gFormatter->formatBuf(nodeValue, lent, XMLFormatter::CharEscapes); break; } case DOMNode::PROCESSING_INSTRUCTION_NODE : { *gFormatter << XMLFormatter::NoEscapes << gStartPI << nodeName; if (lent > 0) { *gFormatter << chSpace << nodeValue; } *gFormatter << XMLFormatter::NoEscapes << gEndPI; break; } case DOMNode::DOCUMENT_NODE : { DOMNode *child = toWrite->getFirstChild(); while( child != 0) { target << child; // add linefeed in requested output encoding *gFormatter << chLF; target << flush; child = child->getNextSibling(); } break; } case DOMNode::ELEMENT_NODE : { // The name has to be representable without any escapes *gFormatter << XMLFormatter::NoEscapes << chOpenAngle << nodeName; // Output the element start tag. // Output any attributes on this element DOMNamedNodeMap *attributes = toWrite->getAttributes(); int attrCount = attributes->getLength(); for (int i = 0; i < attrCount; i++) { DOMNode *attribute = attributes->item(i); // // Again the name has to be completely representable. But the // attribute can have refs and requires the attribute style // escaping. // *gFormatter << XMLFormatter::NoEscapes << chSpace << attribute->getNodeName() << chEqual << chDoubleQuote << XMLFormatter::AttrEscapes << attribute->getNodeValue() << XMLFormatter::NoEscapes << chDoubleQuote; } // // Test for the presence of children, which includes both // text content and nested elements. // DOMNode *child = toWrite->getFirstChild(); if (child != 0) { // There are children. Close start-tag, and output children. // No escapes are legal here *gFormatter << XMLFormatter::NoEscapes << chCloseAngle; while( child != 0) { target << child; child = child->getNextSibling(); } // // Done with children. Output the end tag. // *gFormatter << XMLFormatter::NoEscapes << gEndElement << nodeName << chCloseAngle; } else { // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // *gFormatter << XMLFormatter::NoEscapes << chForwardSlash << chCloseAngle; } break; } case DOMNode::ENTITY_REFERENCE_NODE: { //DOMNode *child; #if 0 for (child = toWrite.getFirstChild(); child != 0; child = child.getNextSibling()) { target << child; } #else // // Instead of printing the refernece tree // we'd output the actual text as it appeared in the xml file. // This would be the case when -e option was chosen // *gFormatter << XMLFormatter::NoEscapes << chAmpersand << nodeName << chSemiColon; #endif break; } case DOMNode::CDATA_SECTION_NODE: { *gFormatter << XMLFormatter::NoEscapes << gStartCDATA << nodeValue << gEndCDATA; break; } case DOMNode::COMMENT_NODE: { *gFormatter << XMLFormatter::NoEscapes << gStartComment << nodeValue << gEndComment; break; } case DOMNode::DOCUMENT_TYPE_NODE: { DOMDocumentType *doctype = (DOMDocumentType *)toWrite;; *gFormatter << XMLFormatter::NoEscapes << gStartDoctype << nodeName; const XMLCh* id = doctype->getPublicId(); if (id != 0) { *gFormatter << XMLFormatter::NoEscapes << chSpace << gPublic << id << chDoubleQuote; id = doctype->getSystemId(); if (id != 0) { *gFormatter << XMLFormatter::NoEscapes << chSpace << chDoubleQuote << id << chDoubleQuote; } } else { id = doctype->getSystemId(); if (id != 0) { *gFormatter << XMLFormatter::NoEscapes << chSpace << gSystem << id << chDoubleQuote; } } id = doctype->getInternalSubset(); if (id !=0) *gFormatter << XMLFormatter::NoEscapes << chOpenSquare << id << chCloseSquare; *gFormatter << XMLFormatter::NoEscapes << chCloseAngle; break; } case DOMNode::ENTITY_NODE: { *gFormatter << XMLFormatter::NoEscapes << gStartEntity << nodeName; const XMLCh * id = ((DOMEntity *)toWrite)->getPublicId(); if (id != 0) *gFormatter << XMLFormatter::NoEscapes << gPublic << id << chDoubleQuote; id = ((DOMEntity *)toWrite)->getSystemId(); if (id != 0) *gFormatter << XMLFormatter::NoEscapes << gSystem << id << chDoubleQuote; id = ((DOMEntity *)toWrite)->getNotationName(); if (id != 0) *gFormatter << XMLFormatter::NoEscapes << gNotation << id << chDoubleQuote; *gFormatter << XMLFormatter::NoEscapes << chCloseAngle << chLF; break; } /* case DOMNode::NOTATION_NODE: { const XMLCh * str; *gFormatter << gXMLDecl1 << ((DOMXMLDecl *)toWrite)->getVersion(); *gFormatter << gXMLDecl2 << gEncodingName; str = ((DOMXMLDecl *)toWrite)->getStandalone(); if (str != 0) *gFormatter << gXMLDecl3 << str; *gFormatter << gXMLDecl4; break; } */ default: cerr << "Unrecognized node type = " << (long)toWrite->getNodeType() << endl; } return target; }
void DomUtilsInternal::Dom2XMLString( const DOMNode* toWrite, XMLFormatter& fmt ) { int i; DOMNode child; int attrCount; DOMString id; DOMNamedNodeMap attributes; DOMDocumentType doctype; // Get the name and value out for convenience DOMString nodeName = toWrite.getNodeName(); DOMString nodeValue = toWrite.getNodeValue(); unsigned long lent = nodeValue.length(); switch (toWrite.getNodeType()) { case DOMNode::ELEMENT_NODE: // The name has to be representable without any escapes fmt << XMLFormatter::NoEscapes << chOpenAngle << nodeName; // Output the element start tag. // Output any attributes on this element attributes = ((DOMElement&)toWrite).getAttributes(); attrCount = attributes.getLength(); for (i = 0; i < attrCount; i++) { DOMNode attribute = attributes.item(i); // // Again the name has to be completely representable. But the // attribute can have refs and requires the attribute style // escaping. // fmt << XMLFormatter::NoEscapes << chSpace << attribute.getNodeName() << chEqual << chDoubleQuote << XMLFormatter::AttrEscapes << attribute.getNodeValue() << XMLFormatter::NoEscapes << chDoubleQuote; } // // Test for the presence of children, which includes both // text content and nested elements. // child = toWrite.getFirstChild(); if ( !child.isNull() ) { // There are children. Close start-tag, and output children. // No escapes are legal here fmt << XMLFormatter::NoEscapes << chCloseAngle; while( !child.isNull() ) { DomUtilsInternal::Dom2XMLString( child, fmt ); child = child.getNextSibling(); } // // Done with children. Output the end tag. // fmt << XMLFormatter::NoEscapes << gEndElement << nodeName << chCloseAngle; } else { // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // fmt << XMLFormatter::NoEscapes << chForwardSlash << chCloseAngle; } break; case DOMNode::TEXT_NODE: fmt.formatBuf( nodeValue.rawBuffer(), lent, XMLFormatter::CharEscapes ); break; case DOMNode::CDATA_SECTION_NODE : fmt << XMLFormatter::NoEscapes << gStartCDATA << nodeValue << gEndCDATA; break; case DOMNode::ENTITY_REFERENCE_NODE: fmt << XMLFormatter::NoEscapes << chAmpersand << nodeName << chSemiColon; break; case DOMNode::PROCESSING_INSTRUCTION_NODE : fmt << XMLFormatter::NoEscapes << gStartPI << nodeName; if (lent > 0) { fmt << chSpace << nodeValue; } fmt << XMLFormatter::NoEscapes << gEndPI; break; case DOMNode::COMMENT_NODE : fmt << XMLFormatter::NoEscapes << gStartComment << nodeValue << gEndComment; break; case DOMNode::DOCUMENT_NODE : child = toWrite.getFirstChild(); while( !child.isNull() ) { DomUtilsInternal::Dom2XMLString( child, fmt ); child = child.getNextSibling(); if ( !child.isNull() ) { fmt << chCR; } } break; case DOMNode::DOCUMENT_TYPE_NODE : doctype = (DOMDocumentType &)toWrite; fmt << XMLFormatter::NoEscapes << gStartDoctype << nodeName; id = doctype.getPublicId(); if (id != 0) { fmt << XMLFormatter::NoEscapes << chSpace << gPublic << id << chDoubleQuote; id = doctype.getSystemId(); if (id != 0) { fmt << XMLFormatter::NoEscapes << chSpace << chDoubleQuote << id << chDoubleQuote; } } else { id = doctype.getSystemId(); if (id != 0) { fmt << XMLFormatter::NoEscapes << chSpace << gSystem << id << chDoubleQuote; } } id = doctype.getInternalSubset(); if (id !=0) fmt << XMLFormatter::NoEscapes << chOpenSquare << id << chCloseSquare; fmt << XMLFormatter::NoEscapes << chCloseAngle; break; case DOMNode::ENTITY_NODE: fmt << XMLFormatter::NoEscapes << gStartEntity << nodeName; id = ((DOMEntity &)toWrite).getPublicId(); if (id != 0) fmt << XMLFormatter::NoEscapes << gPublic << id << chDoubleQuote; id = ((DOMEntity &)toWrite).getSystemId(); if (id != 0) fmt << XMLFormatter::NoEscapes << gSystem << id << chDoubleQuote; id = ((DOMEntity &)toWrite).getNotationName(); if (id != 0) fmt << XMLFormatter::NoEscapes << gNotation << id << chDoubleQuote; fmt << XMLFormatter::NoEscapes << chCloseAngle << chCR << chLF; break; case DOMNode::XML_DECL_NODE: fmt << gXMLDecl1 << ((DOMXMLDecl &)toWrite).getVersion(); fmt << gXMLDecl2 << fmt.getEncodingName(); id = ((DOMXMLDecl &)toWrite).getStandalone(); if ( id!= 0 ) fmt << gXMLDecl3 << id; fmt << gXMLDecl4; break; } }