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-2006 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: package org.netbeans.modules.css.actions;
042:
043: import java.awt.event.ActionEvent;
044: import java.net.*;
045: import java.io.*;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.text.JTextComponent;
049: import org.netbeans.editor.BaseAction;
050: import org.netbeans.editor.BaseDocument;
051: import org.netbeans.editor.Utilities;
052: import org.netbeans.modules.editor.NbEditorUtilities;
053:
054: import org.openide.*;
055: import org.openide.awt.StatusDisplayer;
056: import org.openide.loaders.DataObject;
057: import org.openide.nodes.Node;
058: import org.openide.util.*;
059: import org.openide.util.actions.*;
060: import org.openide.filesystems.*;
061: import org.openide.cookies.*;
062:
063: import org.w3c.css.sac.*;
064:
065: import org.netbeans.modules.css.*;
066:
067: /**
068: * Action that reparses stylesheet and reports any syntax errors.
069: *
070: * @author Petr Kuzel
071: * @author Marek Fukala
072: */
073: public class CheckStyleAction extends BaseAction implements
074: ErrorHandler, DocumentHandler {
075:
076: public static final String checkStyleAction = NbBundle.getMessage(
077: CheckStyleAction.class, "NAME_check_CSS"); // NOI18N
078:
079: public CheckStyleAction() {
080: super (checkStyleAction);
081: putValue("helpID", CheckStyleAction.class.getName()); // NOI18N
082: putValue(SHORT_DESCRIPTION, checkStyleAction);
083: putValue(ICON_RESOURCE_PROPERTY,
084: "org/netbeans/modules/css/resources/checkStyleAction.gif"); // NOI18N
085: }
086:
087: //check status
088: private boolean failed;
089: private int warnings;
090:
091: private DataObject csso;
092: private CSSDisplayer disp; //and its displayer
093:
094: public void actionPerformed(ActionEvent evt, JTextComponent target) {
095:
096: failed = false;
097: warnings = 0;
098:
099: disp = new CSSDisplayer();
100:
101: Parser parser = new org.w3c.flute.parser.Parser();
102: parser.setErrorHandler(this );
103: parser.setDocumentHandler(this );
104:
105: BaseDocument bdoc = Utilities.getDocument(target);
106: if (bdoc == null) {
107: return; //no document?!?!
108: }
109: csso = NbEditorUtilities.getDataObject(bdoc);
110: if (csso == null) {
111: return; //document not backuped by DataObject
112: }
113:
114: try {
115: //save it first
116: SaveCookie cake = csso.getCookie(SaveCookie.class);
117: if (cake != null)
118: cake.save();
119:
120: String uri = csso.getPrimaryFile().getURL()
121: .toExternalForm();
122:
123: parser.parseStyleSheet(uri);
124: } catch (IOException ex) {
125: // ??? provide better feedback TopManager.getDefault().getErrorManager().notify(ex);
126: failed = true;
127: } catch (CSSParseException ex) {
128: // ??? provide better feedback
129: failed = true;
130: } catch (Throwable t) {
131: disp.display("Unexpected exception from CSS parser: "
132: + t.getMessage()); //NOI18N
133: Logger.global.log(Level.INFO,
134: "Unexpected exception from CSS parser", t); //NOI18N
135: failed = true;
136: }
137:
138: if ((failed == true) || (warnings > 0)) {
139: disp.moveToFront();
140: }
141: StatusDisplayer.getDefault().setStatusText(
142: NbBundle.getMessage(XMLDisplayer.class,
143: "TEXT_PART_CSS_checking")
144: + " " + getStatus() + "."); // NOI18N
145: }
146:
147: private String getStatus() {
148: return failed ? NbBundle.getMessage(XMLDisplayer.class,
149: "TEXT_PART_failed") : warnings > 1 ? NbBundle
150: .getMessage(XMLDisplayer.class,
151: "TEXT_PART_finished_with_warnings") : NbBundle
152: .getMessage(XMLDisplayer.class, "TEXT_PART_finished");
153: }
154:
155: // ~~~~~~~~~~~~~~~~~~~~~ PARSER LISTENER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156:
157: public void warning(CSSParseException exception)
158: throws CSSException {
159: disp.display(csso, exception);
160: warnings++;
161: }
162:
163: public void error(CSSParseException exception) throws CSSException {
164: disp.display(csso, exception);
165: failed = true;
166: }
167:
168: public void fatalError(CSSParseException exception)
169: throws CSSException {
170: disp.display(csso, exception);
171: failed = true;
172: }
173:
174: // ~~~~~~~~~~~~~~~~~ VOID ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
175:
176: public void startDocument(InputSource source) throws CSSException {
177: }
178:
179: public void endDocument(InputSource source) throws CSSException {
180: }
181:
182: public void comment(String text) throws CSSException {
183: }
184:
185: public void ignorableAtRule(String atRule) throws CSSException {
186: }
187:
188: public void namespaceDeclaration(String prefix, String uri)
189: throws CSSException {
190: }
191:
192: public void importStyle(String uri, SACMediaList media,
193: String defaultNamespaceURI) throws CSSException {
194: }
195:
196: public void startMedia(SACMediaList media) throws CSSException {
197: }
198:
199: public void endMedia(SACMediaList media) throws CSSException {
200: }
201:
202: public void startPage(String name, String pseudo_page)
203: throws CSSException {
204: }
205:
206: public void endPage(String name, String pseudo_page)
207: throws CSSException {
208: }
209:
210: public void startFontFace() throws CSSException {
211: }
212:
213: public void endFontFace() throws CSSException {
214: }
215:
216: public void startSelector(SelectorList selectors)
217: throws CSSException {
218: }
219:
220: public void endSelector(SelectorList selectors) throws CSSException {
221: }
222:
223: public void property(String name, LexicalUnit value,
224: boolean important) throws CSSException {
225: }
226: }
|