XUtil.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:16k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 2001, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Log: XUtil.cpp,v $
  58.  * Revision 1.6  2001/12/05 20:12:30  knoaman
  59.  * Use getLocalName instead of getNodeName.
  60.  *
  61.  * Revision 1.5  2001/11/02 14:13:45  knoaman
  62.  * Add support for identity constraints.
  63.  *
  64.  * Revision 1.4  2001/05/11 13:27:39  tng
  65.  * Copyright update.
  66.  *
  67.  * Revision 1.3  2001/05/03 21:02:40  tng
  68.  * Schema: Add SubstitutionGroupComparator and update exception messages.  By Pei Yong Zhang.
  69.  *
  70.  * Revision 1.2  2001/04/04 18:02:04  tng
  71.  * Schema: include failure on Unix for XUtil.cpp.  Fixed by Pei Yong Zhang.
  72.  *
  73.  * Revision 1.1  2001/03/30 16:06:00  tng
  74.  * Schema: XUtil, added by Pei Yong Zhang
  75.  *
  76.  */
  77. // ---------------------------------------------------------------------------
  78. //  Includes
  79. // ---------------------------------------------------------------------------
  80. #include <validators/schema/XUtil.hpp>
  81. #include <util/XMLString.hpp>
  82. #include <framework/XMLBuffer.hpp>
  83. #include <dom/AttrImpl.hpp>
  84. #include <dom/ElementImpl.hpp>
  85. #include <dom/DocumentImpl.hpp>
  86. #include <util/IllegalArgumentException.hpp>
  87. void XUtil::copyInto(const DOM_Node &src, DOM_Node &dest)
  88. {
  89.     // get node factory
  90.     DOM_Document factory = dest.getOwnerDocument();
  91.     // placement variables
  92.     DOM_Node start  = src;
  93.     DOM_Node parent = src;
  94.     DOM_Node place  = src;
  95.     // traverse source tree
  96.     while (place != 0)
  97.     {
  98.         // copy this node
  99.         DOM_Node node;
  100.         short  type = place.getNodeType();
  101.         switch (type)
  102. {
  103. case DOM_Node::CDATA_SECTION_NODE:
  104. {
  105.                 node = factory.createCDATASection(place.getNodeValue());
  106.                 break;
  107. }
  108. case DOM_Node::COMMENT_NODE:
  109. {
  110. node = factory.createComment(place.getNodeValue());
  111. break;
  112. }
  113. case DOM_Node::ELEMENT_NODE:
  114. {
  115. DOM_Element element = factory.createElement(place.getNodeName());
  116. node = element;
  117.     ElementImpl *elemImpl = (ElementImpl*)element.fImpl;
  118. DOM_NamedNodeMap attrs  = place.getAttributes();
  119.                 unsigned int attrCount = attrs.getLength();
  120. for (unsigned int i = 0; i < attrCount; i++)
  121. {
  122.                     if (attrs.item(i).getNodeType() == DOM_Node::ATTRIBUTE_NODE)
  123.                     {
  124.          DOM_Node tmpNode = attrs.item(i);
  125.     DOM_Attr attr = (DOM_Attr&)tmpNode;
  126.     AttrImpl *attrImpl = elemImpl->setAttribute(attr.getNodeName(), attr.getNodeValue());
  127.                         if ((factory.getNodeType() == DOM_Node::DOCUMENT_NODE) && !attr.getSpecified())
  128.                             attrImpl->setSpecified(false);
  129.                     }
  130. }//for
  131. break;
  132. }
  133. case DOM_Node::ENTITY_REFERENCE_NODE:
  134. {
  135. node = factory.createEntityReference(place.getNodeName());
  136. break;
  137. }
  138. case DOM_Node::PROCESSING_INSTRUCTION_NODE:
  139. {
  140. node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
  141. break;
  142. }
  143. case DOM_Node::TEXT_NODE:
  144. {
  145. node = factory.createTextNode(place.getNodeValue());
  146. break;
  147. }
  148. default:
  149. {
  150.                 ThrowXML1(IllegalArgumentException
  151.         , XMLExcepts::XUTIL_UnCopyableNodeType
  152.                         , node.getNodeName().rawBuffer());
  153.             }
  154. }//switch
  155. dest.appendChild(node);
  156.         // iterate over children
  157.         if (place.hasChildNodes())
  158. {
  159. parent = place;
  160. place  = place.getFirstChild();
  161. dest   = node;
  162. }
  163.             // advance
  164.         else
  165. {
  166. place = place.getNextSibling();
  167. while (place == null && parent != start)
  168. {
  169. place  = parent.getNextSibling();
  170. parent = parent.getParentNode();
  171. dest   = dest.getParentNode();
  172. }
  173. }
  174. }//while
  175. }
  176. /*
  177.      * Returns the concatenated child text of the specified node.
  178.      * This method only looks at the immediate children of type
  179.      * <code>Node.TEXT_NODE</code> or the children of any child
  180.      * node that is of type <code>Node.CDATA_SECTION_NODE</code>
  181.      * for the concatenation.
  182.      *
  183.      * @param node The node to look at.
  184. */
  185. DOMString XUtil::getChildText(const DOM_Node &node)
  186. {
  187.     // is there anything to do?
  188.     if (node == 0)
  189.         return 0;
  190.     // concatenate children text
  191.     DOMString bufToFill;
  192.     DOM_Node child = node.getFirstChild();
  193.     while (child != 0)
  194. {
  195. short type = child.getNodeType();
  196.         if (type == DOM_Node::TEXT_NODE)
  197.             bufToFill.appendData(child.getNodeValue());
  198.         else if (type == DOM_Node::CDATA_SECTION_NODE)
  199.             bufToFill.appendData(getChildText(child));
  200.         child = child.getNextSibling();
  201.     }
  202.     // return text value
  203.     return bufToFill;
  204. }
  205. // Finds and returns the first child element node.
  206. DOM_Element XUtil::getFirstChildElement(const DOM_Node &parent)
  207. {
  208.     // search for node
  209.     DOM_Node child = parent.getFirstChild();
  210.     while (child != 0)
  211. {
  212.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  213.             return (DOM_Element&)child;
  214.         child = child.getNextSibling();
  215.     }
  216.     // not found
  217.     return DOM_Element();
  218. }
  219. // Finds and returns the first child node with the given name.
  220. DOM_Element XUtil::getFirstChildElement(const DOM_Node    &parent
  221.                                       , const XMLCh* const elemName)
  222. {
  223.     // search for node
  224.     DOM_Node child = parent.getFirstChild();
  225.     while (child != 0)
  226. {
  227.         if ((child.getNodeType() == DOM_Node::ELEMENT_NODE) &&
  228.             (XMLString::compareString(child.getNodeName().rawBuffer(), elemName) ==0))
  229.             return (DOM_Element&)child;
  230.         child = child.getNextSibling();
  231.     }
  232.     // not found
  233.     return DOM_Element();
  234. }
  235. // Finds and returns the first child node with the given name.
  236. DOM_Element XUtil::getFirstChildElement(const DOM_Node     &parent
  237.                                       , const XMLCh** const elemNames
  238.   , unsigned int        length)
  239. {
  240.     // search for node
  241.     DOM_Node child = parent.getFirstChild();
  242.     while (child != 0)
  243. {
  244.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  245. {
  246.             for (unsigned int i = 0; i < length; i++)
  247. {
  248.                 if (XMLString::compareString(child.getNodeName().rawBuffer(), elemNames[i]) ==0)
  249.                     return (DOM_Element&)child;
  250. }
  251. }
  252.         child = child.getNextSibling();
  253.     }
  254.     // not found
  255.     return DOM_Element();
  256. }
  257. // Finds and returns the first child node with the given name and
  258. // attribute name, value pair.
  259. DOM_Element XUtil::getFirstChildElement(const DOM_Node    &parent
  260.                                       , const XMLCh* const elemName
  261.                                       , const XMLCh* const attrName
  262.                                       , const XMLCh* const attrValue)
  263. {
  264.     // search for node
  265.     DOM_Node child = parent.getFirstChild();
  266.     while (child != 0)
  267. {
  268.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  269. {
  270. DOM_Element element = (DOM_Element&)child;
  271.             if ((XMLString::compareString(element.getNodeName().rawBuffer(), elemName) ==0) &&
  272.                 (XMLString::compareString(element.getAttribute(attrName).rawBuffer(), attrValue) ==0))
  273. return element;
  274.         }
  275.         child = child.getNextSibling();
  276.     }
  277.     // not found
  278.     return DOM_Element();
  279. }
  280. // Finds and returns the first child node with the given qualified name.
  281. DOM_Element XUtil::getFirstChildElementNS(const DOM_Node     &parent
  282.                                         , const XMLCh** const elemNames
  283.                                         , const XMLCh* const uriStr
  284.     , unsigned int        length)
  285. {
  286.     // search for node
  287.     DOM_Node child = parent.getFirstChild();
  288.     while (child != 0)
  289. {
  290.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  291. {
  292.             for (unsigned int i = 0; i < length; i++)
  293. {
  294.                 if (child.getNamespaceURI().equals(uriStr) &&
  295.                     XMLString::compareString(child.getLocalName().rawBuffer(), elemNames[i]) ==0)
  296.                     return (DOM_Element&)child;
  297. }
  298. }
  299.         child = child.getNextSibling();
  300.     }
  301.     // not found
  302.     return DOM_Element();
  303. }
  304. // Finds and returns the last child element node.
  305. DOM_Element XUtil::getLastChildElement(const DOM_Node &parent) {
  306.     // search for node
  307.     DOM_Node child = parent.getLastChild();
  308.     while (child != 0)
  309. {
  310.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  311.             return (DOM_Element&)child;
  312.         child = child.getPreviousSibling();
  313.     }
  314.     // not found
  315.     return DOM_Element();
  316. } // getLastChildElement(Node):Element
  317. // Finds and returns the last child node with the given name.
  318. DOM_Element XUtil::getLastChildElement(const DOM_Node    &parent
  319.                                      , const XMLCh* const elemName)
  320. {
  321.     // search for node
  322.     DOM_Node child = parent.getLastChild();
  323.     while (child != 0)
  324. {
  325.         if ((child.getNodeType() == DOM_Node::ELEMENT_NODE) &&
  326.             (XMLString::compareString(child.getNodeName().rawBuffer(), elemName) ==0))
  327.                 return (DOM_Element&)child;
  328.         child = child.getPreviousSibling();
  329.     }
  330.     // not found
  331.     return DOM_Element();
  332. }
  333. // Finds and returns the last child node with the given name.
  334. DOM_Element XUtil::getLastChildElement(const DOM_Node     &parent
  335.                                      , const XMLCh** const elemNames
  336.  , unsigned int        length)
  337. {
  338.     // search for node
  339.     DOM_Node child = parent.getLastChild();
  340.     while (child != 0)
  341. {
  342.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  343. {
  344.             for (unsigned int i = 0; i < length; i++)
  345. {
  346.                 if (XMLString::compareString(child.getNodeName().rawBuffer(), elemNames[i]) ==0)
  347.                     return (DOM_Element&)child;
  348. }
  349. }
  350.         child = child.getPreviousSibling();
  351.     }
  352.     // not found
  353.     return DOM_Element();
  354. }
  355. // Finds and returns the last child node with the given name and
  356. // attribute name, value pair.
  357. DOM_Element XUtil::getLastChildElement(const DOM_Node    &parent
  358.                                      , const XMLCh* const elemName
  359.                                      , const XMLCh* const attrName
  360.                                      , const XMLCh* const attrValue)
  361. {
  362.     // search for node
  363.     DOM_Node child = parent.getLastChild();
  364.     while (child != 0)
  365. {
  366.         if (child.getNodeType() == DOM_Node::ELEMENT_NODE)
  367. {
  368. DOM_Element element = (DOM_Element&)child;
  369.             if ((XMLString::compareString(element.getNodeName().rawBuffer(), elemName) ==0) &&
  370.                 (XMLString::compareString(element.getAttribute(attrName).rawBuffer(), attrValue) ==0))
  371. return element;
  372.         }
  373.         child = child.getPreviousSibling();
  374.     }
  375.     // not found
  376.     return DOM_Element();
  377. }
  378. // Finds and returns the next sibling element node.
  379. DOM_Element XUtil::getNextSiblingElement(const DOM_Node &node)
  380. {
  381.     // search for node
  382.     DOM_Node sibling = node.getNextSibling();
  383.     while (sibling != 0)
  384. {
  385.         if (sibling.getNodeType() == DOM_Node::ELEMENT_NODE)
  386.             return (DOM_Element&)sibling;
  387.         sibling = sibling.getNextSibling();
  388.     }
  389.     // not found
  390.     return DOM_Element();
  391. }
  392. // Finds and returns the next sibling element node with the give name.
  393. DOM_Element XUtil::getNextSiblingElement(const DOM_Node    &node
  394.                                        , const XMLCh* const elemName)
  395. {
  396.     // search for node
  397.     DOM_Node sibling = node.getNextSibling();
  398.     while (sibling != 0)
  399. {
  400.         if ((sibling.getNodeType() == DOM_Node::ELEMENT_NODE) &&
  401.             (XMLString::compareString(sibling.getNodeName().rawBuffer(), elemName) ==0))
  402.             return (DOM_Element&)sibling;
  403.         sibling = sibling.getNextSibling();
  404.     }
  405.     // not found
  406.     return DOM_Element();
  407. }
  408. // Finds and returns the next sibling element node with the give name.
  409. DOM_Element XUtil::getNextSiblingElement(const DOM_Node     &node
  410.                                        , const XMLCh** const elemNames
  411.    , unsigned int        length)
  412. {
  413.     // search for node
  414.     DOM_Node sibling = node.getNextSibling();
  415.     while (sibling != 0)
  416. {
  417.         if (sibling.getNodeType() == DOM_Node::ELEMENT_NODE)
  418. {
  419.             for (unsigned int i = 0; i < length; i++)
  420. {
  421.                 if (XMLString::compareString(sibling.getNodeName().rawBuffer(), elemNames[i]) ==0)
  422.                     return (DOM_Element&)sibling;
  423. }
  424. }
  425.         sibling = sibling.getNextSibling();
  426.     }
  427.     // not found
  428.     return DOM_Element();
  429. }
  430. // Finds and returns the next sibling element node with the given name and
  431. // attribute name, value pair.
  432. DOM_Element XUtil::getNextSiblingElement(const DOM_Node    &node
  433.                                        , const XMLCh* const elemName
  434.                                        , const XMLCh* const attrName
  435.                                        , const XMLCh* const attrValue)
  436. {
  437.     // search for node
  438.     DOM_Node sibling = node.getNextSibling();
  439.     while (sibling != 0)
  440. {
  441.         if (sibling.getNodeType() == DOM_Node::ELEMENT_NODE)
  442. {
  443. DOM_Element element = (DOM_Element&)sibling;
  444.             if ((XMLString::compareString(element.getNodeName().rawBuffer(), elemName) ==0) &&
  445.                 (XMLString::compareString(element.getAttribute(attrName).rawBuffer(), attrValue) ==0))
  446. return element;
  447.         }
  448.         sibling = sibling.getNextSibling();
  449.     }
  450.     // not found
  451.     return DOM_Element();
  452. }
  453. // Finds and returns the next sibling element node with the qualified name.
  454. DOM_Element XUtil::getNextSiblingElementNS(const DOM_Node     &node
  455.                                          , const XMLCh** const elemNames
  456.                                          , const XMLCh* const uriStr
  457.      , unsigned int        length)
  458. {
  459.     // search for node
  460.     DOM_Node sibling = node.getNextSibling();
  461.     while (sibling != 0)
  462. {
  463.         if (sibling.getNodeType() == DOM_Node::ELEMENT_NODE)
  464. {
  465.             for (unsigned int i = 0; i < length; i++)
  466. {
  467.                 if (sibling.getNamespaceURI().equals(uriStr) &&
  468.                     XMLString::compareString(sibling.getLocalName().rawBuffer(), elemNames[i]) ==0)
  469.                     return (DOM_Element&)sibling;
  470. }
  471. }
  472.         sibling = sibling.getNextSibling();
  473.     }
  474.     // not found
  475.     return DOM_Element();
  476. }