plugins/org.w3c.css/cssvalidator-eclipse-src/org/symbian/tools/wrttools/core/internal/validation/CssBuildValidator.java
author Eugene Ostroukhov <eugeneo@symbian.org>
Thu, 14 Oct 2010 14:46:23 -0700
changeset 493 e4a4cac2cdeb
permissions -rw-r--r--
CSS validator plugin was made completely self-contained
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
493
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     1
/*******************************************************************************
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     2
 * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies).
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     3
 * All rights reserved.
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     8
 *
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
     9
 * Initial Contributors:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    10
 * Symbian Foundation - initial contribution.
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    11
 * Contributors:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    12
 * Description:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    13
 * Overview:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    14
 * Details:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    15
 * Platforms/Drives/Compatibility:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    16
 * Assumptions/Requirement/Pre-requisites:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    17
 * Failures and causes:
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    18
 *******************************************************************************/
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    19
package org.symbian.tools.wrttools.core.internal.validation;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    20
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    21
import java.io.IOException;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    22
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    23
import org.eclipse.core.resources.IMarker;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    24
import org.eclipse.core.resources.IResource;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    25
import org.eclipse.core.runtime.IProgressMonitor;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    26
import org.eclipse.core.runtime.IStatus;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    27
import org.eclipse.core.runtime.Platform;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    28
import org.eclipse.core.runtime.Status;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    29
import org.eclipse.wst.validation.AbstractValidator;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    30
import org.eclipse.wst.validation.ValidationResult;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    31
import org.eclipse.wst.validation.ValidationState;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    32
import org.eclipse.wst.validation.ValidatorMessage;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    33
import org.w3c.css.css.DocumentParser;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    34
import org.w3c.css.css.StyleSheet;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    35
import org.w3c.css.parser.CssError;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    36
import org.w3c.css.properties.PropertiesLoader;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    37
import org.w3c.css.util.ApplContext;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    38
import org.w3c.css.util.Warning;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    39
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    40
public class CssBuildValidator extends AbstractValidator {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    41
    @Override
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    42
    public ValidationResult validate(IResource resource, int kind, ValidationState state, IProgressMonitor monitor) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    43
        return main(resource.getLocationURI().toString(), resource);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    44
    }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    45
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    46
    public ValidationResult main(String uri, IResource resource) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    47
        String language = "en";
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    48
        String profile = "css3"; // css2, css21 (default), css3, svg, svgbasic,
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    49
        // svgtiny, atsc-tv, mobile, tv
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    50
        String medium = ""; // (default), aural, braille, embossed,
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    51
        // handheld, print, projection, screen,
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    52
        // tty, tv, presentation
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    53
        int warningLevel = 2; // -1 (no warning), 0, 1, 2 (default, all the
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    54
        // warnings)
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    55
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    56
        // first, we get the parameters and create an application context
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    57
        ApplContext ac = new ApplContext(language);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    58
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    59
        if (profile != null && !"none".equals(profile)) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    60
            if ("css1".equals(profile) || "css2".equals(profile) || "css21".equals(profile) || "css3".equals(profile)
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    61
                    || "svg".equals(profile) || "svgbasic".equals(profile) || "svgtiny".equals(profile)) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    62
                ac.setCssVersion(profile);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    63
            } else {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    64
                ac.setProfile(profile);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    65
                ac.setCssVersion(PropertiesLoader.config.getProperty("defaultProfile"));
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    66
            }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    67
        } else {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    68
            ac.setProfile(profile);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    69
            ac.setCssVersion(PropertiesLoader.config.getProperty("defaultProfile"));
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    70
        }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    71
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    72
        // medium to use
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    73
        ac.setMedium(medium);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    74
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    75
        // HTML document
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    76
        try {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    77
            DocumentParser urlParser = new DocumentParser(ac, uri);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    78
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    79
            return handleRequest(ac, uri, urlParser.getStyleSheet(), warningLevel, true, resource);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    80
        } catch (Exception e) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    81
            Platform.getLog(Platform.getBundle("org.w3c.css")).log(new Status(IStatus.ERROR, "org.w3c.css", null, e));
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    82
        }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    83
        return null;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    84
    }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    85
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    86
    private ValidationResult handleRequest(ApplContext ac, String title, StyleSheet styleSheet, int warningLevel,
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    87
            boolean errorReport, IResource resource) throws Exception {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    88
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    89
        if (styleSheet == null) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    90
            throw new IOException(ac.getMsg().getServletString("process") + " " + title);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    91
        }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    92
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    93
        styleSheet.findConflicts(ac);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    94
        ValidationResult result = new ValidationResult();
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    95
        CssError[] errors = styleSheet.getErrors().getErrors();
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    96
        for (CssError cssError : errors) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    97
            String msg = cssError.getException().getLocalizedMessage();
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    98
            if (msg != null && msg.trim().length() > 0) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    99
                ValidatorMessage message = createMessage(resource, cssError.getLine(), msg, IMarker.SEVERITY_WARNING);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   100
                result.add(message);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   101
            }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   102
        }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   103
        Warning[] warnings = styleSheet.getWarnings().getWarnings();
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   104
        for (Warning warning : warnings) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   105
            ValidatorMessage message = createMessage(resource, warning.getLine(), warning.getWarningMessage(),
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   106
                    IMarker.SEVERITY_WARNING);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   107
            result.add(message);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   108
        }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   109
        return result;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   110
    }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   111
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   112
    private ValidatorMessage createMessage(IResource resource, int line, String msg, int severity) {
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   113
        ValidatorMessage message = ValidatorMessage.create(msg, resource);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   114
        message.setAttribute(IMarker.LINE_NUMBER, line);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   115
        message.setAttribute(IMarker.SEVERITY, severity);
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   116
        return message;
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   117
    }
e4a4cac2cdeb CSS validator plugin was made completely self-contained
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   118
}