Home | History | Annotate | Download | only in xml
      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 "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 (m_styleSheet)
     44         m_styleSheet->setParentStyleSheet(0);
     45 
     46     if (m_resource)
     47         m_resource->removeClient(this);
     48 }
     49 
     50 void 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 
     68 bool XSLImportRule::isLoading()
     69 {
     70     return (m_loading || (m_styleSheet && m_styleSheet->isLoading()));
     71 }
     72 
     73 void XSLImportRule::loadSheet()
     74 {
     75     ResourceFetcher* fetcher = 0;
     76 
     77     XSLStyleSheet* rootSheet = parentStyleSheet();
     78 
     79     if (rootSheet) {
     80         while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet())
     81             rootSheet = parentSheet;
     82     }
     83 
     84     if (rootSheet)
     85         fetcher = rootSheet->fetcher();
     86 
     87     String absHref = m_strHref;
     88     XSLStyleSheet* parentSheet = parentStyleSheet();
     89     if (!parentSheet->baseURL().isNull())
     90         // use parent styleheet's URL as the base URL
     91         absHref = KURL(parentSheet->baseURL(), m_strHref).string();
     92 
     93     // Check for a cycle in our import chain.  If we encounter a stylesheet
     94     // in 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     FetchRequest request(ResourceRequest(fetcher->document()->completeURL(absHref)), FetchInitiatorTypeNames::xml);
    101     m_resource = fetcher->fetchXSLStyleSheet(request);
    102 
    103     if (m_resource) {
    104         m_resource->addClient(this);
    105 
    106         // If the imported sheet is in the cache, then setXSLStyleSheet gets called,
    107         // and the sheet even gets parsed (via parseString).  In this case we have
    108         // loaded (even if our subresources haven't), so if we have a stylesheet after
    109         // checking the cache, then we've clearly loaded.
    110         if (!m_styleSheet)
    111             m_loading = true;
    112     }
    113 }
    114 
    115 } // namespace WebCore
    116