1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16#include <util/xml/XMLElementImpl.h>
17#include <util/domcore/TextImpl.h>
18
19/** see XMLElementImpl.h */
20XMLElementImpl::XMLElementImpl(const DOMString *tag)
21{
22    if (tag)
23    {
24        mTagName = *tag;
25    }
26}
27
28/** see XMLElementImpl.h */
29XMLElementImpl::~XMLElementImpl()
30{
31}
32
33/** see XMLElementImpl.h */
34const DOMString* XMLElementImpl::getTagName() const
35{
36    return &mTagName;
37}
38
39/** see XMLElementImpl.h */
40void XMLElementImpl::setAttribute(const DOMString* name, const DOMString* value)
41                                  throw (DOMException)
42{
43    if (name && value)
44    {
45        mAttributeMap[*name] = *value;
46    }
47}
48
49/** see XMLElementImpl.h */
50void XMLElementImpl::removeAttribute(const DOMString* name) throw (DOMException)
51{
52    if (name)
53    {
54       mAttributeMap.erase(*name);
55    }
56}
57
58/** see XMLElementImpl.h */
59const DOMString* XMLElementImpl::getAttribute(const DOMString* name) const
60{
61    if (name)
62    {
63        DOMStringMap::const_iterator pos = mAttributeMap.find(*name);
64
65        if (pos != mAttributeMap.end())
66        {
67           return &(pos->second);
68        }
69
70    }
71    return NULL;
72}
73
74/** see XMLElementImpl.h */
75bool XMLElementImpl::hasAttributes() const
76{
77    return !mAttributeMap.empty();
78}
79
80/** see XMLElementImpl.h */
81const DOMStringMap* XMLElementImpl::getAttributeMap() const
82{
83    return &mAttributeMap;
84}
85
86/** see XMLElementImpl.h */
87const NodeImpl* XMLElementImpl::findSoloChildNode(const char* tag) const
88{
89    if (NULL == tag)
90    {
91        return NULL;
92    }
93
94    string token;
95    NodeListImpl *nodeList = NULL;
96    const NodeImpl *childNode = NULL;
97
98    token.assign(tag);
99    nodeList = getElementsByTagName(&token);
100
101    if (nodeList->getLength() > 0)
102    {
103         childNode = nodeList->item(0);
104    }
105
106    return childNode;
107}
108
109/** see XMLElementImpl.h */
110const string* XMLElementImpl::getSoloText(const char* tag) const
111{
112    const NodeImpl *textNode = this->findSoloChildNode(tag);
113
114    if (textNode)
115    {
116        textNode = textNode->getFirstChild();
117        if (textNode)
118        {
119            return static_cast<const TextImpl*>(textNode)->getData();
120        }
121    }
122
123    return NULL;
124}
125
126/** see XMLElementImpl.h */
127const XMLElementImpl* XMLElementImpl::getSoloElement(const char* tag) const
128{
129    const NodeImpl *node = findSoloChildNode(tag);
130    if (node)
131    {
132        return static_cast<const XMLElementImpl*>(node);
133    }
134
135    return NULL;
136}
137