You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2008/03/15 16:21:24 UTC

svn commit: r637426 - in /myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin: ./ src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/ src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/model/

Author: skitching
Date: Sat Mar 15 08:21:21 2008
New Revision: 637426

URL: http://svn.apache.org/viewvc?rev=637426&view=rev
Log:
Add new goal that just generates the metadata and saves it to xml.

Added:
    myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/BuildMetaDataMojo.java   (with props)
Modified:
    myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/pom.xml
    myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/MakeConfigMojo.java
    myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/model/Model.java

Modified: myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/pom.xml?rev=637426&r1=637425&r2=637426&view=diff
==============================================================================
--- myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/pom.xml (original)
+++ myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/pom.xml Sat Mar 15 08:21:21 2008
@@ -76,6 +76,18 @@
       <version>1.6.3</version>
       <scope>compile</scope>
     </dependency>
+    <dependency>
+      <groupId>commons-betwixt</groupId>
+      <artifactId>commons-betwixt</artifactId>
+      <version>0.8</version>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <version>1.2.13</version>
+      <scope>compile</scope>
+    </dependency>
   </dependencies>
 
 </project>

Added: myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/BuildMetaDataMojo.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/BuildMetaDataMojo.java?rev=637426&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/BuildMetaDataMojo.java (added)
+++ myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/BuildMetaDataMojo.java Sat Mar 15 08:21:21 2008
@@ -0,0 +1,118 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.buildtools.maven2.plugin.builder;
+
+import java.beans.IntrospectionException;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.apache.commons.betwixt.io.BeanWriter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.myfaces.buildtools.maven2.plugin.builder.model.Model;
+import org.apache.myfaces.buildtools.maven2.plugin.builder.qdox.QdoxModelBuilder;
+import org.xml.sax.SAXException;
+
+/**
+ * Creates myfaces-metadata.xml file.
+ * 
+ * @requiresDependencyResolution compile
+ * @goal build-metadata
+ * @phase generate-sources
+ */
+public class BuildMetaDataMojo extends AbstractMojo {
+	final Log log = LogFactory.getLog(BuildMetaDataMojo.class.getName());
+
+	/**
+	 * @parameter expression="${project}"
+	 * @readonly
+	 */
+	private MavenProject project;
+
+	/**
+	 * @parameter expression="${project.build.directory}"
+	 */
+	private File targetDirectory;
+
+	/**
+	 * @parameter
+	 */
+	private String outputFile = "classes/META-INF/myfaces-metadata.xml";
+
+	/**
+	 * Execute the Mojo.
+	 */
+	public void execute() throws MojoExecutionException {
+		Model model = buildModel(project);
+		saveModel(model);
+	}
+
+	/**
+	 * Execute ModelBuilder classes to create the Model data-structure.
+	 */
+	private Model buildModel(MavenProject project)
+			throws MojoExecutionException {
+		Model model = new Model();
+		QdoxModelBuilder builder = new QdoxModelBuilder();
+		builder.buildModel(model, project);
+		return model;
+	}
+
+	/**
+	 * Write the contents of the model to an xml file.
+	 */
+	private void saveModel(Model model) throws MojoExecutionException {
+		File outfile = new File(targetDirectory, outputFile);
+		FileWriter outputWriter = null;
+
+		try {
+			log.info("Writing file " + outfile);
+			outfile.getParentFile().mkdirs();
+
+			outputWriter = new FileWriter(outfile);
+			outputWriter.write("<?xml version='1.0' ?>\n");
+			BeanWriter beanWriter = new BeanWriter(outputWriter);
+
+			beanWriter.getXMLIntrospector().getConfiguration()
+					.setAttributesForPrimitives(false);
+			beanWriter.getBindingConfiguration().setMapIDs(false);
+			beanWriter.enablePrettyPrint();
+
+			beanWriter.write("model", model);
+		} catch (IntrospectionException e) {
+			throw new MojoExecutionException("Unable to save data", e);
+		} catch (IOException e) {
+			throw new MojoExecutionException("Unable to save data", e);
+		} catch (SAXException e) {
+			throw new MojoExecutionException("Unable to save data", e);
+		} finally {
+			try {
+				if (outputWriter != null) {
+					outputWriter.close();
+				}
+			} catch (IOException e) {
+				// ignore
+			}
+		}
+	}
+}

Propchange: myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/BuildMetaDataMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/MakeConfigMojo.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/MakeConfigMojo.java?rev=637426&r1=637425&r2=637426&view=diff
==============================================================================
--- myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/MakeConfigMojo.java (original)
+++ myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/MakeConfigMojo.java Sat Mar 15 08:21:21 2008
@@ -18,9 +18,12 @@
  */
 package org.apache.myfaces.buildtools.maven2.plugin.builder;
 
+import java.beans.IntrospectionException;
 import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
@@ -31,13 +34,14 @@
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.commons.betwixt.io.BeanReader;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
 import org.apache.myfaces.buildtools.maven2.plugin.builder.model.ComponentModel;
 import org.apache.myfaces.buildtools.maven2.plugin.builder.model.Model;
 import org.apache.myfaces.buildtools.maven2.plugin.builder.model.PropertyModel;
-import org.apache.myfaces.buildtools.maven2.plugin.builder.qdox.QdoxModelBuilder;
+import org.xml.sax.SAXException;
 
 /**
  * Creates taglib (tld) and faces-config files.
@@ -46,18 +50,38 @@
  * @goal make-config
  * @phase generate-sources
  */
-public class MakeConfigMojo  extends AbstractMojo {
+public class MakeConfigMojo extends AbstractMojo {
 	final Logger log = Logger.getLogger(MakeConfigMojo.class.getName());
 
 	/**
+	 * @parameter expression="${project}"
+	 * @readonly
+	 */
+	private MavenProject project;
+
+	/**
+	 * @parameter expression="${project.build.directory}"
+	 */
+	private File targetDirectory;
+
+	/**
+	 * @parameter
+	 */
+	private String metadataFile = "classes/META-INF/myfaces-metadata.xml";
+
+	/**
+	 * @parameter
+	 */
+	private String facesConfigFile = "classes/META-INF/standard-faces-config.xml";
+
+	/**
 	 * Execute the Mojo.
 	 */
 	public void execute() throws MojoExecutionException {
 		try {
-			Model artifacts = scanSource(project);
-			dumpModel(artifacts);
-			generateConfig(artifacts);
-	        throw new MojoExecutionException("Error during generation");
+			Model model = readModel(project);
+			generateConfig(model);
+			throw new MojoExecutionException("Error during generation");
 		} catch (IOException e) {
 			throw new MojoExecutionException("Error generating components", e);
 		}
@@ -66,124 +90,104 @@
 	/**
 	 * Scan the source tree for annotations. Sets
 	 */
-	private Model scanSource(MavenProject project) throws MojoExecutionException {
-		Model model = new Model();
-		QdoxModelBuilder builder = new QdoxModelBuilder();
-		builder.buildModel(model, project);
-		return model;
-	}
-
-
-	private void dumpModel(Model artifacts)
-	{
-		System.out.println("--dumping artifacts--");
-		Iterator components = artifacts.components();
-		while (components.hasNext())
-		{
-			dumpComponent((ComponentModel) components.next());
-		}
-		System.out.println("--dumped artifacts--");
-	}
-	
-	private void dumpComponent(ComponentModel c)
-	{
-		PrintStream out = System.out;
-		out.println("==Component");
-		out.println("class:" + c.getComponentClass());
-		out.println("type:" + c.getComponentType());
-		Iterator p = c.properties();
-		while (p.hasNext())
-		{
-			PropertyModel prop = (PropertyModel) p.next();
-			out.println("prop:" + prop.getPropertyName());
-			out.println("  class:" + prop.getPropertyClass());
-			out.println("  isLiteral:" + prop.isLiteralOnly());
-			out.println("  desc:" + prop.getDescription());
+	private Model readModel(MavenProject project) throws MojoExecutionException {
+		File infile = new File(targetDirectory, metadataFile);
+		try {
+			FileReader xmlReader = new FileReader(infile);
+			BeanReader beanReader = new BeanReader();
+			beanReader.getXMLIntrospector().getConfiguration()
+					.setAttributesForPrimitives(false);
+			beanReader.getBindingConfiguration().setMapIDs(false);
+
+			beanReader.registerBeanClass("model", Model.class);
+			Model model = (Model) beanReader.parse(xmlReader);
+			xmlReader.close();
+			return model;
+		} catch (FileNotFoundException e) {
+			throw new MojoExecutionException("No metadata file:" + infile);
+		} catch (IntrospectionException e) {
+			throw new MojoExecutionException("Unable to load metadata", e);
+		} catch (IOException e) {
+			throw new MojoExecutionException("Unable to load metadata", e);
+		} catch (SAXException e) {
+			throw new MojoExecutionException("Unable to load metadata", e);
 		}
 	}
 
 	/**
 	 * Generates parsed components.
 	 * 
-	 * TODO: probably better to use Velocity or similar to generate the
-	 * output from the model than direct print statements. Alternately, 
-	 * build a DOM representation of the model then apply an xslt stylesheet
-	 * to the dom.
+	 * TODO: probably better to use Velocity or similar to generate the output
+	 * from the model than direct print statements. Alternately, build a DOM
+	 * representation of the model then apply an xslt stylesheet to the dom.
 	 */
 	private void generateConfig(Model model) throws IOException,
 			MojoExecutionException {
 
-        File targetFile = new File(targetDirectory, facesConfigFile);
+		File targetFile = new File(targetDirectory, facesConfigFile);
+
+		try {
+			getLog().info("Generating " + targetFile);
 
-        try
-        {
-          getLog().info("Generating " + targetFile);
-
-          targetFile.delete();
-          targetFile.getParentFile().mkdirs();
-
-          OutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile));
-          XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
-          XMLStreamWriter writer =  outputFactory.createXMLStreamWriter(out);
-          writer.writeStartDocument("1.0");
-          writer.writeCharacters("\n");
-          writer.writeStartElement("faces-config");
-          writer.writeDefaultNamespace("http://java.sun.com/xml/ns/javaee");
-          writer.writeCharacters("\n");
-
-          // TODO:
-          // * APPLICATION element
-          // * write application-listeners
-          // * write navigation-handlers
-          // * write view-handelrs
-          // * write state-managers
-          // * write supported-locales
-          //
-          // * FACTORY element          
-          // * faces-context-factory
-          // * application-factory
-          // * lifecycle-factory
-          // * render-kit-factory
-          
-          writeConverters(model, writer);
-          writeValidators(model, writer);
-          writeComponents(model, writer);
-          writeRenderers(model, writer);
-          writeRenderKits(model, writer);
-
-          writer.writeEndDocument();
-          writer.close();
-
-          targetFile.setReadOnly();
-        }
-      catch (XMLStreamException e)
-      {
-        throw new MojoExecutionException("Error during generation", e);
-      }
-      catch (IOException e)
-      {
-        throw new MojoExecutionException("Error during generation", e);
-      }
-      catch(Exception e)
-      {
-          throw new MojoExecutionException("Error during generation", e);
-      }
-	}
-
-	private void writeConverters(Model model, XMLStreamWriter writer) throws Exception
-	{
-		
-	}
-
-	private void writeValidators(Model model, XMLStreamWriter writer) throws Exception
-	{
-		
-	}
-
-	private void writeComponents(Model model, XMLStreamWriter writer) throws Exception
-	{
-		for(Iterator i = model.components(); i.hasNext(); )
-		{
+			targetFile.delete();
+			targetFile.getParentFile().mkdirs();
+
+			OutputStream out = new BufferedOutputStream(new FileOutputStream(
+					targetFile));
+			XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+			XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out);
+			writer.writeStartDocument("1.0");
+			writer.writeCharacters("\n");
+			writer.writeStartElement("faces-config");
+			writer.writeDefaultNamespace("http://java.sun.com/xml/ns/javaee");
+			writer.writeCharacters("\n");
+
+			// TODO:
+			// * APPLICATION element
+			// * write application-listeners
+			// * write navigation-handlers
+			// * write view-handelrs
+			// * write state-managers
+			// * write supported-locales
+			//
+			// * FACTORY element
+			// * faces-context-factory
+			// * application-factory
+			// * lifecycle-factory
+			// * render-kit-factory
+
+			writeConverters(model, writer);
+			writeValidators(model, writer);
+			writeComponents(model, writer);
+			writeRenderers(model, writer);
+			writeRenderKits(model, writer);
+
+			writer.writeEndDocument();
+			writer.close();
+
+			targetFile.setReadOnly();
+		} catch (XMLStreamException e) {
+			throw new MojoExecutionException("Error during generation", e);
+		} catch (IOException e) {
+			throw new MojoExecutionException("Error during generation", e);
+		} catch (Exception e) {
+			throw new MojoExecutionException("Error during generation", e);
+		}
+	}
+
+	private void writeConverters(Model model, XMLStreamWriter writer)
+			throws Exception {
+
+	}
+
+	private void writeValidators(Model model, XMLStreamWriter writer)
+			throws Exception {
+
+	}
+
+	private void writeComponents(Model model, XMLStreamWriter writer)
+			throws Exception {
+		for (Iterator i = model.components(); i.hasNext();) {
 			ComponentModel cmp = (ComponentModel) i.next();
 			writer.writeStartElement("component");
 			writer.writeCharacters("\n");
@@ -199,34 +203,14 @@
 			writer.writeCharacters("\n");
 		}
 	}
-	
-	private void writeRenderers(Model model, XMLStreamWriter writer) throws Exception
-	{
-		
-	}
-	
-	private void writeRenderKits(Model model, XMLStreamWriter writer) throws Exception
-	{
-		
-	}
-	
 
-	/**
-	 * @parameter expression="${project}"
-	 * @readonly
-	 */
-	private MavenProject project;
+	private void writeRenderers(Model model, XMLStreamWriter writer)
+			throws Exception {
 
+	}
 
-	  /**
-	   * @parameter
-	   * @readonly
-	   */
-	  private String facesConfigFile = "META-INF/faces-config.xml";
-
-	  /**
-	   * @parameter expression="${project.build.directory}"
-	   * @readonly
-	   */
-	  private File targetDirectory;
+	private void writeRenderKits(Model model, XMLStreamWriter writer)
+			throws Exception {
+
+	}
 }

Modified: myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/model/Model.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/model/Model.java?rev=637426&r1=637425&r2=637426&view=diff
==============================================================================
--- myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/model/Model.java (original)
+++ myfaces/myfaces-build-tools/branches/skitching/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/model/Model.java Sat Mar 15 08:21:21 2008
@@ -18,8 +18,10 @@
  */
 package org.apache.myfaces.buildtools.maven2.plugin.builder.model;
 
-import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 import java.util.logging.Logger;
@@ -45,6 +47,14 @@
   }
 
   /**
+   * Returns all converters
+   */
+  public List getConverters()
+  {
+	  return new ArrayList(_converters.values());
+  }
+
+  /**
    * Returns an iterator for all converters
    */
   public Iterator converters()
@@ -61,6 +71,14 @@
   }
 
   /**
+   * Returns all validators
+   */
+  public List getValidators()
+  {
+	  return new ArrayList(_validators.values());
+  }
+
+  /**
    * Returns an iterator for all validators
    */
   public Iterator validators()
@@ -83,43 +101,21 @@
   }
 
   /**
-   * Returns the component for this component type.
-   *
-   * @param componentType  the component type to find
+   * Returns all components
    */
-  public ComponentModel findComponent(
-    String componentType)
+  public List getComponents()
   {
-    return (ComponentModel)_components.get(componentType);
+	  return new ArrayList(_components.values());
   }
 
   /**
-   * Returns true if this faces config has any components.
-   *
-   * @return true  if this faces config has any components,
-   *         otherwise false
-   */
-  public boolean hasComponents()
-  {
-    return !_components.isEmpty();
-  }
-
-  /**
-   * Returns an iterator for all components in this faces
-   * config.
-   *
-   * @return  the component iterator
+   * Returns an iterator for all components.
    */
   public Iterator components()
   {
     return _components.values().iterator();
   }
 
-  private void _warning(String s)
-  {
-    _LOG.warning(s);
-  }
-
   /**
    * Adds a render kit to this faces config document.
    *
@@ -140,36 +136,29 @@
     }
   }
 
-  /**
-   * Returns the render kit for this render kit id.
-   *
-   * @param renderKitId  the render kit id to find
-   */
-  public RenderKitModel findRenderKit(
-    String renderKitId)
+  public List getRenderKits()
   {
-    return (RenderKitModel)_renderKits.get(renderKitId);
+	  return new ArrayList(_renderKits.values());
   }
 
   /**
-   * Returns true if this faces config has any render kits.
+   * Returns an iterator for all render kits in this faces
+   * config.
    *
-   * @return true  if this faces config has any render kits,
-   *         otherwise false
+   * @return  the render kit iterator
    */
-  public boolean hasRenderKits()
+  public Iterator renderKits()
   {
-    return !_renderKits.isEmpty();
+    return _renderKits.values().iterator();
   }
 
   /**
-   * Returns an iterator for all render kits in this faces
-   * config.
+   * Returns the render kit for this render kit id.
    *
-   * @return  the render kit iterator
+   * @param renderKitId  the render kit id to find
    */
-  public Iterator renderKits()
+  private RenderKitModel findRenderKit(String renderKitId)
   {
-    return _renderKits.values().iterator();
+    return (RenderKitModel)_renderKits.get(renderKitId);
   }
 }