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 java.net.URL;
096:
097: import org.apache.batik.css.engine.CSSStyleSheetNode;
098: import org.apache.batik.css.engine.StyleSheet;
099: import org.apache.xerces.dom.CoreDocumentImpl;
100: import org.w3c.dom.DOMException;
101: import org.w3c.dom.Document;
102: import org.w3c.dom.Element;
103: import org.w3c.dom.Node;
104: import org.w3c.dom.events.Event;
105: import org.w3c.dom.events.EventListener;
106:
107: import org.netbeans.modules.visualweb.designer.html.HtmlAttribute;
108: import org.netbeans.modules.visualweb.designer.html.HtmlTag;
109:
110: /**
111: * This class is derived from org.apache.batik.dom.svg.SVGOMStyleElement,
112: * so I've left the Batik copyright on it.
113: * This class represents a <style> element in the DOM.
114: *
115: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
116: * @author Tor Norbye
117: */
118: class RenderedStyleElement extends RaveElement implements
119: CSSStyleSheetNode, StyleRefreshable {
120:
121: /**
122: *
123: */
124: private static final long serialVersionUID = 4050484529888048693L;
125:
126: public RenderedStyleElement(CoreDocumentImpl ownerDocument,
127: String namespaceURI, String qualifiedName)
128: throws DOMException {
129: // XXX Is this redundant? Should we just hardcode
130: // HtmlTag.STYLE.toString() here and not have it passed in?
131: super (ownerDocument, namespaceURI, qualifiedName);
132: }
133:
134: /**
135: * The DOM CSS style-sheet.
136: */
137: protected transient StyleSheet styleSheet;
138:
139: public void refresh() {
140: styleSheet = null;
141: }
142:
143: /**
144: * The listener used to track the content changes.
145: */
146: protected transient EventListener domCharacterDataModifiedListener = new DOMCharacterDataModifiedListener();
147:
148: /**
149: * <b>DOM</b>: Implements {@link org.w3c.dom.Node#getLocalName()}.
150: */
151: public String getLocalName() {
152: return HtmlTag.STYLE.name;
153: }
154:
155: // /** Get the text or comment text children of this element, which
156: // * should correspond to style rules.
157: // */
158: // public static String getStyleText(Element e) {
159: // String text = "";
160: // Node n = e.getFirstChild();
161: // if (n != null) {
162: // StringBuffer sb = new StringBuffer();
163: // while (n != null) {
164: // if (n.getNodeType() == Node.CDATA_SECTION_NODE
165: // // Unlike javascript, where the first line in a comment should be treated
166: // // as a comment, the browsers seem to treat all comment text as style rules
167: // || n.getNodeType() == Node.COMMENT_NODE
168: //
169: // || n.getNodeType() == Node.TEXT_NODE)
170: // // XXX should pick up comments contents too!!
171: // sb.append(n.getNodeValue());
172: // n = n.getNextSibling();
173: // }
174: // text = sb.toString();
175: // // Strip out comments?
176: // }
177: // return text;
178: // }
179:
180: /**
181: * Returns the associated style-sheet.
182: */
183: public StyleSheet getCSSStyleSheet() {
184: if (styleSheet == null) {
185: String type = getType();
186: if (type.length() == 0 || type.equals("text/css")) {
187: // RaveDocument doc = (RaveDocument)getOwnerDocument();
188: // CSSEngine e = doc.getCssEngine();
189: Document doc = getOwnerDocument();
190: // CSSEngine e = CssEngineServiceProvider.getDefault().getCssEngine(doc);
191:
192: String text = MarkupServiceImpl.getStyleText(this );
193: URL burl = MarkupUtilities.getCascadedXMLBase(this );
194: if (burl != null) {
195: String media = getAttributeNS(null,
196: HtmlAttribute.MEDIA);
197: // styleSheet = e.parseStyleSheet(text, burl, media, this);
198: styleSheet = CssProvider.getEngineService()
199: .parseStyleSheetForDocument(doc, text,
200: burl, media, this );
201: addEventListener("DOMCharacterDataModified",
202: domCharacterDataModifiedListener, false);
203: }
204: }
205: }
206: return styleSheet;
207: }
208:
209: /**
210: * <b>DOM</b>: Implements {@link SVGStyleElement#getXMLspace()}.
211: */
212: /*
213: public String getXMLspace() {
214: return XMLSupport.getXMLSpace(this);
215: }
216: */
217:
218: /**
219: * <b>DOM</b>: Implements {@link SVGStyleElement#setXMLspace(String)}.
220: */
221: /*
222: public void setXMLspace(String space) throws DOMException {
223: setAttributeNS(XMLSupport.XML_NAMESPACE_URI,
224: XMLSupport.XML_SPACE_ATTRIBUTE,
225: space);
226: }
227: */
228:
229: /**
230: * <b>DOM</b>: Implements {@link SVGStyleElement#getType()}.
231: */
232: public String getType() {
233: return getAttributeNS(null, HtmlAttribute.TYPE);
234: }
235:
236: /**
237: * <b>DOM</b>: Implements {@link SVGStyleElement#setType(String)}.
238: */
239: /*
240: public void setType(String type) throws DOMException {
241: setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
242: }
243: */
244:
245: /**
246: * The DOMCharacterDataModified listener.
247: */
248: protected class DOMCharacterDataModifiedListener implements
249: EventListener {
250: public void handleEvent(Event evt) {
251: styleSheet = null;
252: }
253: }
254: }
|