2007-02-02 17:17:13 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 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
|
|
|
|
*****************************************************************************/
|
|
|
|
package com.ibm.wala.cast.tree.impl;
|
|
|
|
|
2007-02-08 20:45:07 +00:00
|
|
|
import java.io.IOException;
|
2014-04-29 15:21:01 +00:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
2007-02-08 20:45:07 +00:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2007-02-08 20:45:07 +00:00
|
|
|
import com.ibm.wala.cast.tree.CAstNode;
|
|
|
|
import com.ibm.wala.cast.tree.CAstSourcePositionMap;
|
2007-07-11 21:07:32 +00:00
|
|
|
import com.ibm.wala.util.collections.HashMapFactory;
|
2007-02-02 17:17:13 +00:00
|
|
|
|
|
|
|
public class CAstSourcePositionRecorder implements CAstSourcePositionMap {
|
|
|
|
|
2007-07-11 21:07:32 +00:00
|
|
|
private final HashMap<CAstNode, Position> positions = HashMapFactory.make();
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:17:13 +00:00
|
|
|
public Position getPosition(CAstNode n) {
|
2007-02-08 20:45:07 +00:00
|
|
|
return positions.get(n);
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-08 20:45:07 +00:00
|
|
|
public Iterator<CAstNode> getMappedNodes() {
|
2007-02-02 17:17:13 +00:00
|
|
|
return positions.keySet().iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(CAstNode n, Position p) {
|
|
|
|
positions.put(n, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(CAstNode n,
|
|
|
|
final int fl,
|
|
|
|
final int fc,
|
|
|
|
final int ll,
|
|
|
|
final int lc,
|
|
|
|
final String url,
|
|
|
|
final String file)
|
|
|
|
throws MalformedURLException
|
|
|
|
{
|
|
|
|
setPosition(n, fl, fc, ll, lc, new URL(url), new URL(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(CAstNode n,
|
|
|
|
final int fl,
|
|
|
|
final int fc,
|
|
|
|
final int ll,
|
|
|
|
final int lc,
|
|
|
|
final URL url,
|
|
|
|
final URL file)
|
|
|
|
{
|
|
|
|
setPosition(n,
|
|
|
|
new AbstractSourcePosition() {
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
|
|
|
public int getFirstLine() { return fl; }
|
|
|
|
@Override
|
|
|
|
public int getLastLine() { return ll; }
|
|
|
|
@Override
|
|
|
|
public int getFirstCol() { return fc; }
|
|
|
|
@Override
|
|
|
|
public int getLastCol() { return lc; }
|
|
|
|
@Override
|
|
|
|
public int getFirstOffset() { return -1; }
|
|
|
|
@Override
|
|
|
|
public int getLastOffset() { return -1; }
|
|
|
|
@Override
|
|
|
|
public URL getURL() { return url; }
|
|
|
|
@Override
|
2014-04-29 15:21:01 +00:00
|
|
|
public Reader getReader() throws IOException {
|
|
|
|
return new InputStreamReader(file.openConnection().getInputStream());
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2007-02-02 17:17:13 +00:00
|
|
|
return "["+fl+":"+fc+"]->["+ll+":"+lc+"]";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(CAstNode n, int lineNumber, String url, String file)
|
|
|
|
throws MalformedURLException
|
|
|
|
{
|
|
|
|
setPosition(n, lineNumber, new URL(url), new URL(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(CAstNode n, int lineNumber, URL url, URL file) {
|
|
|
|
setPosition(n, new LineNumberPosition(url, file, lineNumber));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addAll(CAstSourcePositionMap other) {
|
2007-02-08 20:45:07 +00:00
|
|
|
for(Iterator<CAstNode> nodes = other.getMappedNodes(); nodes.hasNext(); ) {
|
|
|
|
CAstNode node = nodes.next();
|
2007-02-02 17:17:13 +00:00
|
|
|
setPosition(node, other.getPosition(node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|