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 "core/xml/XSLImportRule.h" 24 25 #include "core/FetchInitiatorTypeNames.h" 26 #include "core/dom/Document.h" 27 #include "core/fetch/FetchRequest.h" 28 #include "core/fetch/ResourceFetcher.h" 29 #include "core/fetch/XSLStyleSheetResource.h" 30 31 namespace WebCore { 32 33 XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href) 34 : m_parentStyleSheet(parent) 35 , m_strHref(href) 36 , m_resource(0) 37 , m_loading(false) 38 { 39 } 40 41 XSLImportRule::~XSLImportRule() 42 { 43 #if !ENABLE(OILPAN) 44 if (m_styleSheet) 45 m_styleSheet->setParentStyleSheet(0); 46 #endif 47 48 if (m_resource) 49 m_resource->removeClient(this); 50 } 51 52 void 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 70 bool XSLImportRule::isLoading() 71 { 72 return m_loading || (m_styleSheet && m_styleSheet->isLoading()); 73 } 74 75 void XSLImportRule::loadSheet() 76 { 77 ResourceFetcher* fetcher = 0; 78 XSLStyleSheet* rootSheet = parentStyleSheet(); 79 80 if (rootSheet) { 81 while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet()) 82 rootSheet = parentSheet; 83 } 84 85 if (rootSheet) 86 fetcher = rootSheet->fetcher(); 87 88 String absHref = m_strHref; 89 XSLStyleSheet* parentSheet = parentStyleSheet(); 90 if (!parentSheet->baseURL().isNull()) { 91 // Use parent styleheet's URL as the base URL 92 absHref = KURL(parentSheet->baseURL(), m_strHref).string(); 93 } 94 95 // Check for a cycle in our import chain. If we encounter a stylesheet in 96 // 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 FetchRequest request(ResourceRequest(fetcher->document()->completeURL(absHref)), FetchInitiatorTypeNames::xml); 103 m_resource = fetcher->fetchXSLStyleSheet(request); 104 105 if (m_resource) { 106 m_resource->addClient(this); 107 108 // If the imported sheet is in the cache, then setXSLStyleSheet gets 109 // called, and the sheet even gets parsed (via parseString). In this 110 // case we have loaded (even if our subresources haven't), so if we have 111 // a stylesheet after checking the cache, then we've clearly loaded. 112 if (!m_styleSheet) 113 m_loading = true; 114 } 115 } 116 117 void XSLImportRule::trace(Visitor* visitor) 118 { 119 visitor->trace(m_parentStyleSheet); 120 visitor->trace(m_styleSheet); 121 } 122 123 } // namespace WebCore 124