ch.sahits.phpclassgenerator
Class CodeGenerator

java.lang.Object
  extended by ch.sahits.phpclassgenerator.CodeGenerator

public class CodeGenerator
extends java.lang.Object

The CodeGenerator produces the PHP code for the resulting PHP class. Therefore it uses the XML template parsed by XMLTemplateParser and the DBStructure provided by DBConnection. The template may contain several template or marker strings that are replaced in this class. The replacement needs knowledge of the underling DB structure.
The schema of the XML template can be found at Sahits. The marker strings are defined in Constants.

An example of an XML template file can be found in the root of this plugin: exampleTemplate.xml.

Version:
1.0 $Revision: 1.5 $ changed by $Author: hotzst $ at $Date: 2008/01/01 15:15:47 $
Author:
Andi Hotz, (c) 2007
See Also:
Constants

Field Summary
private  ClassDeclaration _class
           
private  java.lang.String db
           
private  java.lang.String host
           
private  java.lang.String nl
           
private  XMLTemplateParser parser
           
private  java.lang.String passwd
           
private  java.lang.String product
           
private  DBStructure structur
           
private  java.lang.String tableName
           
private  java.lang.String templatePath
           
private  java.lang.String user
           
 
Constructor Summary
CodeGenerator(java.lang.String templatePath, java.lang.String product, java.lang.String host, java.lang.String db, java.lang.String user, java.lang.String passwd, java.lang.String tableName)
          Constructor initializes the connection data
 
Method Summary
 java.lang.String generate()
          Generate the contents of the php file
private  void generateCode4Constants(ClassDeclaration cd)
          The class definition may contain constants defined in Constants that have to be replaced by actual code.
private  java.util.Vector<MethodDeclaration> generateGetterMethods()
          Produce the PHP code for the getter methods
private  MethodDeclaration generateInitGetMethod()
          Produce the PHP code for the initGET method.
private  MethodDeclaration generateInitMethod()
          Produce the PHP code for the init method
private  MethodDeclaration generateInitPostMethod()
          Produce the PHP code for the initPOST method.
private  MethodDeclaration generateInsertMethod(MethodDeclaration methodDeclaration)
          Generate the PHP code for the insert method based on the method fragment.
private  java.util.Vector<MethodDeclaration> generateLoadByFKMethods(MethodDeclaration methodDeclaration)
          Produce the PHP code for the loadbyFK... methods.
private  MethodDeclaration generateLoadUniqueMethods(MethodDeclaration methodDeclaration)
          Produce the PHP code for the loadUnique method.
private  java.util.Vector<MethodDeclaration> generateSetterMethods()
          Produce the PHP code for the setter methods
private  MethodDeclaration generateToStringMethod()
          Produce the PHP code for the toSting methods
private  MethodDeclaration generateUpdateMethod(MethodDeclaration methodDeclaration)
          Generate the PHP code for the update method based on the method fragment.
private  java.lang.String getClassName()
          Get the name of the class by the table name
private  java.lang.String getFieldArray()
          Generate an array PHP style of all the DB fields
private  java.lang.String getWhereClause(java.lang.String[] columns)
          Generate a where clause for the given columns The values of the columns is in a variable name like DBField.getFormatedFieldName()
private  void init()
           
private  java.lang.String replaceAll(java.lang.String needle, java.lang.String haystack, java.lang.String with)
          Re-implementation of the replaceAll method of String to circumvent the inconvinience with regular expressions
private  java.lang.String replaceConstants(java.lang.String input)
          Replace constants in the input
private  java.lang.String[] replaceMember(java.lang.String[] members)
          check all member variable definition.
private  MethodDeclaration[] replaceMethod(MethodDeclaration[] methods)
          Check all methods.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

templatePath

private java.lang.String templatePath

product

private java.lang.String product

host

private java.lang.String host

db

private java.lang.String db

user

private java.lang.String user

passwd

private java.lang.String passwd

tableName

private java.lang.String tableName

parser

private XMLTemplateParser parser

structur

private DBStructure structur

_class

private ClassDeclaration _class

nl

private java.lang.String nl
Constructor Detail

CodeGenerator

public CodeGenerator(java.lang.String templatePath,
                     java.lang.String product,
                     java.lang.String host,
                     java.lang.String db,
                     java.lang.String user,
                     java.lang.String passwd,
                     java.lang.String tableName)
              throws org.xml.sax.SAXException,
                     java.io.IOException,
                     javax.xml.parsers.ParserConfigurationException,
                     java.sql.SQLException
Constructor initializes the connection data

Parameters:
templatePath - path to the XML template to be parsed
product - what kind of DB (MySQL, PostgeSQL, Oracle, Derby, MS SQL Server, ...)
host - of the db eg. localhost
user - user name to connect to the DB
passwd - password of the user
db - or schema of the table
tableName - table name
Throws:
java.sql.SQLException - Connecting failed probably due to incorrect input
org.xml.sax.SAXException - Something in the parsing is gone wrong
java.io.IOException - The template could not be read
javax.xml.parsers.ParserConfigurationException - Bad configuration of the parser.
Method Detail

init

private void init()
           throws org.xml.sax.SAXException,
                  java.io.IOException,
                  javax.xml.parsers.ParserConfigurationException,
                  java.sql.SQLException
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
java.sql.SQLException

generateCode4Constants

private void generateCode4Constants(ClassDeclaration cd)
The class definition may contain constants defined in Constants that have to be replaced by actual code.


replaceMethod

private MethodDeclaration[] replaceMethod(MethodDeclaration[] methods)
Check all methods. When found one with the constant Constants.DB_FIELD_GETTER or Constants.DB_FIELD_SETTER generate the getter or setter methods of the DB fields.

Parameters:
methods - array with method declarations
Returns:
all Method Declarations
See Also:
MethodDeclaration

generateInsertMethod

private MethodDeclaration generateInsertMethod(MethodDeclaration methodDeclaration)
Generate the PHP code for the insert method based on the method fragment.

Returns:
MethodDeclaration describing the insert method

generateUpdateMethod

private MethodDeclaration generateUpdateMethod(MethodDeclaration methodDeclaration)
Generate the PHP code for the update method based on the method fragment.

Returns:
MethodDeclaration describing the insert method

generateLoadByFKMethods

private java.util.Vector<MethodDeclaration> generateLoadByFKMethods(MethodDeclaration methodDeclaration)
Produce the PHP code for the loadbyFK... methods. Take the method fragment, add the appropriate parameter and make adjustments in the method body

Parameters:
methodDeclaration - method declaration fragment
Returns:
MethodDeclaration describing the loadUnique method

generateLoadUniqueMethods

private MethodDeclaration generateLoadUniqueMethods(MethodDeclaration methodDeclaration)
Produce the PHP code for the loadUnique method. Take the method fragment, add the appropriate parameter and make adjustments in the method body

Parameters:
methodDeclaration - method declaration fragment
Returns:
MethodDeclaration describing the loadUnique method

generateInitMethod

private MethodDeclaration generateInitMethod()
Produce the PHP code for the init method

Returns:
MethodDeclaration describing the init method

generateInitPostMethod

private MethodDeclaration generateInitPostMethod()
Produce the PHP code for the initPOST method. It is expected, that the values in the _POST array are named as DBField.getFormatedFieldName() suggests.

Returns:
MethodDeclaration describing the initPOST method

generateInitGetMethod

private MethodDeclaration generateInitGetMethod()
Produce the PHP code for the initGET method. It is expected, that the values in the _POST array are named as DBField.getFormatedFieldName() suggests.

Returns:
MethodDeclaration describing the initGET method

generateGetterMethods

private java.util.Vector<MethodDeclaration> generateGetterMethods()
Produce the PHP code for the getter methods

Returns:
Vector with MethodDeclarations containing the getter methods

generateSetterMethods

private java.util.Vector<MethodDeclaration> generateSetterMethods()
Produce the PHP code for the setter methods

Returns:
Vector with MethodDeclarations containing the getter methods

generateToStringMethod

private MethodDeclaration generateToStringMethod()
Produce the PHP code for the toSting methods

Returns:
MethodDeclaration containing the toString method

replaceMember

private java.lang.String[] replaceMember(java.lang.String[] members)
check all member variable definition. When found one with constant Constants.DB_FIELDS replace it with member variables for all DB fields. If in this member declaration the there is a value assigned that is the initial value for the primary key.

Parameters:
members - array with member variable declarations
Returns:
all member variable declarations

generate

public java.lang.String generate()
Generate the contents of the php file

Returns:
contents of the php file as string

getClassName

private java.lang.String getClassName()
Get the name of the class by the table name

Returns:
formated class name

getWhereClause

private java.lang.String getWhereClause(java.lang.String[] columns)
Generate a where clause for the given columns The values of the columns is in a variable name like DBField.getFormatedFieldName()

Parameters:
columns - Array of the columns in the where clause
Returns:
where clause

replaceConstants

private java.lang.String replaceConstants(java.lang.String input)
Replace constants in the input

Parameters:
input - String
Returns:
input with replaced constants

getFieldArray

private java.lang.String getFieldArray()
Generate an array PHP style of all the DB fields

Returns:
PHP array with all DB fields

replaceAll

private java.lang.String replaceAll(java.lang.String needle,
                                    java.lang.String haystack,
                                    java.lang.String with)
Re-implementation of the replaceAll method of String to circumvent the inconvinience with regular expressions

Parameters:
needle - String to be replaced
haystack - String in which to search
with - replacement string
Returns:
haystack with all occurrences of needle replaced by with