2012-05-08 18:03:43 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2007 IBM Corporation.
|
|
|
|
* 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
|
|
|
|
*******************************************************************************/
|
2015-10-14 17:32:34 +00:00
|
|
|
package com.ibm.wala.cast.tree.impl;
|
2012-05-08 18:03:43 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-04-29 15:21:01 +00:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
2012-05-08 18:03:43 +00:00
|
|
|
import java.net.URL;
|
2015-10-06 20:44:16 +00:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
2012-05-08 18:03:43 +00:00
|
|
|
import com.ibm.wala.cast.tree.CAstSourcePositionMap.Position;
|
|
|
|
|
2017-03-18 03:13:02 +00:00
|
|
|
public class RangePosition extends AbstractSourcePosition {
|
2015-10-06 20:44:16 +00:00
|
|
|
private final static int TABSIZE = 4;
|
2012-05-08 18:03:43 +00:00
|
|
|
private final URL url;
|
2015-12-09 22:28:45 +00:00
|
|
|
private final int startLine;
|
|
|
|
private final int endLine;
|
2012-05-08 18:03:43 +00:00
|
|
|
private final int startOffset;
|
|
|
|
private final int endOffset;
|
2015-10-06 20:44:16 +00:00
|
|
|
|
|
|
|
private int col = -1;
|
|
|
|
private int lastCol = -1;
|
|
|
|
|
2015-12-09 22:28:45 +00:00
|
|
|
public RangePosition(URL url, int startLine, int endLine, int startOffset, int endOffset) {
|
2012-05-08 18:03:43 +00:00
|
|
|
super();
|
|
|
|
this.url = url;
|
2015-12-09 22:28:45 +00:00
|
|
|
this.startLine = startLine;
|
|
|
|
this.endLine = endLine;
|
2012-05-08 18:03:43 +00:00
|
|
|
this.startOffset = startOffset;
|
|
|
|
this.endOffset = endOffset;
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:28:45 +00:00
|
|
|
public RangePosition(URL url, int line, int startOffset, int endOffset) {
|
|
|
|
this(url, line, -1, startOffset, endOffset);
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int compareTo(Object o) {
|
|
|
|
Position other = (Position) o;
|
|
|
|
if (startOffset != other.getFirstOffset()) {
|
|
|
|
return startOffset - other.getFirstOffset();
|
|
|
|
} else {
|
|
|
|
return endOffset - other.getLastOffset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int getFirstLine() {
|
2015-12-09 22:28:45 +00:00
|
|
|
return startLine;
|
2012-05-08 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int getLastLine() {
|
2016-08-07 10:58:43 +00:00
|
|
|
return endLine;
|
2012-05-08 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int getFirstCol() {
|
2015-10-06 20:44:16 +00:00
|
|
|
if (col == -1) {
|
|
|
|
col = getCol(getFirstLine(), startOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return col;
|
2012-05-08 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int getLastCol() {
|
2015-10-06 20:44:16 +00:00
|
|
|
if (lastCol == -1) {
|
|
|
|
lastCol = getCol(getLastLine(), endOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastCol;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getCol(int line, int offset) {
|
|
|
|
int col = -1;
|
2018-10-21 19:20:13 +00:00
|
|
|
try (Reader reader = getReader()){
|
2015-10-06 20:44:16 +00:00
|
|
|
col = getCol(IOUtils.toString(reader), line, offset);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getCol(String content, int line, int offset) {
|
|
|
|
if (line == 1) {
|
|
|
|
return offset + 1;
|
|
|
|
}
|
|
|
|
int pos = -1;
|
|
|
|
for (int i = 0; i < line - 1; i++) {
|
|
|
|
pos = content.indexOf('\n', pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
String lineBeginning = content.substring(pos, offset);
|
|
|
|
int nrOfTabs = lineBeginning.length() - lineBeginning.replace("\t", "").length();
|
|
|
|
return offset - pos + (TABSIZE - 1) * nrOfTabs;
|
|
|
|
} catch (StringIndexOutOfBoundsException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return -1;
|
|
|
|
}
|
2012-05-08 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int getFirstOffset() {
|
|
|
|
return startOffset;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public int getLastOffset() {
|
|
|
|
return endOffset;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-05-08 18:03:43 +00:00
|
|
|
public URL getURL() {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2014-04-29 15:21:01 +00:00
|
|
|
public Reader getReader() throws IOException {
|
|
|
|
return new InputStreamReader(url.openStream());
|
2012-05-08 18:03:43 +00:00
|
|
|
}
|
|
|
|
}
|