\r
#define XT_log (*static_cast<Category*>(m_log))\r
\r
-AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName)\r
- : m_targetQName(targetNamespaceURI, targetLocalName),\r
- m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Marshaller")) {\r
- if (!targetLocalName || !*targetLocalName)\r
- throw MarshallingException("targetLocalName cannot be null or empty");\r
-}\r
+AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller()\r
+ : m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Marshaller")) {}\r
\r
DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document) const\r
{\r
\r
void AbstractXMLObjectMarshaller::marshallInto(XMLObject& xmlObject, DOMElement* targetElement) const\r
{\r
- targetElement->setPrefix(xmlObject.getElementQName().getPrefix());\r
+ if (xmlObject.getElementQName().hasPrefix())\r
+ targetElement->setPrefix(xmlObject.getElementQName().getPrefix());\r
marshallElementType(xmlObject, targetElement);\r
marshallNamespaces(xmlObject, targetElement);\r
marshallAttributes(xmlObject, targetElement);\r
const XMLCh* uri=ns.getNamespaceURI();\r
\r
// Check to see if the prefix is already declared properly above this node.\r
- if (!ns.alwaysDeclare() && domElement->getParentNode() &&\r
- XMLString::equals(domElement->getParentNode()->lookupNamespaceURI(prefix),uri))\r
- return;\r
+ if (!ns.alwaysDeclare()) {\r
+ const XMLCh* declared=lookupNamespaceURI(domElement->getParentNode(),prefix);\r
+ if (declared && XMLString::equals(declared,uri))\r
+ return;\r
+ }\r
\r
if (prefix && *prefix) {\r
XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLConstants::XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];\r
domElement->setAttributeNS(XMLConstants::XMLNS_NS, XMLConstants::XMLNS_PREFIX, uri);\r
}\r
}\r
+\r
+ const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {\r
+ // Return NULL if no declaration in effect. The empty string signifies the null namespace.\r
+ if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) {\r
+ // At the root, the default namespace is set to the null namespace.\r
+ if (!prefix || !*prefix)\r
+ return &chNull;\r
+ return NULL; // we're done\r
+ }\r
+ DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();\r
+ if (!attributes)\r
+ return lookupNamespaceURI(n->getParentNode(),prefix); // defer to parent\r
+ DOMNode* childNode;\r
+ DOMAttr* attribute;\r
+ for (XMLSize_t i=0; i<attributes->getLength(); i++) {\r
+ childNode = attributes->item(i);\r
+ if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute?\r
+ continue;\r
+ attribute = static_cast<DOMAttr*>(childNode);\r
+ if (!XMLString::equals(attribute->getNamespaceURI(),XMLConstants::XMLNS_NS))\r
+ continue; // not a namespace declaration\r
+ // Local name should be the prefix and the value would be the URI, except for the default namespace.\r
+ if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))\r
+ return attribute->getNodeValue();\r
+ else if (XMLString::equals(prefix,attribute->getLocalName()))\r
+ return attribute->getNodeValue();\r
+ }\r
+ // Defer to parent.\r
+ return lookupNamespaceURI(n->getParentNode(),prefix);\r
+ }\r
};\r
\r
void AbstractXMLObjectMarshaller::marshallNamespaces(const XMLObject& xmlObject, DOMElement* domElement) const\r
public:\r
_marshallchild(void* log) : m_log(log) {}\r
void operator()(XMLObject* obj, DOMElement* element) const {\r
+ if (!obj)\r
+ return;\r
if (XT_log.isDebugEnabled()) {\r
XT_log.debug("getting marshaller for child XMLObject: %s", obj->getElementQName().toString().c_str());\r
}\r
{\r
XT_log.debug("marshalling child elements for XMLObject");\r
\r
- vector<XMLObject*> children;\r
- if (xmlObject.getOrderedChildren(children)) {\r
- for_each(children.begin(),children.end(),bind2nd(_marshallchild(m_log),domElement));\r
- }\r
+ const list<XMLObject*>& children=xmlObject.getOrderedChildren();\r
+ for_each(children.begin(),children.end(),bind2nd(_marshallchild(m_log),domElement));\r
}\r