blob: e3cc641af3648c9266cd884baa8dc7a02481bdbc [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010-2011, Istvan Rath and Daniel Varro
* 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:
* Daniel Varro - initial API and implementation
*******************************************************************************/
/**
* Copyright (c) 2007 OptXware Research and Development LLC.
* 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:
* OptXware - Initial API and implementation
*
* Created:
* 2007.08.26 Daniel Varro
*/
package org.eclipse.viatra2.errors.info;
/**
* Summary:
* This class provides generic handling for error information
* in the parser and validator modules.
*
* Description:
* ErrorInformation stores the {@link Location} of the error as beginLine, beginColumn,
* endLine, endColumn. ErrorSeverity can be an info, a warning or an error.
* ErrorKind shows if the error originates from the parser or the validator.
* Finally, the textual representation of the error message is also stored.
*
* @author Daniel Varro
*
*/
public class ErrorInformation
{
public enum ErrorKind {PARSE_ERROR, VALIDATION_ERROR};
public enum ErrorSeverity {INFO, WARNING , ERROR};
private String message;
private Location location;
private ErrorSeverity errorSeverity;
private ErrorKind errorKind;
private static final String defaultMarkerId = "org.eclipse.viatra2.loaders.vtclparsermarker";
private String markerId = defaultMarkerId;
/**
* Creates a new ErrorInformation with default location (0,0,0,0) with severity "error"
* @param m : Text of the error message
* @param errorKind : source of the error information (parser / validator)
* @deprecated Use instead {@link ErrorInformation#ErrorInformation(String, String, ErrorKind)}
*/
public ErrorInformation(String m, ErrorKind errorKind)
{
this(m, defaultMarkerId, errorKind, new Location(0,0,0,0), ErrorSeverity.ERROR);
}
/**
* Creates a new ErrorInformation with default location (0,0,0,0) with severity "error"
* @param m : Text of the error message
* @param markerId : the identifier of the marker to use
* @param errorKind : source of the error information (parser / validator)
*/
public ErrorInformation(String m, String markerId, ErrorKind errorKind){
this(m, markerId, new Location(0,0,0,0), ErrorSeverity.ERROR);
}
/**
* Creates a new ErrorInformation at a given location with severity "error" using
* the given error message and error kind
*
* @param m : Text of the error message
* @param errorKind : source of the error information (parser / validator)
* @param bl : begin line of the location
* @param el : end line of the location
* @param bc : begin column of the location
* @param ec : end column of the location
* @deprecated
*/
public ErrorInformation(String m, ErrorKind errorKind, int bl,int el,int bc,int ec)
{
this(m, defaultMarkerId, errorKind, new Location(bl, el, bc, ec), ErrorSeverity.ERROR);
}
/**
* Creates a new ErrorInformation at a given location with severity "error" using
* the given error message and error kind
*
* @param m : Text of the error message
* @param markerId : the markerId to use
* @param errorKind : source of the error information (parser / validator)
* @param bl : begin line of the location
* @param el : end line of the location
* @param bc : begin column of the location
* @param ec : end column of the location
*/
public ErrorInformation(String m, String markerId, ErrorKind errorKind, int bl,int el,int bc,int ec)
{
this(m, markerId, errorKind, new Location(bl, el, bc, ec), ErrorSeverity.ERROR);
}
/**
* Creates a new ErrorInformation at a given location with severity "error" using
* the given error message and error kind
*
* @param m : Text of the error message
* @param errorKind : source of the error information (parser / validator)
* @param loc : location of the error
* @deprecated Use instead {@link ErrorInformation#ErrorInformation(String, String, ErrorKind, Location, ErrorSeverity)}
*/
public ErrorInformation(String m, ErrorKind errorKind, Location loc)
{
this(m, defaultMarkerId, errorKind, loc, ErrorSeverity.ERROR);
}
/**
* Creates a new ErrorInformation at a given location with a given severity using
* the given error message and error kind
*
* @param m : Text of the error message
* @param errorKind : source of the error information (parser / validator)
* @param loc : location of the error
* @param level : severity of the error (INFO / WARNING / ERROR)
* @deprecated Use instead {@link ErrorInformation#ErrorInformation(String, String, ErrorKind, Location, ErrorSeverity)}
*/
public ErrorInformation(String m, ErrorKind errorKind, Location loc, ErrorSeverity level)
{
this(m, defaultMarkerId, errorKind, loc, level);
}
/**
* Creates a new ErrorInformation at a given location with a given severity using
* the given error message and error kind
*
* @param m : Text of the error message
* @param markerId : the markerId to use
* @param errorKind : source of the error information (parser / validator)
* @param loc : location of the error
* @param level : severity of the error (INFO / WARNING / ERROR)
*/
public ErrorInformation(String m, String markerId, ErrorKind errorKind, Location loc, ErrorSeverity level)
{
this.message = m;
this.location = loc;
this.errorSeverity = level;
this.errorKind = errorKind;
}
/**
* Creates a new ErrorInformation at a given location with a given severity using
* the given error message and error kind
*
* @param m : Text of the error message
* @param markerId : marker Id for error information
* @param loc : location of the error
* @param level : severity of the error (INFO / WARNING / ERROR)
*/
public ErrorInformation(String m, String markerId, Location loc, ErrorSeverity level)
{
this.message = m;
this.location = loc;
this.errorSeverity = level;
this.errorKind = ErrorKind.PARSE_ERROR;
this.markerId = markerId;
}
/**
* Returns the error kind of the error information
* @return : parser or validator
*/
public ErrorKind getErrorKind() {
return errorKind;
}
/**
* Returns the message of the error information (without textual location information)
* @return : textual error message (no location info)
*/
public String getMessage()
{
return message;
}
/**
* Returns the message of the error information (with textual location information)
* @return : textual error message (with location info)
*/
public String getMessageWithLocation() {
String locStr = new String (
String.valueOf(location.getBeginLine()) + ":" +
String.valueOf(location.getBeginColumn()) + ":" +
String.valueOf(location.getEndLine()) + ":" +
String.valueOf(location.getEndColumn()));
return new String(locStr + " : " + message);
}
/**
* Returns the severity field of the error information
* @return INFO / WARNING / ERROR
*/
public ErrorSeverity getErrorSeverity() {
return errorSeverity;
}
/**
* Returns the location of the error information
* @return {@link Location} of the error
*/
public Location getLocation() {
return location;
}
/**
* Returns the markerId string of the error information
* @return {@link String} of the error
*/
public String getMarkerId() {
return markerId;
}
/**
* Binding the '{N}' (N = 1..5 or ALL) strings to contextual conditions in 'context'
* @param context : array of context-sensitive Strings
*/
public void bind(String[] context) {
if (context.length >= 1) {
message = message.replace("{1}", context[0]);
}
if (context.length >= 2) {
message = message.replace("{2}", context[1]);
}
if (context.length >= 3) {
message = message.replace("{3}", context[2]);
}
if (context.length >= 4) {
message = message.replace("{4}", context[3]);
}
if (context.length >= 5) {
message = message.replace("{5}", context[4]);
}
if (message.contains("{ALL}")) {
String allContext = new String();
for (int i = 0; i < context.length; i++) {
allContext += (context[i] + (i < context.length - 1 ? ", " : ""));
}
message = message.replace("{ALL}", allContext);
}
}
/**
* Weak comparison function which decides whether two
* ErrorInformation DTO instances can be considered equivalent.
*
* Criterions:
* - equivalent {@link ErrorKind} fields
* - equivalent {@link ErrorSeverity} fields
* - same startline for {@Location} fields
* - message fields are substrings (or-or)
* @author Istvan Rath
* @param ei
* @return true if the information are considered the same
*
*/
public boolean isEquivalent(ErrorInformation ei) {
boolean crit1 = this.errorKind.equals(ei.errorKind);
boolean crit2 = this.errorSeverity.equals(ei.errorSeverity);
boolean crit3 = this.location.getBeginLine() ==
ei.location.getBeginLine();
boolean crit4 = this.message.toLowerCase().indexOf(ei.message.toLowerCase()) > -1;
boolean crit5 = ei.message.toLowerCase().indexOf(this.message.toLowerCase()) > -1;
return (crit1 && crit2 && crit3 && (crit4 || crit5));
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append('[');
builder.append(errorSeverity);
builder.append("] ");
builder.append(message);
builder.append(" (");
builder.append(location);
builder.append(", ");
builder.append(errorKind);
builder.append(")");
return builder.toString();
}
}