initial import
[vuplus_webkit] / Source / WebCore / xml / XSLImportRule.cpp
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 "Document.h"
30 #include "XSLStyleSheet.h"
31
32 namespace WebCore {
33
34 XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href)
35     : StyleBase(parent)
36     , m_strHref(href)
37     , m_cachedSheet(0)
38     , m_loading(false)
39 {
40 }
41
42 XSLImportRule::~XSLImportRule()
43 {
44     if (m_styleSheet)
45         m_styleSheet->setParent(0);
46     
47     if (m_cachedSheet)
48         m_cachedSheet->removeClient(this);
49 }
50
51 XSLStyleSheet* XSLImportRule::parentStyleSheet() const
52 {
53     return (parent() && parent()->isXSLStyleSheet()) ? static_cast<XSLStyleSheet*>(parent()) : 0;
54 }
55
56 void XSLImportRule::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
57 {
58     if (m_styleSheet)
59         m_styleSheet->setParent(0);
60
61     m_styleSheet = XSLStyleSheet::create(this, href, baseURL);
62
63     XSLStyleSheet* parent = parentStyleSheet();
64     if (parent)
65         m_styleSheet->setParentStyleSheet(parent);
66
67     m_styleSheet->parseString(sheet);
68     m_loading = false;
69     
70     if (parent)
71         parent->checkLoaded();
72 }
73
74 bool XSLImportRule::isLoading()
75 {
76     return (m_loading || (m_styleSheet && m_styleSheet->isLoading()));
77 }
78
79 void XSLImportRule::loadSheet()
80 {
81     CachedResourceLoader* cachedResourceLoader = 0;
82     StyleBase* root = this;
83     StyleBase* parent;
84     while ((parent = root->parent()))
85         root = parent;
86     if (root->isXSLStyleSheet())
87         cachedResourceLoader = static_cast<XSLStyleSheet*>(root)->cachedResourceLoader();
88     
89     String absHref = m_strHref;
90     XSLStyleSheet* parentSheet = parentStyleSheet();
91     if (!parentSheet->finalURL().isNull())
92         // use parent styleheet's URL as the base URL
93         absHref = KURL(parentSheet->finalURL(), 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 (parent = this->parent(); parent; parent = parent->parent()) {
98         if (parent->isXSLStyleSheet() && absHref == static_cast<XSLStyleSheet*>(parent)->finalURL().string())
99             return;
100     }
101     
102     ResourceRequest request(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)