Skip to main content
summaryrefslogtreecommitdiffstats
blob: 660de042d82caaa7bcf868838b741725eb9076a7 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.cheatsheets.composite.parser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
import org.eclipse.ui.internal.cheatsheets.Messages;
import org.eclipse.ui.internal.cheatsheets.composite.model.AbstractTask;
import org.eclipse.ui.internal.cheatsheets.composite.model.CompositeCheatSheetModel;
import org.eclipse.ui.internal.cheatsheets.composite.model.EditableTask;
import org.eclipse.ui.internal.cheatsheets.composite.model.TaskGroup;
import org.eclipse.ui.internal.cheatsheets.data.CheatSheetParserException;
import org.eclipse.ui.internal.cheatsheets.data.IParserTags;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class CompositeCheatSheetParser implements IStatusContainer {
	
	private DocumentBuilder documentBuilder;
	
	private IStatus status;
	
	private int nextTaskId = 0;
	
	
	/**
	 *  Gets the status of the last call to parseGuide
	 */
	
	public IStatus getStatus() {
		return status;
	}
	
	/**
	 * Returns the DocumentBuilder to be used by composite cheat sheets.
	 */
	public DocumentBuilder getDocumentBuilder() {
		if(documentBuilder == null) {
			try {
				documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			} catch (Exception e) {
				addStatus(IStatus.ERROR, Messages.ERROR_CREATING_DOCUMENT_BUILDER, e);
			}
		}
		return documentBuilder;
	}
	
	private final int PARSER_ERROR = 1001; // TODO is there another number that would be more meaningful
	
	/**
	 * If the status is OK set it to reflect the new error condition, otherwise
	 * add to the existing status making it a MultiStatus if necessary.
	 */
	public void addStatus(int severity, String message, Throwable exception) {       
		Status newStatus = new Status(severity, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, PARSER_ERROR, message, exception);
		if (status.isOK()) {
			status = newStatus;
		} else if (status instanceof MultiStatus) {
			((MultiStatus)status).add(newStatus);
		} else {
			MultiStatus multiStatus = new MultiStatus(ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, 
					Messages.ERROR_MULTIPLE_ERRORS,  exception);
			multiStatus.add(status);
			multiStatus.add(newStatus);
			status = multiStatus;
		}
	}
	
	/**
	 * Parse a composite cheat sheet from a url. The parser status will be set as a result
	 * of this operation, if the status is IStatus.ERROR the parser returns null  
	 * @param url The url of the input
	 * @return A valid composite cheat sheet or null if there was an error
	 */
	public CompositeCheatSheetModel parseGuide(URL url) {
		status = Status.OK_STATUS;
		if(url == null) {
			String message = NLS.bind(Messages.ERROR_OPENING_FILE, (new Object[] {""})); //$NON-NLS-1$
			addStatus(IStatus.ERROR,  message,  null);
			return null;
		}

		InputStream is = null;	

		try {
			is = url.openStream();

			if (is == null) {
				String message = NLS.bind(Messages.ERROR_OPENING_FILE, (new Object[] {url.getFile()}));
				addStatus(IStatus.ERROR,  message,  null);
				return null;
			}
		} catch (Exception e) {
			String message = NLS.bind(Messages.ERROR_OPENING_FILE, (new Object[] {url.getFile()}));
			addStatus(IStatus.ERROR,  message,  e);
			return null;
		}
		
		Document document;	
		String filename = url.getFile();
		try {
			InputSource inputSource = new InputSource(is);
			document = getDocumentBuilder().parse(inputSource);
		} catch (IOException e) {
			String message = NLS.bind(Messages.ERROR_OPENING_FILE_IN_PARSER, (new Object[] {filename}));
			addStatus(IStatus.ERROR,  message,  e);
			return null;
		} catch (SAXParseException spe) {
			String message = NLS.bind(Messages.ERROR_SAX_PARSING_WITH_LOCATION, (new Object[] {filename, new Integer(spe.getLineNumber()), new Integer(spe.getColumnNumber())}));
			addStatus(IStatus.ERROR,  message,  spe);
			return null;
		} catch (SAXException se) {
			String message = NLS.bind(Messages.ERROR_SAX_PARSING, (new Object[] {filename}));
			addStatus(IStatus.ERROR,  message, se);
			return null;
		} finally {
			try {
				is.close();
			} catch (Exception e) {
			}
		}
		
		CompositeCheatSheetModel result = parseCompositeCheatSheet(document, url);
		return result;		
	}

	/**
	 * Parse a composite cheatsheet. The parser status will be set as a result
	 * of this operation, if the status is IStatus.ERROR the parser returns null  
	 * @param url The url of the input. This is only used so the model can record
	 * its input location
	 * @param document the document to be parse
	 * @return A valid composite cheat sheet or null if there was an error
	 * @return
	 */
	public CompositeCheatSheetModel parseCompositeCheatSheet(Document document, URL url)   {
		status = Status.OK_STATUS;
		try {
			// If the document passed is null return a null tree and update the status
			if (document != null) {
				Node rootnode = document.getDocumentElement();
				
				// Is the root node correct?
				if( !rootnode.getNodeName().equals(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET )) {
					String message = NLS.bind(Messages.ERROR_PARSING_ROOT_NODE_TYPE, (
							new Object[] {ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET}));
					throw new CheatSheetParserException(message);
				}
				
				String name = ""; //$NON-NLS-1$
				String explorerId = ICompositeCheatsheetTags.TREE;
				
				NamedNodeMap attributes = rootnode.getAttributes();
				if (attributes != null) {
					for (int x = 0; x < attributes.getLength(); x++) {
						Node attribute = attributes.item(x);
						String attributeName = attribute.getNodeName();
						if ( attributeName != null && attributeName.equals(ICompositeCheatsheetTags.NAME)) {
							name= attribute.getNodeValue();
						}
						if (attributeName.equals(ICompositeCheatsheetTags.EXPLORER)) {
							explorerId= attribute.getNodeValue();
						}
					}
				}
				CompositeCheatSheetModel compositeCS = new CompositeCheatSheetModel(name, name, explorerId);
				
				parseCompositeCheatSheetChildren(rootnode, compositeCS);
				
				compositeCS.getDependencies().resolveDependencies(this);
				
				if (compositeCS.getRootTask() == null) {
					addStatus(IStatus.ERROR, Messages.ERROR_PARSING_NO_ROOT, null);
				} else if (status.getSeverity() != IStatus.ERROR) {
					compositeCS.setContentUrl(url);
				    return compositeCS;
				}
			} 
			return null;
		} catch(CheatSheetParserException e) {
			addStatus(IStatus.ERROR, e.getMessage(), null);
			return null;
		}	
	}

	private void parseCompositeCheatSheetChildren(Node compositeCSNode, CompositeCheatSheetModel model) {
		nextTaskId = 0;
		NodeList childNodes = compositeCSNode.getChildNodes();
		for (int index = 0; index < childNodes.getLength(); index++) {
			Node nextNode = childNodes.item(index);
			if (isAbstractTask(nextNode.getNodeName()) ) {
				AbstractTask task = parseAbstractTask(nextNode, model);
			    if (model.getRootTask() == null ) { 
					model.setRootTask(task);
					parseTaskChildren(nextNode, task, model);
			    } else {
				    addStatus(IStatus.ERROR, Messages.ERROR_PARSING_MULTIPLE_ROOT, null);
			    }
			}
		}		
	}

	public static boolean isAbstractTask(String nodeName) {
		return nodeName == ICompositeCheatsheetTags.TASK ||
			nodeName == ICompositeCheatsheetTags.TASK_GROUP;
	}
	
	private void parseTaskChildren(Node parentNode, AbstractTask parentTask, CompositeCheatSheetModel model) {
		NodeList childNodes = parentNode.getChildNodes();
		ITaskParseStrategy strategy = parentTask.getParserStrategy();
		strategy.init();
		for (int index = 0; index < childNodes.getLength(); index++) {
			Node childNode = childNodes.item(index);
			if (childNode.getNodeType() == Node.ELEMENT_NODE) {
				String nodeName = childNode.getNodeName();
				if (nodeName == IParserTags.PARAM) {
					addParameter(parentTask, childNode.getAttributes());			
				} else if (nodeName == IParserTags.INTRO) {
					parentTask.setDescription(parseTextMarkup(childNode, parentTask, this));
				} else if (nodeName == ICompositeCheatsheetTags.ON_COMPLETION) {
					parentTask.setCompletionMessage(parseTextMarkup(childNode, parentTask, this));						
				} else if (nodeName == ICompositeCheatsheetTags.DEPENDS_ON) {
					parseDependency(childNode, parentTask, model);
				} else if (CompositeCheatSheetParser.isAbstractTask(nodeName)) {
					if (parentTask instanceof TaskGroup) {
					    AbstractTask task = parseAbstractTask(childNode, model);
					    ((TaskGroup)parentTask).addSubtask(task);
					    parseTaskChildren(childNode, task, model);
					}
			    } else {
					if (!strategy.parseElementNode(childNode, parentNode, parentTask, this)) {
						String message = NLS
						.bind(
								Messages.WARNING_PARSING_UNKNOWN_ELEMENT,
								(new Object[] { nodeName,
										parentNode.getNodeName() }));
				        addStatus(IStatus.WARNING, message, null);
					}
				}
			}
		}	
	}

	private void parseDependency(Node taskNode, AbstractTask task, CompositeCheatSheetModel model) {
		NamedNodeMap attributes = taskNode.getAttributes();
		if (attributes != null) {
		     Node taskAttribute = attributes.getNamedItem(ICompositeCheatsheetTags.TASK);
		     if (taskAttribute != null) {
		    	 String requiredTaskId = taskAttribute.getNodeValue();
		    	 model.getDependencies().addDependency(task, requiredTaskId);
		     } else {
		    	 addStatus(IStatus.ERROR, Messages.ERROR_PARSING_NO_ID, null);
		     }
		}	
	}

	private void addParameter(AbstractTask parentTask, NamedNodeMap attributes) {
		String name = null;
		String value = null;

		if (attributes != null) {
			for (int x = 0; x < attributes.getLength(); x++) {
				Node attribute = attributes.item(x);
				String attributeName = attribute.getNodeName();
				if (attribute == null || attributeName == null)
					continue;
				if (attributeName.equals(ICompositeCheatsheetTags.NAME)) {
					name = attribute.getNodeValue();
				}
				if (attributeName.equals(ICompositeCheatsheetTags.VALUE)) {
					value= attribute.getNodeValue();
				}	
			}
		}
		if (name == null) {
			addStatus(IStatus.WARNING, Messages.ERROR_PARSING_NO_NAME, null);
			return;
		} else if (value == null) {
			addStatus(IStatus.WARNING, Messages.ERROR_PARSING_NO_VALUE, null);
			return;
		} else {
			parentTask.getParameters().put(name, value);
		}
		
	}

	private AbstractTask parseAbstractTask(Node taskNode, CompositeCheatSheetModel model) {
		AbstractTask task;
		NamedNodeMap attributes = taskNode.getAttributes();
		String kind = null;
		String name = null;
		String id = null;
		boolean skippable = false;
		if (attributes != null) {
			for (int x = 0; x < attributes.getLength(); x++) {
				Node attribute = attributes.item(x);
				String attributeName = attribute.getNodeName();
				if (attribute == null || attributeName == null)
					continue;
				if (attributeName.equals(ICompositeCheatsheetTags.KIND)) {
					kind = attribute.getNodeValue();
				}
				if (attributeName.equals(ICompositeCheatsheetTags.NAME)) {
					name= attribute.getNodeValue();
				}
				if (attributeName.equals(IParserTags.ID)) {
					id = attribute.getNodeValue();
				}			
				if (attributeName.equals(IParserTags.SKIP)) {
					skippable = "true".equalsIgnoreCase(attribute.getNodeValue()); //$NON-NLS-1$
				}				
			}
		}

		if (id == null) {
			id = autoGenerateId();
		}
		String nodeName = taskNode.getNodeName();
		task = createTask(nodeName, model, kind, id, name);
		task.setSkippable(skippable);

		if (model.getDependencies().getTask(id) != null) {
			String message = NLS.bind(Messages.ERROR_PARSING_DUPLICATE_TASK_ID, (new Object[] {id}));
			addStatus(IStatus.ERROR, message, null);
		} else {
		    model.getDependencies().saveId(task);
		}

		return task;
	}

	private AbstractTask createTask(String nodeKind, CompositeCheatSheetModel model, String kind, String id, String name) {
		AbstractTask task;
		if (ICompositeCheatsheetTags.TASK_GROUP.equals(nodeKind)) {
			task = new TaskGroup(model, id, name, kind);
		} else {
			task = new EditableTask(model, id, name, kind);
		}
		String completionMessage = NLS.bind(Messages.COMPLETED_TASK, (new Object[] {name}));
		task.setCompletionMessage(completionMessage);
		return task;
	}
	
	private String autoGenerateId() {
		return "TaskId_" + nextTaskId++; //$NON-NLS-1$
	}

	public static String parseTextMarkup(Node descriptionNode, 
			                             AbstractTask parentTask,
			                             IStatusContainer status) {
	    NodeList nodes = descriptionNode.getChildNodes();
		StringBuffer text = new StringBuffer();
		for (int i = 0; i < nodes.getLength(); i++) {
			Node node = nodes.item(i);
			if (node.getNodeType() == Node.TEXT_NODE) {
				text.append(node.getNodeValue());
			} else if (node.getNodeType() == Node.ELEMENT_NODE) {
				// handle <b></b> and <br/>
				if (node.getNodeName().equals(IParserTags.BOLD)) {
					text.append(IParserTags.BOLD_START_TAG);
					text.append(node.getFirstChild().getNodeValue());
					text.append(IParserTags.BOLD_END_TAG);
				} else if (node.getNodeName().equals(IParserTags.BREAK)) {
					text.append(IParserTags.BREAK_TAG);
				} else {
					String message = NLS
							.bind(
									Messages.WARNING_PARSING_DESCRIPTION_UNKNOWN_ELEMENT,
									(new Object[] { parentTask.getName(),
											node.getNodeName() }));
					status.addStatus(IStatus.WARNING, message, null);
				}
			}
		}

		// Remove the new line, form feed and tab chars
		return text.toString().trim();
	}

}

Back to the top