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 /* cache the value of the interpolation switch */
32 private final boolean interpolate = true;
33
34 /**
35 * @param id
36 */
37 public ASTStringLiteral(final int id) {
38 super(id);
39 }
40
41 /**
42 * @param p
43 * @param id
44 */
45 public ASTStringLiteral(final VmParser p, final int id) {
46 super(p, id);
47 }
48
49 /**
50 * Adjust all the line and column numbers that comprise a node so that they are corrected for the string literals
51 * position within the template file. This is neccessary if an exception is thrown while processing the node so that
52 * the line and column position reported reflects the error position within the template and not just relative to
53 * the error position within the string literal.
54 */
55 public void adjTokenLineNums(final AbstractVmNode node) {
56 Token tok = node.getFirstToken();
57 // Test against null is probably not neccessary, but just being safe
58 while (tok != null && tok != node.getLastToken()) {
59 // If tok is on the first line, then the actual column is
60 // offset by the template column.
61
62 if (tok.beginLine == 1)
63 tok.beginColumn += getColumn();
64
65 if (tok.endLine == 1)
66 tok.endColumn += getColumn();
67
68 tok.beginLine += getLine() - 1;
69 tok.endLine += getLine() - 1;
70 tok = tok.next;
71 }
72 }
73
74 /**
75 * @since 1.6
76 */
77 public static String unescape(final String string) {
78 int u = string.indexOf("\\u");
79 if (u < 0)
80 return string;
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 !interpolate;
122 }
123
124 }