1 17 package org.eclipse.emf.codegen.jet; 18 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 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 32 public class JETSkeleton 33 { 34 protected final String NL = System.getProperties().getProperty("line.separator"); 35 36 protected final String 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 STATIC_NL_DECLARATION = " protected static String nl;" + NL; 40 protected final String CREATE_METHOD_DECLARATION_HEAD = " public static synchronized "; 41 protected final String CREATE_METHOD_DECLARATION_MIDDLE = " create(String lineSeparator)" + NL + " {" + NL + " nl = lineSeparator;" + NL + " "; 42 protected final String CREATE_METHOD_DECLARATION_MIDDLE2 = " result = new "; 43 protected final String CREATE_METHOD_DECLARATION_TAIL = "();" + NL + " nl = null;" + NL + " return result;" + NL + " }" + NL + NL; 44 45 protected final String NL_DECLARATION = " protected final String NL = nl == null ? ("; 46 protected final String NL_DECLARATION_TAIL = ") : nl;" + NL; 47 protected final String STRING_BUFFER_DECLARATION = " StringBuffer stringBuffer = new StringBuffer();" + NL; 48 protected final String STRING_BUFFER_RETURN = " return stringBuffer.toString();" + NL; 49 50 protected DOMFactory jdomFactory = new DOMFactory(); 51 protected IDOMCompilationUnit compilationUnit; 52 protected String nlString = "System.getProperties().getProperty(\"line.separator\")"; 53 54 56 public JETSkeleton() 57 { 58 compilationUnit = jdomFactory.createCompilationUnit(SKELETON_COMPILATION_UNIT, "CLASS"); 59 } 60 61 public String getCompilationUnitContents() 62 { 63 return compilationUnit.getContents(); 64 } 65 66 public IDOMCompilationUnit getCompilationUnit() 67 { 68 return compilationUnit; 69 } 70 71 public void setCompilationUnitContents(String contents) 72 { 73 compilationUnit = jdomFactory.createCompilationUnit(contents, "CLASS"); 74 } 75 76 public String getNLString() 77 { 78 return nlString; 79 } 80 81 public void setNLString(String nlString) 82 { 83 this.nlString = nlString; 84 } 85 86 public String 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 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 constants) 114 { 115 for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode()) 116 { 117 if (node.getNodeType() == IDOMNode.TYPE) 118 { 119 String 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 i = constants.iterator(); i.hasNext(); ) 130 { 131 String constant = " " + (String )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 lines) 143 { 144 IDOMMethod method = getLastMethod(); 145 if (method != null) 146 { 147 StringBuffer body = new StringBuffer (); 148 body.append(NL + " {" + NL); 149 body.append(STRING_BUFFER_DECLARATION); 150 for (Iterator i = lines.iterator(); i.hasNext(); ) 151 { 152 String line = (String )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 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 importList) 178 { 179 for (IDOMNode node = compilationUnit.getFirstChild(); node != null; node = node.getNextNode()) 180 { 181 if (node.getNodeType() == IDOMNode.TYPE) 182 { 183 for (StringTokenizer stringTokenizer = new StringTokenizer (importList, " \t\n\r"); stringTokenizer.hasMoreTokens(); ) 184 { 185 String token = stringTokenizer.nextToken(); 186 String 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 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 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 |