From 55740e737eb4875d9a0aa4c8d5ef0a4f9a1aa219 Mon Sep 17 00:00:00 2001 From: cantor Date: Fri, 17 Feb 2006 07:48:08 +0000 Subject: [PATCH] Low level helpers git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@3 de75baf8-a10c-0410-a50a-987c0e22f00f --- xmltooling/util/NDC.cpp | 8 +-- xmltooling/util/NDC.h | 4 +- xmltooling/util/XMLConstants.cpp | 82 +++++++++++++++++++++++++++ xmltooling/util/XMLConstants.h | 71 ++++++++++++++++++++++++ xmltooling/util/XMLHelper.cpp | 116 +++++++++++++++++++++++++++++++++++++++ xmltooling/util/XMLHelper.h | 92 +++++++++++++++++++++++++++++++ 6 files changed, 365 insertions(+), 8 deletions(-) create mode 100644 xmltooling/util/XMLConstants.cpp create mode 100644 xmltooling/util/XMLConstants.h create mode 100644 xmltooling/util/XMLHelper.cpp create mode 100644 xmltooling/util/XMLHelper.h diff --git a/xmltooling/util/NDC.cpp b/xmltooling/util/NDC.cpp index 1886721..da465a0 100644 --- a/xmltooling/util/NDC.cpp +++ b/xmltooling/util/NDC.cpp @@ -14,11 +14,10 @@ * limitations under the License. */ -/* NDC.cpp +/** + * @file NDC.cpp * * Diagnostic context for logging - * - * $Id:$ */ #include "internal.h" @@ -26,7 +25,6 @@ #include -using namespace std; using namespace xmltooling; NDC::NDC(const char* context) @@ -34,7 +32,7 @@ NDC::NDC(const char* context) log4cpp::NDC::push(context); } -NDC::NDC(const string& context) +NDC::NDC(const std::string& context) { log4cpp::NDC::push(context); } diff --git a/xmltooling/util/NDC.h b/xmltooling/util/NDC.h index 57ef9d4..9d5714c 100644 --- a/xmltooling/util/NDC.h +++ b/xmltooling/util/NDC.h @@ -51,9 +51,7 @@ namespace xmltooling { */ ~NDC(); - private: - NDC(const NDC&); - NDC& operator=(const NDC&); + MAKE_NONCOPYABLE(NDC); }; }; diff --git a/xmltooling/util/XMLConstants.cpp b/xmltooling/util/XMLConstants.cpp new file mode 100644 index 0000000..f38966b --- /dev/null +++ b/xmltooling/util/XMLConstants.cpp @@ -0,0 +1,82 @@ +/* + * Copyright 2001-2006 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file XMLConstants.cpp + * + * Fundamental XML namespace constants + */ + + +#include "internal.h" +#include "util/XMLConstants.h" +#include + +using namespace xercesc; +using namespace xmltooling; + +const XMLCh XMLConstants::XML_NS[] = // http://www.w3.org/XML/1998/namespace +{ chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, + chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash, + chLatin_X, chLatin_M, chLatin_L, chForwardSlash, chDigit_1, chDigit_9, chDigit_9, chDigit_8, chForwardSlash, + chLatin_n, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chLatin_p, chLatin_a, chLatin_c, chLatin_e, chNull +}; + +const XMLCh XMLConstants::XMLNS_NS[] = // http://www.w3.org/2000/xmlns/ +{ chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, + chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash, + chDigit_2, chDigit_0, chDigit_0, chDigit_0, chForwardSlash, + chLatin_x, chLatin_m, chLatin_l, chLatin_n, chLatin_s, chForwardSlash, chNull +}; + +const XMLCh XMLConstants::XMLNS_PREFIX[] = { chLatin_x, chLatin_m, chLatin_l, chLatin_n, chLatin_s, chNull }; + +const XMLCh XMLConstants::XSD_NS[] = // http://www.w3.org/2001/XMLSchema +{ chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, + chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash, + chDigit_2, chDigit_0, chDigit_0, chDigit_1, chForwardSlash, + chLatin_X, chLatin_M, chLatin_L, chLatin_S, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a, chNull +}; + +const XMLCh XMLConstants::XSD_PREFIX[] = { chLatin_x, chLatin_s, chNull }; + +const XMLCh XMLConstants::XSI_NS[] = // http://www.w3.org/2001/XMLSchema-instance +{ chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, + chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash, + chDigit_2, chDigit_0, chDigit_0, chDigit_1, chForwardSlash, + chLatin_X, chLatin_M, chLatin_L, chLatin_S, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a, chDash, + chLatin_i, chLatin_n, chLatin_s, chLatin_t, chLatin_a, chLatin_n, chLatin_c, chLatin_e, chNull +}; + +const XMLCh XMLConstants::XSI_PREFIX[] = { chLatin_x, chLatin_s, chLatin_i, chNull }; + +const XMLCh XMLConstants::XMLSIG_NS[] = // http://www.w3.org/2000/09/xmldsig# +{ chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, + chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash, + chDigit_2, chDigit_0, chDigit_0, chDigit_0, chForwardSlash, chDigit_0, chDigit_9, chForwardSlash, + chLatin_x, chLatin_m, chLatin_l, chLatin_d, chLatin_s, chLatin_i, chLatin_g, chPound, chNull +}; + +const XMLCh XMLConstants::XMLSIG_PREFIX[] = { chLatin_d, chLatin_s, chNull }; + +const XMLCh XMLConstants::XMLENC_NS[] = // http://www.w3.org/2001/04/xmlenc# +{ chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, + chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash, + chDigit_2, chDigit_0, chDigit_0, chDigit_1, chForwardSlash, chDigit_0, chDigit_4, chForwardSlash, + chLatin_x, chLatin_m, chLatin_l, chLatin_e, chLatin_n, chLatin_c, chPound, chNull +}; + +const XMLCh XMLConstants::XMLENC_PREFIX[] = { chLatin_x, chLatin_e, chLatin_n, chLatin_c, chNull }; diff --git a/xmltooling/util/XMLConstants.h b/xmltooling/util/XMLConstants.h new file mode 100644 index 0000000..78b4318 --- /dev/null +++ b/xmltooling/util/XMLConstants.h @@ -0,0 +1,71 @@ +/* + * Copyright 2001-2006 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file XMLConstants.h + * + * Fundamental XML namespace constants + */ + +#if !defined(__xmltooling_xmlconstants_h__) +#define __xmltooling_xmlconstants_h__ + +#include + +namespace xmltooling { + + /** + * XML related constants. + */ + struct XMLTOOL_API XMLConstants + { + /** XML core namespace ("http://www.w3.org/XML/1998/namespace") */ + static const XMLCh XML_NS[]; + + /** XML namespace for xmlns attributes ("http://www.w3.org/2000/xmlns/") */ + static const XMLCh XMLNS_NS[]; + + /** XML namespace prefix for xmlns attributes ("xmlns") */ + static const XMLCh XMLNS_PREFIX[]; + + /** XML Schema namespace ("http://www.w3.org/2001/XMLSchema") */ + static const XMLCh XSD_NS[]; + + /** XML Schema QName prefix ("xs") */ + static const XMLCh XSD_PREFIX[]; + + /** XML Schema Instance namespace ("http://www.w3.org/2001/XMLSchema-instance") */ + static const XMLCh XSI_NS[]; + + /** XML Schema Instance QName prefix ("xsi") */ + static const XMLCh XSI_PREFIX[]; + + /** XML Signature namespace ("http://www.w3.org/2000/09/xmldsig#") */ + static const XMLCh XMLSIG_NS[]; + + /** XML Signature QName prefix ("ds") */ + static const XMLCh XMLSIG_PREFIX[]; + + /** XML Encryption namespace ("http://www.w3.org/2001/04/xmlenc#") */ + static const XMLCh XMLENC_NS[]; + + /** XML Encryption QName prefix ("xenc") */ + static const XMLCh XMLENC_PREFIX[]; + }; + +}; + +#endif /* __xmltooling_xmlconstants_h__ */ diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp new file mode 100644 index 0000000..7b7c44b --- /dev/null +++ b/xmltooling/util/XMLHelper.cpp @@ -0,0 +1,116 @@ +/* + * Copyright 2001-2006 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file XMLHelper.cpp + * + * A helper class for working with W3C DOM objects. + */ + +#include "internal.h" +#include "util/XMLHelper.h" +#include "util/XMLConstants.h" + +#include + +using namespace xmltooling; + +static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull }; + +bool XMLHelper::hasXSIType(DOMElement* e) +{ + if (e) { + if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) { + return true; + } + } + + return false; +} + +QName* XMLHelper::getXSIType(DOMElement* e) +{ + DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type); + if (attribute) { + int i; + const XMLCh* attributeValue = attribute->getTextContent(); + if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) { + XMLCh* prefix=new XMLCh[i+1]; + XMLString::subString(prefix,attributeValue,0,i); + prefix[i]=chNull; + QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix); + delete[] prefix; + return ret; + } + } + + return NULL; +} + +DOMAttr* XMLHelper::getIdAttribute(DOMElement* domElement) +{ + if(!domElement->hasAttributes()) { + return NULL; + } + + DOMNamedNodeMap* attributes = domElement->getAttributes(); + DOMAttr* attribute; + for(XMLSize_t i = 0; i < attributes->getLength(); i++) { + attribute = static_cast(attributes->item(i)); + if(attribute->isId()) { + return attribute; + } + } + + return NULL; +} + +QName* XMLHelper::getNodeQName(DOMNode* domNode) +{ + if (domNode) + return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix()); + return NULL; +} + +QName* XMLHelper::getAttributeValueAsQName(DOMAttr* attribute) +{ + if (!attribute) + return NULL; + + int i; + const XMLCh* attributeValue=attribute->getTextContent(); + if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) { + XMLCh* prefix=new XMLCh[i+1]; + XMLString::subString(prefix,attributeValue,0,i); + prefix[i]=chNull; + QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix); + delete[] prefix; + return ret; + } + + return new QName(attribute->lookupNamespaceURI(NULL), attributeValue); +} + +DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement) +{ + DOMDocument* parentDocument = parentElement->getOwnerDocument(); + if (childElement->getOwnerDocument() != parentDocument) { + childElement = static_cast(parentDocument->importNode(childElement, true)); + } + + parentElement->appendChild(childElement); + return childElement; +} diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h new file mode 100644 index 0000000..689370a --- /dev/null +++ b/xmltooling/util/XMLHelper.h @@ -0,0 +1,92 @@ +/* + * Copyright 2001-2006 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file XMLHelper.h + * + * A helper class for working with W3C DOM objects. + */ + +#if !defined(__xmltooling_xmlhelper_h__) +#define __xmltooling_xmlhelper_h__ + +#include +#include + +using namespace xercesc; + +namespace xmltooling { + + /** + * 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 + */ + static bool hasXSIType(DOMElement* e); + + /** + * Gets the XSI type for a given element if it has one. + * + * @param e the element + * @return the type or null + */ + static QName* getXSIType(DOMElement* e); + + /** + * Gets the ID attribute of a DOM element. + * + * @param domElement the DOM element + * @return the ID attribute or null if there isn't one + */ + static DOMAttr* getIdAttribute(DOMElement* domElement); + + /** + * 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(DOMNode* domNode); + + /** + * Constructs a QName from an attributes value. + * + * @param attribute the attribute with a QName value + * @return a QName from an attributes value, or null if the given attribute is null + */ + static QName* getAttributeValueAsQName(DOMAttr* attribute); + + /** + * 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 DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement); + }; + +}; + +#endif /* __xmltooling_xmlhelper_h__ */ -- 2.1.4