From c8954c4805d6fab88944447597055ea4496a0fdf Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Fri, 20 Feb 2009 18:34:21 +0000 Subject: [PATCH] Generalize "QName in content" helper function. --- xmltooling/util/XMLHelper.cpp | 17 +++++--- xmltooling/util/XMLHelper.h | 95 +++++++++++++++++++++++-------------------- 2 files changed, 63 insertions(+), 49 deletions(-) diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp index 77f8d0f..6128bff 100644 --- a/xmltooling/util/XMLHelper.cpp +++ b/xmltooling/util/XMLHelper.cpp @@ -132,21 +132,26 @@ xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode) xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute) { - if (!attribute) + return getNodeValueAsQName(attribute); +} + +xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode) +{ + if (!domNode) return NULL; int i; - const XMLCh* attributeValue=attribute->getTextContent(); - if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) { + const XMLCh* value=domNode->getTextContent(); + if (value && (i=XMLString::indexOf(value,chColon))>0) { XMLCh* prefix=new XMLCh[i+1]; - XMLString::subString(prefix,attributeValue,0,i); + XMLString::subString(prefix,value,0,i); prefix[i]=chNull; - xmltooling::QName* ret=new xmltooling::QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix); + xmltooling::QName* ret=new xmltooling::QName(domNode->lookupNamespaceURI(prefix), value + i + 1, prefix); delete[] prefix; return ret; } - return new xmltooling::QName(attribute->lookupNamespaceURI(NULL), attributeValue); + return new xmltooling::QName(domNode->lookupNamespaceURI(NULL), value); } DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement) diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h index 28757a8..08a4e80 100644 --- a/xmltooling/util/XMLHelper.h +++ b/xmltooling/util/XMLHelper.h @@ -1,6 +1,6 @@ /* * Copyright 2001-2007 Internet2 - * + * * 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 @@ -16,8 +16,8 @@ /** * @file xmltooling/util/XMLHelper.h - * - * A helper class for working with W3C DOM objects. + * + * A helper class for working with W3C DOM objects. */ #ifndef __xmltooling_xmlhelper_h__ @@ -29,7 +29,7 @@ #include namespace xmltooling { - + /** * RAII wrapper for Xerces resources. */ @@ -40,19 +40,19 @@ namespace xmltooling { public: /** * Constructor - * + * * @param resource object to release when leaving scope */ XercesJanitor(T* resource) : m_held(resource) {} - + ~XercesJanitor() { if (m_held) m_held->release(); } - + /** * Returns resource held by this object. - * + * * @return the resource held or NULL */ T* get() { @@ -61,7 +61,7 @@ namespace xmltooling { /** * Returns resource held by this object. - * + * * @return the resource held or NULL */ T* operator->() { @@ -70,7 +70,7 @@ namespace xmltooling { /** * Returns resource held by this object and releases it to the caller. - * + * * @return the resource held or NULL */ T* release() { @@ -79,16 +79,16 @@ namespace xmltooling { return ret; } }; - + /** - * A helper class for working with W3C DOM objects. + * A helper class for working with W3C DOM objects. */ class XMLTOOL_API XMLHelper { public: /** * Checks if the given element has an xsi:type defined for it - * + * * @param e the DOM element * @return true if there is a type, false if not */ @@ -96,7 +96,7 @@ namespace xmltooling { /** * Gets the XSI type for a given element if it has one. - * + * * @param e the element * @return the type or null */ @@ -104,7 +104,7 @@ namespace xmltooling { /** * Gets the ID attribute of a DOM element. - * + * * @param domElement the DOM element * @return the ID attribute or null if there isn't one */ @@ -113,17 +113,17 @@ namespace xmltooling { /** * Attempts to locate an XMLObject from this point downward in the tree whose * XML ID matches the supplied value. - * + * * @param tree root of tree to search * @param id ID value to locate * @return XMLObject in the tree with a matching ID value, or NULL */ static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id); - + /** * Attempts to locate an XMLObject from this point downward in the tree whose * XML ID matches the supplied value. - * + * * @param tree root of tree to search * @param id ID value to locate * @return XMLObject in the tree with a matching ID value, or NULL @@ -132,33 +132,42 @@ namespace xmltooling { /** * Gets the QName for the given DOM node. - * + * * @param domNode the DOM node * @return the QName for the element or null if the element was null */ static QName* getNodeQName(const xercesc::DOMNode* domNode); /** + * @deprecated * Constructs a QName from an attribute's value. - * + * * @param attribute the attribute with a QName value * @return a QName from an attribute's value, or null if the given attribute is null */ static QName* getAttributeValueAsQName(const xercesc::DOMAttr* attribute); /** + * Constructs a QName from a node's value. + * + * @param domNode the DOM node with a QName value + * @return a QName from a node's value, or null if the given node has no value + */ + static QName* getNodeValueAsQName(const xercesc::DOMNode* domNode); + + /** * Appends the child Element to the parent Element, * importing the child Element into the parent's Document if needed. - * + * * @param parentElement the parent Element * @param childElement the child Element * @return the child Element that was added (may be an imported copy) */ static xercesc::DOMElement* appendChildElement(xercesc::DOMElement* parentElement, xercesc::DOMElement* childElement); - + /** * Checks the qualified name of a node. - * + * * @param n node to check * @param ns namespace to compare with * @param local local name to compare with @@ -170,73 +179,73 @@ namespace xmltooling { /** * Returns the first matching child element of the node if any. - * + * * @param n node to check * @param localName local name to compare with or NULL for any match * @return the first matching child node of type Element, or NULL */ static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL); - + /** * Returns the last matching child element of the node if any. - * + * * @param n node to check * @param localName local name to compare with or NULL for any match * @return the last matching child node of type Element, or NULL */ static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL); - + /** * Returns the next matching sibling element of the node if any. - * + * * @param n node to check * @param localName local name to compare with or NULL for any match * @return the next matching sibling node of type Element, or NULL */ static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL); - + /** * Returns the previous matching sibling element of the node if any. - * + * * @param n node to check * @param localName local name to compare with or NULL for any match * @return the previous matching sibling node of type Element, or NULL */ static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL); - + /** * Returns the first matching child element of the node if any. - * + * * @param n node to check * @param ns namespace to compare with * @param localName local name to compare with * @return the first matching child node of type Element, or NULL */ static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName); - + /** * Returns the last matching child element of the node if any. - * + * * @param n node to check * @param ns namespace to compare with * @param localName local name to compare with * @return the last matching child node of type Element, or NULL */ static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName); - + /** * Returns the next matching sibling element of the node if any. - * + * * @param n node to check * @param ns namespace to compare with * @param localName local name to compare with * @return the next matching sibling node of type Element, or NULL */ static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName); - + /** * Returns the previous matching sibling element of the node if any. - * + * * @param n node to check * @param ns namespace to compare with * @param localName local name to compare with @@ -257,7 +266,7 @@ namespace xmltooling { /** * Serializes the DOM node provided into a buffer using UTF-8 encoding and * the default XML serializer available. No manipulation or formatting is applied. - * + * * @param n node to serialize * @param buf buffer to serialize element into * @param pretty enable pretty printing if supported @@ -267,7 +276,7 @@ namespace xmltooling { /** * Serializes the DOM node provided to a stream using UTF-8 encoding and * the default XML serializer available. No manipulation or formatting is applied. - * + * * @param n node to serialize * @param out stream to serialize element into * @param pretty enable pretty printing if supported @@ -279,7 +288,7 @@ namespace xmltooling { /** * Serializes the DOM node provided to a stream using UTF-8 encoding and * the default XML serializer available. No manipulation or formatting is applied. - * + * * @param n node to serialize * @param ostr stream to serialize element into * @return reference to output stream @@ -289,9 +298,9 @@ namespace xmltooling { /** * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and * the default XML serializer available. No manipulation or formatting is applied. - * + * *

The marshaller operation takes no parameters. - * + * * @param obj object to serialize * @param ostr stream to serialize object into * @return reference to output stream -- 2.1.4