jvanzyl     2002/07/21 13:13:54

  Added:       src/plugins-build/pom/src/java/org/apache/maven
                        UpdatePomCheck.java XmlPomFormatter.java
                        XmlPomValidator.java
  Log:
  o pom related goodies
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-maven/src/plugins-build/pom/src/java/org/apache/maven/UpdatePomCheck.java
  
  Index: UpdatePomCheck.java
  ===================================================================
  package org.apache.maven;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  
  import org.apache.maven.project.Project;
  import org.apache.maven.executor.AbstractExecutor;
  
  /**
   * XML project descriptor validator. This tasks exposes 4 Ant properties :
   * <ul>
   *   <li>
   *     pomUpdateRequired : true if the POM version specified in the project
   *     descriptor does not match the current Maven version and thus Maven needs
   *     to be updated
   *   </li>
   *   <li>
   *     fromVersion : version defined in the project POM
   *   </li>
   *   <li>
   *     toVersion : POM version of currently installed Maven
   *   </li>
   *   <li>
   *     pomChecked : true. Tells the Ant build that the POM has been checked
   *   </li>
   * </ul>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author dion
   * @author <a href="[EMAIL PROTECTED]">Vincent Massol</a>
   * @version $Id: UpdatePomCheck.java,v 1.1 2002/07/21 20:13:54 jvanzyl Exp $
   */
  public class UpdatePomCheck extends AbstractExecutor
  {
      /**
       * XML project descriptor to validate
       */
      private File projectDescriptor;
  
      /**
       * Set the project descriptor to validate.
       * @param projectDescriptor the project descriptor as a {@link File}
       */
      public void setProjectDescriptor(File projectDescriptor)
      {
          this.projectDescriptor = projectDescriptor;
      }
  
      /**
       * Get the project descriptor to validate.
       * @return the project descriptor as a {@link File}
       */
      public File getProjectDescriptor()
      {
          return this.projectDescriptor;
      }
  
      /**
       * @see AbstractExecutor#execute()
       */
      public void execute() throws Exception
      {
          Project project = null;
  
          try
          {
              project = MavenUtils.getProject(this.projectDescriptor);
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
  
          // Check and see if the version of the POM being used matches
          // the version of the POM that this version of Maven expects.
          if (!project.isPomCurrent())
          {
              getProject().setProperty("maven.pomUpdateRequired", "true");
              getProject().setProperty("maven.fromVersion", 
                  project.getPomVersion());
              getProject().setProperty("maven.toVersion",
                  Integer.toString(MavenConstants.POM_VERSION));
  
              log("maven.fromVersion -> " + project.getPomVersion(),
                  org.apache.tools.ant.Project.MSG_DEBUG);
              log("  maven.toVersion -> " + MavenConstants.POM_VERSION,
                  org.apache.tools.ant.Project.MSG_DEBUG);
  
          }
  
          // Let the build system know that we have checked the POM.
          getProject().setProperty("maven.pomChecked", "true");
      }
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/plugins-build/pom/src/java/org/apache/maven/XmlPomFormatter.java
  
  Index: XmlPomFormatter.java
  ===================================================================
  package org.apache.maven;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.FileWriter;
  import java.io.File;
  
  import org.dom4j.Document;
  import org.dom4j.io.OutputFormat;
  import org.dom4j.io.XMLWriter;
  import org.dom4j.io.SAXReader;
  
  import org.apache.maven.executor.AbstractExecutor;
  
  /**
   * A little XML document formatter. Used to format a POM in XML format
   * after it has been transformed.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: XmlPomFormatter.java,v 1.1 2002/07/21 20:13:54 jvanzyl Exp $
   */
  public class XmlPomFormatter extends AbstractExecutor
  {
      /** Input xml */
      private File input;
      
      /** Output xml */
      private File output;
      
      /**
       * Set input file.
       * @param input the {@link File} to read the unformatted project object
       *      model from
       */
      public void setInput(File input)
      {
          this.input = input;
      }        
      
      /**
       * Get input file.
       * @return the file the unformatted project object model will read from
       */
      public File getInput()
      {
          return input;
      }
  
      /**
       * Set output file.
       * @param output the {@link File} to write the formatted project object
       *      object model to
       */
      public void setOutput(File output)
      {
          this.output = output;
      }        
      
      /**
       * Get output file.
       * @return the file the formatted project object model will be written to
       */
      public File getOutput()
      {
          return output;
      }
  
  
      /**
       * Format the project descriptor.
       * @throws Exception when any error occurs 
       */
      public void execute() throws Exception
      {
          Document document = createDocument();
          OutputFormat format = new OutputFormat();
          format.setIndentSize(2);
          format.setNewlines(true);
          format.setTrimText(true);
  
          FileWriter out = new FileWriter(output);
          XMLWriter writer = new XMLWriter(out, format);
          writer.write(document);
          out.close();
      }
  
      /**
       * Description of the Method
       * @return a {@link Document} from parsing the {@link #input input file}
       * @throws Exception when any error occurs 
       */
      protected Document createDocument() throws Exception
      {
          SAXReader reader = new SAXReader();
          return reader.read(input);
      }
  
      /** Left over testing method - to be removed
       * @param args command line arguments
       * @throws Exception when any error occurs 
       */
      public static void main(String[] args) throws Exception
      {
          System.out.println(args[0]);
          System.out.println(args[1]);
          
          XmlPomFormatter formatter = new XmlPomFormatter();
          formatter.setInput(new File(args[0]));
          formatter.setOutput(new File(args[1]));
          formatter.execute();
      }
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/plugins-build/pom/src/java/org/apache/maven/XmlPomValidator.java
  
  Index: XmlPomValidator.java
  ===================================================================
  package org.apache.maven;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  import java.io.IOException;
  
  import org.xml.sax.EntityResolver;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.ErrorHandler;
  
  import org.apache.maven.executor.AbstractExecutor;
  
  /**
   * Validator for the XML version of the Maven project descriptor.
   *
   * NOTE:
   * If it's possible I would like to use JAXP only here, but for now I can
   * only seem to get the validation with a schema to work with xerces2.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: XmlPomValidator.java,v 1.1 2002/07/21 20:13:54 jvanzyl Exp $
   */
  public class XmlPomValidator
      extends AbstractExecutor
      implements ErrorHandler
  {
      /**
       * Default parser name.
       */
      private static final String DEFAULT_PARSER_NAME =
          "org.apache.xerces.parsers.SAXParser";
  
      /** Control document validation. */
      private boolean validate = true;
  
      /** Control use of namespaces in documents. */
      private boolean setNameSpaces = true;
  
      /** Control use of XML Schemas in documents. */
      private boolean setSchemaSupport = true;
  
      /** Control use of full XML Schema support. */
      private boolean setSchemaFullSupport = true;
  
      /** Maven project descriptor. */
      private File projectDescriptor;
  
      /** XML Schema used to validate against. */
      private File xmlSchema;
  
      /**
       * Set the project descriptor file. This file must exist.
       * @param projectDescriptor the pom to be validated
       */
      public void setProjectDescriptor(File projectDescriptor)
      {
          this.projectDescriptor = projectDescriptor;
      }
  
      /**
       * Set the project descriptor file. This file must exist.
       * @param xmlSchema the schema to validate against
       */
      public void setXmlSchema(File xmlSchema)
      {
          this.xmlSchema = xmlSchema;
      }
  
      /**
       * Prints the output from the SAX callbacks.
       * @throws Exception when any error occurs
       */
      public void execute() throws Exception
      {
          try
          {
              String parserName = DEFAULT_PARSER_NAME;
  
              XMLReader parser = (XMLReader) Class.forName(parserName).
                  newInstance();
              parser.setErrorHandler(this);
  
              // xml.org features and properties
              parser.setFeature("http://xml.org/sax/features/validation";, 
                  validate);
              parser.setFeature("http://xml.org/sax/features/namespaces";, 
                  setNameSpaces);
  
              parser.setProperty("http://apache.org/xml/properties/schema/";
                  + "external-noNamespaceSchemaLocation",
                  xmlSchema.getAbsolutePath());
  
              // apache.org features and properties
              parser.setFeature("http://apache.org/xml/features/validation/schema";
                  , setSchemaSupport);
  
              parser.setFeature(
                  "http://apache.org/xml/features/validation/schema-full-checking";
                  , setSchemaFullSupport);
  
  
              //parser.setEntityResolver(new XmlPomEntityResolver());
              parser.parse(projectDescriptor.getCanonicalPath());
          }
          catch (org.xml.sax.SAXParseException spe)
          {
              spe.printStackTrace(System.err);
          }
          catch (org.xml.sax.SAXException se)
          {
              if (se.getException() != null)
              {
                  se.getException().printStackTrace(System.err);
              }
              else
              {
                  se.printStackTrace(System.err);
              }
          }
          catch (Exception e)
          {
              e.printStackTrace(System.err);
          }
      }
  
      /**
       * SAX Warning.
       *
       * @param ex SAXParseException
       */
      public void warning(SAXParseException ex)
      {
          System.err.println("[Warning] "
              + getLocationString(ex) + ": "
              + ex.getMessage());
      }
  
      /**
       * SAX Error.
       *
       * @param ex SAXParseException
       */
      public void error(SAXParseException ex)
      {
          System.err.println("[Error] "
              + getLocationString(ex) + ": "
              + ex.getMessage());
      }
  
      /**
       * SAX Fatal error.
       *
       * @param ex a {@link SAXParseException}
       * @throws SAXException when an error occurs
       */
      public void fatalError(SAXParseException ex) throws SAXException
      {
          System.err.println("[Fatal Error] "
              + getLocationString(ex) + ": "
              + ex.getMessage());
      }
  
      /**
       * Returns a string of the location of the exception
       * @param ex a {@link SAXParseException}
       * @return a string of the format: 'file:line#:column#'
       */
      private String getLocationString(SAXParseException ex)
      {
          StringBuffer str = new StringBuffer();
  
          String systemId = ex.getSystemId();
          if (systemId != null)
          {
              int index = systemId.lastIndexOf('/');
              if (index != -1)
              {
                  systemId = systemId.substring(index + 1);
              }
              str.append(systemId);
          }
          str.append(':');
          str.append(ex.getLineNumber());
          str.append(':');
          str.append(ex.getColumnNumber());
  
          return str.toString();
      }
  
      /**
       * Dummy entity resolver
       */
      public static class XmlPomEntityResolver
          implements EntityResolver
      {
          /** resolve the entity given by the provided Ids
           * @param publicId the public id of the entity
           * @param systemId the 'system location' (typically a URL) of the entity
           * @return an {@link InputSource input source} for retrieval of the 
           *      entity
           * @throws IOException when an I/O error occurs retrieving the entity
           * @throws SAXException if there are any problems
           */
          public InputSource resolveEntity(String publicId, String systemId)
              throws IOException, SAXException
          {
              System.out.println("publicId: " + publicId);
              System.out.println("systemId: " + systemId);
  
              return null;
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to