1/*
2 * This file is part of the XSL implementation.
3 *
4 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "XSLImportRule.h"
24
25#if ENABLE(XSLT)
26
27#include "CachedXSLStyleSheet.h"
28#include "CachedResourceLoader.h"
29#include "XSLStyleSheet.h"
30
31namespace WebCore {
32
33XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href)
34    : StyleBase(parent)
35    , m_strHref(href)
36    , m_cachedSheet(0)
37    , m_loading(false)
38{
39}
40
41XSLImportRule::~XSLImportRule()
42{
43    if (m_styleSheet)
44        m_styleSheet->setParent(0);
45
46    if (m_cachedSheet)
47        m_cachedSheet->removeClient(this);
48}
49
50XSLStyleSheet* XSLImportRule::parentStyleSheet() const
51{
52    return (parent() && parent()->isXSLStyleSheet()) ? static_cast<XSLStyleSheet*>(parent()) : 0;
53}
54
55void XSLImportRule::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
56{
57    if (m_styleSheet)
58        m_styleSheet->setParent(0);
59
60    m_styleSheet = XSLStyleSheet::create(this, href, baseURL);
61
62    XSLStyleSheet* parent = parentStyleSheet();
63    if (parent)
64        m_styleSheet->setParentStyleSheet(parent);
65
66    m_styleSheet->parseString(sheet);
67    m_loading = false;
68
69    if (parent)
70        parent->checkLoaded();
71}
72
73bool XSLImportRule::isLoading()
74{
75    return (m_loading || (m_styleSheet && m_styleSheet->isLoading()));
76}
77
78void XSLImportRule::loadSheet()
79{
80    CachedResourceLoader* cachedResourceLoader = 0;
81    StyleBase* root = this;
82    StyleBase* parent;
83    while ((parent = root->parent()))
84        root = parent;
85    if (root->isXSLStyleSheet())
86        cachedResourceLoader = static_cast<XSLStyleSheet*>(root)->cachedResourceLoader();
87
88    String absHref = m_strHref;
89    XSLStyleSheet* parentSheet = parentStyleSheet();
90    if (!parentSheet->finalURL().isNull())
91        // use parent styleheet's URL as the base URL
92        absHref = KURL(parentSheet->finalURL(), m_strHref).string();
93
94    // Check for a cycle in our import chain.  If we encounter a stylesheet
95    // in our parent chain with the same URL, then just bail.
96    for (parent = this->parent(); parent; parent = parent->parent()) {
97        if (parent->isXSLStyleSheet() && absHref == static_cast<XSLStyleSheet*>(parent)->finalURL().string())
98            return;
99    }
100
101    m_cachedSheet = cachedResourceLoader->requestXSLStyleSheet(absHref);
102
103    if (m_cachedSheet) {
104        m_cachedSheet->addClient(this);
105
106        // If the imported sheet is in the cache, then setXSLStyleSheet gets called,
107        // and the sheet even gets parsed (via parseString).  In this case we have
108        // loaded (even if our subresources haven't), so if we have a stylesheet after
109        // checking the cache, then we've clearly loaded.
110        if (!m_styleSheet)
111            m_loading = true;
112    }
113}
114
115} // namespace WebCore
116
117#endif // ENABLE(XSLT)
118