/**
  * Sets the string value of this literal node to the given string literal token. The token is the
  * sequence of characters that would appear in the source program, including enclosing double
  * quotes and embedded escapes. For example,
  *
  * <ul>
  *   <li><code>""</code> <code>setLiteral("\"\"")</code>
  *   <li><code>"hello world"</code> <code>setLiteral("\"hello world\"")</code>
  *   <li><code>"boo\nhoo"</code> <code>setLiteral("\"boo\\nhoo\"")</code>
  * </ul>
  *
  * @param token the string literal token, including enclosing double quotes and embedded escapes
  * @exception IllegalArgumentException if the argument is incorrect
  */
 public void setEscapedValue(String token) {
   // update internalSetEscapedValue(String) if this is changed
   if (token == null) {
     throw new IllegalArgumentException("Token cannot be null"); // $NON-NLS-1$
   }
   Scanner scanner = this.ast.scanner;
   char[] source = token.toCharArray();
   scanner.setSource(source);
   scanner.resetTo(0, source.length);
   try {
     int tokenType = scanner.getNextToken();
     switch (tokenType) {
       case TerminalTokens.TokenNameStringLiteral:
         break;
       default:
         throw new IllegalArgumentException(
             "Invalid string literal : >" + token + "<"); // $NON-NLS-1$//$NON-NLS-2$
     }
   } catch (InvalidInputException e) {
     throw new IllegalArgumentException(
         "Invalid string literal : >" + token + "<"); // $NON-NLS-1$//$NON-NLS-2$
   }
   preValueChange(ESCAPED_VALUE_PROPERTY);
   this.escapedValue = token;
   postValueChange(ESCAPED_VALUE_PROPERTY);
 }
예제 #2
0
 /**
  * Sets or clears the leading comment string. The comment string must include the starting and
  * ending comment delimiters, and any embedded linebreaks.
  *
  * <p>A leading comment is a comment that appears before the statement. It may be either a
  * traditional comment or an end-of-line comment. Traditional comments must begin with "/&#42;,
  * may contain line breaks, and must end with "&#42;/. End-of-line comments must begin with "//"
  * (as per JLS 3.7), and must not contain line breaks.
  *
  * <p>Examples: <code>
  * <pre>
  * setLeadingComment("/&#42; traditional comment &#42;/");  // correct
  * setLeadingComment("missing comment delimiters");  // wrong
  * setLeadingComment("/&#42; unterminated traditional comment ");  // wrong
  * setLeadingComment("/&#42; broken\n traditional comment &#42;/");  // correct
  * setLeadingComment("// end-of-line comment\n");  // correct
  * setLeadingComment("// end-of-line comment without line terminator");  // correct
  * setLeadingComment("// broken\n end-of-line comment\n");  // wrong
  * </pre>
  * </code>
  *
  * @param comment the comment string, or <code>null</code> if none
  * @exception IllegalArgumentException if the comment string is invalid
  * @deprecated This feature was removed in the 2.1 release because it was only a partial, and
  *     inadequate, solution to the issue of associating comments with statements.
  */
 public void setLeadingComment(String comment) {
   if (comment != null) {
     char[] source = comment.toCharArray();
     Scanner scanner = this.ast.scanner;
     scanner.resetTo(0, source.length);
     scanner.setSource(source);
     try {
       int token;
       boolean onlyOneComment = false;
       while ((token = scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
         switch (token) {
           case TerminalTokens.TokenNameCOMMENT_BLOCK:
           case TerminalTokens.TokenNameCOMMENT_JAVADOC:
           case TerminalTokens.TokenNameCOMMENT_LINE:
             if (onlyOneComment) {
               throw new IllegalArgumentException();
             }
             onlyOneComment = true;
             break;
           default:
             onlyOneComment = false;
         }
       }
       if (!onlyOneComment) {
         throw new IllegalArgumentException();
       }
     } catch (InvalidInputException e) {
       throw new IllegalArgumentException();
     }
   }
   // we do not consider the obsolete comment as a structureal property
   // but we protect them nevertheless
   checkModifiable();
   this.optionalLeadingComment = comment;
 }
  /**
   * Returns the value of this literal node.
   *
   * <p>For example,
   *
   * <pre>
   * StringLiteral s;
   * s.setEscapedValue("\"hello\\nworld\"");
   * assert s.getLiteralValue().equals("hello\nworld");
   * </pre>
   *
   * <p>Note that this is a convenience method that converts from the stored string literal token
   * returned by <code>getEscapedLiteral</code>.
   *
   * @return the string value without enclosing double quotes and embedded escapes
   * @exception IllegalArgumentException if the literal value cannot be converted
   */
  public String getLiteralValue() {
    String s = getEscapedValue();
    int len = s.length();
    if (len < 2 || s.charAt(0) != '\"' || s.charAt(len - 1) != '\"') {
      throw new IllegalArgumentException();
    }

    Scanner scanner = this.ast.scanner;
    char[] source = s.toCharArray();
    scanner.setSource(source);
    scanner.resetTo(0, source.length);
    try {
      int tokenType = scanner.getNextToken();
      switch (tokenType) {
        case TerminalTokens.TokenNameStringLiteral:
          return scanner.getCurrentStringLiteral();
        default:
          throw new IllegalArgumentException();
      }
    } catch (InvalidInputException e) {
      throw new IllegalArgumentException();
    }
  }