KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > formatter > InfixExpressionWrappingBuilder


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.formatter;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.ASTVisitor;
18 import org.eclipse.jdt.core.dom.Expression;
19 import org.eclipse.jdt.core.dom.InfixExpression;
20
21 public class InfixExpressionWrappingBuilder extends ASTVisitor {
22     private final static int DEFAULT_SIZE = 10;
23     ArrayList JavaDoc fragments = new ArrayList JavaDoc();
24     int[] operators = new int[DEFAULT_SIZE];
25     int operatorCounter = 0;
26     
27     public int getFragmentsCounter() {
28         return this.fragments.size();
29     }
30     
31     public int[] getOperators() {
32         if (operators.length != operatorCounter) {
33             // resize
34
System.arraycopy(this.operators, 0, (this.operators = new int[this.operatorCounter]), 0, this.operatorCounter);
35         }
36         return this.operators;
37     }
38     private boolean isSplittable(Expression expression) {
39 // if (expression instanceof InfixExpression) {
40
// InfixExpression infixExpression = (InfixExpression) expression;
41
// return infixExpression.getLeftOperand() instanceof InfixExpression
42
// || infixExpression.getRightOperand() instanceof InfixExpression;
43
// }
44
// return false;
45
return true;
46     }
47     public boolean visit(InfixExpression node) {
48         final Expression leftOperand = node.getLeftOperand();
49         final Expression rightOperand = node.getRightOperand();
50         if (leftOperand.getNodeType() == ASTNode.INFIX_EXPRESSION && isSplittable(leftOperand)) {
51             leftOperand.accept(this);
52         } else {
53             this.fragments.add(leftOperand);
54         }
55         if (operatorCounter == this.operators.length) {
56             // need to resize
57
System.arraycopy(this.operators, 0, (this.operators = new int[this.operatorCounter * 2]), 0, this.operatorCounter);
58         }
59         final int extractInfixExpressionOperator = CodeFormatterVisitor2.extractInfixExpressionOperator(node);
60         this.operators[this.operatorCounter++] = extractInfixExpressionOperator;
61         if (rightOperand.getNodeType() == ASTNode.INFIX_EXPRESSION && isSplittable(leftOperand)) {
62             rightOperand.accept(this);
63         } else {
64             this.fragments.add(rightOperand);
65         }
66         final List JavaDoc extendedOperands = node.extendedOperands();
67         final int extendedOperandsLength = extendedOperands.size();
68         if (extendedOperandsLength != 0) {
69             for (int i = 0; i < extendedOperandsLength; i++) {
70                 if (operatorCounter == this.operators.length) {
71                     // need to resize
72
System.arraycopy(this.operators, 0, (this.operators = new int[this.operatorCounter * 2]), 0, this.operatorCounter);
73                 }
74                 this.operators[this.operatorCounter++] = extractInfixExpressionOperator;
75                 final Expression extendedOperand = ((Expression) extendedOperands.get(i));
76                 if (extendedOperand instanceof InfixExpression) {
77                     extendedOperand.accept(this);
78                 } else {
79                     this.fragments.add(extendedOperand);
80                 }
81             }
82         }
83         return false;
84     }
85
86 }
87
Popular Tags