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#include "platform/SharedBuffer.h"
31#include "wtf/text/TextEncoding.h"
32
33namespace blink {
34
35XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href)
36    : m_parentStyleSheet(parent)
37    , m_strHref(href)
38    , m_loading(false)
39{
40}
41
42XSLImportRule::~XSLImportRule()
43{
44#if !ENABLE(OILPAN)
45    if (m_styleSheet)
46        m_styleSheet->setParentStyleSheet(0);
47#endif
48}
49
50void XSLImportRule::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
51{
52    if (m_styleSheet)
53        m_styleSheet->setParentStyleSheet(0);
54
55    m_styleSheet = XSLStyleSheet::create(this, href, baseURL);
56
57    XSLStyleSheet* parent = parentStyleSheet();
58    if (parent)
59        m_styleSheet->setParentStyleSheet(parent);
60
61    m_styleSheet->parseString(sheet);
62    m_loading = false;
63
64    if (parent)
65        parent->checkLoaded();
66}
67
68bool XSLImportRule::isLoading()
69{
70    return m_loading || (m_styleSheet && m_styleSheet->isLoading());
71}
72
73void XSLImportRule::loadSheet()
74{
75    ResourceFetcher* fetcher = 0;
76    XSLStyleSheet* rootSheet = parentStyleSheet();
77
78    if (rootSheet) {
79        while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet())
80            rootSheet = parentSheet;
81    }
82
83    if (rootSheet)
84        fetcher = rootSheet->fetcher();
85
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();
91    }
92
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())
97            return;
98    }
99
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);
104    if (!resource)
105        return;
106
107    ASSERT(!m_styleSheet);
108    if (SharedBuffer* data = resource->resourceBuffer())
109        setXSLStyleSheet(absHref, parentSheet->baseURL(), UTF8Encoding().decode(data->data(), data->size()));
110}
111
112void XSLImportRule::trace(Visitor* visitor)
113{
114    visitor->trace(m_parentStyleSheet);
115    visitor->trace(m_styleSheet);
116}
117
118} // namespace blink
119