01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.compiler.ast;
11:
12: import org.eclipse.jdt.internal.compiler.ASTVisitor;
13: import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
14:
15: /**
16: * Flatten string literal
17: */
18: public class StringLiteralConcatenation extends StringLiteral {
19: private static final int INITIAL_SIZE = 5;
20: public Expression[] literals;
21: public int counter;
22:
23: /**
24: * Build a two-strings literal
25: * */
26: public StringLiteralConcatenation(StringLiteral str1,
27: StringLiteral str2) {
28: super (str1.sourceStart, str1.sourceEnd);
29: this .source = str1.source;
30: this .literals = new StringLiteral[INITIAL_SIZE];
31: this .counter = 0;
32: this .literals[this .counter++] = str1;
33: extendsWith(str2);
34: }
35:
36: /**
37: * Add the lit source to mine, just as if it was mine
38: */
39: public StringLiteralConcatenation extendsWith(StringLiteral lit) {
40: this .sourceEnd = lit.sourceEnd;
41: final int literalsLength = this .literals.length;
42: if (this .counter == literalsLength) {
43: // resize
44: System.arraycopy(this .literals, 0,
45: this .literals = new StringLiteral[literalsLength
46: + INITIAL_SIZE], 0, literalsLength);
47: }
48: //uddate the source
49: int length = source.length;
50: System.arraycopy(source, 0, source = new char[length
51: + lit.source.length], 0, length);
52: System.arraycopy(lit.source, 0, source, length,
53: lit.source.length);
54: this .literals[this .counter++] = lit;
55: return this ;
56: }
57:
58: public StringBuffer printExpression(int indent, StringBuffer output) {
59: output.append("StringLiteralConcatenation{"); //$NON-NLS-1$
60: for (int i = 0, max = this .counter; i < max; i++) {
61: this .literals[i].printExpression(indent, output);
62: output.append("+\n");//$NON-NLS-1$
63: }
64: return output.append('}');
65: }
66:
67: public char[] source() {
68: return source;
69: }
70:
71: public void traverse(ASTVisitor visitor, BlockScope scope) {
72: if (visitor.visit(this , scope)) {
73: for (int i = 0, max = this.counter; i < max; i++) {
74: this.literals[i].traverse(visitor, scope);
75: }
76: }
77: visitor.endVisit(this, scope);
78: }
79: }
|