001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.el;
018
019/**
020 *
021 * <p>An expression representing a String literal value.
022 * 
023 * @author Nathan Abramson - Art Technology Group
024 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
025 **/
026
027public class StringLiteral
028  extends Literal
029{
030  //-------------------------------------
031  /**
032   *
033   * Constructor
034   **/
035  StringLiteral (Object pValue)
036  {
037    super (pValue);
038  }
039
040  //-------------------------------------
041  /**
042   *
043   * Returns a StringLiteral parsed from the given token (enclosed by
044   * single or double quotes)
045   **/
046  public static StringLiteral fromToken (String pToken)
047  {
048    return new StringLiteral (getValueFromToken (pToken));
049  }
050
051  //-------------------------------------
052  /**
053   *
054   * Returns a StringLiteral with the given string value
055   **/
056  public static StringLiteral fromLiteralValue (String pValue)
057  {
058    return new StringLiteral (pValue);
059  }
060
061  //-------------------------------------
062  /**
063   *
064   * Parses the given token into the literal value
065   **/
066  public static String getValueFromToken (String pToken)
067  {
068    StringBuffer buf = new StringBuffer ();
069    int len = pToken.length () - 1;
070    boolean escaping = false;
071    for (int i = 1; i < len; i++) {
072      char ch = pToken.charAt (i);
073      if (escaping) {
074        buf.append (ch);
075        escaping = false;
076      }
077      else if (ch == '\\') {
078        escaping = true;
079      }
080      else {
081        buf.append (ch);
082      }
083    }
084    return buf.toString ();
085  }
086
087  //-------------------------------------
088  /**
089   *
090   * Converts the specified value to a String token, using " as the
091   * enclosing quotes and escaping any characters that need escaping.
092   **/
093  public static String toStringToken (String pValue)
094  {
095    // See if any escaping is needed
096    if (pValue.indexOf ('\"') < 0 &&
097        pValue.indexOf ('\\') < 0) {
098      return "\"" + pValue + "\"";
099    }
100
101    // Escaping is needed
102    else {
103      StringBuffer buf = new StringBuffer ();
104      buf.append ('\"');
105      int len = pValue.length ();
106      for (int i = 0; i < len; i++) {
107        char ch = pValue.charAt (i);
108        if (ch == '\\') {
109          buf.append ('\\');
110          buf.append ('\\');
111        }
112        else if (ch == '\"') {
113          buf.append ('\\');
114          buf.append ('\"');
115        }
116        else {
117          buf.append (ch);
118        }
119      }
120      buf.append ('\"');
121      return buf.toString ();
122    }
123  }
124
125  //-------------------------------------
126  /**
127   *
128   * Converts the specified value to an identifier token, escaping it
129   * as a string literal if necessary.
130   **/
131  public static String toIdentifierToken (String pValue)
132  {
133    // See if it's a valid java identifier
134    if (isJavaIdentifier (pValue)) {
135      return pValue;
136    }
137
138    // Return as a String literal
139    else {
140      return toStringToken (pValue);
141    }
142  }
143
144  //-------------------------------------
145  /**
146   *
147   * Returns true if the specified value is a legal java identifier
148   **/
149  static boolean isJavaIdentifier (String pValue)
150  {
151    int len = pValue.length ();
152    if (len == 0) {
153      return false;
154    }
155    else {
156      if (!Character.isJavaIdentifierStart (pValue.charAt (0))) {
157        return false;
158      }
159      else {
160        for (int i = 1; i < len; i++) {
161          if (!Character.isJavaIdentifierPart (pValue.charAt (i))) {
162            return false;
163          }
164        }
165        return true;
166      }
167    }
168  }
169
170  //-------------------------------------
171  // Expression methods
172  //-------------------------------------
173  /**
174   *
175   * Returns the expression in the expression language syntax
176   **/
177  public String getExpressionString ()
178  {
179    return toStringToken ((String) getValue ());
180  }
181
182  //-------------------------------------
183}