Download Sources

com.liguorien.flex.generator
Class FlexGenerator

java.lang.Object
  extended by com.liguorien.flex.generator.FlexGenerator

public class FlexGenerator
extends java.lang.Object

Main class of FlexGenerator.

The purpose of this class is to generate classes based on the properties defined on a JavaBean.

The generator can easily be customized by creating a FlexGeneratorHandler for each new type of generated classes.

Here is a little exemple on how to use this class :

 FlexGenerator generator = new FlexGenerator();
 
 // add an handler to generate code which will 
 // read and create dom4j element from Java entities
 g.addHandler(new Dom4jBuilderHandler());
 
 // add an handler to generate code which will be
 // the ActionScript version of the JavaEntity
 g.addHandler(new FlexModelHandler());
 
 // add an handler to generated code which will 
 // read and create XML object from an ActionScript entities
 g.addHandler(new FlexModelBuilderHandler());
 
 // set formatting options
 generator.setNewLineBeforeCurlyBrace(false);
 generator.setUsingUnderscore(true);
 
 // indicate to the generator where generate the actionscript and java files
 generator.setOutputMode(OutputMode.FILE);
 generator.setFlexOutputDirectory("./src/flex/");
 generator.setJavaOutputDirectory("./src/java/");
 
 
 // add a single class
 generator.addClass(MyEntity.class);
 
 // add an entire package
 generator.addPackage("com.liguorien.myproject.entities");
 
 // proceed to generation
 generator.generate();
 
 

Version:
0.2
Author:
Nicolas Désy
See Also:
FlexBindable, FlexDefault, FlexTransient, FlexLazy, FlexMap, FlexNode, FlexSet, FlexTransient, OutputMode, FlexGeneratorHandler, AbstractGeneratorHandler, Dom4jBuilderHandler, FlexModelHandler, FlexModelBuilderHandler

Constructor Summary
FlexGenerator()
          Creates a new instance of FlexGenerator
 
Method Summary
 void addClass(java.lang.Class<?> clazz)
          Add a single class
 void addHandler(FlexGeneratorHandler handler)
          Add an handler to this generator.
 void addPackage(java.lang.String packageName)
          Add an entire package.
 void generate()
          Proceed to generation.
 java.lang.String getClassPrefix()
          Get the class prefix
 java.lang.String getClassSuffix()
          Get the class suffix
static GeneratorContext getContext()
           
 java.io.File getFlexOutputDirectory()
          Returns the Flex output directory
 java.lang.String getIndentation(int count)
          Get the indentation string for a given indentation count
 java.lang.String getIndentationString()
          Getter for property indentationString.
 java.io.File getJavaOutputDirectory()
          Getter for property javaOutputDirectory.
 java.util.Locale getLocale()
          Getter for property locale.
 java.lang.String getMethodName(java.lang.Class<?> clazz, java.lang.Object... params)
          Writes the result of writeMethodName(java.io.Writer, java.lang.Class, java.lang.Object...) into a StringWriter and return the result as a String.
static java.lang.String getNodeName(java.lang.Class<?> clazz)
          Return the node name of an entity class.
static java.lang.String getNodeName(java.beans.PropertyDescriptor prop)
          Return the node name of a property.
 OutputMode getOutputMode()
          Return the current output mode.
 java.lang.String getPackageName(java.lang.Class<?> clazz)
          Returns the package name of the generated class.
 boolean isGeneratedClass(java.lang.Class<?> clazz)
          Determine if the class sent is a generated class.
static boolean isLazy(java.beans.PropertyDescriptor prop, LazyMode mode)
          Determines if a property is lazy.
 boolean isNewLineBeforeCurlyBrace()
          Indicates if a new line should be added between package/class/method declaration and the curly brace.
 boolean isUsingUnderscore()
          Indicates if an underscore(_) should be added to the private field name.
 void setClassPrefix(java.lang.String classPrefix)
          Assign a prefix which will be added to the AS3 class name.
 void setClassSuffix(java.lang.String classSuffix)
          Assign a suffix which will be added to the AS3 class name.
 void setFlexOutputDirectory(java.io.File dir)
          Set the output directory used by the generator.
 void setFlexOutputDirectory(java.lang.String dir)
          Set the output directory used by the generator.
 void setIndentationString(java.lang.String indentationString)
          Setter for property indentationString.
 void setJavaOutputDirectory(java.io.File dir)
          Setter for property javaOutputDirectory.
 void setJavaOutputDirectory(java.lang.String dir)
          Setter for property javaOutputDirectory.
 void setLocale(java.util.Locale locale)
          Setter for property locale.
 void setNewLineBeforeCurlyBrace(boolean newLineBeforeCurlyBrace)
          Indicates if a new line should be added between package/class/method declaration and the curly brace.
 void setOutputMode(OutputMode outputMode)
          Set the output mode that will be used by the generator.
 void setPackage(ClassType type, java.lang.String path)
          Set the destination package for a given ClassType.
 void setUsingUnderscore(boolean usingUnderscore)
          Indicates if an underscore(_) should be added to the private field name.
 void writeComment(java.io.Writer w, java.lang.String comment, int indentation)
          Write a JavaDoc-styled comment.
 void writeCurlyBrace(java.io.Writer w, int indentationCount)
          Write an opening curly brace.
 void writeDocumentation(java.io.Writer w, java.lang.Class<?> clazz, int indentation, java.lang.Object... params)
          Write internationalized document for a given class.
 void writeIndentation(java.io.Writer w, int indentationCount)
          Write indentation to a writer
 void writeMethodName(java.io.Writer w, java.lang.Class<?> clazz, java.lang.Object... params)
          Write the name of the method of a given class.
<T extends FlexGeneratorHandler>
void
writeMethods(java.io.Writer w, T handler, java.util.Collection<MethodWriter<T>> methods, java.lang.Class<?> clazz)
          Iterates over a List of MethodWriter and let them write code if they accept to class sent in parameters.
static
<T extends FlexGeneratorHandler>
void
writeProperties(java.io.Writer w, FlexGenerator g, T handler, java.lang.String varName, java.util.List<java.beans.PropertyDescriptor> props, java.util.List<PropertyWriter<T>> writers, LazyMode mode)
          Write a List of properties to a given Writer.
static
<T extends FlexGeneratorHandler>
void
writeProperty(java.io.Writer w, FlexGenerator g, T handler, java.lang.String varName, java.beans.PropertyDescriptor prop, java.util.List<PropertyWriter<T>> writers, LazyMode mode)
          Write a single property to a given Writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FlexGenerator

public FlexGenerator()
Creates a new instance of FlexGenerator

Method Detail

setPackage

public void setPackage(ClassType type,
                       java.lang.String path)

Set the destination package for a given ClassType.

By default, the path will be the same as the Java entity class. If the path begins with a slash(/), then the output package will be to this absolute path. If the slash is omitted, the package is relative to the Java entity package.

Exemple :

Let's supose that we are generating classes for the entity com.liguorien.myapp.entities.MyEntity

g.setPackage(ClassType.CLIENT_BUILDERS, "builder") com.mycompany.myapp.entities.builder.MyEntityBuilder
g.setPackage(ClassType.CLIENT_BUILDERS, "../builder") com.mycompany.myapp.builder.MyEntityBuilder
g.setPackage(ClassType.CLIENT_BUILDERS, "/org.otherproject.builder") org.otherproject.builder.MyEntityBuilder

Parameters:
type - A ClassType
path - The path used to generated the classes.

addHandler

public void addHandler(FlexGeneratorHandler handler)

Add an handler to this generator.

Parameters:
handler - A FlexGeneratorHandler instance.

isGeneratedClass

public boolean isGeneratedClass(java.lang.Class<?> clazz)

Determine if the class sent is a generated class.

Parameters:
clazz - A class
Returns:
A boolean which indicates if the class will be generated in this session.

generate

public void generate()

Proceed to generation.


addClass

public void addClass(java.lang.Class<?> clazz)
Add a single class

Parameters:
clazz - A class which represents a Java entity

addPackage

public void addPackage(java.lang.String packageName)

Add an entire package. Every classes in the package will be processed

Classes can be excluded with the FlexTransient annotation.

Parameters:
packageName - The name of the package (ie. "com.liguorien.myproject.entities")
See Also:
FlexTransient

setFlexOutputDirectory

public void setFlexOutputDirectory(java.io.File dir)

Set the output directory used by the generator. It should be the classpath of your flex projet.

Parameters:
dir - A File object which represents the classpath of your flex project

setFlexOutputDirectory

public void setFlexOutputDirectory(java.lang.String dir)

Set the output directory used by the generator. It should be the classpath of your flex projet.

Parameters:
dir - A String which represents the classpath of your flex project

getFlexOutputDirectory

public java.io.File getFlexOutputDirectory()

Returns the Flex output directory

Returns:
A File object

isUsingUnderscore

public boolean isUsingUnderscore()

Indicates if an underscore(_) should be added to the private field name.

Returns:
Value of property usingUnderscore.

setUsingUnderscore

public void setUsingUnderscore(boolean usingUnderscore)

Indicates if an underscore(_) should be added to the private field name.

The default value is true.

Parameters:
usingUnderscore - New value of property usingUnderscore.

isNewLineBeforeCurlyBrace

public boolean isNewLineBeforeCurlyBrace()

Indicates if a new line should be added between package/class/method declaration and the curly brace.

Returns:
Value of property newLineBeforeCurlyBrace.

setNewLineBeforeCurlyBrace

public void setNewLineBeforeCurlyBrace(boolean newLineBeforeCurlyBrace)

Indicates if a new line should be added between package/class/method declaration and the curly brace.

The default value is false.

Parameters:
newLineBeforeCurlyBrace - New value of property newLineBeforeCurlyBrace.

getOutputMode

public OutputMode getOutputMode()
Return the current output mode.

Returns:
Value of property outputMode.

setOutputMode

public void setOutputMode(OutputMode outputMode)

Set the output mode that will be used by the generator. The default value is OutputMode.CONSOLE.

Parameters:
outputMode - The output mode.
See Also:
OutputMode

getClassPrefix

public java.lang.String getClassPrefix()
Get the class prefix

Returns:
Value of property classPrefix.

setClassPrefix

public void setClassPrefix(java.lang.String classPrefix)

Assign a prefix which will be added to the AS3 class name.

For example, if you have a class "Person" and the prefix is "Model", the AS3 class will be named "ModelPerson".

Parameters:
classPrefix - The prefix.

getClassSuffix

public java.lang.String getClassSuffix()
Get the class suffix

Returns:
Value of property classSuffix.

setClassSuffix

public void setClassSuffix(java.lang.String classSuffix)

Assign a suffix which will be added to the AS3 class name.

For example, if you have a class "Person" and the suffix is "Model", the AS3 class will be named "PersonModel".

Parameters:
classSuffix - The suffix.

getJavaOutputDirectory

public java.io.File getJavaOutputDirectory()
Getter for property javaOutputDirectory.

Returns:
Value of property javaOutputDirectory.

setJavaOutputDirectory

public void setJavaOutputDirectory(java.io.File dir)
Setter for property javaOutputDirectory.

Parameters:
dir - New value of property javaOutputDirectory.

setJavaOutputDirectory

public void setJavaOutputDirectory(java.lang.String dir)
Setter for property javaOutputDirectory.

Parameters:
dir - New value of property javaOutputDirectory.

isLazy

public static boolean isLazy(java.beans.PropertyDescriptor prop,
                             LazyMode mode)

Determines if a property is lazy.

Parameters:
prop - The property to check
mode - The LazyMode expected
Returns:
a boolean value
See Also:
FlexLazy, LazyMode

getPackageName

public java.lang.String getPackageName(java.lang.Class<?> clazz)

Returns the package name of the generated class.

Parameters:
clazz - The current generated class.
Returns:
The package name of the generated class.
Throws:
java.lang.IllegalArgumentException - If the generator is misconfigurated.

writeProperties

public static <T extends FlexGeneratorHandler> void writeProperties(java.io.Writer w,
                                                                    FlexGenerator g,
                                                                    T handler,
                                                                    java.lang.String varName,
                                                                    java.util.List<java.beans.PropertyDescriptor> props,
                                                                    java.util.List<PropertyWriter<T>> writers,
                                                                    LazyMode mode)
                            throws java.io.IOException

Write a List of properties to a given Writer.

Parameters:
w - The Writer used to write the properties.
g - The current FlexGenerator instance.
handler - The current FlexGeneratorHandler.
varName - The name of the instance where the properties are accessed.
props - A List of PropertyDescriptor
writers - A List of PropertyWriter used to write the code to access the property.
mode - The LazyMode for the context where properties will be accessed.
Throws:
java.io.IOException - If an IO error occurs.

writeProperty

public static <T extends FlexGeneratorHandler> void writeProperty(java.io.Writer w,
                                                                  FlexGenerator g,
                                                                  T handler,
                                                                  java.lang.String varName,
                                                                  java.beans.PropertyDescriptor prop,
                                                                  java.util.List<PropertyWriter<T>> writers,
                                                                  LazyMode mode)
                          throws java.io.IOException

Write a single property to a given Writer.

Parameters:
w - The Writer used to write the property.
g - The current FlexGenerator instance.
handler - The current FlexGeneratorHandler.
varName - The name of the instance where the property is accessed.
prop - A PropertyDescriptor
writers - A List of PropertyWriter used to write the code to access the property.
mode - The LazyMode for the context where the property will be accessed.
Throws:
java.io.IOException - If an IO error occurs.

writeMethods

public <T extends FlexGeneratorHandler> void writeMethods(java.io.Writer w,
                                                          T handler,
                                                          java.util.Collection<MethodWriter<T>> methods,
                                                          java.lang.Class<?> clazz)
                  throws java.io.IOException

Iterates over a List of MethodWriter and let them write code if they accept to class sent in parameters.

Parameters:
w - The writer used to write the generated code.
handler - The current handler.
methods - A List of MethodWriter.
clazz - The current class.
Throws:
java.io.IOException - If an IO error occurs.

writeIndentation

public void writeIndentation(java.io.Writer w,
                             int indentationCount)
                      throws java.io.IOException
Write indentation to a writer

Parameters:
w - A Writer instance
indentationCount - The indentation count.
Throws:
java.io.IOException - If an IO error occurs.

getIndentation

public java.lang.String getIndentation(int count)
Get the indentation string for a given indentation count

Parameters:
count - The indentation count.
Returns:
An indentation String.

writeCurlyBrace

public void writeCurlyBrace(java.io.Writer w,
                            int indentationCount)
                     throws java.io.IOException

Write an opening curly brace. It check in config if a new line must be added before the curly.

Parameters:
w - A Writer
indentationCount - The current indentation level
Throws:
java.io.IOException - If an IO error occurs.
See Also:
setNewLineBeforeCurlyBrace(boolean)

getNodeName

public static java.lang.String getNodeName(java.beans.PropertyDescriptor prop)

Return the node name of a property.

Parameters:
prop - A PropertyDescriptor.
Returns:
The node name.

getNodeName

public static java.lang.String getNodeName(java.lang.Class<?> clazz)

Return the node name of an entity class.

Parameters:
clazz - An entity class.
Returns:
The node name.

getIndentationString

public java.lang.String getIndentationString()
Getter for property indentationString.

Returns:
Value of property indentationString.

setIndentationString

public void setIndentationString(java.lang.String indentationString)
Setter for property indentationString.

Parameters:
indentationString - New value of property indentationString.

writeComment

public void writeComment(java.io.Writer w,
                         java.lang.String comment,
                         int indentation)
Write a JavaDoc-styled comment.

Parameters:
w - A Writer
comment - The comment to write
indentation - The current indentation level

getContext

public static GeneratorContext getContext()

writeDocumentation

public void writeDocumentation(java.io.Writer w,
                               java.lang.Class<?> clazz,
                               int indentation,
                               java.lang.Object... params)

Write internationalized document for a given class.

Parameters:
w - A Writer
clazz - The target class
indentation - The current indentation level
params - String params sent to the MessageFormat

writeMethodName

public void writeMethodName(java.io.Writer w,
                            java.lang.Class<?> clazz,
                            java.lang.Object... params)
                     throws java.io.IOException
Write the name of the method of a given class.

Parameters:
w - A Writer
clazz - The target class
params - The parameters sent to the MessageFormat
Throws:
java.io.IOException

getMethodName

public java.lang.String getMethodName(java.lang.Class<?> clazz,
                                      java.lang.Object... params)

Writes the result of writeMethodName(java.io.Writer, java.lang.Class, java.lang.Object...) into a StringWriter and return the result as a String.

Parameters:
clazz - The target class
params - The parameters sent to the MessageFormat
Returns:
The method name

getLocale

public java.util.Locale getLocale()
Getter for property locale.

Returns:
Value of property locale.

setLocale

public void setLocale(java.util.Locale locale)
Setter for property locale.

Parameters:
locale - New value of property locale.

FlexGenerator

Copyright 2006 Nicolas Désy. All rights reserved.