[Idrs-commit] CVS: Idrs/dev/src/net/sourceforge/idrs/deploy/compile Compiler.java,NONE,1.1 CompilerS
Brought to you by:
bigman921
|
From: Marc B. <big...@us...> - 2002-01-30 01:43:17
|
Update of /cvsroot/idrs/Idrs/dev/src/net/sourceforge/idrs/deploy/compile
In directory usw-pr-cvs1:/tmp/cvs-serv14554/dev/src/net/sourceforge/idrs/deploy/compile
Added Files:
Compiler.java CompilerState.java RMLHandler.java
Log Message:
Built the initial compilation system. new package net.sourceforge.idrs.deploy.compile has been added with three new classes. Not tested yet though. Javadoc's re-ran and updated.
--- NEW FILE: Compiler.java ---
/*
Compiler.java
Copyright (C) 2001 Marc Boorshtein
The contents of this file are subject to the Mozilla Public License Version 1.0 (the License);
you may not use this file except in compliance with the License. You may obtain a copy of the
License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific
language governing rights and limitations under the License.
*/
package net.sourceforge.idrs.deploy.compile;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.util.*;
/**
* Abstract class extended by RML compilation units
* @author Marc Boorshtein
*/
public abstract class Compiler {
Attributes atts;
CompilerState state;
/**
*allows for a compiler to use the attributes of the calling tag
*/
public void setAttributes(Attributes atts) {
this.atts = atts;
}
/**
*allows for the compiler to work with the current state of the page compilation
*/
public void setState(CompilerState state) {
this.state = state;
}
/**
*allows for the execution of any wrap-up code at the hitting of the end tag
*/
public abstract void seal();
}
--- NEW FILE: CompilerState.java ---
/*
* CompilerState.java
*
* Created on January 28, 2002, 9:25 AM
*/
package net.sourceforge.idrs.deploy.compile;
import java.util.*;
import net.sourceforge.idrs.core.report.*;
/**
*
* @author mlb
* @version
*/
public class CompilerState {
java.util.Stack nodes;
Line currentLine;
RepeatLine currRepLine;
IDRSRep rep;
/** Creates new CompilerState */
public CompilerState() {
rep = new IDRSRep(new IDRSHead(),new IDRSBody());
nodes = new Stack();
}
/**
*Pushes a new <code>Compiler</code> instance onto the stack
*@param node New <code>Compiler</code> instace
*/
public void pushNode(Compiler node) {
nodes.push(node);
}
/**
*Pops current <code>Compiler</code> instance from the stack
*/
public Compiler popNode() {
return (Compiler) nodes.pop();
}
/**
*Peeks at current <code>Compiler</code> instance from the stack
*/
public Compiler peekNode() {
return (Compiler) nodes.peek();
}
/**
*Retrieves the <code>Line</code> currently being worked on
*/
public Line getCurrentLine() {
return this.currentLine;
}
/**
*Retrieves the <code>RepeatLine</code> currently being worked on
*/
public RepeatLine getCurrentRepeatLine() {
return this.currRepLine;
}
/**
*Retrieves the final compiled rml page
*/
public IDRSRep getReport() {
return this.rep;
}
}
--- NEW FILE: RMLHandler.java ---
/*
RMLHandler.java
Copyright (C) 2001 Marc Boorshtein
The contents of this file are subject to the Mozilla Public License Version 1.0 (the License);
you may not use this file except in compliance with the License. You may obtain a copy of the
License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific
language governing rights and limitations under the License.
*/
package net.sourceforge.idrs.deploy.compile;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.util.*;
import net.sourceforge.idrs.core.report.*;
import java.lang.reflect.*;
/**
*XMLHandler used to parse an RML page and call the appropriate Compiler classes
* @author Marc Boorshtein
*/
public class RMLHandler extends DefaultHandler {
StringBuffer currBuff;
HashMap classes;
HashMap methods;
boolean isMethod;
CompilerState state;
Attributes atts;
Stack isMethodState;
public static String BASE_PACKAGE = "net.sourceforge.idrs.deploy.compile.units";
/** Creates new RMLHandler */
public RMLHandler() {
classes = new HashMap();
methods = new HashMap();
state = new CompilerState();
isMethodState = new Stack();
}
/**
*Executed at the beginning of a tag, performs the following tasks:<br>
*<ul>
* <li>Determine if the tag is associated with a class or a method</li>
* <li>If the tag is a class, initialize it and call the <code>setAttributes</code> and <code>setState</code> methods of the class</li>
* <li>If the tag is a method, store pertinent information</li>
*</ul>
*/
public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException {
currBuff = new StringBuffer();
Class compileClass;
Compiler compiler;
String className = RMLHandler.BASE_PACKAGE + getClassName(localName);
compileClass = (Class) classes.get(className);
if (compileClass == null) {
try {
compileClass = Class.forName(className);
this.isMethod=false;
classes.put(className,compileClass);
}
catch (ClassNotFoundException cnf) {
this.isMethod = true;
this.atts = atts;
}
if (compileClass != null) {
try {
compiler = (Compiler) compileClass.newInstance();
state.pushNode(compiler);
compiler.setAttributes(atts);
compiler.setState(state);
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}
this.isMethodState.push(new Boolean(isMethod));
}
}
/**
*Executed at the end of a tag, performs the following tasks:<br>
*<ul>
* <li>If the tag is a method, execute the method on the top of the compiler state</li>
* <li>If the tag is a class, pop the state stack and call the <code>seal</code> method</li>
*</ul>
*/
public void endElement(String namespaceURI, String localName, String rawName) throws SAXException {
String methodName = getMethodName(localName);
Compiler curr;
Class[] paramTypes = {String.class, Attributes.class};
Object[] params = {currBuff.toString(),this.atts};
Method meth;
this.isMethod = ((Boolean) this.isMethodState.pop()).booleanValue();
if (this.isMethod) {
curr = state.peekNode();
meth = (Method) this.methods.get(methodName);
if (meth == null) {
try {
meth = curr.getClass().getMethod(methodName,paramTypes);
}
catch (Exception e1) {
e1.printStackTrace(System.out);
}
this.methods.put(methodName,meth);
}
try {
meth.invoke(curr,params);
}
catch (Exception e2) {
e2.printStackTrace(System.out);
}
}
else {
curr = state.popNode();
curr.seal();
}
}
public void characters(char[] ch, int start, int end) {
for (int i=start;i<start + end;i++) {
currBuff.append(ch[i]);
}
}
/**
*Retrieves the compiled report
*/
public IDRSRep getCompiledReport() {
return state.getReport();
}
/**
*Generates a full class name by capitalizing the first character of the tag and tacking
*it onto the end of the <code>RMLHandler.BASE_PACKAGE</code>
*/
protected String getClassName(String tagName) {
String upper = tagName.toUpperCase();
return RMLHandler.BASE_PACKAGE + "." + upper.substring(0,1) + tagName.substring(1);
}
/**
*Generates a method name by lower-casing the first letter
*/
protected String getMethodName(String tagName) {
String lower = tagName.toLowerCase();
return lower.substring(0,1) + tagName.substring(1);
}
}
|