2 * This file is part of the XSL implementation.
4 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "core/xml/XSLImportRule.h"
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 #include "platform/SharedBuffer.h"
31 #include "wtf/text/TextEncoding.h"
35 XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href)
36 : m_parentStyleSheet(parent)
42 XSLImportRule::~XSLImportRule()
46 m_styleSheet->setParentStyleSheet(0);
50 void XSLImportRule::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
53 m_styleSheet->setParentStyleSheet(0);
55 m_styleSheet = XSLStyleSheet::create(this, href, baseURL);
57 XSLStyleSheet* parent = parentStyleSheet();
59 m_styleSheet->setParentStyleSheet(parent);
61 m_styleSheet->parseString(sheet);
65 parent->checkLoaded();
68 bool XSLImportRule::isLoading()
70 return m_loading || (m_styleSheet && m_styleSheet->isLoading());
73 void XSLImportRule::loadSheet()
75 ResourceFetcher* fetcher = 0;
76 XSLStyleSheet* rootSheet = parentStyleSheet();
79 while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet())
80 rootSheet = parentSheet;
84 fetcher = rootSheet->fetcher();
86 String absHref = m_strHref;
87 XSLStyleSheet* parentSheet = parentStyleSheet();
88 if (!parentSheet->baseURL().isNull()) {
89 // Use parent styleheet's URL as the base URL
90 absHref = KURL(parentSheet->baseURL(), m_strHref).string();
93 // Check for a cycle in our import chain. If we encounter a stylesheet in
94 // our parent chain with the same URL, then just bail.
95 for (XSLStyleSheet* parentSheet = parentStyleSheet(); parentSheet; parentSheet = parentSheet->parentStyleSheet()) {
96 if (absHref == parentSheet->baseURL().string())
100 ResourceLoaderOptions fetchOptions(ResourceFetcher::defaultResourceOptions());
101 FetchRequest request(ResourceRequest(fetcher->document()->completeURL(absHref)), FetchInitiatorTypeNames::xml, fetchOptions);
102 request.setOriginRestriction(FetchRequest::RestrictToSameOrigin);
103 ResourcePtr<Resource> resource = fetcher->fetchSynchronously(request);
107 ASSERT(!m_styleSheet);
108 if (SharedBuffer* data = resource->resourceBuffer())
109 setXSLStyleSheet(absHref, parentSheet->baseURL(), UTF8Encoding().decode(data->data(), data->size()));
112 void XSLImportRule::trace(Visitor* visitor)
114 visitor->trace(m_parentStyleSheet);
115 visitor->trace(m_styleSheet);