1/*
2 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
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
21#include "config.h"
22
23#if ENABLE(WML)
24#include "WMLTemplateElement.h"
25
26#include "Attribute.h"
27#include "NodeList.h"
28#include "WMLCardElement.h"
29#include "WMLDocument.h"
30#include "WMLIntrinsicEventHandler.h"
31#include "WMLNames.h"
32
33namespace WebCore {
34
35using namespace WMLNames;
36
37WMLTemplateElement::WMLTemplateElement(const QualifiedName& tagName, Document* doc)
38    : WMLElement(tagName, doc)
39{
40}
41
42WMLTemplateElement::~WMLTemplateElement()
43{
44}
45
46PassRefPtr<WMLTemplateElement> WMLTemplateElement::create(const QualifiedName& tagName, Document* document)
47{
48    return adoptRef(new WMLTemplateElement(tagName, document));
49}
50
51void WMLTemplateElement::parseMappedAttribute(Attribute* attr)
52{
53    WMLIntrinsicEventType eventType = WMLIntrinsicEventUnknown;
54
55    if (attr->name() == onenterforwardAttr)
56        eventType = WMLIntrinsicEventOnEnterForward;
57    else if (attr->name() == onenterbackwardAttr)
58        eventType = WMLIntrinsicEventOnEnterBackward;
59    else if (attr->name() == ontimerAttr)
60        eventType = WMLIntrinsicEventOnTimer;
61    else {
62        WMLElement::parseMappedAttribute(attr);
63        return;
64    }
65
66    if (eventType == WMLIntrinsicEventUnknown)
67        return;
68
69    // Register intrinsic event in card
70    RefPtr<WMLIntrinsicEvent> event = WMLIntrinsicEvent::create(document(), attr->value());
71
72    createEventHandlerIfNeeded();
73    eventHandler()->registerIntrinsicEvent(eventType, event);
74}
75
76void WMLTemplateElement::registerTemplatesInDocument(Document* doc)
77{
78    ASSERT(doc);
79
80    // Build list of cards in document
81    RefPtr<NodeList> nodeList = doc->getElementsByTagName("card");
82    if (!nodeList)
83        return;
84
85    unsigned length = nodeList->length();
86    if (length < 1)
87        return;
88
89    HashSet<WMLCardElement*> cards;
90    for (unsigned i = 0; i < length; ++i)
91        cards.add(static_cast<WMLCardElement*>(nodeList->item(i)));
92
93    if (cards.isEmpty())
94        return;
95
96    // Register template element to all cards
97    nodeList = doc->getElementsByTagName("template");
98    if (!nodeList)
99        return;
100
101    length = nodeList->length();
102    if (length < 1)
103        return;
104
105    // Only one template element should be allowed in a document
106    // Calling setTemplateElement() twice on a WMLCardElement, will result in a parser error.
107    for (unsigned i = 0; i < length; ++i) {
108        WMLTemplateElement* temp = static_cast<WMLTemplateElement*>(nodeList->item(i));
109
110        HashSet<WMLCardElement*>::iterator it = cards.begin();
111        HashSet<WMLCardElement*>::iterator end = cards.end();
112
113        for (; it != end; ++it)
114            (*it)->setTemplateElement(temp);
115    }
116}
117
118}
119
120#endif
121