Skip to main content
summaryrefslogtreecommitdiffstats
blob: 783a810e8e0259c6c0d16d3c881035957e362714 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*******************************************************************************
 * Copyright (c) 2011 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ide.css.validation;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.URI;
import org.eclipse.fx.ide.css.cssDsl.CssDslPackage;
import org.eclipse.fx.ide.css.cssDsl.CssTok;
import org.eclipse.fx.ide.css.cssDsl.IdentifierTok;
import org.eclipse.fx.ide.css.cssDsl.SymbolTok;
import org.eclipse.fx.ide.css.cssDsl.UrlTok;
import org.eclipse.fx.ide.css.cssDsl.WSTok;
import org.eclipse.fx.ide.css.cssDsl.css_declaration;
import org.eclipse.fx.ide.css.cssDsl.css_property;
import org.eclipse.fx.ide.css.cssDsl.font_face;
import org.eclipse.fx.ide.css.cssDsl.ruleset;
import org.eclipse.fx.ide.css.cssDsl.selector;
import org.eclipse.fx.ide.css.extapi.CssExt;
import org.eclipse.fx.ide.css.extapi.Proposal;
import org.eclipse.fx.ide.css.util.Utils;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.project.PDEProject;
import org.eclipse.xtext.nodemodel.BidiTreeIterator;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.validation.Check;

import com.google.inject.Inject;


public class CssDslJavaValidator extends AbstractCssDslJavaValidator {
	private static final List<String> PREDEFINED_VAR_PROPS = Arrays.asList(
			"-fx-base",							"-fx-background",				"-fx-control-inner-background",		"-fx-control-inner-background-alt",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"-fx-dark-text-color",				"-fx-mid-text-color", 			"-fx-light-text-color", 			"-fx-accent", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"-fx-default-button",				"-fx-focus-color",				"-fx-faint-focus-color",			"-fx-color",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"CHART_COLOR_1",					"CHART_COLOR_2",				"CHART_COLOR_3",					"CHART_COLOR_4", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"CHART_COLOR_5",					"CHART_COLOR_6",				"CHART_COLOR_7",					"CHART_COLOR_8",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"CHART_COLOR_1_TRANS_20",			"CHART_COLOR_2_TRANS_20", 		"CHART_COLOR_3_TRANS_20", 			"CHART_COLOR_4_TRANS_20", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"CHART_COLOR_5_TRANS_20",			"CHART_COLOR_6_TRANS_20",		"CHART_COLOR_7_TRANS_20",			"CHART_COLOR_8_TRANS_20",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"CHART_COLOR_1_TRANS_70",			"CHART_COLOR_2_TRANS_70",		"CHART_COLOR_3_TRANS_70",			"CHART_COLOR_4_TRANS_70", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"CHART_COLOR_5_TRANS_70",			"CHART_COLOR_6_TRANS_70",		"CHART_COLOR_7_TRANS_70",			"CHART_COLOR_8_TRANS_70",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"-fx-hover-base",					"-fx-pressed-base",				"-fx-text-background-color",		"-fx-box-border", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"-fx-text-box-border",				"-fx-shadow-highlight-color", 	"-fx-outer-border",					"-fx-inner-border",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"-fx-inner-border-horizontal", 		"-fx-inner-border-bottomup", 	"-fx-body-color", 					"-fx-body-color-bottomup", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"-fx-body-color-to-right",			"-fx-text-base-color", 			"-fx-text-inner-color",				"-fx-mark-color",    //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
			"-fx-mark-highlight-color",			"-fx-selection-bar", 			"-fx-selection-bar-non-focused", 	"-fx-selection-bar-text", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			"-fx-cell-hover-color",				"-fx-cell-focus-inner-border", 	"-fx-cell-focus-inner-border", 		"-fx-page-bullet-border",   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
			"-fx-page-indicator-hover-border", "-fx-focused-text-base-color", 	"-fx-focused-mark-color"  //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
			);
	//private @Inject CssDialectExtensionRegistry extension;

	private @Inject CssExt ext;

//	@Check
//	public void checkGreetingStartsWithCapital(Greeting greeting) {
//		if (!Character.isUpperCase(greeting.getName().charAt(0))) {
//			warning("Name should start with a capital", MyDslPackage.Literals.GREETING__NAME);
//		}
//	}

	private boolean filterWS(CssTok t) {
		return !(t instanceof WSTok);
	}

	private boolean filterSymbol(CssTok t) {
		return !(t instanceof SymbolTok);
	}

	@Check
	public void checkProperty(css_property property) {
	}

	@SuppressWarnings("restriction")
	@Check
	public void checkDeclaration(css_declaration dec) {
//		System.err.println("CHECK DECL " + dec);
		css_property property = dec.getProperty();

		// Only validate files who are:
		// * in a plug-in project
		//   - when css is part of build.properties bin.includes
		//   - when css is part of the source-folder
		IFile file = Utils.getFile(dec.eResource());

		//TODO We should add a service possibility to contribute these lookups
		boolean validate = false;

		try {
			if( file.getProject().hasNature("org.eclipse.pde.PluginNature") ) { //$NON-NLS-1$
				// validate = true;
				IFile properties = PDEProject.getBuildProperties(file.getProject());
				Properties p = new Properties();
				try( InputStream in = properties.getContents() ) {
					p.load(in);
					String includes = p.getProperty("bin.includes"); //$NON-NLS-1$
					if( includes != null ) {
						IPath path = file.getProjectRelativePath();
						for( String s : includes.split(",") ) { //$NON-NLS-1$
							if( path.toString().startsWith(s.trim()) ) {
								validate = true;
								break;
							}
						}
					}
				} catch(IOException e) {
					e.printStackTrace();
				}
			}

			if( ! validate && file.getProject().hasNature("org.eclipse.jdt.core.javanature") ) { //$NON-NLS-1$
				IJavaProject jp = JavaCore.create(file.getProject());
				for( IPackageFragmentRoot r : jp.getPackageFragmentRoots() ) {
					if( r.getKind() == IPackageFragmentRoot.K_SOURCE ) {
						if( file.getProjectRelativePath().toString().startsWith(r.getPath().removeFirstSegments(1).toString()) ) {
							validate = true;
							break;
						}
					}
				}
			}
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if( ! validate ) {
			return;
		}

		if( dec.eContainer() instanceof font_face ) {
			if( "font-family".equals(property.getName()) ) {
				if( dec.getValueTokens().stream().filter(this::filterWS).count() != 1 ) {
					error("Font family has to define a name", dec, CssDslPackage.Literals.CSS_DECLARATION__VALUE_TOKENS);
				} else {
					if( ! (dec.getValueTokens().stream().filter(this::filterWS).findFirst().get() instanceof IdentifierTok) ) {
						CssTok tok = dec.getValueTokens().stream().filter(this::filterWS).findFirst().get();
						error("Invalid font family name", dec, CssDslPackage.Literals.CSS_DECLARATION__VALUE_TOKENS, dec.getValueTokens().indexOf(tok));
					}
				}
			} else if( "src".equals(property.getName()) ) {
				if( dec.getValueTokens().stream().filter(this::filterWS).count() == 0 ) {
					error("At least one URL is required", dec, CssDslPackage.Literals.CSS_DECLARATION__VALUE_TOKENS);
				} else {
					dec.getValueTokens().stream().filter(this::filterWS).filter(this::filterSymbol).filter((t) -> !(t instanceof UrlTok)).forEach((t) ->
						{
							error("Only url-values are allowed", dec, CssDslPackage.Literals.CSS_DECLARATION__VALUE_TOKENS,dec.getValueTokens().indexOf(t));
						}
					);
				}
			} else if( "font-stretch".equals(property.getName()) ) {

			} else if( "font-style".equals(property.getName()) ) {

			} else if( "font-weight".equals(property.getName()) ) {

			} else if( "unicode-range".equals(property.getName()) ) {

			} else {
				warning("Unknown property: \""+property.getName()+"\"", property, CssDslPackage.Literals.CSS_PROPERTY__NAME);
			}

			return;
		}


		List<Proposal> properties = ext.getPropertyProposalsForSelector(file,dec,null);
				//extension.getAllProperties(uri);

		boolean known = false;
		for (Proposal p : properties) {
			if (p.getProposal().equals(property.getName())) {
				known = true;
				break;
			}
		}

		if (!known) {
			ICompositeNode node = NodeModelUtils.getNode(dec.getProperty());

			boolean suppress = node.getText().contains("@SuppressWarning");
			if( ! suppress && ! PREDEFINED_VAR_PROPS.contains(property.getName()) ) {
				warning("Unknown property: \""+property.getName()+"\"", property, CssDslPackage.Literals.CSS_PROPERTY__NAME);
			}
		}
		else {

			ruleset rs = (ruleset) dec.eContainer();
			List<selector> selectors = rs.getSelectors();
//			Set<CssProperty> selectorProps = new HashSet<>();
//			for (selector selector : selectors) {
//				selectorProps.addAll(extension.getPropertiesForSelector(uri, selector));
//			}

			List<Proposal> selectorProps = ext.getPropertyProposalsForSelector(Utils.getFile(dec.eResource()),dec,selectors);

			if (selectorProps.size() > 0) {
				boolean supported = false;
				for (Proposal p : selectorProps) {
					if (p.getProposal().equals(property.getName())) {
						supported = true;
						break;
					}
				}

				if (!supported) {
					ICompositeNode node = NodeModelUtils.getNode(dec.getProperty());

					boolean suppress = node.getText().contains("@SuppressWarning"); //$NON-NLS-1$

					if( ! PREDEFINED_VAR_PROPS.contains(property.getName()) ) {
						warning("\""+property.getName()+"\" is not supported by the given selectors", CssDslPackage.Literals.CSS_DECLARATION__PROPERTY);
					}
				}
			}

//			List<ValidationResult> result = extension.validateProperty(uri, null, property.getName(), tokens);

//			System.err.println(result);
//
//			System.err.println("validation of " + property.getName());

//			if (!result.isEmpty()) {
//				for (ValidationResult r : result) {
//					if (r.status == ValidationStatus.ERROR) {
//						if (r.object != null) {
//							if (r.object instanceof FuncTok) {
//								FuncTok f = (FuncTok) r.object;
//								error(r.message, f, CssDslPackage.Literals.FUNC_TOK__NAME, -1);
//							}
//							else {
//								error(r.message, r.object, null, 0);
//							}
//						}
//						else {
//							error(r.message, dec, CssDslPackage.Literals.CSS_DECLARATION__VALUE_TOKENS, r.index);
//						}
//					}
//				}
//			}
		}
	}
//	@Check
//	public void checkDeclaration(css_declaration dec) {
//		css_property property = dec.getProperty();
//		String propertyName = property.getName();
//
//		if( propertyName == null || propertyName.trim().length() == 0 ) {
//			return;
//		}
//
//		if( dec.getExpression().getTermGroups().size() == 1 && dec.getExpression().getTermGroups().get(0).getTerms().size() == 1 && ("null".equals(dec.getExpression().getTermGroups().get(0).getTerms().get(0).getIdentifier()) || "inherit".equals(dec.getExpression().getTermGroups().get(0).getTerms().get(0).getIdentifier()) ) ) {
//			return;
//		}
//
//		ValidationResult[] results = new ValidationResult[0];
//		for( Property p : extension.getProperties(dec.eResource().getURI()) ) {
//			if( propertyName.equals(p.getName()) ) {
//
//				ValidationResult[] r = p.validate(dec);
//
//				// At least one validation succeed
//				if( r == null || r.length == 0 ) {
//					return;
//				} else if( r != null ) {
//					results = r;
//				}
//			}
//		}
//
//		for( ValidationResult r : results ) {
//			if( r.status == ValidationStatus.ERROR ) {
//				if( r.object == null ) {
//					error( r.message, CssDslPackage.Literals.CSS_DECLARATION__EXPRESSION );
//				} else if( r.index == -1 ) {
//					error(r.message, r.object, r.feature, 0);
//				} else {
//					error(r.message, r.object, r.feature, r.index);
//				}
//			} else if( r.status == ValidationStatus.WARNING ) {
//				if( r.object == null ) {
//					warning( r.message, CssDslPackage.Literals.CSS_DECLARATION__EXPRESSION );
//				} else if( r.index == -1 ) {
//					warning(r.message, r.object, r.feature, 0);
//				} else {
//					warning(r.message, r.object, r.feature, r.index);
//				}
//			}
//		}
//
////		System.err.println("Checking: " + extension + " => " + dec);
//	}
}

Back to the top