[ajax4jsf-svn-commits] JBoss Ajax4JSF SVN: r91 - in trunk/cdk/generator/src/main: resources/META-INF/templates and 1 other directory.

ajax4jsf-svn-commits at lists.jboss.org ajax4jsf-svn-commits at lists.jboss.org
Tue Apr 17 17:04:04 EDT 2007


Author: nbelaevski
Date: 2007-04-17 17:04:04 -0400 (Tue, 17 Apr 2007)
New Revision: 91

Added:
   trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourceConfigGeneratorBean.java
   trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java
   trunk/cdk/generator/src/main/resources/META-INF/templates/resources-config.vm
Log:
RF-84 first variant

Added: trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourceConfigGeneratorBean.java
===================================================================
--- trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourceConfigGeneratorBean.java	                        (rev 0)
+++ trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourceConfigGeneratorBean.java	2007-04-17 21:04:04 UTC (rev 91)
@@ -0,0 +1,64 @@
+/**
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+package org.ajax4jsf.builder.generator;
+
+
+
+/**
+ * @author Nick - mailto:nbelaevski at exadel.com
+ * created 17.04.2007
+ * 
+ */
+public class ResourceConfigGeneratorBean {
+	private static final String DEFAULT_CLASS_NAME = "org.ajax4jsf.framework.resource.JarResource";
+
+	private String path;
+	private String contentType;
+	private String className = DEFAULT_CLASS_NAME;
+	private String key;
+	
+	public String getPath() {
+		return path;
+	}
+	public void setPath(String path) {
+		this.path = path;
+	}
+	public String getContentType() {
+		return contentType;
+	}
+	public void setContentType(String contentType) {
+		this.contentType = contentType;
+	}
+	public String getClassName() {
+		return className;
+	}
+	public void setClassName(String className) {
+		this.className = className;
+	}
+	public String getKey() {
+		return key;
+	}
+	public void setKey(String key) {
+		this.key = key;
+	}
+
+}
\ No newline at end of file

Added: trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java
===================================================================
--- trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java	                        (rev 0)
+++ trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java	2007-04-17 21:04:04 UTC (rev 91)
@@ -0,0 +1,196 @@
+/**
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+package org.ajax4jsf.builder.generator;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.ajax4jsf.builder.config.BuilderConfig;
+import org.ajax4jsf.builder.config.ComponentBean;
+import org.ajax4jsf.builder.config.RendererBean;
+import org.ajax4jsf.templatecompiler.builder.CompilationContext;
+import org.ajax4jsf.templatecompiler.builder.CompilationException;
+import org.ajax4jsf.templatecompiler.builder.TemplateCompiler;
+import org.ajax4jsf.templatecompiler.elements.TemplateElement;
+import org.ajax4jsf.templatecompiler.elements.vcp.FResourceTemplateElement;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+
+/**
+ * @author Nick - mailto:nbelaevski at exadel.com
+ * created 17.04.2007
+ * 
+ */
+public class ResourcesConfigGenerator extends FacesConfigGenerator {
+
+	private File resourcesConfig;
+	private File templatesDirectory;
+
+	public ResourcesConfigGenerator(JSFGeneratorConfiguration task, Logger log) {
+		super(task, log);
+	}
+
+	private void addResources(List<ResourceConfigGeneratorBean> resources, TemplateElement templateElement,
+			String packageName) {
+		if (templateElement instanceof FResourceTemplateElement) {
+			FResourceTemplateElement resourceTemplateElement = (FResourceTemplateElement) templateElement;
+			
+			ResourceConfigGeneratorBean resourceConfig = new ResourceConfigGeneratorBean();
+			String name = resourceTemplateElement.getName();
+			resourceConfig.setKey(name);
+			resourceConfig.setPath(resolvePath(name, packageName));
+			
+			resources.add(resourceConfig);
+		}
+		
+		ArrayList<TemplateElement> subElements = templateElement.getSubElements();
+		for (TemplateElement element : subElements) {
+			addResources(resources, element, packageName);
+		}
+	}
+	
+	private String resolvePath(String name, String packageName) {
+		String resolvedName = name;
+		if (!name.startsWith("/") && name.contains("/")) {
+			//need to resolve
+			StringBuffer normalizedName = new StringBuffer();
+			if (!packageName.startsWith("/")) {
+				normalizedName.append('/');
+			}
+
+			normalizedName.append(packageName.replace('.', '/'));
+			
+			if (!packageName.endsWith("/")) {
+				normalizedName.append('/');
+			}
+
+			normalizedName.append(name);
+			
+			resolvedName = normalizedName.toString();
+		} 
+		
+		return resolvedName;
+	}
+
+	private void addResources(List<ResourceConfigGeneratorBean> resources, RendererBean renderer, BuilderConfig builderConfig) throws CompilationException, IOException, ClassNotFoundException {
+		if (null != renderer && renderer.isGenerate()
+				&& null != renderer.getTemplate()) {
+
+			File template;
+			if (null != getTemplates()) {
+				template = new File(getTemplates(), renderer.getTemplate());
+			} else {
+				template = new File(renderer.getTemplate());
+			}
+			CompilationContext rendererBean = new RendererCompilationContext(
+					getLog(), getClassLoader(),getConfig());
+
+			TemplateCompiler templateCompiler = new TemplateCompiler();
+			InputStream templateStream = new FileInputStream(template);
+			templateCompiler.processing(templateStream, rendererBean);
+		
+			TemplateElement root = rendererBean.getTree();
+			
+			String classname = renderer.getClassname();
+			String packageName;
+			int idx = classname.lastIndexOf('.');
+			if (idx != -1) {
+				packageName = classname.substring(0, idx);
+			} else {
+				packageName = "";
+			}
+			
+			addResources(resources, root, packageName);
+		}
+	}
+
+	public void createFiles(BuilderConfig config) throws GeneratorException {
+		VelocityContext context = new VelocityContext();
+		Template template = getTemplate();
+		try {
+			// Put common properties
+
+			ArrayList<ResourceConfigGeneratorBean> resourcesList = new ArrayList<ResourceConfigGeneratorBean>();
+
+			List<ComponentBean> components = config.getComponents();
+			for (ComponentBean componentBean : components) {
+				RendererBean rendererBean = componentBean.getRenderer();
+
+				addResources(resourcesList, rendererBean, config);
+			}
+
+			List<RendererBean> renderers = config.getRenderers();
+			for (RendererBean rendererBean : renderers) {
+				addResources(resourcesList, rendererBean, config);
+			}
+
+			context.put("resources", resourcesList);
+			context.put("facesConfig", this);
+			File configFile = getResourcesConfig();
+			File javaDir = configFile.getParentFile();
+			if (!javaDir.exists()) {
+				javaDir.mkdirs();
+			}
+			if (configFile.exists()) {
+				configFile.delete();
+			}
+			Writer out = new BufferedWriter(new FileWriter(configFile));
+			template.merge(context, out);
+			out.flush();
+			out.close();
+		} catch (Exception e) {
+			throw new GeneratorException("Error create new resources-config.xml ",
+					e);
+		}
+	}
+
+	protected String getRootTag() {
+		return "resource-config";
+	}
+
+	public File getResourcesConfig() {
+		return resourcesConfig;
+	}
+
+	public void setResourcesConfig(File resourcesConfig) {
+		this.resourcesConfig = resourcesConfig;
+	}
+
+	protected String getDefaultTemplateName() {
+		return "resources-config.vm";
+	}
+
+	public void setTemplates(File templatesDirectory) {
+		this.templatesDirectory = templatesDirectory;
+	}
+
+	public File getTemplates() {
+		return this.templatesDirectory;
+	}
+}

Added: trunk/cdk/generator/src/main/resources/META-INF/templates/resources-config.vm
===================================================================
--- trunk/cdk/generator/src/main/resources/META-INF/templates/resources-config.vm	                        (rev 0)
+++ trunk/cdk/generator/src/main/resources/META-INF/templates/resources-config.vm	2007-04-17 21:04:04 UTC (rev 91)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<resource-config>
+
+#foreach( $resource in ${resources})
+<resource class="${resource.className}">
+	<!-- renderer class="${resource.renderer.className}">
+		<content-type>${resource.renderer.contentType}</content-type>
+	</renderer -->
+	<name>${resource.key}</name>
+	<path>${resource.path}</path>
+	<!-- content-type>${resource.contentType}</content-type -->
+
+</resource>
+#end
+
+</resource-config>




More information about the ajax4jsf-svn-commits mailing list