99
|
1 |
package org.symbian.tools.wrttools.core.internal.validation;
|
|
2 |
|
|
3 |
import java.io.IOException;
|
|
4 |
import java.text.MessageFormat;
|
|
5 |
import java.util.Set;
|
|
6 |
import java.util.TreeSet;
|
|
7 |
import java.util.regex.Matcher;
|
|
8 |
import java.util.regex.Pattern;
|
|
9 |
|
|
10 |
import org.eclipse.core.resources.IContainer;
|
|
11 |
import org.eclipse.core.resources.IFile;
|
|
12 |
import org.eclipse.core.resources.IMarker;
|
|
13 |
import org.eclipse.core.resources.IProject;
|
|
14 |
import org.eclipse.core.resources.IResource;
|
|
15 |
import org.eclipse.core.runtime.CoreException;
|
|
16 |
import org.eclipse.core.runtime.IPath;
|
|
17 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
18 |
import org.eclipse.core.runtime.Path;
|
|
19 |
import org.eclipse.jface.text.IRegion;
|
|
20 |
import org.eclipse.wst.sse.core.StructuredModelManager;
|
|
21 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
|
|
22 |
import org.eclipse.wst.validation.AbstractValidator;
|
|
23 |
import org.eclipse.wst.validation.ValidationResult;
|
|
24 |
import org.eclipse.wst.validation.ValidationState;
|
|
25 |
import org.eclipse.wst.validation.ValidatorMessage;
|
|
26 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
|
|
27 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
|
|
28 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
|
|
29 |
import org.symbian.tools.wrttools.Activator;
|
|
30 |
import org.symbian.tools.wrttools.core.validator.ValidatorPropMessages;
|
|
31 |
import org.symbian.tools.wrttools.core.validator.PlistValidator.plistElements;
|
|
32 |
import org.symbian.tools.wrttools.util.CoreUtil;
|
|
33 |
import org.symbian.tools.wrttools.util.ProjectUtils;
|
|
34 |
import org.symbian.tools.wrttools.util.Util;
|
|
35 |
import org.w3c.dom.Element;
|
|
36 |
import org.w3c.dom.Node;
|
|
37 |
import org.w3c.dom.NodeList;
|
|
38 |
|
|
39 |
@SuppressWarnings("restriction")
|
|
40 |
public class WrtApplicationValidator extends AbstractValidator {
|
|
41 |
@Override
|
|
42 |
public ValidationResult validate(IResource resource, int kind,
|
|
43 |
ValidationState state, IProgressMonitor monitor) {
|
|
44 |
if (ProjectUtils.isPlist(resource)) {
|
|
45 |
return validatePlist((IFile) resource);
|
|
46 |
} else if (isProject(resource)) {
|
|
47 |
return validateProject((IProject) resource);
|
|
48 |
} else {
|
|
49 |
return null;
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
private ValidationResult validateProject(IProject resource) {
|
|
54 |
ValidationResult result = new ValidationResult();
|
|
55 |
try {
|
|
56 |
isPlistPresent(resource, result);
|
|
57 |
} catch (CoreException e) {
|
|
58 |
Activator.log(e);
|
|
59 |
}
|
|
60 |
return result;
|
|
61 |
}
|
|
62 |
|
|
63 |
private boolean isProject(IResource resource) {
|
|
64 |
return resource.getType() == IResource.PROJECT
|
|
65 |
&& ProjectUtils.hasWrtNature(resource.getProject());
|
|
66 |
}
|
|
67 |
|
|
68 |
private ValidationResult validatePlist(IFile resource) {
|
|
69 |
ValidationResult result = new ValidationResult();
|
|
70 |
IStructuredModel structuredModel;
|
|
71 |
try {
|
|
72 |
structuredModel = StructuredModelManager.getModelManager()
|
|
73 |
.getModelForRead(resource);
|
|
74 |
try {
|
|
75 |
if (structuredModel instanceof IDOMModel) {
|
|
76 |
IDOMModel model = (IDOMModel) structuredModel;
|
|
77 |
validateElement((IDOMElement) model.getDocument()
|
|
78 |
.getDocumentElement(), result, resource);
|
|
79 |
checkHtml(model, resource, result);
|
|
80 |
checkPlistValues(model, resource, result);
|
|
81 |
}
|
|
82 |
} finally {
|
|
83 |
if (structuredModel != null) {
|
|
84 |
structuredModel.releaseFromRead();
|
|
85 |
}
|
|
86 |
}
|
|
87 |
} catch (IOException e) {
|
|
88 |
Activator.log(e);
|
|
89 |
} catch (CoreException e) {
|
|
90 |
Activator.log(e);
|
|
91 |
}
|
|
92 |
return result;
|
|
93 |
}
|
|
94 |
|
|
95 |
private void validateElement(IDOMElement element, ValidationResult result,
|
|
96 |
IResource resource) {
|
|
97 |
// showData("");
|
|
98 |
plistElements[] values = plistElements.values();
|
|
99 |
boolean isValidElement = false;
|
|
100 |
for (plistElements validElement : values) {
|
|
101 |
if (validElement.toString().equalsIgnoreCase(
|
|
102 |
element.getNodeName().trim())) {
|
|
103 |
isValidElement = true;
|
|
104 |
break;
|
|
105 |
|
|
106 |
}
|
|
107 |
continue;
|
|
108 |
}
|
|
109 |
if (!isValidElement) {
|
|
110 |
Object[] arguments = { " ", element.getNodeName().trim() };
|
|
111 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
112 |
.getString("plist.element.not.supported")
|
|
113 |
+ "{0}" + "{1}", arguments);
|
|
114 |
result.add(createMessage(resource, message, element
|
|
115 |
.getStartOffset(), element.getEndOffset()));
|
|
116 |
}
|
|
117 |
|
|
118 |
NodeList childNodes = element.getChildNodes();
|
|
119 |
for (int i = 0; i < childNodes.getLength(); i++) {
|
|
120 |
Node item = childNodes.item(i);
|
|
121 |
if (item.getNodeType() == Node.ELEMENT_NODE) {
|
|
122 |
validateElement((IDOMElement) item, result, resource);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
private ValidatorMessage createMessage(IResource resource, String message,
|
|
128 |
int startOffset, int endOffset) {
|
|
129 |
ValidatorMessage msg = ValidatorMessage.create(message, resource);
|
|
130 |
msg.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
|
|
131 |
msg.setAttribute(IMarker.CHAR_START, startOffset);
|
|
132 |
msg.setAttribute(IMarker.CHAR_END, endOffset);
|
|
133 |
return msg;
|
|
134 |
}
|
|
135 |
|
|
136 |
private boolean checkHtml(IDOMModel model, IFile plist,
|
|
137 |
ValidationResult result) {
|
|
138 |
String text = model.getStructuredDocument().get();
|
|
139 |
IRegion region = CoreUtil.getIndexFileNameRegion(text);
|
|
140 |
boolean checkHtml = false;
|
|
141 |
if (region != null) {
|
|
142 |
String htmlName = text.substring(region.getOffset(), region
|
|
143 |
.getOffset()
|
|
144 |
+ region.getLength());
|
|
145 |
IFile htmlFile = null;
|
|
146 |
try {
|
|
147 |
htmlFile = getFile(plist.getProject(), new Path(htmlName), 0,
|
|
148 |
"html", "htm");
|
|
149 |
} catch (CoreException e) {
|
|
150 |
Activator.log(e);
|
|
151 |
}
|
|
152 |
if (htmlFile == null) {
|
|
153 |
Object[] arguments = { htmlName, "<>", htmlName };
|
|
154 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
155 |
.getString("plist.html.element.mismatch")
|
|
156 |
+ "{0}" + "{1}" + "{2}", arguments);
|
|
157 |
result.add(createMessage(plist, message, region.getOffset(),
|
|
158 |
region.getOffset() + region.getLength()));
|
|
159 |
}
|
|
160 |
} else {
|
|
161 |
String message = ValidatorPropMessages
|
|
162 |
.getString("plist.html.element.mailHtml.missing");
|
|
163 |
ValidatorMessage msg = createMessage(plist, message, 0, 0);
|
|
164 |
setMessageOnRootElement(msg, model);
|
|
165 |
result.add(msg);
|
|
166 |
}
|
|
167 |
|
|
168 |
return checkHtml;
|
|
169 |
}
|
|
170 |
|
|
171 |
private void setMessageOnRootElement(ValidatorMessage msg, IDOMModel model) {
|
|
172 |
Element element = model.getDocument().getDocumentElement();
|
|
173 |
int begin = 0, end = 0;
|
|
174 |
if (element instanceof IDOMNode) {
|
|
175 |
IDOMNode node = (IDOMNode) element;
|
|
176 |
begin = node.getStartOffset();
|
|
177 |
end = node.getFirstStructuredDocumentRegion().getEnd();
|
|
178 |
}
|
|
179 |
msg.setAttribute(IMarker.CHAR_START, begin);
|
|
180 |
msg.setAttribute(IMarker.CHAR_END, end);
|
|
181 |
}
|
|
182 |
|
|
183 |
private IFile getFile(IContainer container, IPath path, int segment,
|
|
184 |
String... extensions) throws CoreException {
|
|
185 |
if (segment + 1 == path.segmentCount()) {
|
|
186 |
return getFile(container, path.lastSegment(), extensions);
|
|
187 |
} else {
|
|
188 |
String name = path.segment(segment).toLowerCase();
|
|
189 |
IResource[] members = container.members();
|
|
190 |
for (IResource resource : members) {
|
|
191 |
if (resource.getName().toLowerCase().equals(name)) {
|
|
192 |
if (resource.getType() == IResource.FOLDER) {
|
|
193 |
return getFile((IContainer) resource, path,
|
|
194 |
segment + 1, extensions);
|
|
195 |
}
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
return null;
|
|
200 |
}
|
|
201 |
|
|
202 |
private IFile getFile(IContainer container, String lastSegment,
|
|
203 |
String[] extensions) throws CoreException {
|
|
204 |
final Set<String> names = new TreeSet<String>();
|
|
205 |
names.add(lastSegment.toLowerCase());
|
|
206 |
for (String extension : extensions) {
|
|
207 |
names.add(lastSegment.concat(".").concat(extension).toLowerCase());
|
|
208 |
}
|
|
209 |
IResource[] members = container.members();
|
|
210 |
for (IResource resource : members) {
|
|
211 |
if (resource.getType() == IResource.FILE
|
|
212 |
&& names.contains(resource.getName().toLowerCase())
|
|
213 |
&& resource.isAccessible()) {
|
|
214 |
return (IFile) resource;
|
|
215 |
}
|
|
216 |
}
|
|
217 |
return null;
|
|
218 |
}
|
|
219 |
|
|
220 |
private void checkPlistValues(IDOMModel model, IResource plist,
|
|
221 |
ValidationResult result) {
|
|
222 |
String source = model.getStructuredDocument().get();
|
|
223 |
if (!hasAttributeValue(source, "DisplayName")) {
|
|
224 |
Object[] arguments = { " 'Display Name' ",
|
|
225 |
ValidatorPropMessages.getString("not.in.plist.file") };
|
|
226 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
227 |
.getString("plist.mankey.mising")
|
|
228 |
+ "{0}" + "{1}", arguments);
|
|
229 |
ValidatorMessage msg = createMessage(plist, message, 0, 0);
|
|
230 |
setMessageOnRootElement(msg, model);
|
|
231 |
result.add(msg);
|
|
232 |
} else /* validating Widget name (Display name) */
|
|
233 |
{
|
|
234 |
Matcher matcher = CoreUtil.getPropertyLookupPattern("DisplayName")
|
|
235 |
.matcher(source);
|
|
236 |
if (matcher.find()) {
|
|
237 |
String strError = Util.validateWidgetName(matcher.group(1));
|
|
238 |
if (strError != null) {
|
|
239 |
Object[] arguments = {
|
|
240 |
" 'Display Name' ",
|
|
241 |
ValidatorPropMessages
|
|
242 |
.getString("contains.invalid.character") };
|
|
243 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
244 |
.getString("plist.mankey.mising")
|
|
245 |
+ "{0}" + "{1}", arguments);
|
|
246 |
|
|
247 |
ValidatorMessage msg = createMessage(plist, message,
|
|
248 |
matcher.start(), matcher.end());
|
|
249 |
result.add(msg);
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
if (!hasAttributeValue(source, "MainHTML")) {
|
|
255 |
Object[] arguments = { " 'MainHTML ' ",
|
|
256 |
ValidatorPropMessages.getString("not.in.plist.file") };
|
|
257 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
258 |
.getString("plist.mankey.mising")
|
|
259 |
+ "{0}" + "{1}", arguments);
|
|
260 |
ValidatorMessage msg = createMessage(plist, message, 0, 0);
|
|
261 |
setMessageOnRootElement(msg, model);
|
|
262 |
result.add(msg);
|
|
263 |
}
|
|
264 |
if (!hasAttributeValue(source, "Identifier")) {
|
|
265 |
Object[] arguments = { " 'Identifier ' ",
|
|
266 |
ValidatorPropMessages.getString("not.in.plist.file") };
|
|
267 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
268 |
.getString("plist.mankey.mising")
|
|
269 |
+ "{0}" + "{1}", arguments);
|
|
270 |
ValidatorMessage msg = createMessage(plist, message, 0, 0);
|
|
271 |
setMessageOnRootElement(msg, model);
|
|
272 |
result.add(msg);
|
|
273 |
} else /* validating Widget Idenfier (UID) */
|
|
274 |
{
|
|
275 |
Matcher matcher = CoreUtil.getPropertyLookupPattern("Identifier")
|
|
276 |
.matcher(source);
|
|
277 |
if (matcher.find()) {
|
|
278 |
String strError = Util.validateWidgetID(matcher.group(1));
|
|
279 |
if (strError != null) {
|
|
280 |
Object[] arguments = {
|
|
281 |
" 'Identifier' ",
|
|
282 |
ValidatorPropMessages
|
|
283 |
.getString("contains.invalid.character") };
|
|
284 |
String message = MessageFormat.format(ValidatorPropMessages
|
|
285 |
.getString("plist.mankey.mising")
|
|
286 |
+ "{0}" + "{1}", arguments);
|
|
287 |
|
|
288 |
ValidatorMessage msg = createMessage(plist, message,
|
|
289 |
matcher.start(1), matcher.end(1));
|
|
290 |
result.add(msg);
|
|
291 |
}
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
private boolean hasAttributeValue(String source, String propertyName) {
|
|
297 |
Pattern pattern = CoreUtil.getPropertyLookupPattern(propertyName);
|
|
298 |
boolean hasAttr = true;
|
|
299 |
Matcher matcher = pattern.matcher(source);
|
|
300 |
if (!matcher.find()) {
|
|
301 |
hasAttr = false;
|
|
302 |
} else {
|
|
303 |
hasAttr = matcher.group(1).trim().length() > 0;
|
|
304 |
}
|
|
305 |
return hasAttr;
|
|
306 |
}
|
|
307 |
|
|
308 |
private void isPlistPresent(IProject project, ValidationResult result)
|
|
309 |
throws CoreException {
|
|
310 |
IResource[] members = project.members();
|
|
311 |
for (IResource resource : members) {
|
|
312 |
if (ProjectUtils.isPlist(resource)) {
|
|
313 |
return;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
ValidatorMessage msg = ValidatorMessage.create(ValidatorPropMessages
|
|
317 |
.getString("plist.File.Not.Present"), project);
|
|
318 |
msg.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
|
|
319 |
|
|
320 |
result.add(msg);
|
|
321 |
}
|
|
322 |
}
|