001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042:
043: ============================================================================
044: The Apache Software License, Version 1.1
045: ============================================================================
046:
047: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
048:
049: Redistribution and use in source and binary forms, with or without modifica-
050: tion, are permitted provided that the following conditions are met:
051:
052: 1. Redistributions of source code must retain the above copyright notice,
053: this list of conditions and the following disclaimer.
054:
055: 2. Redistributions in binary form must reproduce the above copyright notice,
056: this list of conditions and the following disclaimer in the documentation
057: and/or other materials provided with the distribution.
058:
059: 3. The end-user documentation included with the redistribution, if any, must
060: include the following acknowledgment: "This product includes software
061: developed by the Apache Software Foundation (http://www.apache.org/)."
062: Alternately, this acknowledgment may appear in the software itself, if
063: and wherever such third-party acknowledgments normally appear.
064:
065: 4. The names "Batik" and "Apache Software Foundation" must not be
066: used to endorse or promote products derived from this software without
067: prior written permission. For written permission, please contact
068: apache@apache.org.
069:
070: 5. Products derived from this software may not be called "Apache", nor may
071: "Apache" appear in their name, without prior written permission of the
072: Apache Software Foundation.
073:
074: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
075: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
076: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
077: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
078: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
079: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
080: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
081: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
082: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
083: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
084:
085: This software consists of voluntary contributions made by many individuals
086: on behalf of the Apache Software Foundation. For more information on the
087: Apache Software Foundation, please see <http://www.apache.org/>.
088:
089: */
090:
091: package org.netbeans.modules.visualweb.designer.markup;
092:
093: import org.netbeans.modules.visualweb.api.designer.cssengine.CssProvider;
094: import org.netbeans.modules.visualweb.api.designer.cssengine.StyleRefreshable;
095: import org.netbeans.modules.visualweb.api.insync.InSyncService;
096: import java.net.URL;
097:
098: import org.apache.batik.css.engine.CSSStyleSheetNode;
099: import org.apache.batik.css.engine.StyleSheet;
100: import org.apache.xerces.dom.CoreDocumentImpl;
101: import org.w3c.dom.DOMException;
102:
103: import org.netbeans.modules.visualweb.designer.html.HtmlAttribute;
104: import org.w3c.dom.Document;
105:
106: /**
107: * This class is derived from
108: * org.apache.batik.dom.svg.SVGStyleSheetProcessingInstruction so I've
109: * left the Batik copyright on it. This class represents a
110: * <link> element with a stylesheet rel in the DOM.
111: * (The original class was a ProcessingInstruction; according to
112: * http://www.w3.org/TR/xml-stylesheet/ we should be supporting that,
113: * but I don't see any mention in the XHTML spec, and Mozilla doesn't
114: * handle it well on a test I tried. So I guess we'll return to this.
115: *
116: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
117: * @author Tor Norbye
118: */
119: class RenderedStylesheetLinkElement extends RaveElement implements
120: CSSStyleSheetNode, StyleRefreshable {
121:
122: /**
123: *
124: */
125: private static final long serialVersionUID = 3905243420396959284L;
126:
127: public RenderedStylesheetLinkElement(
128: CoreDocumentImpl ownerDocument, String namespaceURI,
129: String qualifiedName) throws DOMException {
130: // XXX Is this redundant? Should we just hardcode CSS_LINK_TAG here
131: // and not have it passed in?
132: super (ownerDocument, namespaceURI, qualifiedName);
133: }
134:
135: /**
136: * The style-sheet.
137: */
138: protected StyleSheet styleSheet;
139:
140: public void refresh() {
141: styleSheet = null;
142: }
143:
144: /**
145: * Returns the associated style-sheet.
146: */
147: public StyleSheet getCSSStyleSheet() {
148: if (styleSheet == null) {
149: String rel = getAttribute(HtmlAttribute.REL);
150: if (rel.equalsIgnoreCase("stylesheet")) {
151: String type = getAttribute(HtmlAttribute.TYPE);
152: if (type.length() == 0
153: || type.equalsIgnoreCase("text/css")) {
154: String title = getAttribute(HtmlAttribute.TITLE);
155: String media = getAttribute(HtmlAttribute.MEDIA);
156: String href = getAttribute(HtmlAttribute.HREF);
157: String alternate = getAttribute("alternate");
158: // RaveDocument doc = (RaveDocument)getOwnerDocument();
159: // URL durl = doc.getUrl();
160: Document doc = getOwnerDocument();
161: URL durl = InSyncService.getProvider().getUrl(doc);
162: URL burl = InSyncService.getProvider().resolveUrl(
163: durl, doc, href);
164:
165: // CSSEngine e = doc.getCssEngine();
166: // CSSEngine e = CssEngineServiceProvider.getDefault().getCssEngine(doc);
167: // styleSheet = e.parseStyleSheet
168: // (burl, media, burl);
169: styleSheet = CssProvider.getEngineService()
170: .parseStyleSheetForDocument(doc, burl,
171: media, burl);
172:
173: boolean isAlternate = "yes".equals(alternate);
174: styleSheet.setAlternate(isAlternate);
175: if (title.length() == 0 && !isAlternate) {
176: // If a title wasn't specified at least provide a somewhat useful one
177: title = href;
178: }
179: styleSheet.setTitle(title);
180: styleSheet.setupFilters();
181: }
182: }
183: }
184: return styleSheet;
185: }
186:
187: /**
188: * <b>DOM</b>: Implements {@link
189: * org.w3c.dom.ProcessingInstruction#setData(String)}.
190: */
191: /*
192: public void setData(String data) throws DOMException {
193: super.setData(data);
194: styleSheet = null;
195: }
196: */
197:
198: public String toString() {
199: return super .toString() + "[href="
200: + getAttribute(HtmlAttribute.HREF) + "]";
201: }
202: }
|