KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > dom > rewrite > NodeInfoStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core.dom.rewrite;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.IdentityHashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.jdt.core.dom.AST;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.Block;
21 import org.eclipse.jdt.core.dom.FieldDeclaration;
22 import org.eclipse.jdt.core.dom.Modifier;
23 import org.eclipse.jdt.core.dom.ParameterizedType;
24 import org.eclipse.jdt.core.dom.TryStatement;
25 import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
26 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
27
28 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.CopySourceInfo;
29
30 /**
31  *
32  */

33 public final class NodeInfoStore {
34     private AST ast;
35     
36     private Map JavaDoc placeholderNodes;
37     private Set JavaDoc collapsedNodes;
38
39     public NodeInfoStore(AST ast) {
40         super();
41         this.ast= ast;
42         this.placeholderNodes= null;
43         this.collapsedNodes= null;
44     }
45
46     /**
47      * Marks a node as a placehoder for a plain string content. The type of the node should correspond to the
48      * code's code content.
49      * @param placeholder The placeholder node that acts for the string content.
50      * @param code The string content.
51      */

52     public final void markAsStringPlaceholder(ASTNode placeholder, String JavaDoc code) {
53         StringPlaceholderData data= new StringPlaceholderData();
54         data.code= code;
55         setPlaceholderData(placeholder, data);
56     }
57     
58     /**
59      * Marks a node as a copy or move target. The copy target represents a copied node at the target (copied) site.
60      * @param target The node at the target site. Can be a placeholder node but also the source node itself.
61      * @param copySource The info at the source site.
62      */

63     public final void markAsCopyTarget(ASTNode target, CopySourceInfo copySource) {
64         CopyPlaceholderData data= new CopyPlaceholderData();
65         data.copySource= copySource;
66         setPlaceholderData(target, data);
67     }
68     
69     /**
70      * Creates a placeholder node of the given type. <code>null</code> if the type is not supported
71      * @param nodeType Type of the node to create. Use the type constants in {@link NodeInfoStore}.
72      * @return Returns a place holder node.
73      */

74     public final ASTNode newPlaceholderNode(int nodeType) {
75         try {
76             ASTNode node= this.ast.createInstance(nodeType);
77             switch (node.getNodeType()) {
78                 case ASTNode.FIELD_DECLARATION:
79                     ((FieldDeclaration) node).fragments().add(this.ast.newVariableDeclarationFragment());
80                     break;
81                 case ASTNode.MODIFIER:
82                     ((Modifier) node).setKeyword(Modifier.ModifierKeyword.ABSTRACT_KEYWORD);
83                     break;
84                 case ASTNode.TRY_STATEMENT :
85                     ((TryStatement) node).setFinally(this.ast.newBlock()); // have to set at least a finally block to be legal code
86
break;
87                 case ASTNode.VARIABLE_DECLARATION_EXPRESSION :
88                     ((VariableDeclarationExpression) node).fragments().add(this.ast.newVariableDeclarationFragment());
89                     break;
90                 case ASTNode.VARIABLE_DECLARATION_STATEMENT :
91                     ((VariableDeclarationStatement) node).fragments().add(this.ast.newVariableDeclarationFragment());
92                     break;
93                 case ASTNode.PARAMETERIZED_TYPE :
94                     ((ParameterizedType) node).typeArguments().add(this.ast.newWildcardType()); //$NON-NLS-1$
95
break;
96             }
97             return node;
98         } catch (IllegalArgumentException JavaDoc e) {
99             return null;
100         }
101     }
102     
103     
104     // collapsed nodes: in source: use one node that represents many; to be used as
105
// copy/move source or to replace at once.
106
// in the target: one block node that is not flattened.
107

108     public Block createCollapsePlaceholder() {
109         Block placeHolder= this.ast.newBlock();
110         if (this.collapsedNodes == null) {
111             this.collapsedNodes= new HashSet JavaDoc();
112         }
113         this.collapsedNodes.add(placeHolder);
114         return placeHolder;
115     }
116     
117     public boolean isCollapsed(ASTNode node) {
118         if (this.collapsedNodes != null) {
119             return this.collapsedNodes.contains(node);
120         }
121         return false;
122     }
123     
124     public Object JavaDoc getPlaceholderData(ASTNode node) {
125         if (this.placeholderNodes != null) {
126             return this.placeholderNodes.get(node);
127         }
128         return null;
129     }
130     
131     private void setPlaceholderData(ASTNode node, PlaceholderData data) {
132         if (this.placeholderNodes == null) {
133             this.placeholderNodes= new IdentityHashMap JavaDoc();
134         }
135         this.placeholderNodes.put(node, data);
136     }
137     
138     private static class PlaceholderData {
139         // base class
140
}
141             
142     protected static final class CopyPlaceholderData extends PlaceholderData {
143         public CopySourceInfo copySource;
144         public String JavaDoc toString() {
145             return "[placeholder " + this.copySource +"]"; //$NON-NLS-1$//$NON-NLS-2$
146
}
147     }
148     
149     protected static final class StringPlaceholderData extends PlaceholderData {
150         public String JavaDoc code;
151         public String JavaDoc toString() {
152             return "[placeholder string: " + this.code +"]"; //$NON-NLS-1$ //$NON-NLS-2$
153
}
154     }
155
156     /**
157      *
158      */

159     public void clear() {
160         this.placeholderNodes= null;
161         this.collapsedNodes= null;
162     }
163 }
164
Popular Tags