1/*
2 * This file is part of the XSL implementation.
3 *
4 * Copyright (C) 2004, 2006, 2008, 2012 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
23#ifndef XSLStyleSheet_h
24#define XSLStyleSheet_h
25
26#if ENABLE(XSLT)
27
28#include "ProcessingInstruction.h"
29#include "StyleSheet.h"
30
31#if !USE(QXMLQUERY)
32#include <libxml/parser.h>
33#include <libxslt/transform.h>
34#endif
35
36#include <wtf/PassRefPtr.h>
37
38namespace WebCore {
39
40class CachedResourceLoader;
41class XSLImportRule;
42
43class XSLStyleSheet : public StyleSheet {
44public:
45#if !USE(QXMLQUERY)
46    static PassRefPtr<XSLStyleSheet> create(XSLImportRule* parentImport, const String& originalURL, const KURL& finalURL)
47    {
48        return adoptRef(new XSLStyleSheet(parentImport, originalURL, finalURL));
49    }
50#endif
51    static PassRefPtr<XSLStyleSheet> create(ProcessingInstruction* parentNode, const String& originalURL, const KURL& finalURL)
52    {
53        return adoptRef(new XSLStyleSheet(parentNode, originalURL, finalURL, false));
54    }
55    static PassRefPtr<XSLStyleSheet> createEmbedded(ProcessingInstruction* parentNode, const KURL& finalURL)
56    {
57        return adoptRef(new XSLStyleSheet(parentNode, finalURL.string(), finalURL, true));
58    }
59
60    // Taking an arbitrary node is unsafe, because owner node pointer can become stale.
61    // XSLTProcessor ensures that the stylesheet doesn't outlive its parent, in part by not exposing it to JavaScript.
62    static PassRefPtr<XSLStyleSheet> createForXSLTProcessor(Node* parentNode, const String& originalURL, const KURL& finalURL)
63    {
64        return adoptRef(new XSLStyleSheet(parentNode, originalURL, finalURL, false));
65    }
66
67    virtual ~XSLStyleSheet();
68
69    bool parseString(const String&);
70
71    void checkLoaded();
72
73    const KURL& finalURL() const { return m_finalURL; }
74
75    void loadChildSheets();
76    void loadChildSheet(const String& href);
77
78    CachedResourceLoader* cachedResourceLoader();
79
80    Document* ownerDocument();
81    virtual XSLStyleSheet* parentStyleSheet() const OVERRIDE { return m_parentStyleSheet; }
82    void setParentStyleSheet(XSLStyleSheet* parent);
83
84#if USE(QXMLQUERY)
85    String sheetString() const { return m_sheetString; }
86#else
87    xmlDocPtr document();
88    xsltStylesheetPtr compileStyleSheet();
89    xmlDocPtr locateStylesheetSubResource(xmlDocPtr parentDoc, const xmlChar* uri);
90#endif
91
92    void clearDocuments();
93
94    void markAsProcessed();
95    bool processed() const { return m_processed; }
96
97    virtual String type() const OVERRIDE { return "text/xml"; }
98    virtual bool disabled() const OVERRIDE { return m_isDisabled; }
99    virtual void setDisabled(bool b) OVERRIDE { m_isDisabled = b; }
100    virtual Node* ownerNode() const OVERRIDE { return m_ownerNode; }
101    virtual String href() const OVERRIDE { return m_originalURL; }
102    virtual String title() const OVERRIDE { return emptyString(); }
103
104    virtual void clearOwnerNode() OVERRIDE { m_ownerNode = 0; }
105    virtual KURL baseURL() const OVERRIDE { return m_finalURL; }
106    virtual bool isLoading() const OVERRIDE;
107
108    virtual bool isXSLStyleSheet() const OVERRIDE { return true; }
109
110private:
111    XSLStyleSheet(Node* parentNode, const String& originalURL, const KURL& finalURL, bool embedded);
112#if !USE(QXMLQUERY)
113    XSLStyleSheet(XSLImportRule* parentImport, const String& originalURL, const KURL& finalURL);
114#endif
115
116    Node* m_ownerNode;
117    String m_originalURL;
118    KURL m_finalURL;
119    bool m_isDisabled;
120
121    Vector<OwnPtr<XSLImportRule> > m_children;
122
123    bool m_embedded;
124    bool m_processed;
125
126#if USE(QXMLQUERY)
127    String m_sheetString;
128#else
129    xmlDocPtr m_stylesheetDoc;
130    bool m_stylesheetDocTaken;
131#endif
132
133    XSLStyleSheet* m_parentStyleSheet;
134};
135
136} // namespace WebCore
137
138#endif // ENABLE(XSLT)
139
140#endif // XSLStyleSheet_h
141