package com.att.nga.parser.util; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import com.att.nga.parser.EntryPointDirectory; import com.att.nga.parser.nomad.Application; import com.att.nga.parser.nomad.BusinessRule; import com.att.nga.parser.nomad.Callout; import com.att.nga.parser.nomad.Change; import com.att.nga.parser.nomad.DataStatement; import com.att.nga.parser.nomad.DataStatementType; import com.att.nga.parser.nomad.DbAdd; import com.att.nga.parser.nomad.Declare; import com.att.nga.parser.nomad.EntryBlock; import com.att.nga.parser.nomad.Field; import com.att.nga.parser.nomad.Form; import com.att.nga.parser.nomad.Procedure; import com.att.nga.parser.nomad.RKey; import com.att.nga.parser.nomad.SQL; import com.att.nga.parser.nomad.SqlLink; import com.att.nga.util.Platform; import com.att.nga.util.Tool; public class NomadAppLoader extends Tool { private Application nomadApplication = null; private EntryPointDirectory entryPointDirectory = null; private DomLoaderDom4j domLoader = null; private ProcedurePurposes procPurposes = null; public NomadAppLoader(Platform platform) { this.entryPointDirectory = new EntryPointDirectory(platform); //init(); procPurposes = new ProcedurePurposes(platform); domLoader = new DomLoaderDom4j(platform); } public Application load() { Document doc = null; try { doc = domLoader.parse(); } catch (DocumentException de) { System.out.println("ERROR - exception loading Nomad Application"); System.out.println(" - exception: " + de); de.printStackTrace(); return null; } // only here if successful XML Document loading... nomadApplication = new Application("nga"); nomadApplication.setEntryPointsDirectory(entryPointDirectory.getDir()); //logln("EntryPoints: " + app.getEntryPointsDirectory()); Element root = doc.getRootElement(); // iterate through child elements of root String procName = null; String procFile = null; for ( Iterator procIt = root.elementIterator(); procIt.hasNext(); ) { Element element = (Element) procIt.next(); String elementType = element.getName(); if ("procedure".equals(elementType)) { loadProc(nomadApplication, element); } else if ("globalDeclarations".equals(elementType)) { // load global Declarations here... } } return nomadApplication; } /** * Load procedure, IF the procedure hasn't already been loaded as * a callout during the processing of another procedure. * * @param app * @param procElement */ void loadProc(Application app, Element procElement) { String elementType = procElement.getName(); String procName = procElement.attributeValue("name"); Procedure proc = nomadApplication.produceProcedure(procName); proc.setPurpose(procPurposes.getPurpose(procName)); for ( Iterator entryIt = procElement.elementIterator(); entryIt.hasNext(); ) { Element element = (Element) entryIt.next(); if ("entry".equals(element.getName())) { loadEntry(proc, element); } else { logln("ERROR. Unexpected element encountered while processing procedure (" + procName +"):" + element.toString()); } } } void loadEntry(Procedure proc, Element entryElement) { String elementType = entryElement.getName(); String entryName = entryElement.attributeValue("name"); EntryBlock entry = null; if (entryName.equals("")) { /* * An unnamed entry point (UEP) is the default EP for the procedure. * For our purposes, when an UEP in the XML, it will be used * to augment the default EP that was created when the Procedure * was parsed. The default EP will contain extra info like the * purpose comment that can occur in the Proc *before* the UEP. * This information should be retained. * Other information, like the UEP code should replace the default * EP's information. */ EntryBlock defaultEntry = proc.getEntryPoint(proc.getName()); if (defaultEntry != null) { entry = defaultEntry; } else { entry = new EntryBlock(proc, proc.getName()); proc.addEntryPoint(entry); } } else { entry = new EntryBlock(proc, entryName); proc.addEntryPoint(entry); } //logln("element - : " + elementType + " : " + entryName); Element subElement = null; for ( Iterator entryIt = entryElement.elementIterator(); entryIt.hasNext(); ) { subElement = (Element) entryIt.next(); //logln("element - s: " +subElement.asXML()); processEntrySubElement(proc, entry, subElement); } } void processEntrySubElement(Procedure proc, EntryBlock entry, Element element) { String subType = element.getName(); //logln(" entry subE: " + subType + " :: " + codeLine); if ("purpose".equals(subType)) { proc.setPurpose(element.getText()); } else if ("businessRule".equals(subType)) { processBusinessRule(entry, element); } else if ("params".equals(subType)) { processParams(entry, element); } else if ("code".equals(subType)) { processCode(entry, element); } else if ("declare".equals(subType)) { processDeclare(entry, element); } else if ("form".equals(subType)) { processForm(entry, element); } else if ("window".equals(subType)) { processWindow(entry, element); } else if ("sqllink".equals(subType)) { processSqlLink(entry, element); } else if ("callout".equals(subType)) { processCallout(entry, element); } else if ("dbadd".equals(subType)) { processDbAdd(entry, element); } else if ("rkey".equals(subType)) { processRkey(entry, element); } else if ("change".equals(subType)) { processDataStmt(entry, element, DataStatementType.Change); } else if ("delete".equals(subType)) { processDataStmt(entry, element, DataStatementType.Delete); } else if ("insert".equals(subType)) { processDataStmt(entry, element, DataStatementType.Insert); } else if ("replace".equals(subType)) { processDataStmt(entry, element, DataStatementType.Replace); } else if ("update".equals(subType)) { processDataStmt(entry, element, DataStatementType.Update); } else if ("sql".equals(subType)) { // processDataStmt(entry, element, DataStatementType.SQL); processSql(entry, element); } } void processCode(EntryBlock entry, Element element) { String codeLine = element.getStringValue(); entry.setCode(codeLine); } void processBusinessRule(EntryBlock entry, Element element) { String code = element.getText(); // System.out.println("**** LOADING BUSINESS RULE: " + code); BusinessRule bizRule = new BusinessRule(code); entry.addLine(bizRule); } void processParams(EntryBlock entry, Element element) { for (Iterator paramIt = element.elementIterator("param"); paramIt.hasNext(); ) { Element paramElement = (Element)paramIt.next(); entry.addParam(paramElement.getText()); } } void processDeclare(EntryBlock entry, Element element) { String code = element.elementText("code"); Declare declare = new Declare(code); entry.addDeclare(declare); } void processForm(EntryBlock entry, Element element) { String code = element.elementText("code"); Form form = new Form(code); entry.addForm(form); Element fieldElement = null; for ( Iterator entryIt = element.elementIterator("field"); entryIt.hasNext(); ) { fieldElement = (Element) entryIt.next(); form.addField(new Field(fieldElement.attributeValue("name"))); } } void processWindow(EntryBlock entry, Element element) { String code = element.elementText("code"); } void processSqlLink(EntryBlock entry, Element element) { String code = element.elementText("code"); SqlLink link = new SqlLink(code); entry.addSqlLink(link); } void processCallout(EntryBlock entry, Element element) { String code = element.elementText("code"); Callout callout = Callout.getInstance(code, entryPointDirectory); entry.addCallout(callout); logln("callout from: " + entry.getName() + "\t to: " + callout.getName()); nomadApplication.registerCall(callout.getName(), entry.getName()); } void processDbAdd(EntryBlock entry, Element element) { String code = element.elementText("code"); DbAdd dba = new DbAdd(code); entry.addDbAdd(dba); } void processRkey(EntryBlock entry, Element element) { String code = element.elementText("code"); RKey elem = new RKey(code); entry.addRkey(elem); } void processChange(EntryBlock entry, Element element) { String code = element.elementText("code"); Change change = new Change(code); entry.addChange(change); } void processDataStmt(EntryBlock entry, Element element, DataStatementType type) { String code = element.elementText("code"); DataStatement dataStmt = new DataStatement(type, code); entry.addDataStatement(dataStmt); } void processSql(EntryBlock entry, Element element) { String code = element.elementText("code"); SQL sql = new SQL(code); entry.addSqlStmt(sql); } }