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 "CachedResourceRequest.h"
30#include "Document.h"
31#include "XSLStyleSheet.h"
32
33namespace WebCore {
34
35XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href)
36    : m_parentStyleSheet(parent)
37    , m_strHref(href)
38    , m_cachedSheet(0)
39    , m_loading(false)
40{
41}
42
43XSLImportRule::~XSLImportRule()
44{
45    if (m_styleSheet)
46        m_styleSheet->setParentStyleSheet(0);
47
48    if (m_cachedSheet)
49        m_cachedSheet->removeClient(this);
50}
51
52void XSLImportRule::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
53{
54    if (m_styleSheet)
55        m_styleSheet->setParentStyleSheet(0);
56
57    m_styleSheet = XSLStyleSheet::create(this, href, baseURL);
58
59    XSLStyleSheet* parent = parentStyleSheet();
60    if (parent)
61        m_styleSheet->setParentStyleSheet(parent);
62
63    m_styleSheet->parseString(sheet);
64    m_loading = false;
65
66    if (parent)
67        parent->checkLoaded();
68}
69
70bool XSLImportRule::isLoading()
71{
72    return (m_loading || (m_styleSheet && m_styleSheet->isLoading()));
73}
74
75void XSLImportRule::loadSheet()
76{
77    CachedResourceLoader* cachedResourceLoader = 0;
78
79    XSLStyleSheet* rootSheet = parentStyleSheet();
80
81    if (rootSheet) {
82        while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet())
83            rootSheet = parentSheet;
84    }
85
86    if (rootSheet)
87        cachedResourceLoader = rootSheet->cachedResourceLoader();
88
89    String absHref = m_strHref;
90    XSLStyleSheet* parentSheet = parentStyleSheet();
91    if (!parentSheet->baseURL().isNull())
92        // use parent styleheet's URL as the base URL
93        absHref = KURL(parentSheet->baseURL(), m_strHref).string();
94
95    // Check for a cycle in our import chain.  If we encounter a stylesheet
96    // in our parent chain with the same URL, then just bail.
97    for (XSLStyleSheet* parentSheet = parentStyleSheet(); parentSheet; parentSheet = parentSheet->parentStyleSheet()) {
98        if (absHref == parentSheet->baseURL().string())
99            return;
100    }
101
102    CachedResourceRequest request(ResourceRequest(cachedResourceLoader->document()->completeURL(absHref)));
103    m_cachedSheet = cachedResourceLoader->requestXSLStyleSheet(request);
104
105    if (m_cachedSheet) {
106        m_cachedSheet->addClient(this);
107
108        // If the imported sheet is in the cache, then setXSLStyleSheet gets called,
109        // and the sheet even gets parsed (via parseString).  In this case we have
110        // loaded (even if our subresources haven't), so if we have a stylesheet after
111        // checking the cache, then we've clearly loaded.
112        if (!m_styleSheet)
113            m_loading = true;
114    }
115}
116
117} // namespace WebCore
118
119#endif // ENABLE(XSLT)
120