X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2FAbstractXMLObject.cpp;h=3f6726a7c349408feee1518e50cfcec30fc96244;hb=3f34c742109a7773703fbf9beab9d34722586c05;hp=7c73f3f893b56b3743cf982e0634054cf7680c87;hpb=64dcaec957e9befd960779498d7fe35bbb62141a;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/AbstractXMLObject.cpp b/xmltooling/AbstractXMLObject.cpp index 7c73f3f..3f6726a 100644 --- a/xmltooling/AbstractXMLObject.cpp +++ b/xmltooling/AbstractXMLObject.cpp @@ -1,6 +1,6 @@ /* -* Copyright 2001-2007 Internet2 - * +* Copyright 2001-2010 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,38 +16,183 @@ /** * AbstractXMLObject.cpp - * + * * An abstract implementation of XMLObject. */ #include "internal.h" -#include "AbstractXMLObject.h" #include "exceptions.h" +#include "AbstractXMLObject.h" +#include "util/DateTime.h" #include using namespace xmltooling; +using std::set; + +using xercesc::XMLString; + +XMLObject::XMLObject() +{ +} + +XMLObject::~XMLObject() +{ +} + +void XMLObject::releaseThisandParentDOM() const +{ + releaseDOM(); + releaseParentDOM(true); +} + +void XMLObject::releaseThisAndChildrenDOM() const +{ + releaseChildrenDOM(true); + releaseDOM(); +} AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType) - : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), m_schemaLocation(NULL), m_noNamespaceSchemaLocation(NULL), + : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), + m_schemaLocation(NULL), m_noNamespaceSchemaLocation(NULL), m_nil(xmlconstants::XML_BOOL_NULL), m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL) { - addNamespace(Namespace(nsURI, prefix)); + addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed)); if (schemaType) { m_typeQname = new QName(*schemaType); - addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix())); + addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, Namespace::NonVisiblyUsed)); } } AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src) : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)), - m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), + m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), m_nil(src.m_nil), m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL) { if (src.m_typeQname) m_typeQname=new QName(*src.m_typeQname); } +AbstractXMLObject::~AbstractXMLObject() +{ + delete m_typeQname; + xercesc::XMLString::release(&m_schemaLocation); + xercesc::XMLString::release(&m_noNamespaceSchemaLocation); +} + +void AbstractXMLObject::detach() +{ + if (!getParent()) + return; + else if (getParent()->hasParent()) + throw XMLObjectException("Cannot detach an object whose parent is itself a child."); + + // Pull ourselves out of the parent and then blast him. + getParent()->removeChild(this); + delete m_parent; + m_parent = NULL; +} + +const QName& AbstractXMLObject::getElementQName() const +{ + return m_elementQname; +} + +const set& AbstractXMLObject::getNamespaces() const +{ + return m_namespaces; +} + +void XMLObject::setNil(const XMLCh* value) +{ + if (value) { + switch (*value) { + case xercesc::chLatin_t: + nil(xmlconstants::XML_BOOL_TRUE); + break; + case xercesc::chLatin_f: + nil(xmlconstants::XML_BOOL_FALSE); + break; + case xercesc::chDigit_1: + nil(xmlconstants::XML_BOOL_ONE); + break; + case xercesc::chDigit_0: + nil(xmlconstants::XML_BOOL_ZERO); + break; + default: + nil(xmlconstants::XML_BOOL_NULL); + } + } + else { + nil(xmlconstants::XML_BOOL_NULL); + } +} + +void AbstractXMLObject::addNamespace(const Namespace& ns) const +{ + std::set::iterator i = m_namespaces.find(ns); + if (i == m_namespaces.end()) + m_namespaces.insert(ns); + else { + if (ns.alwaysDeclare()) + const_cast(*i).setAlwaysDeclare(true); + switch (ns.usage()) { + case Namespace::Indeterminate: + break; + case Namespace::VisiblyUsed: + const_cast(*i).setUsage(Namespace::VisiblyUsed); + break; + case Namespace::NonVisiblyUsed: + if (i->usage() == Namespace::Indeterminate) + const_cast(*i).setUsage(Namespace::NonVisiblyUsed); + break; + } + } +} + +void AbstractXMLObject::removeNamespace(const Namespace& ns) +{ + m_namespaces.erase(ns); +} + +const QName* AbstractXMLObject::getSchemaType() const +{ + return m_typeQname; +} + +const XMLCh* AbstractXMLObject::getXMLID() const +{ + return NULL; +} + +xmlconstants::xmltooling_bool_t AbstractXMLObject::getNil() const +{ + return m_nil; +} + +void AbstractXMLObject::nil(xmlconstants::xmltooling_bool_t value) +{ + if (m_nil != value) { + releaseThisandParentDOM(); + m_nil = value; + } +} + +bool AbstractXMLObject::hasParent() const +{ + return m_parent != NULL; +} + +XMLObject* AbstractXMLObject::getParent() const +{ + return m_parent; +} + +void AbstractXMLObject::setParent(XMLObject* parent) +{ + m_parent = parent; +} + XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue) { if (!XMLString::equals(oldValue,newValue)) { @@ -64,8 +209,7 @@ QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* new if (!oldValue) { if (newValue) { releaseThisandParentDOM(); - Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix()); - addNamespace(newNamespace); + addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed)); return new QName(*newValue); } return NULL; @@ -74,8 +218,8 @@ QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* new delete oldValue; releaseThisandParentDOM(); if (newValue) { - Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix()); - addNamespace(newNamespace); + // Attach a non-visibly used namespace. + addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed)); return new QName(*newValue); } return NULL; @@ -96,21 +240,29 @@ DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const Date return newValue ? new DateTime(*newValue) : NULL; } -DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue) +DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration) { delete oldValue; releaseThisandParentDOM(); - DateTime* ret = new DateTime(newValue); - ret->parseDateTime(); + DateTime* ret = new DateTime(newValue, duration); + if (duration) + ret->parseDuration(); + else + ret->parseDateTime(); return ret; } -DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue) +DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration) { delete oldValue; releaseThisandParentDOM(); + if (!newValue || !*newValue) + return NULL; DateTime* ret = new DateTime(newValue); - ret->parseDateTime(); + if (duration) + ret->parseDuration(); + else + ret->parseDateTime(); return ret; } @@ -136,16 +288,3 @@ XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObjec return newValue; } - -void AbstractXMLObject::detach() -{ - if (!getParent()) - return; - else if (getParent()->hasParent()) - throw XMLObjectException("Cannot detach an object whose parent is itself a child."); - - // Pull ourselves out of the parent and then blast him. - getParent()->removeChild(this); - delete m_parent; - m_parent = NULL; -}