File: AbstractXMLObjectUnmarshaller.cpp

package info (click to toggle)
xmltooling 1.6.0-4+deb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,928 kB
  • sloc: cpp: 22,289; sh: 11,065; makefile: 337; xml: 170; ansic: 49
file content (227 lines) | stat: -rw-r--r-- 9,215 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 * Licensed to the University Corporation for Advanced Internet
 * Development, Inc. (UCAID) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * UCAID licenses this file to you 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.
 */

/**
 * AbstractXMLObjectUnmarshaller.cpp
 * 
 * A thread-safe abstract unmarshaller.
 */

#include "internal.h"
#include "exceptions.h"
#include "XMLObjectBuilder.h"
#include "io/AbstractXMLObjectUnmarshaller.h"
#include "util/NDC.h"
#include "util/XMLConstants.h"
#include "util/XMLHelper.h"

#include <xercesc/util/XMLUniDefs.hpp>

using namespace xmlconstants;
using namespace xmltooling;
using namespace xercesc;
using namespace std;

AbstractXMLObjectUnmarshaller::AbstractXMLObjectUnmarshaller()
{
}

AbstractXMLObjectUnmarshaller::~AbstractXMLObjectUnmarshaller()
{
}

XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool bindDocument)
{
#ifdef _DEBUG
    xmltooling::NDC ndc("unmarshall");
#endif

    if (getDOM() || hasParent())
        throw UnmarshallingException("Object already contains data, it cannot be unmarshalled at this stage.");

    if (!XMLString::equals(element->getNamespaceURI(),getElementQName().getNamespaceURI()) ||
        !XMLString::equals(element->getLocalName(),getElementQName().getLocalPart())) {
        throw UnmarshallingException("Unrecognized element supplied to implementation for unmarshalling.");
    }

    if (m_log.isDebugEnabled()) {
        auto_ptr_char dname(element->getNodeName());
        m_log.debug("unmarshalling DOM element (%s)", dname.get());
    }

    if (element->hasAttributes()) {
        unmarshallAttributes(element);
    }

    unmarshallContent(element);

    setDOM(element,bindDocument);
    return this;
}

void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domElement)
{
#ifdef _DEBUG
    xmltooling::NDC ndc("unmarshallAttributes");
#endif

    if (m_log.isDebugEnabled()) {
        auto_ptr_char dname(domElement->getNodeName());
        m_log.debug("unmarshalling attributes for DOM element (%s)", dname.get());
    }

    DOMNamedNodeMap* attributes = domElement->getAttributes();
    if (!attributes) {
        m_log.debug("no attributes to unmarshall");
        return;
    }

    DOMNode* childNode;
    DOMAttr* attribute;
    for (XMLSize_t i=0; i<attributes->getLength(); i++) {
        childNode = attributes->item(i);

        // The child node should always be an attribute, but just in case
        if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) {
            m_log.debug("encountered child node of type %d in attribute list, ignoring it", childNode->getNodeType());
            continue;
        }

        attribute = static_cast<DOMAttr*>(childNode);
        
        const XMLCh* nsuri=attribute->getNamespaceURI();
        if (XMLString::equals(nsuri,XMLNS_NS)) {
            if (XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) {
                m_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject");
                addNamespace(Namespace(attribute->getValue(), nullptr, true));
            }
            else if (XMLString::equals(attribute->getLocalName(),XML_PREFIX) && XMLString::equals(attribute->getNodeValue(),XML_NS)) {
                m_log.debug("found standard xml prefix declaration, ignoring as superfluous");
            }
            else {
                m_log.debug("found namespace declaration, adding it to the list of namespaces on the XMLObject");
                addNamespace(Namespace(attribute->getValue(), attribute->getLocalName(), true));
            }
            continue;
        }
        else if (XMLString::equals(nsuri,XSI_NS)) {
            static const XMLCh type[]= UNICODE_LITERAL_4(t,y,p,e);
            static const XMLCh schemaLocation[]= UNICODE_LITERAL_14(s,c,h,e,m,a,L,o,c,a,t,i,o,n);
            static const XMLCh noNamespaceSchemaLocation[]= UNICODE_LITERAL_25(n,o,N,a,m,e,s,p,a,c,e,S,c,h,e,m,a,L,o,c,a,t,i,o,n);
            static const XMLCh _nil[]= UNICODE_LITERAL_3(n,i,l);
            if (XMLString::equals(attribute->getLocalName(),type)) {
                m_log.debug("skipping xsi:type declaration");
                continue;
            }
            else if (XMLString::equals(attribute->getLocalName(),schemaLocation)) {
                m_log.debug("storing off xsi:schemaLocation attribute");
                if (m_schemaLocation)
                    XMLString::release(&m_schemaLocation);
                m_schemaLocation=XMLString::replicate(attribute->getValue());
                continue;
            }
            else if (XMLString::equals(attribute->getLocalName(),noNamespaceSchemaLocation)) {
                m_log.debug("storing off xsi:noNamespaceSchemaLocation attribute");
                if (m_noNamespaceSchemaLocation)
                    XMLString::release(&m_noNamespaceSchemaLocation);
                m_schemaLocation=XMLString::replicate(attribute->getValue());
                m_noNamespaceSchemaLocation=XMLString::replicate(attribute->getValue());
                continue;
            }
            else if (XMLString::equals(attribute->getLocalName(), _nil)) {
            	m_log.debug("processing xsi:nil attribute");
            	setNil(attribute->getValue());
            	continue;
            }
            // Note that the prefix is visibly used.
            addNamespace(Namespace(nsuri, attribute->getPrefix(), false, Namespace::VisiblyUsed));
        }
        else if (nsuri && !XMLString::equals(nsuri,XML_NS)) {
            m_log.debug("found namespace-qualified attribute, adding prefix to the list of visible namespaces on the XMLObject");
            addNamespace(Namespace(nsuri, attribute->getPrefix(), false, Namespace::VisiblyUsed));
        }

        m_log.debug("processing generic attribute");
        processAttribute(attribute);
    }
}

void AbstractXMLObjectUnmarshaller::unmarshallContent(const DOMElement* domElement)
{
#ifdef _DEBUG
    xmltooling::NDC ndc("unmarshallContent");
#endif

    if (m_log.isDebugEnabled()) {
        auto_ptr_char dname(domElement->getNodeName());
        m_log.debug("unmarshalling child nodes of DOM element (%s)", dname.get());
    }

    DOMNode* childNode = domElement->getFirstChild();
    if (!childNode) {
        m_log.debug("element had no children");
        return;
    }

    unsigned int position = 0;
    while (childNode) {
        if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
            const XMLObjectBuilder* builder = XMLObjectBuilder::getBuilder(static_cast<DOMElement*>(childNode));
            if (!builder) {
                auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
                m_log.error("no default builder installed, found unknown child element (%s)", cname->toString().c_str());
                throw UnmarshallingException("Unmarshaller found unknown child element, but no default builder was found.");
            }

            if (m_log.isDebugEnabled()) {
                auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
                m_log.debug("unmarshalling child element (%s)", cname->toString().c_str());
            }

            // Retain ownership of the unmarshalled child until it's processed by the parent.
            auto_ptr<XMLObject> childObject(builder->buildFromElement(static_cast<DOMElement*>(childNode)));
            processChildElement(childObject.get(), static_cast<DOMElement*>(childNode));
            childObject.release();
            
            // Advance the text node position marker.
            ++position;
        }
        else if (childNode->getNodeType() == DOMNode::TEXT_NODE || childNode->getNodeType() == DOMNode::CDATA_SECTION_NODE) {
            m_log.debug("processing text content at position (%d)", position);
            setTextContent(childNode->getNodeValue(), position);
        }
        else if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) {
            throw UnmarshallingException("Unmarshaller found unsupported node type.");
        }
        
        childNode = childNode->getNextSibling();
    }
}

void AbstractXMLObjectUnmarshaller::processChildElement(XMLObject* child, const DOMElement* childRoot)
{
    throw UnmarshallingException("Invalid child element: $1",params(1,child->getElementQName().toString().c_str()));
}

void AbstractXMLObjectUnmarshaller::processAttribute(const DOMAttr* attribute)
{
    auto_ptr<QName> q(XMLHelper::getNodeQName(attribute));
    throw UnmarshallingException("Invalid attribute: $1",params(1,q->toString().c_str()));
}