コード例 #1
0
  /**
   * Locates the array literal specified by the <code>array</code> node, in the source using the
   * source location information provided by the node, and inserts <code>str</code> as a string
   * literal (quoted) in the source at the end of the array.
   *
   * @param array Node with source position information
   * @param str the string to insert into the source at the end of the specified array
   */
  public void appendToArrayLit(Node array, String str) {
    final String sourceMethod = "appendToArrayLit"; // $NON-NLS-1$
    if (isTraceLogging) {
      log.entering(JSSource.class.getName(), sourceMethod, new Object[] {array, str});
    }
    // Get the node for the last element in the array
    Node lastChild = array.getLastChild();

    // If token is not a string, then punt
    if (lastChild.getType() != Token.STRING && lastChild.getType() != Token.NAME) {
      if (log.isLoggable(Level.WARNING)) {
        log.warning(
            "Last element of array at "
                + mid
                + "("
                + array.getLineno()
                + ","
                + array.getCharno()
                + ") is type "
                + array.getType()); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
      if (isTraceLogging) {
        log.exiting(JSSource.class.getName(), sourceMethod);
      }
      return;
    }
    int len =
        lastChild.getString().length()
            + (lastChild.getType() == Token.STRING ? 2 : 0); // add 2 for string quotes

    // Search the source for the closing array bracket ']', skipping over
    // whitespace and comments.  In order to be valid javascript, the next
    // token must be the closing bracket.
    int lineno = lastChild.getLineno();
    int charno = lastChild.getCharno() + len;
    PositionLocator pos = new PositionLocator(lineno, charno);
    if (pos.findNextJSToken() == ']') {
      insert(",\"" + str + "\"", pos.getLineno(), pos.getCharno()); // $NON-NLS-1$ //$NON-NLS-2$
    } else {
      if (log.isLoggable(Level.WARNING)) {
        log.warning(
            "Closing array bracket not found in "
                + mid
                + "("
                + lineno
                + ","
                + charno
                + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
    }
    if (isTraceLogging) {
      log.exiting(JSSource.class.getName(), sourceMethod);
    }
  }
コード例 #2
0
 /**
  * Inserts the specified string into the source at the specified location
  *
  * @param str the string to insert
  * @param lineno the line number to insert to
  * @param colno the column number to insert to
  */
 public void insert(String str, int lineno, int colno) {
   final String sourceMethod = "insert"; // $NON-NLS-1$
   if (isTraceLogging) {
     log.entering(JSSource.class.getName(), sourceMethod, new Object[] {str, lineno, colno});
   }
   PositionLocator locator = new PositionLocator(lineno, colno);
   locator.insert(str);
   if (isTraceLogging) {
     log.exiting(
         JSSource.class.getName(),
         sourceMethod,
         "String \""
             + str
             + "\" inserted at "
             + mid
             + "("
             + lineno
             + ","
             + colno
             + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
   }
 }