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