You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2016/11/20 08:41:50 UTC

svn commit: r1770535 - in /sling/trunk/tooling/ide/eclipse-core: plugin.xml src/org/apache/sling/ide/eclipse/internal/validation/IgnoreMissingGrammarXmlValidator.java

Author: kwin
Date: Sun Nov 20 08:41:50 2016
New Revision: 1770535

URL: http://svn.apache.org/viewvc?rev=1770535&view=rev
Log:
SLING-6237 only enable xml validator for xml files within content projects, also call validator for ".content.xml" files

Modified:
    sling/trunk/tooling/ide/eclipse-core/plugin.xml
    sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/internal/validation/IgnoreMissingGrammarXmlValidator.java

Modified: sling/trunk/tooling/ide/eclipse-core/plugin.xml
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/plugin.xml?rev=1770535&r1=1770534&r2=1770535&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-core/plugin.xml (original)
+++ sling/trunk/tooling/ide/eclipse-core/plugin.xml Sun Nov 20 08:41:50 2016
@@ -283,10 +283,14 @@
                      caseSensitive="false"
                      ext="xml">
                </fileext>
-               <!--only enable for sling.content projects -->
-               <facet id="sling.content"/>
             </rules>
          </include>
+         <include>
+             <rules>
+                <!--only enable for sling.content projects -->
+                <facet id="sling.content"/>
+             </rules>
+         </include>
          <exclude>
             <rules>
 				<projectNature id="org.eclipse.jst.j2ee.ejb.EJBNature"/>

Modified: sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/internal/validation/IgnoreMissingGrammarXmlValidator.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/internal/validation/IgnoreMissingGrammarXmlValidator.java?rev=1770535&r1=1770534&r2=1770535&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/internal/validation/IgnoreMissingGrammarXmlValidator.java (original)
+++ sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/internal/validation/IgnoreMissingGrammarXmlValidator.java Sun Nov 20 08:41:50 2016
@@ -16,12 +16,37 @@
  */
 package org.apache.sling.ide.eclipse.internal.validation;
 
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.sling.ide.eclipse.core.internal.Activator;
+import org.apache.sling.ide.log.Logger;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.wst.validation.ValidationResult;
+import org.eclipse.wst.validation.ValidationState;
+import org.eclipse.wst.validation.internal.core.ValidationException;
+import org.eclipse.wst.validation.internal.provisional.core.IReporter;
+import org.eclipse.wst.validation.internal.provisional.core.IValidationContext;
+import org.eclipse.wst.xml.core.internal.validation.core.AbstractNestedValidator;
 import org.eclipse.wst.xml.core.internal.validation.core.NestedValidatorContext;
 import org.eclipse.wst.xml.core.internal.validation.eclipse.Validator;
 
 @SuppressWarnings("restriction")
+/** Almost the standard xml validator from WST with the following deviations - missing grammar is never marked - also .content.xml is
+ * validated (the default XML validator skips all files starting with ".") */
 public class IgnoreMissingGrammarXmlValidator extends Validator {
 
+    private final String GET_FILE = "getFile"; //$NON-NLS-1$
+    private final String GET_PROJECT_FILES = "getAllFiles"; //$NON-NLS-1$
+    private final String GET_INPUTSTREAM = "inputStream"; //$NON-NLS-1$
+
     @Override
     protected void setupValidation(NestedValidatorContext context) {
         super.setupValidation(context);
@@ -29,5 +54,137 @@ public class IgnoreMissingGrammarXmlVali
         indicateNoGrammar = 0;
     }
 
+    /** Perform the validation using version 2 of the validation framework. Copied from {@link AbstractNestedValidator#validate(IResource,
+     * int, ValidationState, IProgressMonitor). Cannot use original one as that skips resources starting with "." */
+    @Override
+    public ValidationResult validate(IResource resource, int kind, ValidationState state, IProgressMonitor monitor) {
+        ValidationResult result = new ValidationResult();
+        IFile file = null;
+        if (resource instanceof IFile)
+            file = (IFile) resource;
+        if (file != null && shouldValidate(file)) {
+            IReporter reporter = result.getReporter(monitor);
+
+            NestedValidatorContext nestedcontext = getNestedContext(state, false);
+            boolean teardownRequired = false;
+            if (nestedcontext == null) {
+                // validationstart was not called, so manually setup and tear down
+                nestedcontext = getNestedContext(state, true);
+                nestedcontext.setProject(file.getProject());
+                setupValidation(nestedcontext);
+                teardownRequired = true;
+            } else {
+                nestedcontext.setProject(file.getProject());
+            }
+            doValidate(file, null, result, reporter, nestedcontext);
+
+            if (teardownRequired)
+                teardownValidation(nestedcontext);
+        }
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.eclipse.wst.validation.internal.provisional.core.IValidatorJob#validateInJob(org.eclipse.wst.validation.internal.provisional.core
+     * .IValidationContext, org.eclipse.wst.validation.internal.provisional.core.IReporter)
+     * 
+     * Copied from {@link AbstractNestedValidator#validateInJob(IValidationContext context, IReporter reporter). Cannot use original one as
+     * that skips resources starting with "."
+     */
+    @Override
+    public IStatus validateInJob(IValidationContext context, IReporter reporter) throws ValidationException {
+        NestedValidatorContext nestedcontext = new NestedValidatorContext();
+        setupValidation(nestedcontext);
+        String[] fileURIs = context.getURIs();
+        if (fileURIs != null && fileURIs.length > 0) {
+            int numFiles = fileURIs.length;
+            for (int i = 0; i < numFiles && !reporter.isCancelled(); i++) {
+                String fileName = fileURIs[i];
+                if (fileName != null) {
+                    Object[] parms = { fileName };
+
+                    IFile file = (IFile) context.loadModel(GET_FILE, parms);
+                    if (file != null && shouldValidate(file)) {
+                        nestedcontext.setProject(file.getProject());
+                        // The helper may not have a file stored in it but may have an InputStream if being
+                        // called from a source other than the validation framework such as an editor.
+                        if (context.loadModel(GET_INPUTSTREAM) instanceof InputStream) {
+                            doValidate(file, (InputStream) context.loadModel(GET_INPUTSTREAM), null, reporter, nestedcontext); // do we need
+                                                                                                                             // the
+                                                                                                                             // fileName?
+                                                                                                                             // what is int
+                                                                                                                             // ruleGroup?
+                        } else {
+                            doValidate(file, null, null, reporter, nestedcontext);
+                        }
+                    }
+                }
+            }
+        }
+        // TODO: Is this needed? Shouldn't the framework pass the complete list?
+        // Should I know that I'm validating a project as opposed to files?
+        else {
+            Object[] parms = { getValidatorID() };
+            Collection files = (Collection) context.loadModel(GET_PROJECT_FILES, parms);
+            // files can be null if they're outside of the workspace
+            if (files != null) {
+                Iterator iter = files.iterator();
+                while (iter.hasNext() && !reporter.isCancelled()) {
+                    IFile file = (IFile) iter.next();
+                    if (shouldValidate(file)) {
+                        doValidate(file, null, null, reporter, nestedcontext);
+                    }
+                }
+            }
+        }
+
+        teardownValidation(nestedcontext);
+        if (reporter.isCancelled())
+            return Status.CANCEL_STATUS;
+        return Status.OK_STATUS;
+    }
+
+    /** Call the original private method named validate with the same parameters from {@link AbstractNestedValidator} through reflection.
+     * 
+     * @param file
+     * @param inputstream
+     * @param result
+     * @param reporter
+     * @param context */
+    private void doValidate(IFile file, InputStream inputstream, ValidationResult result, IReporter reporter,
+            NestedValidatorContext context) {
+        try {
+            Method method = AbstractNestedValidator.class.getDeclaredMethod("validate", IFile.class, InputStream.class,
+                    ValidationResult.class,
+                    IReporter.class, NestedValidatorContext.class);
+            method.setAccessible(true);
+            method.invoke(this, file, inputstream, result, reporter, context);
+        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
+                | SecurityException e) {
+            Logger logger = Activator.getDefault().getPluginLogger();
+            logger.error("Failed calling validate method on AbstractNestedValidator, probably WTP version is incompatible.", e);
+        }
+    }
+
+    /** Determine if a given file should be validated. Mostly copied from {@link AbstractNestedValidator#shouldValidate(...)} but will not
+     * skip {@code .content.xml} files.
+     * 
+     * @param file The file that may be validated.
+     * @return True if the file should be validated, false otherwise. */
+    private static boolean shouldValidate(IFile file) {
+        IResource resource = file;
+        do {
+            if (resource.isDerived() || resource.isTeamPrivateMember() ||
+                    !resource.isAccessible() || (resource.getName().charAt(0) == '.' && !".content.xml".equals(resource.getName()))) {
+                return false;
+            }
+            resource = resource.getParent();
+        } while ((resource.getType() & IResource.PROJECT) == 0);
+
+        return true;
+    }
 
 }