Java tutorial
/******************************************************************************* * Copyright (c) 2012 Cesar Yeep. * All rights reserved. This program and the accompanying materials * are made available under the terms of the BSD 3-Clause License * ("New BSD" or "BSD Simplified") which accompanies this distribution, * and is available at * http://opensource.org/licenses/BSD-3-Clause * * Contributors: * Cesar Yeep - initial API and implementation ******************************************************************************/ package edu.utep.cs.jasg.specificationGenerator.documentGenerator; import java.util.Iterator; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Element; /** Interface to define ASTBehavior document operations. */ public class ASTDocumentFactory { //TODO: need to import JastAdd module to extract needed AST node behavior private StringBuffer document = new StringBuffer(); private Element astRoot; public ASTDocumentFactory(Element astRoot) { this.astRoot = astRoot; } public String generateDocument() { document.append(documentHeader()); parseASTNodes(); return document.toString(); } protected void parseASTNodes() { Element definitionsElement = astRoot.getChild("ast_definitions"); if (definitionsElement != null) { //Get set of AST definitions List<Element> definitionsList = definitionsElement.getChildren(); Iterator<Element> definitionsListIterator = definitionsList.iterator(); //Add existing AST definitions while (definitionsListIterator.hasNext()) { document.append(parseASTDefinition(definitionsListIterator.next())); document.append("\n"); } } } private String parseASTDefinition(Element astDefinitionElement) { StringBuffer astDefinition = new StringBuffer(); Element astName = astDefinitionElement.getChild("ast_name"); Element astSuperClass = astDefinitionElement.getChild("ast_superClass"); Attribute abstractAttribute = astDefinitionElement.getAttribute("abstract"); Element astChildren = astDefinitionElement.getChild("ast_children"); //Check if class is abstract if (abstractAttribute.equals("true")) { astDefinition.append("abstract "); } //Append AST node name astDefinition.append(astName.getText()); //Check if node extends an existing AST node //TODO: this is where the tool should check if the parent is valid and //necessary AST behaviors to implement if (astSuperClass != null) { astDefinition.append(": " + astSuperClass.getText()); } //Parse AST children if (astChildren != null) { //Get set of existing AST children elements List<Element> astChildrenList = astChildren.getChildren(); Iterator<Element> astChildrenIterator = astChildrenList.iterator(); //Append children operator if (astChildrenList.size() > 0) astDefinition.append(" ::= "); //Add AST children to definition while (astChildrenIterator.hasNext()) { astDefinition.append(astChildrenIterator.next().getText()); if (astChildrenIterator.hasNext()) astDefinition.append(" "); } } astDefinition.append(";\n"); return astDefinition.toString(); } /** Return document in a StringBuffer. */ public StringBuffer getDocument() { return document; } //Template methods /** Document header. */ protected String documentHeader() { return "//JastAdd ast file generated by JASG\n"; } }