Author: alexsmirnov
Date: 2009-08-12 20:49:45 -0400 (Wed, 12 Aug 2009)
New Revision: 15163
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java
Log:
Change classes structure according to
http://www.jboss.org/community/wiki/CdkGeneratorArchitecture
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java 2009-08-12
22:44:17 UTC (rev 15162)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java 2009-08-13
00:49:45 UTC (rev 15163)
@@ -24,30 +24,39 @@
package org.richfaces.cdk;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.PrintWriter;
-import org.richfaces.builder.model.JavaClass;
-import org.richfaces.builder.render.JavaClassRenderer;
-import org.richfaces.builder.templates.TemplateReader;
+import org.richfaces.builder.templates.RendererTemplateParser;
+import org.richfaces.cdk.CdkContext.SourceType;
+import org.richfaces.cdk.CdkWriter.OutputType;
import org.richfaces.cdk.apt.CdkCompiler;
+import org.richfaces.cdk.apt.CdkProcessor;
import org.richfaces.cdk.apt.ComponentProcessor;
+import org.richfaces.cdk.generate.java.ComponentClassGenerator;
import org.richfaces.cdk.model.ComponentLibrary;
+import org.richfaces.cdk.xmlconfig.FacesConfigParser;
/**
- * <p class="changed_added_4_0">That class builds JSF library model from
different sources. It acts as "controller" for whole CDK generation
process.</p>
+ * <p class="changed_added_4_0">
+ * That class builds JSF library model from different sources. It acts as
+ * "controller" for whole CDK generation process.
+ * </p>
+ *
* @author asmirnov(a)exadel.com
- *
+ *
*/
public final class LibraryBuilder {
-
+
/**
- * <p class="changed_added_4_0">Current CDK context</p>
- */
+ * <p class="changed_added_4_0">
+ * Current CDK context
+ * </p>
+ */
private final CdkContext context;
-
+
/**
- * <p class="changed_added_4_0"></p>
+ * <p class="changed_added_4_0">
+ * </p>
+ *
* @param context
*/
LibraryBuilder(CdkContext context) {
@@ -55,87 +64,111 @@
}
/**
- * <p class="changed_added_4_0">Static factory method</p>
+ * <p class="changed_added_4_0">
+ * Static factory method
+ * </p>
+ *
* @return
*/
public static LibraryBuilder createInstance(CdkContext context) {
return new LibraryBuilder(context);
}
-
+
/**
- * <p class="changed_added_4_0">Parse source files for annotations and
populate CDK-related information into model.</p>
- * @param sources Java Source files.
+ * <p class="changed_added_4_0">
+ * Parse source files for annotations and populate CDK-related information
+ * into model.
+ * </p>
+ *
+ * @param sources
+ * Java Source files.
* @return generated library model.
* @throws CdkException
*/
- public ComponentLibrary buildModelFromAnnotations(Iterable<File> sources) throws
CdkException {
- CdkCompiler compiler = CdkCompiler.create(getContext());
- ComponentLibrary library = createLibrary();
- ComponentProcessor componentProcessor = new ComponentProcessor(getContext(),library);
- compiler.process(sources, componentProcessor);
- return library;
+ public ComponentLibrary buildModel(SourceType type) throws CdkException {
+ ModelBuilder modelBuilder = getBuilderFor(type);
+ return modelBuilder.build();
}
/**
- * <p class="changed_added_4_0">Compile and store in the model Renderer
templates.</p>
+ * <p class="changed_added_4_0">
+ * Compile and store in the model Renderer templates.
+ * </p>
+ *
* @param templates
* @throws CdkException
*/
- public ComponentLibrary compileRendererTemplates(Iterable<File> templates) throws
CdkException {
- ComponentLibrary library = createLibrary();
-
- try {
- for (File file : templates) {
- JavaClass javaClass = TemplateReader.parse(new FileInputStream(file));
-
- String fullName = javaClass.getFullName();
-
- File outFile = new File(context.getJavaSourceOutput(),
- fullName.replace('.', '/') + ".java");
-
- if (outFile.exists()) {
- outFile.delete();
- }
-
- outFile.getParentFile().mkdirs();
-
- new JavaClassRenderer().render(javaClass, new PrintWriter(outFile));
+ public ComponentLibrary buildModel() throws CdkException {
+ ComponentLibrary library = null;
+ for (SourceType type : StandardSources.values()) {
+ ComponentLibrary model = buildModel(type);
+ if (null != library) {
+ library.apply(model);
+ } else {
+ library = model;
}
- } catch (Exception e) {
- // TODO: handle exception
- throw new CdkException(e.getMessage(), e);
}
-
return library;
}
+
/**
- * <p class="changed_added_4_0">Append configuration from
faces-config.xml to existing library.</p>
- * @param context
- * @param configs
+ * <p class="changed_added_4_0">Cenerate files from library
model.</p>
+ * @param library
+ * @param type
* @throws CdkException
*/
- public void applyFacesConfigToModel(Iterable<File> configs) throws CdkException {
+ public void generate(ComponentLibrary library, OutputType type) throws CdkException {
+ CdkWriter generator = getGeneratorFor(type);
+ generator.render(library);
}
+
+ protected CdkWriter getGeneratorFor(OutputType type) throws CdkException {
+ CdkWriter generator = null;
+ if(StandardOutputs.COMPONENT_CLASSES.equals(type)){
+ generator = new ComponentClassGenerator();
+ } else {
+ // TODO - make service method for new source types.
+ throw new CdkException("No generator for type " + type.getName());
+ }
+ generator.init(getContext());
+ return generator;
+ }
- public ComponentLibrary buildModelFromFacesConfig(Iterable<File> configs) throws
CdkException {
- ComponentLibrary library = createLibrary();
- return library;
- }
-
-
/**
- * <p class="changed_added_4_0">Builder method that creates new library
instance.</p>
+ * <p class="changed_added_4_0">
+ * Builder method that creates new library instance.
+ * </p>
+ *
* @return
*/
- public ComponentLibrary createLibrary(){
+ public ComponentLibrary createLibrary() {
return new ComponentLibrary();
}
/**
- * <p class="changed_added_4_0">Getter for the current CDK
context</p>
+ * <p class="changed_added_4_0">
+ * Getter for the current CDK context
+ * </p>
+ *
* @return the context
*/
protected CdkContext getContext() {
return context;
}
+
+ protected ModelBuilder getBuilderFor(SourceType type) throws CdkException {
+ ModelBuilder builder;
+ if (StandardSources.JAVA_SOURCES.equals(type)) {
+ builder = new CdkCompiler();
+ } else if (StandardSources.FACES_CONFIGS.equals(type)) {
+ builder = new FacesConfigParser();
+ } else if (StandardSources.RENDERER_TEMPLATES.equals(type)) {
+ builder = new RendererTemplateParser();
+ } else {
+ // TODO - make service method for new source types.
+ throw new CdkException("No model builder for source type " +
type.getName());
+ }
+ builder.init(getContext());
+ return builder;
+ }
}