コード例 #1
0
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;
    }
}