You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2006/04/13 10:18:43 UTC
svn commit: r393758 - in /myfaces/maven/trunk: ./ build-tools/
build-tools/src/main/java/ build-tools/src/main/java/org/
build-tools/src/main/java/org/apache/
build-tools/src/main/java/org/apache/myfaces/
build-tools/src/main/java/org/apache/myfaces/co...
Author: manolito
Date: Thu Apr 13 01:18:40 2006
New Revision: 393758
URL: http://svn.apache.org/viewcvs?rev=393758&view=rev
Log:
the reborn code generator
Added:
myfaces/maven/trunk/build-tools/src/main/java/
myfaces/maven/trunk/build-tools/src/main/java/org/
myfaces/maven/trunk/build-tools/src/main/java/org/apache/
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java (with props)
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java (with props)
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java (with props)
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java (with props)
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java (with props)
myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java (with props)
myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/
myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/
myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd (with props)
myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.vsl
Modified:
myfaces/maven/trunk/build-tools/pom.xml
myfaces/maven/trunk/pom.xml
Modified: myfaces/maven/trunk/build-tools/pom.xml
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/pom.xml?rev=393758&r1=393757&r2=393758&view=diff
==============================================================================
--- myfaces/maven/trunk/build-tools/pom.xml (original)
+++ myfaces/maven/trunk/build-tools/pom.xml Thu Apr 13 01:18:40 2006
@@ -46,6 +46,31 @@
</extensions>
</build>
+ <dependencies>
+
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.0.4</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>velocity</groupId>
+ <artifactId>velocity</artifactId>
+ <version>1.4</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>ant</groupId>
+ <artifactId>ant</artifactId>
+ <version>1.6.5</version>
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
<distributionManagement>
<repository>
<id>apache-maven</id>
@@ -59,6 +84,5 @@
<url>scpexe://minotaur.apache.org/www/cvs.apache.org/maven-snapshot-repository</url>
</snapshotRepository>
</distributionManagement>
-
</project>
Added: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java (added)
+++ myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java Thu Apr 13 01:18:40 2006
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.codegen;
+
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+
+import java.io.Writer;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public abstract class CodeGenerator
+{
+ //private static final Log log = LogFactory.getLog(CodeGenerator.class);
+
+ private VelocityContext _velocityContext = null;
+
+ protected VelocityContext getVelocityContext()
+ {
+ _velocityContext = new VelocityContext();
+ return _velocityContext;
+ }
+
+ protected void generateCode(String template, Writer writer)
+ {
+ try
+ {
+ Velocity.setProperty("resource.loader", "class");
+ Velocity.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+ Velocity.init();
+
+ Template velocityTemplate = Velocity.getTemplate(template, "ISO-8859-1");
+ velocityTemplate.merge(_velocityContext, writer);
+ writer.flush();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/CodeGenerator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java (added)
+++ myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java Thu Apr 13 01:18:40 2006
@@ -0,0 +1,243 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.codegen.component;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.*;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ComponentDef
+{
+ //private static final Log log = LogFactory.getLog(Component.class);
+
+ public static final String[] IMPLICIT_PACKAGES = {"java.util",
+ "java.lang",
+ "javax.faces.el",
+ "javax.faces.context",
+ "javax.faces.convert"};
+
+ private String _qualifiedClassName;
+ private String _baseClassName;
+ private String _componentType;
+ private String _componentFamily;
+ private String _rendererType;
+ private Map _fieldsMap;
+ private boolean _generateStateMethods = true;
+ private boolean _generateConstructor = true;
+ private boolean _generateUserRoleMethods = false;
+
+ public ComponentDef()
+ {
+ }
+
+ public ComponentDef(String className, String baseClassName, String componentType)
+ {
+ _qualifiedClassName = className;
+ _baseClassName = baseClassName;
+ _componentType = componentType;
+ }
+
+ public void setQualifiedClassName(String qualifiedClassName)
+ {
+ _qualifiedClassName = qualifiedClassName;
+ }
+
+ public String getComponentFamily()
+ {
+ return _componentFamily;
+ }
+
+ public void setComponentFamily(String componentFamily)
+ {
+ _componentFamily = componentFamily;
+ }
+
+ public String getPackage()
+ {
+ int dot = _qualifiedClassName.lastIndexOf('.');
+ return _qualifiedClassName.substring(0, dot);
+ }
+
+ public String getClassName()
+ {
+ int dot = _qualifiedClassName.lastIndexOf('.');
+ return _qualifiedClassName.substring(dot + 1);
+ }
+
+ public String getQualifiedClassName()
+ {
+ return _qualifiedClassName;
+ }
+
+ public String getBaseClassName()
+ {
+ return _baseClassName;
+ }
+
+ public void setBaseClassName(String baseClassName)
+ {
+ _baseClassName = baseClassName;
+ }
+
+ public Collection getFields()
+ {
+ return _fieldsMap != null ? _fieldsMap.values() : Collections.EMPTY_LIST;
+ }
+
+ public void addField(FieldDef field)
+ {
+ if (_fieldsMap == null)
+ {
+ _fieldsMap = new LinkedHashMap();
+ }
+ _fieldsMap.put(field.getName(), field);
+ }
+
+ public FieldDef getField(String fieldName)
+ {
+ return (FieldDef)_fieldsMap.get(fieldName);
+ }
+
+
+ public List getImportPackages()
+ {
+ return Arrays.asList(IMPLICIT_PACKAGES);
+ }
+
+
+ public String getComponentType()
+ {
+ return _componentType;
+ }
+
+ public void setComponentType(String componentType)
+ {
+ _componentType = componentType;
+ }
+
+ public String getRendererType()
+ {
+ return _rendererType;
+ }
+
+ public void setRendererType(String rendererType)
+ {
+ _rendererType = rendererType;
+ }
+
+ public boolean isGenerateStateMethods()
+ {
+ return _generateStateMethods;
+ }
+
+ public void setGenerateStateMethods(boolean generateStateMethods)
+ {
+ _generateStateMethods = generateStateMethods;
+ }
+
+
+ public Collection getPropertyFields()
+ {
+ List lst = new ArrayList();
+ for (Iterator it = getFields().iterator(); it.hasNext(); )
+ {
+ FieldDef field = (FieldDef)it.next();
+ if (field.isGenerateProperty())
+ {
+ lst.add(field);
+ }
+ }
+ return lst;
+ }
+
+ public Collection getSaveStateFields()
+ {
+ List lst = new ArrayList();
+ int counter = 1;
+ for (Iterator it = getFields().iterator(); it.hasNext(); )
+ {
+ FieldDef field = (FieldDef)it.next();
+ if (field.isSaveState())
+ {
+ field.setSaveStateFieldIndex(counter++);
+ lst.add(field);
+ }
+ }
+ return lst;
+ }
+
+ public int getSaveStateFieldsCountPlusOne()
+ {
+ return getSaveStateFields().size() + 1;
+ }
+
+ public boolean isGenerateConstructor()
+ {
+ return _generateConstructor;
+ }
+
+ public void setGenerateConstructor(boolean generateConstructor)
+ {
+ _generateConstructor = generateConstructor;
+ }
+
+
+ public void toXml(PrintWriter writer) throws IOException
+ {
+ writer.print ("<component");
+ if (!isGenerateConstructor()) writer.print(" generateConstructor=\"false\"");
+ if (!isGenerateStateMethods()) writer.print(" generateStateMethods=\"false\"");
+ writer.println(">");
+ writer.println(" <component-class>" + _qualifiedClassName + "</component-class>");
+ writer.println(" <base-class>" + _baseClassName + "</base-class>");
+ if (_componentType != null)
+ {
+ writer.println(" <component-type>" + _componentType + "</component-type>");
+ }
+
+ if (_componentFamily != null)
+ {
+ writer.println(" <component-family>" + _componentFamily + "</component-family>");
+ }
+
+ if (_rendererType != null)
+ {
+ writer.println(" <renderer-type>" + _rendererType + "</renderer-type>");
+ }
+
+ Collection fields = getFields();
+ for (Iterator iterator = fields.iterator(); iterator.hasNext();)
+ {
+ FieldDef f = (FieldDef)iterator.next();
+ f.toXml(writer);
+ }
+ writer.println("</component>");
+ }
+
+ public boolean isGenerateUserRoleMethods()
+ {
+ return _generateUserRoleMethods;
+ }
+
+ public void setGenerateUserRoleMethods(boolean generateUserRoleMethods)
+ {
+ _generateUserRoleMethods = generateUserRoleMethods;
+ }
+}
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDef.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java (added)
+++ myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java Thu Apr 13 01:18:40 2006
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.codegen.component;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ComponentDefinitionParser
+{
+ //private static final Log log = LogFactory.getLog(ComponentDefinitionParser.class);
+
+ public ComponentDef parse(File xmlFile)
+ {
+ try
+ {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setIgnoringElementContentWhitespace(true);
+ dbf.setIgnoringComments(true);
+ dbf.setNamespaceAware(false);
+ dbf.setValidating(false);
+
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ db.setEntityResolver(new _EntityResolver());
+
+ InputSource is = new InputSource(xmlFile.getAbsolutePath());
+
+ Document document = db.parse(is);
+ return parseComponent(document.getDocumentElement());
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ private ComponentDef parseComponent(Element componentElem)
+ {
+ String componentClass = getChildElementText(componentElem, "component-class");
+ String baseClass = getChildElementText(componentElem, "base-class");
+ String componentType = getChildElementText(componentElem, "component-type");
+ String componentFamily = getChildElementText(componentElem, "component-family");
+ String rendererType = getChildElementText(componentElem, "renderer-type");
+
+ ComponentDef component = new ComponentDef(componentClass, baseClass, componentType);
+ component.setRendererType(rendererType);
+ component.setComponentFamily(componentFamily);
+
+ String generateConstructor = componentElem.getAttribute("generateConstructor");
+ if (generateConstructor != null && generateConstructor.length() > 0)
+ {
+ component.setGenerateConstructor(Boolean.valueOf(generateConstructor).booleanValue());
+ }
+
+ String generateStateMethods = componentElem.getAttribute("generateStateMethods");
+ if (generateStateMethods != null && generateStateMethods.length() > 0)
+ {
+ component.setGenerateStateMethods(Boolean.valueOf(generateStateMethods).booleanValue());
+ }
+
+ String generateUserRoleMethods = componentElem.getAttribute("generateUserRoleMethods");
+ if (generateUserRoleMethods != null && generateUserRoleMethods.length() > 0)
+ {
+ component.setGenerateUserRoleMethods(Boolean.valueOf(generateUserRoleMethods).booleanValue());
+ }
+
+ NodeList fields = componentElem.getElementsByTagName("field");
+ for (int i = 0, len = fields.getLength(); i < len; i++)
+ {
+ Element field = (Element)fields.item(i);
+ component.addField(createField(field));
+ }
+
+ return component;
+ }
+
+
+ private FieldDef createField(Element fieldElem)
+ {
+ String fieldName = getChildElementText(fieldElem, "name");
+ FieldDef field = new FieldDef(fieldName);
+
+ String fieldType = getChildElementText(fieldElem, "type");
+ field.setQualifiedType(fieldType);
+
+ String defaultValue = getChildElementText(fieldElem, "default-value");
+ if (defaultValue != null) field.setDefaultValue(defaultValue);
+
+ String proprietary = fieldElem.getAttribute("proprietary");
+ if (proprietary != null && proprietary.length() > 0)
+ {
+ field.setProprietary(Boolean.valueOf(proprietary).booleanValue());
+ }
+
+ String generateProperty = fieldElem.getAttribute("generateProperty");
+ if (generateProperty != null && generateProperty.length() > 0)
+ {
+ field.setGenerateProperty(Boolean.valueOf(generateProperty).booleanValue());
+ }
+
+ String generateSetter = fieldElem.getAttribute("generateSetter");
+ if (generateSetter != null && generateSetter.length() > 0)
+ {
+ field.setGenerateSetter(Boolean.valueOf(generateSetter).booleanValue());
+ }
+
+ String saveState = fieldElem.getAttribute("saveState");
+ if (saveState != null && saveState.length() > 0)
+ {
+ field.setSaveState(Boolean.valueOf(saveState).booleanValue());
+ }
+
+ return field;
+ }
+
+
+ private String getChildElementText(Element elem, String childElementName)
+ {
+ NodeList nodeList = elem.getElementsByTagName(childElementName);
+ for (int i = 0, len = nodeList.getLength(); i < len; i++)
+ {
+ Node child = nodeList.item(i);
+ if (child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ return getElementText((Element)child);
+ }
+ }
+ return null;
+ }
+
+
+ private String getElementText(Element elem)
+ {
+ StringBuffer buf = new StringBuffer();
+ NodeList nodeList = elem.getChildNodes();
+ for (int i = 0, len = nodeList.getLength(); i < len; i++)
+ {
+ Node n = nodeList.item(i);
+ if (n.getNodeType() == Node.TEXT_NODE)
+ {
+ buf.append(n.getNodeValue());
+ }
+ else if (n.getNodeType() == Node.ELEMENT_NODE)
+ {
+ buf.append(getElementText((Element)n));
+ }
+ }
+ return buf.toString();
+ }
+
+
+
+ private InputSource createClassloaderInputSource(String publicId, String systemId)
+ throws IOException
+ {
+ //InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(systemId);
+ InputStream inStream = getClass().getClassLoader().getResourceAsStream(systemId);
+ if (inStream == null)
+ {
+ throw new IOException("Unable to find classloader resource " + systemId);
+ }
+ InputSource is = new InputSource(inStream);
+ is.setPublicId(publicId);
+ is.setSystemId(systemId);
+ is.setEncoding("ISO-8859-1");
+ return is;
+ }
+
+ public class _EntityResolver implements EntityResolver
+ {
+ public InputSource resolveEntity(String publicId, String systemId) throws IOException
+ {
+ if (systemId == null)
+ {
+ throw new UnsupportedOperationException("systemId must not be null");
+ }
+
+ if (systemId.equals("http://myfaces.apache.org/dtd/Component.dtd"))
+ {
+ //Load DTD from servlet.jar
+ return createClassloaderInputSource(publicId, "org/apache/myfaces/codegen/component/Component.dtd");
+ }
+
+ throw new UnsupportedOperationException();
+ }
+
+ }
+
+
+
+}
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentDefinitionParser.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java (added)
+++ myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java Thu Apr 13 01:18:40 2006
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.codegen.component;
+
+import org.apache.myfaces.codegen.CodeGenerator;
+import org.apache.velocity.VelocityContext;
+
+import java.io.*;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ComponentGenerator
+ extends CodeGenerator
+{
+ //private static final Log log = LogFactory.getLog(ComponentGenerator.class);
+
+ private static final String GENERATED_CODE_BEGIN_MARKER = "GENERATED CODE BEGIN";
+ private static final String GENERATED_CODE_END_MARKER = "GENERATED CODE END";
+
+ private File _destdir;
+
+ //private String _velocityTemplate = "Component.vsl";
+
+ public ComponentGenerator(File destdir)
+ {
+ _destdir = destdir;
+ }
+
+ public void generate(File xmlFile)
+ {
+ ComponentDefinitionParser parser = new ComponentDefinitionParser();
+ ComponentDef component = parser.parse(xmlFile);
+ try
+ {
+ generateComponent(component);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void generateComponent(ComponentDef component) throws IOException
+ {
+ System.out.println("Generating component " + component.getQualifiedClassName());
+ String componentClass = component.getQualifiedClassName();
+
+ File bakFile = new File(_destdir, componentClass.replace('.', '/') + ".bak");
+ bakFile.delete();
+
+ File oldFile = new File(_destdir, componentClass.replace('.', '/') + ".java");
+ if (!oldFile.exists())
+ {
+ throw new IllegalStateException("Destination file does not exist: " + oldFile);
+ }
+ if (!oldFile.renameTo(bakFile))
+ {
+ throw new RuntimeException("Destination file could not be renamed: " + oldFile);
+ }
+
+ File newFile = new File(_destdir, componentClass.replace('.', '/') + ".java");
+ FileWriter fileWriter = new FileWriter(newFile, false);
+ PrintWriter printWriter = new PrintWriter(fileWriter);
+
+ FileReader fileReader = new FileReader(bakFile);
+ BufferedReader bufferedReader = new BufferedReader(fileReader);
+
+ //copy lines until "GENERATED CODE BEGIN" marker
+ boolean ignore = false;
+ boolean generated = false;
+ String line = bufferedReader.readLine();
+ while (line != null)
+ {
+ if (!ignore)
+ {
+ printWriter.println(line);
+ if (line.indexOf(GENERATED_CODE_BEGIN_MARKER) != -1)
+ {
+ ignore = true;
+ }
+ }
+ else
+ {
+ if (line.indexOf(GENERATED_CODE_END_MARKER) != -1)
+ {
+ generateComponentLines(printWriter, component);
+ generated = true;
+ printWriter.println(line);
+ ignore = false;
+ }
+ }
+ line = bufferedReader.readLine();
+ }
+
+ bufferedReader.close();
+ fileReader.close();
+
+ printWriter.close();
+ fileWriter.close();
+
+ if (!generated)
+ {
+ System.err.println("No markers found - nothing generated for class " + componentClass);
+ }
+ }
+
+
+ public void generateComponentLines(PrintWriter printWriter,
+ ComponentDef component) throws IOException
+ {
+ VelocityContext vc = getVelocityContext();
+ vc.put("c", component);
+ generateCode("org/apache/myfaces/codegen/component/Component.vsl",
+ printWriter);
+
+ }
+
+
+
+}
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGenerator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java (added)
+++ myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java Thu Apr 13 01:18:40 2006
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.codegen.component;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DirectoryScanner;
+import org.apache.tools.ant.taskdefs.MatchingTask;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.File;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ComponentGeneratorTask
+ extends MatchingTask
+{
+ private static final Log log = LogFactory.getLog(ComponentGeneratorTask.class);
+
+ private File _basedir;
+ private File _destdir;
+ private String _includes;
+
+ public File getBasedir()
+ {
+ return _basedir;
+ }
+
+ public void setBasedir(File basedir)
+ {
+ _basedir = basedir;
+ }
+
+ /*
+ public String getIncludes()
+ {
+ return _includes;
+ }
+
+ public void setIncludes(String includes)
+ {
+ _includes = includes;
+ }
+ */
+
+ public File getDestdir()
+ {
+ return _destdir;
+ }
+
+ public void setDestdir(File destdir)
+ {
+ _destdir = destdir;
+ }
+
+ public void execute() throws BuildException
+ {
+ log.info("Executing ComponentGeneratorTask: ");
+ log.info("baseDir : "+_basedir);
+ log.info("destDir : "+_destdir);
+
+ ComponentGenerator generator = new ComponentGenerator(_destdir);
+
+ if(_includes==null)
+ {
+ DirectoryScanner ds = getDirectoryScanner(_basedir);
+ ds.scan();
+
+ String[] files = ds.getIncludedFiles();
+ for (int i = 0; i < files.length; i++)
+ {
+ String xmlFileName = files[i];
+ log.info("on xmlFileName : "+files[i]);
+ generator.generate(new File(_basedir, xmlFileName));
+ }
+ }
+ else
+ {
+ //_includes = _includes.replace('/','\\');
+
+ log.info("on xmlFileName : "+_basedir+"/"+_includes);
+ generator.generate(new File(_basedir, _includes));
+ }
+ }
+
+ public static void main(String[] args)
+ {
+ }
+
+}
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/ComponentGeneratorTask.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java (added)
+++ myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java Thu Apr 13 01:18:40 2006
@@ -0,0 +1,297 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.codegen.component;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class FieldDef
+{
+ //private static final Log log = LogFactory.getLog(Field.class);
+
+ private static final Set ATTACHED_CLASSES = new HashSet();
+ static
+ {
+ ATTACHED_CLASSES.add("javax.faces.convert.Converter");
+ ATTACHED_CLASSES.add("javax.faces.el.MethodBinding");
+ ATTACHED_CLASSES.add("javax.faces.el.ValueBinding");
+ }
+
+ private String _qualifiedType;
+ private String _name;
+ private String _defaultValue;
+ private boolean _proprietary = false;
+ private boolean _generateProperty = true;
+ private boolean _generateSetter = true;
+ private boolean _saveState = true;
+ private int _saveStateFieldIndex;
+
+ public FieldDef(String name)
+ {
+ _name = name;
+ }
+
+ public String getQualifiedType()
+ {
+ return _qualifiedType;
+ }
+
+ public void setQualifiedType(String type)
+ {
+ _qualifiedType = type;
+ }
+
+ public String getType()
+ {
+ if (isPrimitiveType())
+ {
+ return _qualifiedType;
+ }
+ else
+ {
+ return getSimpleType(_qualifiedType);
+ }
+ }
+
+ public String getFieldType()
+ {
+ if (isPrimitiveType())
+ {
+ return getSimpleType(getNonPrimitiveType(_qualifiedType));
+ }
+ else
+ {
+ return getSimpleType(_qualifiedType);
+ }
+ }
+
+ public String getBaseFieldType()
+ {
+ if (_qualifiedType.equals("int") ||
+ _qualifiedType.equals("long") ||
+ _qualifiedType.equals("short"))
+ {
+ return getSimpleType("java.lang.Number");
+ }
+ else
+ {
+ return getFieldType();
+ }
+ }
+
+ private static String getSimpleType(String type)
+ {
+ for (int i = 0; i < ComponentDef.IMPLICIT_PACKAGES.length; i++)
+ {
+ String p = ComponentDef.IMPLICIT_PACKAGES[i] + ".";
+ if (type.startsWith(p))
+ {
+ return type.substring(p.length());
+ }
+ }
+ return type;
+ }
+
+ private static String getNonPrimitiveType(String type)
+ {
+ if (type.equals("boolean")) return "java.lang.Boolean";
+ else if (type.equals("int")) return "java.lang.Integer";
+ else if (type.equals("long")) return "java.lang.Long";
+ else throw new IllegalArgumentException(type);
+ }
+
+ public String getName()
+ {
+ return _name;
+ }
+
+ public void setName(String name)
+ {
+ _name = name;
+ }
+
+ public String getSaveName()
+ {
+ if (_name.equals("for")) return "forValue";
+ if (_name.equals("transient")) return "transientFlag";
+ return _name;
+ }
+
+ public String getUcaseName()
+ {
+ return _name.toUpperCase();
+ }
+
+ public String getDefaultValue()
+ {
+ return _defaultValue;
+ }
+
+ public void setDefaultValue(String defaultValue)
+ {
+ _defaultValue = defaultValue;
+ }
+
+ public String getDefaultLiteral()
+ {
+ if (_defaultValue == null)
+ {
+ if (_qualifiedType.equals("boolean")) return "false";
+ else if (_qualifiedType.equals("int")) return "Integer.MIN_VALUE";
+ else if (_qualifiedType.equals("long")) return "Long.MIN_VALUE";
+ else return "null";
+ }
+ else
+ {
+ if (_qualifiedType.equals("java.lang.String"))
+ {
+ return '\"' + _defaultValue + '\"';
+ }
+ else if (_defaultValue.equals(Integer.toString(Integer.MIN_VALUE)) && _qualifiedType.equals("int"))
+ {
+ return "Integer.MIN_VALUE";
+ }
+ else if (_defaultValue.equals(Long.toString(Long.MIN_VALUE)) && _qualifiedType.equals("long"))
+ {
+ return "Long.MIN_VALUE";
+ }
+ else
+ {
+ return _defaultValue;
+ }
+ }
+ }
+
+ public boolean isPrimitiveType()
+ {
+ return _qualifiedType.equals("boolean") ||
+ _qualifiedType.equals("int") ||
+ _qualifiedType.equals("long");
+ }
+
+ public boolean isPrimitiveBoolean()
+ {
+ return _qualifiedType.equals("boolean");
+ }
+
+ public String getSetterMethodName()
+ {
+ return "set" + Character.toUpperCase(_name.charAt(0)) + _name.substring(1);
+ }
+
+ public String getGetterMethodName()
+ {
+ if (_qualifiedType.equals("boolean"))
+ {
+ return "is" + Character.toUpperCase(_name.charAt(0)) + _name.substring(1);
+ }
+ else
+ {
+ return "get" + Character.toUpperCase(_name.charAt(0)) + _name.substring(1);
+ }
+ }
+
+ public String getPrimitiveValueMethod()
+ {
+ if (!isPrimitiveType()) throw new UnsupportedOperationException();
+ return _qualifiedType + "Value";
+ }
+
+ public boolean isProprietary()
+ {
+ return _proprietary;
+ }
+
+ public void setProprietary(boolean proprietary)
+ {
+ _proprietary = proprietary;
+ }
+
+ public boolean isSaveState()
+ {
+ return _saveState;
+ }
+
+ public void setSaveState(boolean saveState)
+ {
+ _saveState = saveState;
+ }
+
+ public boolean isGenerateSetter()
+ {
+ return _generateSetter;
+ }
+
+ public void setGenerateSetter(boolean generateSetter)
+ {
+ _generateSetter = generateSetter;
+ }
+
+ public int getSaveStateFieldIndex()
+ {
+ return _saveStateFieldIndex;
+ }
+
+ public void setSaveStateFieldIndex(int saveStateFieldIndex)
+ {
+ _saveStateFieldIndex = saveStateFieldIndex;
+ }
+
+ public boolean isGenerateProperty()
+ {
+ return _generateProperty;
+ }
+
+ public void setGenerateProperty(boolean generateProperty)
+ {
+ _generateProperty = generateProperty;
+ }
+
+ public boolean isAttachedStateSaving()
+ {
+ return ATTACHED_CLASSES.contains(_qualifiedType);
+ }
+
+
+ public void toXml(PrintWriter writer) throws IOException
+ {
+ writer.print (" <field");
+ if (isProprietary()) writer.print(" proprietary=\"true\"");
+ if (!isGenerateProperty()) writer.print(" generateProperty=\"false\"");
+ if (!isGenerateSetter()) writer.print(" generateSetter=\"false\"");
+ if (!isSaveState()) writer.print(" saveState=\"false\"");
+ writer.println(">");
+ writer.print (" <name>");
+ writer.print (_name);
+ writer.println("</name>");
+ writer.print (" <type>");
+ writer.print (_qualifiedType);
+ writer.println("</type>");
+ if (_defaultValue != null)
+ {
+ writer.print (" <default-value>");
+ writer.print (_defaultValue);
+ writer.println("</default-value>");
+ }
+ writer.println(" </field>");
+ }
+}
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/java/org/apache/myfaces/codegen/component/FieldDef.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd (added)
+++ myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd Thu Apr 13 01:18:40 2006
@@ -0,0 +1,25 @@
+
+<!ELEMENT component
+ (component-class, base-class, component-type?, component-family?, renderer-type?, field*)>
+<!ATTLIST component
+ generateConstructor (true|false) #IMPLIED
+ generateStateMethods (true|false) #IMPLIED
+ generateUserRoleMethods (true|false) #IMPLIED>
+
+<!ELEMENT component-class (#PCDATA)>
+<!ELEMENT base-class (#PCDATA)>
+<!ELEMENT component-type (#PCDATA)>
+<!ELEMENT component-family (#PCDATA)>
+<!ELEMENT renderer-type (#PCDATA)>
+
+<!ELEMENT field (name, type, default-value?)>
+<!ATTLIST field
+ proprietary (true|false) #IMPLIED
+ generateProperty (true|false) #IMPLIED
+ generateSetter (true|false) #IMPLIED
+ saveState (true|false) #IMPLIED>
+
+<!ELEMENT name (#PCDATA)>
+<!ELEMENT type (#PCDATA)>
+<!ELEMENT default-value (#PCDATA)>
+
Propchange: myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.dtd
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.vsl
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.vsl?rev=393758&view=auto
==============================================================================
--- myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.vsl (added)
+++ myfaces/maven/trunk/build-tools/src/main/resources/org/apache/myfaces/codegen/component/Component.vsl Thu Apr 13 01:18:40 2006
@@ -0,0 +1,125 @@
+##
+#set ($I = "") #set ($I_ = "") #set ($I__ = "") #set ($I___ = "")
+#set ($I____ = "") #set ($I_____ = "") #set ($I______ = "")
+##
+
+$I#if ($c.componentType)
+ public static final String COMPONENT_TYPE = "$c.componentType";
+$I#end
+$I#if ($c.componentFamily)
+ public static final String COMPONENT_FAMILY = "$c.componentFamily";
+$I#end
+$I#if ($c.rendererType)
+ private static final String DEFAULT_RENDERER_TYPE = "$c.rendererType";
+$I#end
+$I#foreach ($f in $c.propertyFields)
+$I__#if ($f.defaultValue)
+ private static final $f.type DEFAULT_$f.ucaseName = $f.defaultLiteral;
+$I__#end
+$I#end
+
+$I#foreach ($f in $c.propertyFields)
+ private $f.fieldType _$f.name = null;
+$I#end
+
+$I#if ( $c.isGenerateConstructor() )
+ public ${c.className}()
+ {
+$I__#if ($c.rendererType)
+ setRendererType(DEFAULT_RENDERER_TYPE);
+$I__#end
+ }
+$I#end
+
+$I#if ($c.componentFamily)
+ public String getFamily()
+ {
+ return COMPONENT_FAMILY;
+ }
+$I#end
+
+$I#foreach ($f in $c.propertyFields)
+$I__#if ($f.isPrimitiveType())
+$I____#if ( $f.isGenerateSetter() )
+ public void ${f.setterMethodName}($f.type $f.name)
+ {
+$I______#if ($f.isPrimitiveBoolean())
+ _$f.name = ${f.fieldType}.valueOf($f.name);
+$I______#else
+ _$f.name = new ${f.fieldType}($f.name);
+$I______#end
+ }
+$I____#end
+
+ public $f.type ${f.getterMethodName}()
+ {
+ if (_$f.name != null) return _${f.name}.${f.primitiveValueMethod}();
+ ValueBinding vb = getValueBinding("$f.name");
+ ${f.baseFieldType} v = vb != null ? (${f.baseFieldType})vb.getValue(getFacesContext()) : null;
+$I____#if ($f.defaultValue)
+ return v != null ? v.${f.primitiveValueMethod}() : DEFAULT_$f.ucaseName;
+$I____#else
+ return v != null ? v.${f.primitiveValueMethod}() : $f.defaultLiteral;
+$I____#end
+ }
+
+$I__#else
+$I____#if ( $f.isGenerateSetter() )
+ public void ${f.setterMethodName}($f.type $f.saveName)
+ {
+ _$f.name = $f.saveName;
+ }
+$I____#end
+
+ public $f.type ${f.getterMethodName}()
+ {
+ if (_$f.name != null) return _$f.name;
+ ValueBinding vb = getValueBinding("$f.name");
+$I____#if ($f.defaultValue)
+ return vb != null ? ($f.type)vb.getValue(getFacesContext()) : DEFAULT_$f.ucaseName;
+$I____#else
+ return vb != null ? ($f.type)vb.getValue(getFacesContext()) : null;
+$I____#end
+ }
+
+$I__#end
+$I#end
+
+$I#if ($c.isGenerateUserRoleMethods() )
+ public boolean isRendered()
+ {
+ if (!UserRoleUtils.isVisibleOnUserRole(this)) return false;
+ return super.isRendered();
+ }
+$I#end
+
+$I#if ($c.isGenerateStateMethods() )
+$I__#if ($c.saveStateFields.size() > 0)
+ public Object saveState(FacesContext context)
+ {
+ Object values[] = new Object[${c.getSaveStateFieldsCountPlusOne()}];
+ values[0] = super.saveState(context);
+$I____#foreach ($f in $c.saveStateFields)
+$I______#if ($f.isAttachedStateSaving())
+ values[$f.saveStateFieldIndex] = saveAttachedState(context, _${f.name});
+$I______#else
+ values[$f.saveStateFieldIndex] = _$f.name;
+$I______#end
+$I____#end
+ return ((Object) (values));
+ }
+
+ public void restoreState(FacesContext context, Object state)
+ {
+ Object values[] = (Object[])state;
+ super.restoreState(context, values[0]);
+$I____#foreach ($f in $c.saveStateFields)
+$I______#if ($f.isAttachedStateSaving())
+ _$f.name = ($f.fieldType)restoreAttachedState(context, values[$f.saveStateFieldIndex]);
+$I______#else
+ _$f.name = ($f.fieldType)values[$f.saveStateFieldIndex];
+$I______#end
+$I____#end
+ }
+$I__#end
+$I#end
Modified: myfaces/maven/trunk/pom.xml
URL: http://svn.apache.org/viewcvs/myfaces/maven/trunk/pom.xml?rev=393758&r1=393757&r2=393758&view=diff
==============================================================================
--- myfaces/maven/trunk/pom.xml (original)
+++ myfaces/maven/trunk/pom.xml Thu Apr 13 01:18:40 2006
@@ -24,6 +24,7 @@
</build>
<modules>
<module>master-pom</module>
+ <module>build-tools</module>
<module>wagon-maven-plugin</module>
</modules>
<distributionManagement>