| 58 | | private String classPrefix = "C"; |
| 59 | | private Stack<NodePath<ComplexNode>> toBuild; |
| 60 | | XStreamManager manager = new XStreamManager(); |
| 61 | | public static final String applicationName = "application.xml"; |
| 62 | | |
| 63 | | /*transient String inClassName; |
| 64 | | transient String outClassName;*/ |
| 65 | | |
| 66 | | //TODO select a java file for each user to allow multiuser in simexplorer |
| 67 | | public void generateClass(IDEModularApplication application) throws InternalProcessingError { |
| 68 | | try { |
| 69 | | GroovyProject gp = new GroovyProject(); |
| 70 | | gp.initProject(); |
| 71 | | packProcessorList(application, gp.getPackDir()); |
| 72 | | gp.compile(); |
| 73 | | |
| 74 | | File jar = TempDir.GetInstance().createNewTempFile("application", ".jar"); |
| 75 | | mkJar(gp.getBinDir(), jar, gp.getManifest()); |
| 76 | | |
| 77 | | JarLoader loader = new JarLoader(); |
| 78 | | loader.addJar(jar); |
| 79 | | loadClassesInProcessorList(application.getGlobalProcess(), loader); |
| 80 | | |
| 81 | | gp.clean(); |
| 82 | | jar.delete(); |
| 83 | | } catch (IOException e) { |
| 84 | | throw new InternalProcessingError(e); |
| 85 | | } |
| 86 | | } |
| 87 | | |
| 88 | | public void mkjar(IDEModularApplication application, File jar) throws InternalProcessingError { |
| 89 | | try { |
| 90 | | GroovyProject gp = new GroovyProject(); |
| 91 | | gp.initProject(); |
| 92 | | if (application.getApplicativeContext().getGlobal(IDEModularApplication.Structures.getName()) != null) { |
| 93 | | packStructures(application, gp.getPackDir()); |
| 94 | | } |
| 95 | | gp.compile(); |
| 96 | | |
| 97 | | mkJarAndXML(application, gp.getBinDir(), jar, gp.getManifest()); |
| 98 | | gp.clean(); |
| 99 | | } catch (IOException e) { |
| 100 | | throw new InternalProcessingError(e); |
| 101 | | } |
| 102 | | |
| 103 | | } |
| 104 | | |
| 105 | | private void packStructures(IDEModularApplication application, File packDir) throws IOException { |
| 106 | | List<ComplexNode> structs = application.getGlobalValue(IDEModularApplication.Structures.getName()); |
| 107 | | |
| 108 | | for(ComplexNode struct : structs) { |
| 109 | | buildJavaCode(struct, packDir); |
| 110 | | } |
| 111 | | } |
| 112 | | |
| 113 | | |
| 114 | | private void loadClassesInProcessorList(ProcessorsList pList, final JarLoader loader) { |
| 115 | | |
| 116 | | pList.visit(new IVisitor<Processor>() { |
| 117 | | @Override |
| 118 | | public void action(Processor visited) { |
| 119 | | try{ |
| 120 | | if(AbstractExploration.class.isAssignableFrom(visited.getClass())) { |
| 121 | | AbstractExploration loop = (AbstractExploration) visited; |
| 122 | | |
| 123 | | if(loop.getInputClassName() != null) { |
| 124 | | Class in = loader.loadClass("org.simexplorer.user.data." + loop.getInputClassName()); |
| 125 | | loop.setInputDataStructureClass(in); |
| 126 | | } |
| 127 | | |
| 128 | | if(loop.getOutputClassName() != null) { |
| 129 | | Class out = loader.loadClass("org.simexplorer.user.data." + loop.getOutputClassName()); |
| 130 | | loop.setOutputDataStructureClass(out); |
| 131 | | } |
| 132 | | |
| 133 | | |
| 134 | | } |
| 135 | | } |
| 136 | | catch (ClassNotFoundException e) { |
| 137 | | Logger.getLogger(IDEModularApplicationPackager.class.getName()).log(Level.SEVERE, null, e); |
| 138 | | } |
| 139 | | } |
| 140 | | }); |
| 141 | | } |
| 142 | | |
| 143 | | /*private void fillStructures(IDEModularApplication application) { |
| 144 | | |
| 145 | | }*/ |
| 146 | | |
| 147 | | |
| 148 | | private void packProcessorList(final IDEModularApplication application,final File pack) { |
| 149 | | |
| 150 | | final List<ComplexNode> structures = new LinkedList<ComplexNode>(); |
| 151 | | application.putGlobalVariable(IDEModularApplication.Structures, structures); |
| 152 | | |
| 153 | | application.getGlobalProcess().visit(new IVisitor<Processor>() { |
| 154 | | @Override |
| 155 | | public void action(Processor visited) { |
| 156 | | try { |
| 157 | | if(AbstractExploration.class.isAssignableFrom(visited.getClass())) { |
| 158 | | AbstractExploration loop = (AbstractExploration) visited; |
| 159 | | |
| 160 | | if(loop.getInputDataStructureClass() == null) { |
| 161 | | ComplexNode in = loop.getInputDataStructure(); |
| 162 | | if(in != null) { |
| 163 | | structures.add(in); |
| 164 | | String inClassName = buildJavaCode(in, pack); |
| 165 | | loop.setInputClassName(inClassName); |
| 166 | | } |
| 167 | | } |
| 168 | | |
| 169 | | if(loop.getOutputDataStructureClass() == null) { |
| 170 | | ComplexNode out = loop.getOutputDataStructure(); |
| 171 | | if(out != null) { |
| 172 | | structures.add(out); |
| 173 | | String outClassName = buildJavaCode(out, pack); |
| 174 | | loop.setOutputClassName(outClassName); |
| 175 | | } |
| 176 | | } |
| 177 | | } |
| 178 | | } |
| 179 | | catch (IOException e) { |
| 180 | | Logger.getLogger(IDEModularApplicationPackager.class.getName()).log(Level.SEVERE, null, e); |
| 181 | | } |
| 182 | | } |
| 183 | | |
| 184 | | }); |
| 185 | | } |
| 186 | | |
| 187 | | |
| 188 | | void mkJarAndXML(IDEModularApplication application, File binDir, File jar, Manifest man) throws FileNotFoundException, IOException{ |
| 189 | | JarOutputStream zos = new JarOutputStream(new FileOutputStream(jar), man); |
| 190 | | zipDir(binDir, zos); |
| 191 | | |
| 192 | | JarEntry anEntry = new JarEntry(applicationName); |
| 193 | | zos.putNextEntry(anEntry); |
| 194 | | manager.toXML(application, zos); |
| 195 | | zos.close(); |
| 196 | | |
| 197 | | } |
| 198 | | |
| 199 | | void mkJar(File binDir, File jar, Manifest man) throws FileNotFoundException, IOException { |
| 200 | | JarOutputStream zos = new JarOutputStream(new FileOutputStream(jar), man); |
| 201 | | zipDir(binDir, zos); |
| 202 | | zos.close(); |
| 203 | | } |
| 204 | | |
| 205 | | |
| 206 | | private void zipDir(File dir2zip, JarOutputStream zos, String relative) throws IOException |
| 207 | | { |
| 208 | | File zipDir = dir2zip; |
| 209 | | String[] dirList = zipDir.list(); |
| 210 | | byte[] readBuffer = new byte[2156]; |
| 211 | | int bytesIn = 0; |
| 212 | | |
| 213 | | |
| 214 | | for(int i=0; i<dirList.length; i++) |
| 215 | | { |
| 216 | | File f = new File(zipDir, dirList[i]); |
| 217 | | if(f.isDirectory()) |
| 218 | | { |
| 219 | | //String filePath = f.getPath(); |
| 220 | | zipDir(f, zos, relative + f.getName() + "/"); |
| 221 | | continue; |
| 222 | | } |
| 223 | | |
| 224 | | FileInputStream fis = new FileInputStream(f); |
| 225 | | |
| 226 | | JarEntry anEntry = new JarEntry(relative + f.getName()); |
| 227 | | anEntry.setTime(f.lastModified()); |
| 228 | | zos.putNextEntry(anEntry); |
| 229 | | |
| 230 | | try{ |
| 231 | | FastCopy.copy(fis, zos); |
| 232 | | } |
| 233 | | finally { |
| 234 | | fis.close(); |
| 235 | | } |
| 236 | | |
| 237 | | } |
| 238 | | } |
| 239 | | |
| 240 | | |
| 241 | | private void zipDir(File dir2zip, JarOutputStream zos) throws IOException |
| 242 | | { |
| 243 | | zipDir(dir2zip, zos, ""); |
| 244 | | } |
| 245 | | |
| 246 | | /** |
| 247 | | * |
| 248 | | * @param node |
| 249 | | * @return a list containing : <ul> |
| 250 | | * <li>The code declaring the object of the upper type</li> |
| 251 | | * <li>The code declaring the variables and fields</li> |
| 252 | | * <li>A list of the structures types definitions</li> |
| 253 | | * </ul> |
| 254 | | * @throws IOException |
| 255 | | */ |
| 256 | | private String buildJavaCode(ComplexNode rootNode, File pack) throws IOException { |
| 257 | | |
| 258 | | toBuild = new Stack<NodePath<ComplexNode>>(); |
| 259 | | |
| 260 | | NodePath<ComplexNode> root = new NodePath<ComplexNode>("", rootNode); |
| 261 | | toBuild.add(root); |
| 262 | | |
| 263 | | while(!toBuild.isEmpty()) |
| 264 | | { |
| 265 | | NodePath<ComplexNode> complexNodePath = toBuild.pop(); |
| 266 | | String code = buildComplexNode(complexNodePath); |
| 267 | | File out = new File(pack, complexNodePath.getClassName() + ".groovy"); |
| 268 | | // out.deleteOnExit(); |
| 269 | | |
| 270 | | BufferedWriter writter = new BufferedWriter(new FileWriter(out)); |
| 271 | | writter.append(code); |
| 272 | | writter.close(); |
| 273 | | } |
| 274 | | |
| 275 | | return root.getClassName(); |
| 276 | | } |
| 277 | | |
| 278 | | |
| 279 | | |
| 280 | | |
| 281 | | |
| 282 | | private String buildComplexNode(NodePath<ComplexNode> node) { |
| 283 | | StringBuilder typeDeclaration = new StringBuilder("package org.simexplorer.user.data;\n\nimport org.simexplorer.core.data.DataContainer;\nimport org.simexplorer.core.data.DataModifier;\n\n"); |
| 284 | | |
| 285 | | typeDeclaration.append("public class "); |
| 286 | | typeDeclaration.append(node.getClassName()).append(" implements DataContainer"); |
| 287 | | if (node.getNode().isModifiable()) { |
| 288 | | typeDeclaration.append(", DataModifier"); |
| 289 | | } |
| 290 | | typeDeclaration.append(" {\n"); |
| 291 | | |
| 292 | | for (StructureNode child : node.getNode().getChildrenContent()) { |
| 293 | | |
| 294 | | |
| 295 | | String n = null; |
| 296 | | |
| 297 | | if(child instanceof Prototype) { |
| 298 | | Prototype p = (Prototype) child; |
| 299 | | n = buildPrototypeAttribute(p); |
| 300 | | } |
| 301 | | else if(child instanceof SequenceNode) { |
| 302 | | SequenceNode s = (SequenceNode) child; |
| 303 | | n = buildSequenceNodeAttribute(new NodePath<SequenceNode>(node.getClassName() , s)); |
| 304 | | |
| 305 | | } else if(child instanceof ComplexNode) { |
| 306 | | ComplexNode s = (ComplexNode) child; |
| 307 | | toBuild.add(new NodePath<ComplexNode>(node.getClassName() ,s)); |
| 308 | | n = buildComplexNodeAttribute(new NodePath<ComplexNode>(node.getClassName(), s)); |
| 309 | | |
| 310 | | |
| 311 | | } |
| 312 | | |
| 313 | | typeDeclaration.append(" public " + n + " " + child.getName() + ";\n"); |
| 314 | | |
| 315 | | } |
| 316 | | |
| 317 | | typeDeclaration.append(" @Override\n public Object getValue(String name) {\n return this[name];\n }\n"); |
| 318 | | if (node.getNode().isModifiable()) { |
| 319 | | typeDeclaration.append(" @Override\n public void setValue(String name, Object value) {\n this[name] = value;\n }\n"); |
| 320 | | } |
| 321 | | typeDeclaration.append("}\n\n"); |
| 322 | | |
| 323 | | return typeDeclaration.toString(); |
| 324 | | } |
| 325 | | |
| 326 | | private String buildComplexNodeAttribute(NodePath<ComplexNode> node) { |
| 327 | | return node.getClassName(); |
| 328 | | } |
| 329 | | |
| 330 | | private String buildSequenceNodeAttribute(NodePath<SequenceNode> nodePath) { |
| 331 | | |
| 332 | | |
| 333 | | StringBuilder res = new StringBuilder(); |
| 334 | | SequenceNode node = nodePath.getNode(); |
| 335 | | |
| 336 | | res.append("List<"); |
| 337 | | if (node.getInnerNode() instanceof Prototype) { |
| 338 | | res.append(((Prototype) node.getInnerNode()).getType().getCanonicalName()); |
| 339 | | } else if(node.getInnerNode() instanceof SequenceNode) { |
| 340 | | SequenceNode inner = (SequenceNode) node.getInnerNode(); |
| 341 | | String n = buildSequenceNodeAttribute(new NodePath<SequenceNode>(nodePath.getPath(),inner)); |
| 342 | | res.append(n); |
| 343 | | } else if(node.getInnerNode() instanceof ComplexNode) { |
| 344 | | ComplexNode inner = (ComplexNode) node.getInnerNode(); |
| 345 | | toBuild.add(new NodePath<ComplexNode>(nodePath.getPath(),inner)); |
| 346 | | String n = buildComplexNodeAttribute(new NodePath<ComplexNode>(nodePath.getPath(),inner)); |
| 347 | | res.append(n); |
| 348 | | } |
| 349 | | |
| 350 | | res.append(">"); |
| 351 | | |
| 352 | | |
| 353 | | return res.toString(); |
| 354 | | } |
| 355 | | |
| 356 | | |
| 357 | | private String buildPrototypeAttribute(Prototype node) { |
| 358 | | return node.getType().getCanonicalName(); |
| 359 | | } |
| 360 | | |
| 361 | | |
| 362 | | class NodePath<T extends StructureNode> { |
| 363 | | String path; |
| 364 | | T node; |
| 365 | | |
| 366 | | public NodePath(String path, T node) { |
| 367 | | this.node = node; |
| 368 | | this.path = path; |
| 369 | | } |
| 370 | | |
| 371 | | String getPath() { |
| 372 | | return path; |
| 373 | | } |
| 374 | | |
| 375 | | T getNode() { |
| 376 | | return node; |
| 377 | | } |
| 378 | | |
| 379 | | String getClassName() { |
| 380 | | return path + classPrefix + node.getName(); |
| 381 | | } |
| 382 | | |
| 383 | | } |
| 384 | | |
| | 55 | private String classPrefix = "C"; |
| | 56 | private Stack<NodePath<ComplexNode>> toBuild; |
| | 57 | XStreamManager manager = new XStreamManager(); |
| | 58 | public static final String applicationName = "application.xml"; |
| | 59 | |
| | 60 | //TODO select a java file for each user to allow multiuser in simexplorer |
| | 61 | public void generateClass(IDEModularApplication application) throws InternalProcessingError { |
| | 62 | try { |
| | 63 | GroovyProject gp = new GroovyProject(); |
| | 64 | gp.initProject(); |
| | 65 | packProcessorList(application, gp.getPackDir()); |
| | 66 | gp.compile(); |
| | 67 | |
| | 68 | File jar = TempDir.GetInstance().createNewTempFile("application", ".jar"); |
| | 69 | mkJar(gp.getBinDir(), jar, gp.getManifest()); |
| | 70 | |
| | 71 | JarLoader loader = new JarLoader(); |
| | 72 | loader.addJar(jar); |
| | 73 | loadClassesInProcessorList(application.getGlobalProcess(), loader); |
| | 74 | |
| | 75 | gp.clean(); |
| | 76 | jar.delete(); |
| | 77 | } catch (IOException e) { |
| | 78 | throw new InternalProcessingError(e); |
| | 79 | } |
| | 80 | } |
| | 81 | |
| | 82 | public void mkjar(IDEModularApplication application, File jar) throws InternalProcessingError { |
| | 83 | try { |
| | 84 | GroovyProject gp = new GroovyProject(); |
| | 85 | gp.initProject(); |
| | 86 | if (application.getApplicativeContext().getGlobal(IDEModularApplication.Structures.getName()) != null) { |
| | 87 | packStructures(application, gp.getPackDir()); |
| | 88 | } |
| | 89 | gp.compile(); |
| | 90 | |
| | 91 | mkJarAndXML(application, gp.getBinDir(), jar, gp.getManifest()); |
| | 92 | gp.clean(); |
| | 93 | } catch (IOException e) { |
| | 94 | throw new InternalProcessingError(e); |
| | 95 | } |
| | 96 | |
| | 97 | } |
| | 98 | |
| | 99 | private void packStructures(IDEModularApplication application, File packDir) throws IOException { |
| | 100 | List<ComplexNode> structs = application.getGlobalValue(IDEModularApplication.Structures.getName()); |
| | 101 | |
| | 102 | for (ComplexNode struct : structs) { |
| | 103 | buildJavaCode(struct, packDir); |
| | 104 | } |
| | 105 | } |
| | 106 | |
| | 107 | private void loadClassesInProcessorList(ProcessorsList pList, final JarLoader loader) { |
| | 108 | pList.visit(new IVisitor<Processor>() { |
| | 109 | |
| | 110 | @Override |
| | 111 | public void action(Processor visited) { |
| | 112 | try { |
| | 113 | if (AbstractExploration.class.isAssignableFrom(visited.getClass())) { |
| | 114 | AbstractExploration loop = (AbstractExploration) visited; |
| | 115 | if (loop.getInputClassName() != null) { |
| | 116 | Class in = loader.loadClass("org.simexplorer.user.data." + loop.getInputClassName()); |
| | 117 | loop.setInputDataStructureClass(in); |
| | 118 | } |
| | 119 | if (loop.getOutputClassName() != null) { |
| | 120 | Class out = loader.loadClass("org.simexplorer.user.data." + loop.getOutputClassName()); |
| | 121 | loop.setOutputDataStructureClass(out); |
| | 122 | } |
| | 123 | } |
| | 124 | } catch (ClassNotFoundException e) { |
| | 125 | Logger.getLogger(IDEModularApplicationPackager.class.getName()).log(Level.SEVERE, null, e); |
| | 126 | } |
| | 127 | } |
| | 128 | }); |
| | 129 | } |
| | 130 | |
| | 131 | private void packProcessorList(final IDEModularApplication application, final File pack) { |
| | 132 | final List<ComplexNode> structures = new LinkedList<ComplexNode>(); |
| | 133 | application.putGlobalVariable(IDEModularApplication.Structures, structures); |
| | 134 | application.getGlobalProcess().visit(new IVisitor<Processor>() { |
| | 135 | |
| | 136 | @Override |
| | 137 | public void action(Processor visited) { |
| | 138 | try { |
| | 139 | if (AbstractExploration.class.isAssignableFrom(visited.getClass())) { |
| | 140 | AbstractExploration loop = (AbstractExploration) visited; |
| | 141 | if (loop.getInputDataStructureClass() == null) { |
| | 142 | ComplexNode in = loop.getInputDataStructure(); |
| | 143 | if (in != null) { |
| | 144 | structures.add(in); |
| | 145 | String inClassName = buildJavaCode(in, pack); |
| | 146 | loop.setInputClassName(inClassName); |
| | 147 | } |
| | 148 | } |
| | 149 | if (loop.getOutputDataStructureClass() == null) { |
| | 150 | ComplexNode out = loop.getOutputDataStructure(); |
| | 151 | if (out != null) { |
| | 152 | structures.add(out); |
| | 153 | String outClassName = buildJavaCode(out, pack); |
| | 154 | loop.setOutputClassName(outClassName); |
| | 155 | } |
| | 156 | } |
| | 157 | } |
| | 158 | } catch (IOException e) { |
| | 159 | Logger.getLogger(IDEModularApplicationPackager.class.getName()).log(Level.SEVERE, null, e); |
| | 160 | } |
| | 161 | } |
| | 162 | }); |
| | 163 | } |
| | 164 | |
| | 165 | void mkJarAndXML(IDEModularApplication application, File binDir, File jar, Manifest man) throws FileNotFoundException, IOException { |
| | 166 | JarOutputStream zos = new JarOutputStream(new FileOutputStream(jar), man); |
| | 167 | zipDir(binDir, zos); |
| | 168 | JarEntry anEntry = new JarEntry(applicationName); |
| | 169 | zos.putNextEntry(anEntry); |
| | 170 | manager.toXML(application, zos); |
| | 171 | zos.close(); |
| | 172 | |
| | 173 | } |
| | 174 | |
| | 175 | void mkJar(File binDir, File jar, Manifest man) throws FileNotFoundException, IOException { |
| | 176 | JarOutputStream zos = new JarOutputStream(new FileOutputStream(jar), man); |
| | 177 | zipDir(binDir, zos); |
| | 178 | zos.close(); |
| | 179 | } |
| | 180 | |
| | 181 | private void zipDir(File dir2zip, JarOutputStream zos, String relative) throws IOException { |
| | 182 | File zipDir = dir2zip; |
| | 183 | String[] dirList = zipDir.list(); |
| | 184 | for (int i = 0; i < dirList.length; i++) { |
| | 185 | File f = new File(zipDir, dirList[i]); |
| | 186 | if (f.isDirectory()) { |
| | 187 | zipDir(f, zos, relative + f.getName() + "/"); |
| | 188 | continue; |
| | 189 | } |
| | 190 | FileInputStream fis = new FileInputStream(f); |
| | 191 | JarEntry anEntry = new JarEntry(relative + f.getName()); |
| | 192 | anEntry.setTime(f.lastModified()); |
| | 193 | zos.putNextEntry(anEntry); |
| | 194 | try { |
| | 195 | FastCopy.copy(fis, zos); |
| | 196 | } finally { |
| | 197 | fis.close(); |
| | 198 | } |
| | 199 | } |
| | 200 | } |
| | 201 | |
| | 202 | private void zipDir(File dir2zip, JarOutputStream zos) throws IOException { |
| | 203 | zipDir(dir2zip, zos, ""); |
| | 204 | } |
| | 205 | |
| | 206 | /** |
| | 207 | * Generates the classes needed from a structure |
| | 208 | * @param rootNode the structure |
| | 209 | * @param pack the base directory |
| | 210 | * @return The name of the class generated |
| | 211 | * @throws IOException |
| | 212 | */ |
| | 213 | private String buildJavaCode(ComplexNode rootNode, File pack) throws IOException { |
| | 214 | toBuild = new Stack<NodePath<ComplexNode>>(); |
| | 215 | NodePath<ComplexNode> root = new NodePath<ComplexNode>("", rootNode); |
| | 216 | toBuild.add(root); |
| | 217 | while (!toBuild.isEmpty()) { |
| | 218 | NodePath<ComplexNode> complexNodePath = toBuild.pop(); |
| | 219 | String code = buildComplexNode(complexNodePath); |
| | 220 | Logger.getLogger(IDEModularApplicationPackager.class.getName()).log(Level.FINER, "class " + root.getClassName() + " compiled.\n Source:\n" + code); |
| | 221 | File out = new File(pack, complexNodePath.getClassName() + ".groovy"); |
| | 222 | BufferedWriter writter = new BufferedWriter(new FileWriter(out)); |
| | 223 | writter.append(code); |
| | 224 | writter.close(); |
| | 225 | } |
| | 226 | return root.getClassName(); |
| | 227 | } |
| | 228 | |
| | 229 | /** |
| | 230 | * Build the code for defining a class corresponding to a given ComplexNode |
| | 231 | * @param node |
| | 232 | * @return The source code of the class |
| | 233 | */ |
| | 234 | private String buildComplexNode(NodePath<ComplexNode> node) { |
| | 235 | StringBuilder typeDeclaration = new StringBuilder("package org.simexplorer.user.data;\n\nimport org.simexplorer.core.data.DataContainer;\nimport org.simexplorer.core.data.DataModifier;\n\n"); |
| | 236 | StringBuilder sequenceNodesConstructors = new StringBuilder(); |
| | 237 | // The class declaration |
| | 238 | typeDeclaration.append("public class "); |
| | 239 | typeDeclaration.append(node.getClassName()).append(" implements DataContainer"); |
| | 240 | if (node.getNode().isModifiable()) { |
| | 241 | typeDeclaration.append(", DataModifier"); |
| | 242 | } |
| | 243 | typeDeclaration.append(" {\n"); |
| | 244 | // fields generation |
| | 245 | for (StructureNode child : node.getNode().getChildrenContent()) { |
| | 246 | String fieldType = null; |
| | 247 | String fieldDeclarationSuffix = ""; |
| | 248 | if (child instanceof Prototype) { |
| | 249 | Prototype p = (Prototype) child; |
| | 250 | fieldType = buildPrototypeAttribute(p); |
| | 251 | } else if (child instanceof SequenceNode) { |
| | 252 | Pair<String, String> result = buildSequenceNodeAttribute(new NodePath<SequenceNode>(node.getClassName(), (SequenceNode) child)); |
| | 253 | fieldType = result.getLeft(); |
| | 254 | fieldDeclarationSuffix = " = []"; |
| | 255 | sequenceNodesConstructors.append("\n").append(result.getRight()); |
| | 256 | } else if (child instanceof ComplexNode) { |
| | 257 | ComplexNode s = (ComplexNode) child; |
| | 258 | toBuild.add(new NodePath<ComplexNode>(node.getClassName(), s)); |
| | 259 | fieldType = buildComplexNodeAttribute(new NodePath<ComplexNode>(node.getClassName(), s)); |
| | 260 | } |
| | 261 | typeDeclaration.append(" public ").append(fieldType).append(" ").append(child.getName()).append(fieldDeclarationSuffix).append(";\n"); |
| | 262 | } |
| | 263 | // methods generation |
| | 264 | typeDeclaration.append(sequenceNodesConstructors); |
| | 265 | typeDeclaration.append(" @Override\n public Object getValue(String name) {\n return this[name];\n }\n"); |
| | 266 | if (node.getNode().isModifiable()) { |
| | 267 | typeDeclaration.append(" @Override\n public void setValue(String name, Object value) {\n this[name] = value;\n }\n"); |
| | 268 | } |
| | 269 | typeDeclaration.append("}\n\n"); |
| | 270 | return typeDeclaration.toString(); |
| | 271 | } |
| | 272 | |
| | 273 | /** |
| | 274 | * Build the type declaration of an attribute defined by a ComplexNode |
| | 275 | * @param node |
| | 276 | * @return |
| | 277 | */ |
| | 278 | private String buildComplexNodeAttribute(NodePath<ComplexNode> node) { |
| | 279 | return node.getClassName(); |
| | 280 | } |
| | 281 | |
| | 282 | /** |
| | 283 | * Build the type declaration of an attribute defined by a SequenceNode. The |
| | 284 | * type generated is a List parametrized according to the inner node of the node. |
| | 285 | * @param nodePath |
| | 286 | * @return |
| | 287 | */ |
| | 288 | private Pair<String, String> buildSequenceNodeAttribute(NodePath<SequenceNode> nodePath) { |
| | 289 | StringBuilder typeDeclaration = new StringBuilder(); |
| | 290 | String elementConstructor = null; |
| | 291 | SequenceNode node = nodePath.getNode(); |
| | 292 | typeDeclaration.append("List<"); |
| | 293 | if (node.getInnerNode() instanceof Prototype) { |
| | 294 | String type = ((Prototype) node.getInnerNode()).getType().getCanonicalName(); |
| | 295 | typeDeclaration.append(type); |
| | 296 | elementConstructor = buildElementConstructor(node.getInnerNode().getName(), type); |
| | 297 | } else if (node.getInnerNode() instanceof SequenceNode) { |
| | 298 | SequenceNode inner = (SequenceNode) node.getInnerNode(); |
| | 299 | Pair<String, String> result = buildSequenceNodeAttribute(new NodePath<SequenceNode>(nodePath.getPath(), inner)); |
| | 300 | typeDeclaration.append(result.getLeft()); |
| | 301 | elementConstructor = result.getRight(); |
| | 302 | } else if (node.getInnerNode() instanceof ComplexNode) { |
| | 303 | ComplexNode inner = (ComplexNode) node.getInnerNode(); |
| | 304 | toBuild.add(new NodePath<ComplexNode>(nodePath.getPath(), inner)); |
| | 305 | String type = buildComplexNodeAttribute(new NodePath<ComplexNode>(nodePath.getPath(), inner)); |
| | 306 | typeDeclaration.append(type); |
| | 307 | elementConstructor = buildElementConstructor(node.getInnerNode().getName(), type); |
| | 308 | } |
| | 309 | typeDeclaration.append(">"); |
| | 310 | return new Pair<String, String>(typeDeclaration.toString(), elementConstructor); |
| | 311 | } |
| | 312 | |
| | 313 | private String buildElementConstructor(String name, String type) { |
| | 314 | StringBuilder elementConstructor = new StringBuilder(); |
| | 315 | elementConstructor.append(" public ").append(type).append(" new").append(name).append("() {\n"); |
| | 316 | elementConstructor.append(" return new ").append(type).append("()\n"); |
| | 317 | elementConstructor.append(" }\n"); |
| | 318 | return elementConstructor.toString(); |
| | 319 | } |
| | 320 | |
| | 321 | /** |
| | 322 | * Build the type declaration of an attribute defined by a Prototype. |
| | 323 | * @param node |
| | 324 | * @return |
| | 325 | */ |
| | 326 | private String buildPrototypeAttribute(Prototype node) { |
| | 327 | return node.getType().getCanonicalName(); |
| | 328 | } |
| | 329 | |
| | 330 | private class NodePath<T extends StructureNode> { |
| | 331 | |
| | 332 | private String path; |
| | 333 | private T node; |
| | 334 | |
| | 335 | public NodePath(String path, T node) { |
| | 336 | this.node = node; |
| | 337 | this.path = path; |
| | 338 | } |
| | 339 | |
| | 340 | String getPath() { |
| | 341 | return path; |
| | 342 | } |
| | 343 | |
| | 344 | T getNode() { |
| | 345 | return node; |
| | 346 | } |
| | 347 | |
| | 348 | String getClassName() { |
| | 349 | return path + classPrefix + node.getName(); |
| | 350 | } |
| | 351 | } |