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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.ruby.hints;
029:
030: import java.util.Collections;
031: import java.util.HashSet;
032: import java.util.List;
033: import java.util.Set;
034: import java.util.prefs.Preferences;
035: import javax.swing.JComponent;
036: import org.jruby.ast.Node;
037: import org.jruby.ast.NodeTypes;
038: import org.jruby.ast.types.INameNode;
039: import org.netbeans.modules.gsf.api.CompilationInfo;
040: import org.netbeans.modules.gsf.api.OffsetRange;
041: import org.netbeans.modules.ruby.AstUtilities;
042: import org.netbeans.modules.ruby.RubyUtils;
043: import org.netbeans.modules.ruby.hints.spi.AstRule;
044: import org.netbeans.modules.ruby.hints.spi.Description;
045: import org.netbeans.modules.ruby.hints.spi.Fix;
046: import org.netbeans.modules.ruby.hints.spi.HintSeverity;
047: import org.netbeans.modules.ruby.hints.spi.RuleContext;
048: import org.netbeans.modules.ruby.lexer.LexUtilities;
049: import org.openide.util.NbBundle;
050:
051: /**
052: * Check identifiers to see if they are "safe" (a-z,A-Z,digits,_). Other multibyte values
053: * can lead to trouble down the road.
054: *
055: * @author Tor Norbye
056: */
057: public class UnsafeIdentifierChars implements AstRule {
058: public UnsafeIdentifierChars() {
059: }
060:
061: public boolean appliesTo(CompilationInfo info) {
062: return true;
063: }
064:
065: public Set<Integer> getKinds() {
066: Set<Integer> integers = new HashSet<Integer>();
067: integers.add(NodeTypes.LOCALASGNNODE);
068: integers.add(NodeTypes.DEFNNODE);
069: integers.add(NodeTypes.DEFSNODE);
070: integers.add(NodeTypes.CONSTDECLNODE);
071: return integers;
072: }
073:
074: public void run(RuleContext context, List<Description> result) {
075: Node node = context.node;
076: CompilationInfo info = context.compilationInfo;
077:
078: String name = ((INameNode) node).getName();
079:
080: if (!RubyUtils.isSafeIdentifierName(name, 0)) {
081: String displayName = NbBundle.getMessage(
082: UnsafeIdentifierChars.class, "InvalidMultibyte");
083: OffsetRange range = AstUtilities.getNameRange(node);
084: List<Fix> fixList = Collections.emptyList();
085: range = LexUtilities.getLexerOffsets(info, range);
086: if (range != OffsetRange.NONE) {
087: Description desc = new Description(this , displayName,
088: info.getFileObject(), range, fixList, 600);
089: result.add(desc);
090: }
091: }
092: }
093:
094: public String getId() {
095: return "Unsafe_Identifier_Chars"; // NOI18N
096: }
097:
098: public String getDisplayName() {
099: return NbBundle.getMessage(UnsafeIdentifierChars.class,
100: "UnsafeIdentifierChars");
101: }
102:
103: public String getDescription() {
104: return NbBundle.getMessage(UnsafeIdentifierChars.class,
105: "UnsafeIdentifierCharsDesc");
106: }
107:
108: public boolean getDefaultEnabled() {
109: return true;
110: }
111:
112: public boolean showInTasklist() {
113: return true;
114: }
115:
116: public HintSeverity getDefaultSeverity() {
117: return HintSeverity.WARNING;
118: }
119:
120: public JComponent getCustomizer(Preferences node) {
121: return null;
122: }
123: }
|