You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:16:32 UTC

[sling-maven-sling-plugin] 19/28: SLING-653 : Add validaion mojo for validating resources; added the validation of included json files.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag maven-sling-plugin-2.0.4-incubator
in repository https://gitbox.apache.org/repos/asf/sling-maven-sling-plugin.git

commit 1d6da0967e33b04bb66c259da410ad897ae09bbf
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sat Mar 28 14:16:20 2009 +0000

    SLING-653 : Add validaion mojo for validating resources; added the validation of included json files.
    
    git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/maven/maven-sling-plugin@759465 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/maven/bundlesupport/ValidationMojo.java  | 127 +++++++++++++++++++++
 1 file changed, 127 insertions(+)

diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
new file mode 100644
index 0000000..1990ebd
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
@@ -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);
+                }
+            }
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.