View Javadoc

    1   package net.sourceforge.pmd.lang.ecmascript;
    2
    3   import net.sourceforge.pmd.Rule;
    4   import net.sourceforge.pmd.lang.ParserOptions;
    5   import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
    6   import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
    7   import net.sourceforge.pmd.util.StringUtil;
    8
    9   import org.mozilla.javascript.Context;
    10
    11  public class EcmascriptParserOptions extends ParserOptions {
    12
    13      public enum Version {
    14  	VERSION_DEFAULT("default", Context.VERSION_DEFAULT),
    15  	VERSION_1_0("1.0", Context.VERSION_1_0),
    16  	VERSION_1_1("1.1", Context.VERSION_1_1),
    17  	VERSION_1_2("1.2", Context.VERSION_1_2),
    18  	VERSION_1_3("1.3", Context.VERSION_1_3),
    19  	VERSION_1_4("1.4", Context.VERSION_1_4),
    20  	VERSION_1_5("1.5", Context.VERSION_1_5),
    21  	VERSION_1_6("1.6", Context.VERSION_1_6),
    22  	VERSION_1_7("1.7", Context.VERSION_1_7),
    23  	VERSION_1_8("1.8", Context.VERSION_1_8);
    24
    25  	private final String name;
    26  	private final int version;
    27
    28  	private Version(String name, int version) {
    29  	    this.name = name;
    30  	    this.version = version;
    31  	}
    32
    33  	public String getLabel() {
    34  	    return name;
    35  	}
    36
    37  	public int getVersion() {
    38  	    return version;
    39  	}
    40      };
    41
    42      private static final String[] VERSION_LABELS = new String[] { Version.VERSION_DEFAULT.getLabel(),
    43  	    Version.VERSION_1_0.getLabel(), Version.VERSION_1_1.getLabel(), Version.VERSION_1_2.getLabel(),
    44  	    Version.VERSION_1_3.getLabel(), Version.VERSION_1_4.getLabel(), Version.VERSION_1_5.getLabel(),
    45  	    Version.VERSION_1_6.getLabel(), Version.VERSION_1_7.getLabel(), Version.VERSION_1_8.getLabel(), };
    46
    47      // Note: The UI order values are chosen to be larger than those built into XPathRule.
    48      public static final BooleanProperty RECORDING_COMMENTS_DESCRIPTOR = new BooleanProperty("recordingComments",
    49  	    "Specifies that comments are produced in the AST.", Boolean.TRUE, 3.0f);
    50      public static final BooleanProperty RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR = new BooleanProperty(
    51  	    "recordingLocalJsDocComments", "Specifies that JsDoc comments are produced in the AST.", Boolean.TRUE, 4.0f);
    52      public static final EnumeratedProperty<Version> RHINO_LANGUAGE_VERSION = new EnumeratedProperty<Version>(
    53  	    "rhinoLanguageVersion",
    54  	    "Specifies the Rhino Language Version to use for parsing.  Defaults to Rhino default.", VERSION_LABELS,
    55  	    Version.values(), 0, 5.0f);
    56
    57      private boolean recordingComments;
    58      private boolean recordingLocalJsDocComments;
    59      private Version rhinoLanguageVersion;
    60
    61      public EcmascriptParserOptions() {
    62  	this.recordingComments = RECORDING_COMMENTS_DESCRIPTOR.defaultValue().booleanValue();
    63  	this.recordingLocalJsDocComments = RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR.defaultValue().booleanValue();
    64  	this.rhinoLanguageVersion = (Version) RHINO_LANGUAGE_VERSION.valueFrom((String) RHINO_LANGUAGE_VERSION
    65  		.defaultValue());
    66      }
    67
    68      public EcmascriptParserOptions(Rule rule) {
    69  	this.recordingComments = rule.getProperty(RECORDING_COMMENTS_DESCRIPTOR);
    70  	this.recordingLocalJsDocComments = rule.getProperty(RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR);
    71  	this.rhinoLanguageVersion = (Version) RHINO_LANGUAGE_VERSION.valueFrom((String) rule
    72  		.getProperty(RHINO_LANGUAGE_VERSION));
    73      }
    74
    75      public boolean isRecordingComments() {
    76  	return this.recordingComments;
    77      }
    78
    79      public void setRecordingComments(boolean recordingComments) {
    80  	this.recordingComments = recordingComments;
    81      }
    82
    83      public boolean isRecordingLocalJsDocComments() {
    84  	return this.recordingLocalJsDocComments;
    85      }
    86
    87      public void setRecordingLocalJsDocComments(boolean recordingLocalJsDocComments) {
    88  	this.recordingLocalJsDocComments = recordingLocalJsDocComments;
    89      }
    90
    91      public Version getRhinoLanguageVersion() {
    92  	return this.rhinoLanguageVersion;
    93      }
    94
    95      public void setRhinoLanguageVersion(Version rhinoLanguageVersion) {
    96  	this.rhinoLanguageVersion = rhinoLanguageVersion;
    97      }
    98
    99      @Override
    100     public int hashCode() {
    101 	final int prime = 31;
    102 	int result = super.hashCode();
    103 	result = prime * result + (recordingComments ? 1231 : 1237);
    104 	result = prime * result + (recordingLocalJsDocComments ? 1231 : 1237);
    105 	result = prime * result + ((rhinoLanguageVersion == null) ? 0 : rhinoLanguageVersion.hashCode());
    106 	return result;
    107     }
    108
    109     @Override
    110     public boolean equals(Object obj) {
    111 	if (this == obj) {
    112 	    return true;
    113 	}
    114 	if (obj == null || getClass() != obj.getClass()) {
    115 	    return false;
    116 	}
    117 	final EcmascriptParserOptions that = (EcmascriptParserOptions) obj;
    118 	return StringUtil.isSame(this.suppressMarker, that.suppressMarker, false, false, false)
    119 		&& this.recordingComments == that.recordingComments
    120 		&& this.recordingLocalJsDocComments == that.recordingLocalJsDocComments
    121 		&& this.rhinoLanguageVersion == that.rhinoLanguageVersion;
    122     }
    123 }