[Jboss-cvs] JBossAS SVN: r56998 - projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Sep 20 05:51:41 EDT 2006


Author: adrian at jboss.org
Date: 2006-09-20 05:51:38 -0400 (Wed, 20 Sep 2006)
New Revision: 56998

Added:
   projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/JAXPDeployer.java
   projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/ObjectModelFactoryDeployer.java
   projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/XSLDeployer.java
Log:
Add helpers for parser deployers that use:
ObjectModelFactory
JAXP
XSL

Added: projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/JAXPDeployer.java
===================================================================
--- projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/JAXPDeployer.java	2006-09-20 09:50:49 UTC (rev 56997)
+++ projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/JAXPDeployer.java	2006-09-20 09:51:38 UTC (rev 56998)
@@ -0,0 +1,195 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.deployers.plugins.deployers.helpers;
+
+import java.io.InputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.jboss.deployers.spi.deployer.DeploymentUnit;
+import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.virtual.VirtualFile;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ * SchemaResolverDeployer.
+ * 
+ * @param <T> the expected type 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class JAXPDeployer<T> extends AbstractParsingDeployer<T>
+{
+   /** Use a namespace aware parser */
+   private boolean useNamespaceAwareParser = true;
+
+   /** A flag indicating if deployment descriptors should be validated */
+   private boolean validateDTDs;
+   
+   /** The document builder factory */
+   private DocumentBuilderFactory documentBuilderFactory;
+   
+   /**
+    * Create a new JAXPDeployer.
+    * 
+    * @param deploymentType the deployment type
+    * @throws IllegalArgumentException for a null deployment type
+    */
+   public JAXPDeployer(Class<T> deploymentType)
+   {
+      super(deploymentType);
+   }
+
+   /**
+    * Get the useNamespaceAwareParser.
+    * 
+    * @return the useNamespaceAwareParser.
+    */
+   public boolean isUseNamespaceAwareParser()
+   {
+      return useNamespaceAwareParser;
+   }
+
+   /**
+    * Set the useNamespaceAwareParser.
+    * 
+    * @param useNamespaceAwareParser the useNamespaceAwareParser.
+    */
+   public void setUseNamespaceAwareParser(boolean useNamespaceAwareParser)
+   {
+      this.useNamespaceAwareParser = useNamespaceAwareParser;
+   }
+
+   /**
+    * Get the validateDTDs.
+    * 
+    * @return the validateDTDs.
+    */
+   public boolean isValidateDTDs()
+   {
+      return validateDTDs;
+   }
+
+   /**
+    * Set the validateDTDs.
+    * 
+    * @param validateDTDs the validateDTDs.
+    */
+   public void setValidateDTDs(boolean validateDTDs)
+   {
+      this.validateDTDs = validateDTDs;
+   }
+
+   /**
+    * Get the documentBuilderFactory.
+    * 
+    * @return the documentBuilderFactory.
+    * @throws IllegalStateException if the create method has not been invoked
+    */
+   protected DocumentBuilderFactory getDocumentBuilderFactory()
+   {
+      if (documentBuilderFactory == null)
+         throw new IllegalStateException("Document builder factory has not been constructed");
+      return documentBuilderFactory;
+   }
+
+   /**
+    * Create lifecycle
+    * 
+    * @throws Exception for any problem
+    */
+   public void create() throws Exception
+   {
+      documentBuilderFactory = DocumentBuilderFactory.newInstance();
+      documentBuilderFactory.setNamespaceAware(useNamespaceAwareParser);
+      documentBuilderFactory.setValidating(validateDTDs);
+   }
+
+   /**
+    * Destroy lifecycle
+    */
+   public void destroy()
+   {
+      documentBuilderFactory = null;
+   }
+   
+   /**
+    * Parse a deployment
+    * 
+    * @param unit the deployment unit
+    * @param file the metadata file
+    * @return the metadata
+    * @throws Exception for any error
+    */
+   protected T parse(DeploymentUnit unit, VirtualFile file) throws Exception
+   {
+      Document document = doParse(unit, file);
+      return parse(unit, file, document);
+   }
+   
+   /**
+    * Do the parsing
+    * 
+    * @param unit the deployment unit
+    * @param file the metadata file
+    * @return the document
+    * @throws Exception for any error
+    */
+   protected Document doParse(DeploymentUnit unit, VirtualFile file) throws Exception
+   {
+      if (file == null)
+         throw new IllegalArgumentException("Null file");
+      
+      InputStream is = file.openStream();
+      try
+      {
+         DocumentBuilder parser = getDocumentBuilderFactory().newDocumentBuilder();
+         InputSource source = new InputSource(is);
+         source.setSystemId(file.toURI().toString());
+         parser.setEntityResolver(new JBossEntityResolver());
+         return parser.parse(is);
+      }
+      finally
+      {
+         try
+         {
+            is.close();
+         }
+         catch (Exception ignored)
+         {
+         }
+      }
+   }
+
+   /**
+    * Parse a deployment
+    * 
+    * @param unit the deployment unit
+    * @param file the metadata file
+    * @param document the document
+    * @return the metadata
+    * @throws Exception for any error
+    */
+   protected abstract T parse(DeploymentUnit unit, VirtualFile file, Document document) throws Exception;
+}

Added: projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/ObjectModelFactoryDeployer.java
===================================================================
--- projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/ObjectModelFactoryDeployer.java	2006-09-20 09:50:49 UTC (rev 56997)
+++ projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/ObjectModelFactoryDeployer.java	2006-09-20 09:51:38 UTC (rev 56998)
@@ -0,0 +1,94 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.deployers.plugins.deployers.helpers;
+
+import java.net.URL;
+
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.deployer.DeploymentUnit;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.xb.binding.ObjectModelFactory;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+
+/**
+ * SchemaResolverDeployer.
+ * 
+ * @param <T> the expected type 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class ObjectModelFactoryDeployer<T> extends AbstractParsingDeployer<T>
+{
+   /** Unmarshaller factory */
+   private static final UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
+   
+   /**
+    * Create a new SchemaResolverDeployer.
+    * 
+    * @param deploymentType the deployment type
+    * @throws IllegalArgumentException for a null deployment type
+    */
+   public ObjectModelFactoryDeployer(Class<T> deploymentType)
+   {
+      super(deploymentType);
+   }
+
+   /**
+    * Parse a deployment
+    * 
+    * @param unit the deployment unit
+    * @param file the metadata file
+    * @return the metadata
+    * @throws Exception for any error
+    */
+   protected T parse(DeploymentUnit unit, VirtualFile file) throws Exception
+   {
+      if (file == null)
+         throw new IllegalArgumentException("Null file");
+
+      Unmarshaller unmarshaller = factory.newUnmarshaller();
+      Object parsed = null;
+      try
+      {
+         ObjectModelFactory factory = getObjectModelFactory();
+         Object root = null;
+         URL url = file.toURL();
+         parsed = unmarshaller.unmarshal(url.toString(), factory, root);
+      }
+      catch (Throwable t)
+      {
+         DeploymentException.rethrowAsDeploymentException("Error parsing meta data " + file.getPathName(), t);
+      }
+      if (parsed == null)
+         throw new DeploymentException("The xml " + file.getPathName() + " is not well formed!");
+
+      return getDeploymentType().cast(parsed);
+   }
+
+   /**
+    * Get the object model factory 
+    * 
+    * @return the object model factory
+    */
+   protected abstract ObjectModelFactory getObjectModelFactory();
+}

Added: projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/XSLDeployer.java
===================================================================
--- projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/XSLDeployer.java	2006-09-20 09:50:49 UTC (rev 56997)
+++ projects/microcontainer/trunk/deployers/src/main/org/jboss/deployers/plugins/deployers/helpers/XSLDeployer.java	2006-09-20 09:51:38 UTC (rev 56998)
@@ -0,0 +1,190 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.deployers.plugins.deployers.helpers;
+
+import java.io.InputStream;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+
+import org.jboss.deployers.spi.deployer.DeploymentUnit;
+import org.jboss.util.xml.DOMWriter;
+import org.jboss.util.xml.JBossErrorHandler;
+import org.jboss.virtual.VirtualFile;
+import org.w3c.dom.Document;
+
+/**
+ * SchemaResolverDeployer.
+ * 
+ * @param <T> the expected type 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class XSLDeployer<T> extends JAXPDeployer<T>
+{
+   /** The xsl resource path */
+   protected String xslPath;
+
+   /** The templates */
+   private Templates templates;
+
+   /**
+    * Create a new XSLDeployer.
+    * 
+    * @param deploymentType the deployment type
+    * @throws IllegalArgumentException for a null deployment type
+    */
+   public XSLDeployer(Class<T> deploymentType)
+   {
+      super(deploymentType);
+   }
+
+   /**
+    * Get the xslPath.
+    * 
+    * @return the xslPath.
+    */
+   public String getXSLPath()
+   {
+      return xslPath;
+   }
+
+   /**
+    * Set the xslPath.
+    * 
+    * @param xslPath the xslPath.
+    */
+   public void setXSLPath(String xslPath)
+   {
+      this.xslPath = xslPath;
+   }
+
+   /**
+    * Get the templates.
+    * 
+    * @return the templates.
+    * @throws IllegalStateException if the create method has not been invoked
+    */
+   public Templates getTemplates()
+   {
+      if (templates == null)
+         throw new IllegalStateException("Templates have not been constructed");
+      return templates;
+   }
+
+   /**
+    * Create lifecycle
+    * 
+    * @throws Exception for any problem
+    */
+   public void create() throws Exception
+   {
+      super.create();
+
+      TransformerFactory tf = TransformerFactory.newInstance();
+      
+      InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xslPath);
+      try
+      {
+         StreamSource ss = new StreamSource(is);
+         ss.setSystemId(xslPath);
+         templates = tf.newTemplates(ss);
+         log.debug("Created templates: " + templates);
+      }
+      finally
+      {
+         try
+         {
+            is.close();
+         }
+         catch (Exception ignored)
+         {
+         }
+      }
+   }
+
+   /**
+    * Destroy lifecycle
+    */
+   public void destroy()
+   {
+      super.destroy();
+      templates = null;
+   }
+
+   /**
+    * Parse a deployment
+    * 
+    * @param unit the deployment unit
+    * @param file the metadata file
+    * @return the metadata
+    * @throws Exception for any error
+    */
+   protected T parse(DeploymentUnit unit, VirtualFile file) throws Exception
+   {
+      if (file == null)
+         throw new IllegalArgumentException("Null file");
+
+      Document document = doParse(unit, file);
+
+      Transformer trans = getTemplates().newTransformer();
+      trans.setErrorListener(new JBossErrorHandler(file.getPathName(), null));
+      Source s = new DOMSource(document);
+      DOMResult r = new DOMResult();
+      setParameters(trans);
+      
+      trans.transform(s, r);
+      
+      document = (Document) r.getNode();
+      String docStr = DOMWriter.printNode(document, true);
+      log.debug("Transformed " + file.getPathName() + " into " + docStr);
+      
+      return parse(unit, file, document);
+   }
+
+   /**
+    * Set parameters for the transformation
+    * 
+    * @param trans the transformer
+    * @throws Exception for any problem
+    */
+   protected void setParameters(Transformer trans) throws Exception
+   {
+      // nothing by default
+   }
+
+   /**
+    * Parse a deployment
+    * 
+    * @param unit the deployment unit
+    * @param file the metadata file
+    * @param document the document
+    * @return the metadata
+    * @throws Exception for any error
+    */
+   protected abstract T parse(DeploymentUnit unit, VirtualFile file, Document document) throws Exception;
+}




More information about the jboss-cvs-commits mailing list