KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > jet > JETSkeleton


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JETSkeleton.java,v 1.5 2005/06/08 06:15:57 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.jet;
18
19
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.eclipse.jdt.core.jdom.DOMFactory;
25 import org.eclipse.jdt.core.jdom.IDOMCompilationUnit;
26 import org.eclipse.jdt.core.jdom.IDOMMethod;
27 import org.eclipse.jdt.core.jdom.IDOMNode;
28
29
30 /**
31  */

32 public class JETSkeleton
33 {
34   protected final String JavaDoc NL = System.getProperties().getProperty("line.separator");
35
36   protected final String JavaDoc SKELETON_COMPILATION_UNIT =
37     "public class CLASS" + NL + "{" + NL + " public String generate(Object argument)" + NL + " {" + NL + " return \"\";" + NL + " }" + NL + "}" + NL;
38
39   protected final String JavaDoc STATIC_NL_DECLARATION = " protected static String nl;" + NL;
40   protected final String JavaDoc CREATE_METHOD_DECLARATION_HEAD = " public static synchronized ";
41   protected final String JavaDoc CREATE_METHOD_DECLARATION_MIDDLE = " create(String lineSeparator)" + NL + " {" + NL + " nl = lineSeparator;" + NL + " ";
42   protected final String JavaDoc CREATE_METHOD_DECLARATION_MIDDLE2 = " result = new ";
43   protected final String JavaDoc CREATE_METHOD_DECLARATION_TAIL = "();" + NL + " nl = null;" + NL + " return result;" + NL + " }" + NL + NL;
44   
45   protected final String JavaDoc NL_DECLARATION = " protected final String NL = nl == null ? (";
46   protected final String JavaDoc NL_DECLARATION_TAIL = ") : nl;" + NL;
47   protected final String JavaDoc STRING_BUFFER_DECLARATION = " StringBuffer stringBuffer = new StringBuffer();" + NL;
48   protected final String JavaDoc STRING_BUFFER_RETURN = " return stringBuffer.toString();" + NL;
49
50   protected DOMFactory jdomFactory = new DOMFactory();
51   protected IDOMCompilationUnit compilationUnit;
52   protected String JavaDoc nlString = "System.getProperties().getProperty(\"line.separator\")";
53
54   /**
55    */

56   public JETSkeleton()
57   {
58     compilationUnit = jdomFactory.createCompilationUnit(SKELETON_COMPILATION_UNIT, "CLASS");
59   }
60   
61   public String JavaDoc getCompilationUnitContents()
62   {
63     return compilationUnit.getContents();
64   }
65
66   public IDOMCompilationUnit getCompilationUnit()
67   {
68     return compilationUnit;
69   }
70
71   public void setCompilationUnitContents(String JavaDoc contents)
72   {
73     compilationUnit = jdomFactory.createCompilationUnit(contents, "CLASS");
74   }
75
76   public String JavaDoc getNLString()
77   {
78     return nlString;
79   }
80
81   public void setNLString(String JavaDoc nlString)
82   {
83     this.nlString = nlString;
84   }
85
86   public String JavaDoc getPackageName()
87   {
88     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
89     {
90       if (node.getNodeType() == IDOMNode.PACKAGE)
91       {
92         return node.getName();
93       }
94     }
95
96     return "";
97   }
98
99   public void setPackageName(String JavaDoc packageName)
100   {
101     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
102     {
103       if (node.getNodeType() == IDOMNode.PACKAGE)
104       {
105         node.setName(packageName);
106         return;
107       }
108     }
109
110     compilationUnit.getFirstChild().insertSibling(jdomFactory.createPackage("package " + packageName + ";" + NL + NL));
111   }
112
113   public void setConstants(List JavaDoc constants)
114   {
115     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
116     {
117       if (node.getNodeType() == IDOMNode.TYPE)
118       {
119         String JavaDoc name = node.getName();
120         IDOMNode insertionNode = node.getFirstChild();
121         insertionNode.insertSibling(jdomFactory.createField(STATIC_NL_DECLARATION));
122         insertionNode.insertSibling
123           (jdomFactory.createMethod
124               (CREATE_METHOD_DECLARATION_HEAD + name +
125                   CREATE_METHOD_DECLARATION_MIDDLE + name +
126                   CREATE_METHOD_DECLARATION_MIDDLE2 + name +
127                   CREATE_METHOD_DECLARATION_TAIL));
128         insertionNode.insertSibling(jdomFactory.createField(NL_DECLARATION + getNLString() + NL_DECLARATION_TAIL));
129         for (Iterator JavaDoc i = constants.iterator(); i.hasNext(); )
130         {
131           String JavaDoc constant = " " + (String JavaDoc)i.next() + NL;
132           if (!i.hasNext())
133           {
134             constant += NL;
135           }
136           insertionNode.insertSibling(jdomFactory.createField(constant));
137         }
138         break;
139       }
140     }
141   }
142   public void setBody(List JavaDoc lines)
143   {
144     IDOMMethod method = getLastMethod();
145     if (method != null)
146     {
147       StringBuffer JavaDoc body = new StringBuffer JavaDoc();
148       body.append(NL + " {" + NL);
149       body.append(STRING_BUFFER_DECLARATION);
150       for (Iterator JavaDoc i = lines.iterator(); i.hasNext(); )
151       {
152         String JavaDoc line = (String JavaDoc)i.next();
153         body.append(" ");
154         body.append(line);
155         body.append(NL);
156       }
157       body.append(STRING_BUFFER_RETURN);
158       body.append(" }" + NL);
159
160       method.setBody(body.toString());
161     }
162   }
163
164   public String JavaDoc getMethodName()
165   {
166     IDOMMethod method = getLastMethod();
167     if (method != null)
168     {
169       return method.getName();
170     }
171     else
172     {
173       return "";
174     }
175   }
176
177   public void addImports(String JavaDoc importList)
178   {
179     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
180     {
181       if (node.getNodeType() == IDOMNode.TYPE)
182       {
183         for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(importList, " \t\n\r"); stringTokenizer.hasMoreTokens(); )
184         {
185           String JavaDoc token = stringTokenizer.nextToken();
186           String JavaDoc newImport = "import " + token + ";" + NL;
187           if (!stringTokenizer.hasMoreTokens())
188           {
189             newImport += NL;
190           }
191           node.insertSibling(jdomFactory.createImport(newImport));
192         }
193         return;
194       }
195     }
196   }
197
198   public String JavaDoc getClassName()
199   {
200     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
201     {
202       if (node.getNodeType() == IDOMNode.TYPE)
203       {
204         return node.getName();
205       }
206     }
207
208     return null;
209   }
210
211   public void setClassName(String JavaDoc className)
212   {
213     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
214     {
215       if (node.getNodeType() == IDOMNode.TYPE)
216       {
217         node.setName(className);
218       }
219     }
220   }
221
222   protected IDOMMethod getLastMethod()
223   {
224     IDOMMethod method = null;
225     for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode())
226     {
227       if (node.getNodeType() == IDOMNode.TYPE)
228       {
229         for (IDOMNode child = node.getFirstChild(); child != null; child = child.getNextNode())
230         {
231           if (child.getNodeType() == IDOMNode.METHOD)
232           {
233             method = (IDOMMethod)child;
234           }
235         }
236       }
237     }
238     return method;
239   }
240 }
241
Popular Tags