| /******************************************************************************* |
| * 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 a generic Location used for providing error information |
| * in the parser and validator. |
| * |
| * Description: |
| * Location class stores beginLine, beginColumn, endLine, endColumn as integer values, and optionally stores offset information as well. |
| * |
| * @author Daniel Varro |
| * @version 1.0 |
| * |
| */ |
| |
| public class Location { |
| private int beginLine = 0; |
| private int endLine = 0; |
| private int beginColumn = 0; |
| private int endColumn = 0; |
| private int beginOffset = -1; |
| private int endOffset = -1; |
| |
| /** |
| * Creates a new Location with the given line and column parameters |
| * @param bl : begin line |
| * @param bc : begin column |
| * @param el : end line |
| * @param ec : end column |
| */ |
| public Location(int bl, int bc, int el, int ec) |
| { |
| beginLine = bl; |
| endLine = el; |
| beginColumn = bc; |
| endColumn = ec; |
| } |
| |
| public Location(int bl, int bc, int el, int ec, int bo, int eo) { |
| this(bl, bc, el, ec); |
| beginOffset = bo; |
| endOffset = eo; |
| } |
| |
| /** |
| * Creates a new Location from a string representation where |
| * locations are separated by commas (',') |
| * @param locationStr : begin line ',' begin column ',' end line ',' end column |
| */ |
| public Location(String locationStr) { |
| String[] locations = locationStr.split(","); |
| if (locations.length == 4) { |
| beginLine = parseInt(locations[0]); |
| beginColumn = parseInt(locations[1]); |
| endLine = parseInt(locations[2]); |
| endColumn = parseInt(locations[3]); |
| } |
| } |
| |
| /** |
| * Creates a new Location from two strings representing the location by line numbers and offset values |
| * @param locationStr begin line ',' begin column ',' end line ',' end column |
| * @param offsetStr begin offset ':' end offset |
| */ |
| public Location(String locationStr, String offsetStr) { |
| this(locationStr); |
| String[] offsets = offsetStr.split(":"); |
| if (offsets.length == 2) { |
| beginOffset = parseInt(offsets[0]); |
| endOffset = parseInt(offsets[1]); |
| } |
| } |
| private static int parseInt(String s) { |
| int i = -1; |
| try { |
| i = Integer.parseInt(s); |
| } |
| catch (Exception e) { } |
| return i; |
| } |
| |
| /** |
| * Converts the location to a string representation where |
| * locations are separated by commas (','): |
| * begin line ',' begin column ',' end line ',' end column |
| */ |
| public String toString() { |
| return new String ( |
| String.valueOf(beginLine) + "," + |
| String.valueOf(beginColumn) + "," + |
| String.valueOf(endLine) + "," + |
| String.valueOf(endColumn)); |
| } |
| |
| /** |
| * Returns the begin line value of the Location |
| * @return begin line |
| */ |
| public int getBeginLine() |
| { |
| return beginLine; |
| } |
| |
| /** |
| * Returns the end line value of the Location |
| * @return end line |
| */ |
| public int getEndLine() |
| { |
| return endLine; |
| } |
| |
| /** |
| * Returns the begin column value of the Location |
| * @return begin column |
| */ |
| public int getBeginColumn() |
| { |
| return beginColumn; |
| } |
| |
| /** |
| * Returns the end column value of the Location |
| * @return end column |
| */ |
| public int getEndColumn() |
| { |
| return endColumn; |
| } |
| |
| /** |
| * @return the beginOffset |
| */ |
| public int getBeginOffset() { |
| return beginOffset; |
| } |
| |
| /** |
| * @return the endOffset |
| */ |
| public int getEndOffset() { |
| return endOffset; |
| } |
| |
| } |