Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8baddad9371b4fbcc474a76d265513e768de14fd (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
/******************************************************************************
 * Copyright (c) 2006, 2020 Borland Software Corporation, CEA LIST, Artal
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/ 
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors: 
 *    Artem Tikhomirov (Borland) - initial API and implementation
 *     Aurélien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
 *******************************************************************************/
package org.eclipse.papyrus.gmf.internal.xpand.util;

import java.util.Collection;

public class ParserException extends Exception {
	private static final long serialVersionUID = 1L;

	private final ErrorLocationInfo[] errors;
	private final String qualifiedResourceName;

	public ParserException(String qualifiedName, Collection<? extends ErrorLocationInfo> errors) {
		this(qualifiedName, errors.toArray(new ErrorLocationInfo[errors.size()]));
	}

	public ParserException(String qualifiedName, ErrorLocationInfo... errors) {
		assert errors != null && errors.length > 0;
		this.errors = errors;
		this.qualifiedResourceName = qualifiedName;
	}

	public ErrorLocationInfo[] getParsingErrors() {
		return errors;
	}
	
	public String getResourceName() {
		return qualifiedResourceName;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(getClass().getName());
		sb.append(", @");
		sb.append(getResourceName());
		for (ErrorLocationInfo l : getParsingErrors()) {
			sb.append('\n');
			sb.append('\t');
			if (l.startLine == -1 || l.startOffset == -1) {
				sb.append("[unspecified location]");
			} else {
				sb.append('[');
				if (l.startLine != -1 && l.endLine != -1) {
					sb.append(l.startLine);
					sb.append(':');
					sb.append(l.startColumn);
					sb.append('-');
					sb.append(l.endLine);
					sb.append(':');
					sb.append(l.endColumn);
				} else {
					sb.append(l.startOffset);
					sb.append('-');
					sb.append(l.endOffset);
				}
				sb.append(']');
			}
			sb.append(' ');
			sb.append(l.message);
		}
		return sb.toString();
	}

	public static class ErrorLocationInfo {
		public final int startLine;
		public final int startColumn;
		public final int endLine;
		public final int endColumn;
		public final String message;
		public final int startOffset;
		public final int endOffset;

		public ErrorLocationInfo(String message) {
			this(message, -1, -1, -1, -1, -1, -1);
		}

		public ErrorLocationInfo(String message, int startLine, int startColumn, int endLine, int endColumn) {
			this(message, startLine, startColumn, endLine, endColumn, -1, -1);
		}

		public ErrorLocationInfo(String message, int startLine, int startColumn, int endLine, int endColumn, int startOffset, int endOffset) {
			this.message = message;
			this.startLine = startLine;
			this.startColumn = startColumn;
			this.endLine = endLine;
			this.endColumn = endColumn;
			this.startOffset = startOffset;
			this.endOffset = endOffset;
		}
	}
}

Back to the top