Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9668a3e13b5bf91cf5340d5b5af3f61541792267 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.shared.ui;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.internal.xtend.xtend.parser.ErrorHandler;
import org.eclipse.internal.xtend.xtend.parser.XtendError;
import org.eclipse.xtend.expression.Resource;
import org.eclipse.xtend.shared.ui.core.IXtendXpandResource;
import org.eclipse.xtend.shared.ui.core.builder.XtendXpandMarkerManager;

/**
 * Base implementation of the {@link ResourceContributor} interface with common
 * used methods.
 * 
 * @author kthoms
 * @since 4.1.2
 */
public abstract class ResourceContributorBase implements ResourceContributor {

	protected ErrorHandler getErrorHandler(final IStorage source) {
		if (source instanceof IFile) {
			XtendXpandMarkerManager.deleteMarkers((IFile) source);
		}
		return new ErrorHandler() {
			private boolean hasErrors = false;
			public void handleError(XtendError e) {
				if (!hasErrors && e.getStart() > 0) {
					if (source instanceof IFile) {
						IFile f = (IFile) source;
						XtendXpandMarkerManager.addErrorMarker(f, e.getMessage(),
								IMarker.SEVERITY_ERROR, e.getStart(), e
										.getEnd());
						hasErrors=true;
					}
				}
			}
		};
	}

	protected abstract void logInfo(String message);

	protected abstract void logError(String message, Throwable t);

	/**
	 * @see ResourceContributor#parse(IStorage, IXtendXpandResource)
	 */
	public final IXtendXpandResource create(final IStorage file, String fqn) {
		Resource res = parse(file, fqn);

		if (res != null) {
			return createExtXptResource(res, file);
		}
		return null;
	}

	protected Reader createReader(IStorage resource) {
		InputStream in;
		try {
			in = resource.getContents();
			return new InputStreamReader(in);
		} catch (final CoreException e1) {
			logInfo(e1.getMessage());
			return null;
		}
	}

	/**
	 * Parse the resource file.
	 * 
	 * @param source
	 * @return The succesfully
	 */
	protected abstract Resource parse(IStorage source, String fqn);

	protected abstract IXtendXpandResource createExtXptResource(Resource resource,
			IStorage source);

}

Back to the top