2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * A helper class for working with W3C DOM objects.
24 #include "exceptions.h"
26 #include "XMLObject.h"
27 #include "util/XMLHelper.h"
28 #include "util/XMLConstants.h"
30 #include <xercesc/framework/MemBufFormatTarget.hpp>
31 #include <xercesc/util/XMLUniDefs.hpp>
33 using namespace xmltooling;
34 using namespace xercesc;
37 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
39 bool XMLHelper::hasXSIType(const DOMElement* e)
42 if (e->hasAttributeNS(xmlconstants::XSI_NS, type)) {
50 xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e)
52 DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
54 const XMLCh* attributeValue = attribute->getTextContent();
55 if (attributeValue && *attributeValue) {
57 if ((i=XMLString::indexOf(attributeValue,chColon))>0) {
58 XMLCh* prefix=new XMLCh[i+1];
59 XMLString::subString(prefix,attributeValue,0,i);
61 xmltooling::QName* ret=new xmltooling::QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
66 return new xmltooling::QName(e->lookupNamespaceURI(NULL), attributeValue);
74 DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
76 if(!domElement->hasAttributes()) {
80 DOMNamedNodeMap* attributes = domElement->getAttributes();
82 for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
83 attribute = static_cast<DOMAttr*>(attributes->item(i));
84 if(attribute->isId()) {
92 const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* id)
94 if (XMLString::equals(id, tree.getXMLID()))
98 const list<XMLObject*>& children = tree.getOrderedChildren();
99 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
101 ret = getXMLObjectById(*(*i), id);
110 XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
112 if (XMLString::equals(id, tree.getXMLID()))
116 const list<XMLObject*>& children = tree.getOrderedChildren();
117 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
119 ret = getXMLObjectById(*(*i), id);
128 xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode)
131 return new xmltooling::QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
135 xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
137 return getNodeValueAsQName(attribute);
140 xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
146 const XMLCh* value=domNode->getTextContent();
147 if (value && (i=XMLString::indexOf(value,chColon))>0) {
148 XMLCh* prefix=new XMLCh[i+1];
149 XMLString::subString(prefix,value,0,i);
151 xmltooling::QName* ret=new xmltooling::QName(domNode->lookupNamespaceURI(prefix), value + i + 1, prefix);
156 return new xmltooling::QName(domNode->lookupNamespaceURI(NULL), value);
159 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
161 DOMDocument* parentDocument = parentElement->getOwnerDocument();
162 if (childElement->getOwnerDocument() != parentDocument) {
163 childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
166 parentElement->appendChild(childElement);
170 const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
172 DOMNode* child=e->getFirstChild();
174 if (child->getNodeType()==DOMNode::TEXT_NODE)
175 return child->getNodeValue();
176 child=child->getNextSibling();
181 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)
183 DOMNode* child = n->getFirstChild();
184 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
185 child = child->getNextSibling();
186 if (child && localName) {
187 if (!XMLString::equals(localName,child->getLocalName()))
188 return getNextSiblingElement(child, localName);
190 return static_cast<DOMElement*>(child);
193 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* localName)
195 DOMNode* child = n->getLastChild();
196 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
197 child = child->getPreviousSibling();
198 if (child && localName) {
199 if (!XMLString::equals(localName,child->getLocalName()))
200 return getPreviousSiblingElement(child, localName);
202 return static_cast<DOMElement*>(child);
205 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
207 DOMElement* e = getFirstChildElement(n, localName);
208 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
209 e = getNextSiblingElement(e, localName);
213 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
215 DOMElement* e = getLastChildElement(n, localName);
216 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
217 e = getPreviousSiblingElement(e, localName);
221 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* localName)
223 DOMNode* sib = n->getNextSibling();
224 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
225 sib = sib->getNextSibling();
226 if (sib && localName) {
227 if (!XMLString::equals(localName,sib->getLocalName()))
228 return getNextSiblingElement(sib, localName);
230 return static_cast<DOMElement*>(sib);
233 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName)
235 DOMNode* sib = n->getPreviousSibling();
236 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
237 sib = sib->getPreviousSibling();
238 if (sib && localName) {
239 if (!XMLString::equals(localName,sib->getLocalName()))
240 return getPreviousSiblingElement(sib, localName);
242 return static_cast<DOMElement*>(sib);
245 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
247 DOMElement* e = getNextSiblingElement(n, localName);
248 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
249 e = getNextSiblingElement(e, localName);
253 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
255 DOMElement* e = getPreviousSiblingElement(n, localName);
256 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
257 e = getPreviousSiblingElement(e, localName);
261 void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
263 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
264 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, chDigit_8, chNull };
266 MemBufFormatTarget target;
267 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
269 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
270 DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer();
271 XercesJanitor<DOMLSSerializer> janitor(serializer);
272 if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
273 serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
274 DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();
275 XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
276 theOutput->setEncoding(UTF8);
277 theOutput->setByteStream(&target);
278 if (!serializer->write(n, theOutput))
279 throw XMLParserException("unable to serialize XML");
281 DOMWriter* serializer = static_cast<DOMImplementationLS*>(impl)->createDOMWriter();
282 XercesJanitor<DOMWriter> janitor(serializer);
283 serializer->setEncoding(UTF8);
284 if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
285 serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
286 if (!serializer->writeNode(&target, *n))
287 throw XMLParserException("unable to serialize XML");
291 buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
295 class StreamFormatTarget : public XMLFormatTarget
298 StreamFormatTarget(std::ostream& out) : m_out(out) {}
299 ~StreamFormatTarget() {}
301 void writeChars(const XMLByte *const toWrite, const xsecsize_t count, XMLFormatter *const formatter) {
302 m_out.write(reinterpret_cast<const char*>(toWrite),count);
314 ostream& XMLHelper::serialize(const DOMNode* n, ostream& out, bool pretty)
316 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
317 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, chDigit_8, chNull };
319 StreamFormatTarget target(out);
320 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
322 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
323 DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer();
324 XercesJanitor<DOMLSSerializer> janitor(serializer);
325 if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
326 serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
327 DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();
328 XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
329 theOutput->setEncoding(UTF8);
330 theOutput->setByteStream(&target);
331 if (!serializer->write(n, theOutput))
332 throw XMLParserException("unable to serialize XML");
334 DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
335 XercesJanitor<DOMWriter> janitor(serializer);
336 serializer->setEncoding(UTF8);
337 if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
338 serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
339 if (!serializer->writeNode(&target,*n))
340 throw XMLParserException("unable to serialize XML");
346 ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
348 return XMLHelper::serialize(&node, ostr);
351 ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
353 return ostr << *(obj.marshall());