Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / QualifiedName.cpp
1 /*
2  * Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "core/dom/QualifiedName.h"
23
24 #include "core/HTMLNames.h"
25 #include "core/SVGNames.h"
26 #include "core/XLinkNames.h"
27 #include "core/XMLNSNames.h"
28 #include "core/XMLNames.h"
29 #include "wtf/Assertions.h"
30 #include "wtf/HashSet.h"
31 #include "wtf/MainThread.h"
32 #include "wtf/StaticConstructors.h"
33
34 namespace blink {
35
36 static const int staticQualifiedNamesCount = HTMLNames::HTMLTagsCount + HTMLNames::HTMLAttrsCount
37     + SVGNames::SVGTagsCount + SVGNames::SVGAttrsCount
38     + XLinkNames::XLinkAttrsCount
39     + XMLNSNames::XMLNSAttrsCount
40     + XMLNames::XMLAttrsCount;
41
42 struct QualifiedNameHashTraits : public HashTraits<QualifiedName::QualifiedNameImpl*> {
43     static const unsigned minimumTableSize = WTF::HashTableCapacityForSize<staticQualifiedNamesCount>::value;
44 };
45
46 typedef HashSet<QualifiedName::QualifiedNameImpl*, QualifiedNameHash, QualifiedNameHashTraits> QualifiedNameCache;
47
48 static QualifiedNameCache& qualifiedNameCache()
49 {
50     // This code is lockless and thus assumes it all runs on one thread!
51     ASSERT(isMainThread());
52     static QualifiedNameCache* gNameCache = new QualifiedNameCache;
53     return *gNameCache;
54 }
55
56 struct QNameComponentsTranslator {
57     static unsigned hash(const QualifiedNameData& data)
58     {
59         return hashComponents(data.m_components);
60     }
61     static bool equal(QualifiedName::QualifiedNameImpl* name, const QualifiedNameData& data)
62     {
63         return data.m_components.m_prefix == name->m_prefix.impl()
64             && data.m_components.m_localName == name->m_localName.impl()
65             && data.m_components.m_namespace == name->m_namespace.impl();
66     }
67     static void translate(QualifiedName::QualifiedNameImpl*& location, const QualifiedNameData& data, unsigned)
68     {
69         const QualifiedNameComponents& components = data.m_components;
70         location = QualifiedName::QualifiedNameImpl::create(AtomicString(components.m_prefix), AtomicString(components.m_localName), AtomicString(components.m_namespace), data.m_isStatic).leakRef();
71     }
72 };
73
74 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
75 {
76     QualifiedNameData data = { { p.impl(), l.impl(), n.isEmpty() ? nullAtom.impl() : n.impl() }, false };
77     QualifiedNameCache::AddResult addResult = qualifiedNameCache().add<QNameComponentsTranslator>(data);
78     m_impl = addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult.storedValue;
79 }
80
81 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n, bool isStatic)
82 {
83     QualifiedNameData data = { { p.impl(), l.impl(), n.impl() }, isStatic };
84     QualifiedNameCache::AddResult addResult = qualifiedNameCache().add<QNameComponentsTranslator>(data);
85     m_impl = addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult.storedValue;
86 }
87
88 QualifiedName::~QualifiedName()
89 {
90 }
91
92 QualifiedName::QualifiedNameImpl::~QualifiedNameImpl()
93 {
94     qualifiedNameCache().remove(this);
95 }
96
97 String QualifiedName::toString() const
98 {
99     String local = localName();
100     if (hasPrefix())
101         return prefix().string() + ":" + local;
102     return local;
103 }
104
105 // Global init routines
106 DEFINE_GLOBAL(QualifiedName, anyName, nullAtom, starAtom, starAtom)
107
108 void QualifiedName::init()
109 {
110     ASSERT(starAtom.impl());
111     new ((void*)&anyName) QualifiedName(nullAtom, starAtom, starAtom, true );
112 }
113
114 const QualifiedName& QualifiedName::null()
115 {
116     DEFINE_STATIC_LOCAL(QualifiedName, nullName, (nullAtom, nullAtom, nullAtom, true));
117     return nullName;
118 }
119
120 const AtomicString& QualifiedName::localNameUpper() const
121 {
122     if (!m_impl->m_localNameUpper)
123         m_impl->m_localNameUpper = m_impl->m_localName.upper();
124     return m_impl->m_localNameUpper;
125 }
126
127 unsigned QualifiedName::QualifiedNameImpl::computeHash() const
128 {
129     QualifiedNameComponents components = { m_prefix.impl(), m_localName.impl(), m_namespace.impl() };
130     return hashComponents(components);
131 }
132
133 void QualifiedName::createStatic(void* targetAddress, StringImpl* name, const AtomicString& nameNamespace)
134 {
135     new (targetAddress) QualifiedName(nullAtom, AtomicString(name), nameNamespace, true);
136 }
137
138 void QualifiedName::createStatic(void* targetAddress, StringImpl* name)
139 {
140     new (targetAddress) QualifiedName(nullAtom, AtomicString(name), nullAtom, true);
141 }
142
143 }