View Javadoc
1   package net.sourceforge.pmd.lang.vm.ast;
2   
3   import org.apache.commons.lang3.text.StrBuilder;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements. See the NOTICE file distributed with this
8    * work for additional information regarding copyright ownership. The ASF
9    * licenses this file to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   * http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18   * License for the specific language governing permissions and limitations under
19   * the License.
20   */
21  
22  
23  /**
24   * ASTStringLiteral support. Will interpolate!
25   * 
26   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
27   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
28   * @version $Id: ASTStringLiteral.java 705297 2008-10-16 17:59:24Z nbubna $
29   */
30  public class ASTStringLiteral extends AbstractVmNode {
31      /**
32       * @param id
33       */
34      public ASTStringLiteral(final int id) {
35          super(id);
36      }
37  
38      /**
39       * @param p
40       * @param id
41       */
42      public ASTStringLiteral(final VmParser p, final int id) {
43          super(p, id);
44      }
45  
46      /**
47       * Adjust all the line and column numbers that comprise a node so that they are corrected for the string literals
48       * position within the template file. This is neccessary if an exception is thrown while processing the node so that
49       * the line and column position reported reflects the error position within the template and not just relative to
50       * the error position within the string literal.
51       */
52      public void adjTokenLineNums(final AbstractVmNode node) {
53          Token tok = node.getFirstToken();
54          // Test against null is probably not neccessary, but just being safe
55          while (tok != null && tok != node.getLastToken()) {
56              // If tok is on the first line, then the actual column is
57              // offset by the template column.
58  
59              if (tok.beginLine == 1) {
60                  tok.beginColumn += getColumn();
61              }
62  
63              if (tok.endLine == 1) {
64                  tok.endColumn += getColumn();
65              }
66  
67              tok.beginLine += getLine() - 1;
68              tok.endLine += getLine() - 1;
69              tok = tok.next;
70          }
71      }
72  
73      /**
74       * @since 1.6
75       */
76      public static String unescape(final String string) {
77          int u = string.indexOf("\\u");
78          if (u < 0) {
79              return string;
80          }
81  
82          final StrBuilder result = new StrBuilder();
83  
84          int lastCopied = 0;
85  
86          for (;;) {
87              result.append(string.substring(lastCopied, u));
88  
89              /*
90               * we don't worry about an exception here, because the lexer checked that string is correct
91               */
92              final char c = (char) Integer.parseInt(string.substring(u + 2, u + 6), 16);
93              result.append(c);
94  
95              lastCopied = u + 6;
96  
97              u = string.indexOf("\\u", lastCopied);
98              if (u < 0) {
99                  result.append(string.substring(lastCopied));
100                 return result.toString();
101             }
102         }
103     }
104 
105     /**
106      * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.VmParserVisitor,
107      *      java.lang.Object)
108      */
109     @Override
110     public Object jjtAccept(final VmParserVisitor visitor, final Object data) {
111         return visitor.visit(this, data);
112     }
113 
114     /**
115      * Check to see if this is an interpolated string.
116      * 
117      * @return true if this is constant (not an interpolated string)
118      * @since 1.6
119      */
120     public boolean isConstant() {
121         return false;
122     }
123 
124 }