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: package org.netbeans.modules.visualweb.designer.cssengine;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.Reader;
048: import java.io.StringBufferInputStream;
049: import java.net.URI;
050: import java.net.URISyntaxException;
051: import javax.swing.text.BadLocationException;
052: import org.apache.batik.css.parser.Parser;
053: import org.apache.batik.css.parser.Scanner;
054: import org.apache.batik.util.CSSConstants;
055: import org.apache.batik.util.ParsedURL;
056: import org.openide.ErrorManager;
057: import org.openide.cookies.EditorCookie;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.loaders.DataObject;
061: import org.openide.loaders.DataObjectNotFoundException;
062: import org.w3c.css.sac.CSSException;
063: import org.w3c.css.sac.InputSource;
064:
065: /**
066: * XXX Class to get rid of the RAVE modifications
067: * from the original batik code, this case: batik/Parser.
068: *
069: * @author Peter Zavadsky
070: */
071: class RaveParser extends Parser {
072:
073: /** Creates a new instance of Parser */
074: public RaveParser() {
075: }
076:
077: /** XXX Overriden to use the openide facility to get input stream from file.
078: * is that actually correct?
079: * Copied from formerly modified superclass. */
080: protected Scanner createScanner(InputSource source) {
081: documentURI = source.getURI();
082: if (documentURI == null) {
083: documentURI = "";
084: }
085:
086: Reader r = source.getCharacterStream();
087: if (r != null) {
088: return new Scanner(r);
089: }
090:
091: InputStream is = source.getByteStream();
092: if (is != null) {
093: return new Scanner(is, source.getEncoding());
094: }
095:
096: String uri = source.getURI();
097: if (uri == null) {
098: throw new CSSException(formatMessage("empty.source", null));
099: }
100:
101: try {
102: ParsedURL purl = new ParsedURL(uri);
103: // BEGIN RAVE MODIFICATIONS
104: if ("file".equals(purl.getProtocol())) { // NOI18N
105: // is = MarkupService.getOpenCssStream(uri);
106: is = getOpenCssStream(uri);
107: }
108: if (is == null) // NOTE NOTE NOTE: fall through to next line
109: // END RAVE MODIFICATIONS
110: is = purl.openStreamRaw(CSSConstants.CSS_MIME_TYPE);
111: return new Scanner(is, source.getEncoding());
112: } catch (IOException e) {
113: throw new CSSException(e);
114: }
115: }
116:
117: // <rave> Moved from MarkupUtilities.
118: /**
119: * Return an InputStream for the given CSS URI, if the corresponding CSS
120: * file is open and edited. Otherwise return null.
121: *
122: * @param uri The URI to the CSS file. <b>MUST</b> be an absolute file url!
123: * @return An InputStream for the live edited CSS
124: */
125: private static InputStream getOpenCssStream(String uriString) {
126: try {
127: URI uri = new URI(uriString);
128: File file = new File(uri);
129:
130: if (file != null) {
131: FileObject fobj = FileUtil.toFileObject(file);
132:
133: if (fobj != null) {
134: try {
135: DataObject dobj = DataObject.find(fobj);
136: EditorCookie ec = (EditorCookie) dobj
137: .getCookie(EditorCookie.class);
138:
139: if (ec != null) {
140: javax.swing.text.Document doc = ec
141: .getDocument();
142:
143: if (doc != null) {
144: // XXX Isn't there a better way to return an input stream
145: // for a String? Should I have my own?
146: String s = doc.getText(0, doc
147: .getLength());
148:
149: return new StringBufferInputStream(s);
150: }
151: }
152: } catch (BadLocationException ble) {
153: ErrorManager.getDefault().notify(
154: ErrorManager.INFORMATIONAL, ble);
155: } catch (DataObjectNotFoundException dnfe) {
156: ErrorManager.getDefault().notify(
157: ErrorManager.INFORMATIONAL, dnfe);
158: }
159: }
160: }
161: } catch (URISyntaxException e) {
162: ErrorManager.getDefault().notify(e);
163: }
164:
165: return null;
166: }
167: // </rave>
168: }
|