incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r759465 - /incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
Date Sat, 28 Mar 2009 14:16:20 GMT
Author: cziegeler
Date: Sat Mar 28 14:16:20 2009
New Revision: 759465

URL: http://svn.apache.org/viewvc?rev=759465&view=rev
Log:
SLING-653 : Add validaion mojo for validating resources; added the validation of included
json files.

Added:
    incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
  (with props)

Added: incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java?rev=759465&view=auto
==============================================================================
--- incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
(added)
+++ incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
Sat Mar 28 14:16:20 2009
@@ -0,0 +1,127 @@
+/*
+ * 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.sling.maven.bundlesupport;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.model.Resource;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
+import org.codehaus.plexus.util.DirectoryScanner;
+
+/**
+ * Plugin to validate resources:
+ * - validate json files
+ *
+ * @goal validate
+ * @phase process-resources
+ */
+public class ValidationMojo extends AbstractMojo {
+
+    /**
+     * The Maven project.
+     *
+     * @parameter expression="${project}"
+     * @required
+     * @readonly
+     */
+    private MavenProject project;
+
+    /**
+     * Whether to skip the validation
+     *
+     * @parameter expression="${sling.validation.skip}" default-value="false"
+     * @required
+     */
+    private boolean skip;
+
+    /**
+     * Whether to skip the json validation
+     *
+     * @parameter expression="${sling.validation.skipJson}" default-value="false"
+     * @required
+     */
+    private boolean skipJson;
+
+    /**
+     * @see org.apache.maven.plugin.AbstractMojo#execute()
+     */
+    public void execute()
+    throws MojoExecutionException {
+        if ( this.skip ) {
+            getLog().info("Validation is skipped.");
+            return;
+        }
+        @SuppressWarnings("unchecked")
+        final Iterator<Resource> rsrcIterator = this.project.getResources().iterator();
+        while ( rsrcIterator.hasNext() ) {
+            final Resource rsrc = rsrcIterator.next();
+
+            getLog().debug("Scanning " + rsrc.getDirectory());
+            final File directory = new File(rsrc.getDirectory());
+            final DirectoryScanner scanner = new DirectoryScanner();
+            scanner.setBasedir( directory );
+
+            if ( rsrc.getExcludes() != null && rsrc.getExcludes().size() > 0 )
{
+                scanner.setExcludes( (String[]) rsrc.getExcludes().toArray(new String[rsrc.getExcludes().size()]
) );
+            }
+            scanner.addDefaultExcludes();
+            if ( rsrc.getIncludes() != null && rsrc.getIncludes().size() > 0 )
{
+                scanner.setIncludes( (String[]) rsrc.getIncludes().toArray(new String[rsrc.getIncludes().size()]
));
+            }
+
+            scanner.scan();
+
+            final String[] files = scanner.getIncludedFiles();
+            if ( files != null ) {
+                for(int m=0; m<files.length; m++) {
+                    this.validate(directory, files[m]);
+                }
+            }
+        }
+    }
+
+    private void validate(final File directory, final String fileName)
+    throws MojoExecutionException {
+        getLog().debug("Validating " + fileName);
+        final File file = new File(directory, fileName);
+        if ( file.isFile() ) {
+            if ( fileName.endsWith(".json") && !this.skipJson ) {
+                try {
+                    final FileInputStream fis = new FileInputStream(file);
+                    getLog().debug("Validation JSON file " + fileName);
+                    final String json = IOUtils.toString(fis);
+                    new JSONObject(json);
+                    fis.close();
+                } catch (JSONException e) {
+                    throw new MojoExecutionException("An Error occured while validating the
file '"+fileName+"'", e);
+                } catch (IOException e) {
+                    throw new MojoExecutionException("An Error occured while validating the
file '"+fileName+"'", e);
+                }
+            }
+        }
+    }
+}

Propchange: incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message