URI uri = value.getURI();
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
if (!ctx.getResolverRegistry().supportsHost(uri)) {
throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the host field, use authority instead.", getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
}
uri = URIUtil.changeHost(uri, newStringValue);
authority = uri.getAuthority();
uriPartChanged = true;
}
else if (name.equals("path")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
path = newStringValue;
uriPartChanged = true;
}
else if (name.equals("file")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
int i = path.lastIndexOf("/");
if (i != -1) {
path = path.substring(0, i) + "/" + newStringValue;
}
else {
path = path + "/" + newStringValue;
}
uriPartChanged = true;
}
else if (name.equals("parent")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
int i = path.lastIndexOf("/");
String parent = newStringValue;
if (!parent.startsWith("/")) {
parent = "/" + parent;
}
if (i != -1) {
path =parent + path.substring(i);
}
else {
path = parent;
}
uriPartChanged = true;
}
else if (name.equals("ls")) {
throw new UnsupportedOperation("can not update the children of a location", ctx.getCurrentAST());
}
else if (name.equals("extension")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
String ext = newStringValue;
if (path.length() > 1) {
int index = path.lastIndexOf('.');
if (index == -1 && !ext.isEmpty()) {
path = path + (!ext.startsWith(".") ? "." : "") + ext;
}
else if (!ext.isEmpty()) {
path = path.substring(0, index) + (!ext.startsWith(".") ? "." : "") + ext;
}
else {
path = path.substring(0, index);
}
}
uriPartChanged = true;
}
else if (name.equals("top")) {
if (replType.isString()) {
URI uri = URIUtil.assumeCorrect(newStringValue);
scheme = uri.getScheme();
authority = uri.getAuthority();
path = uri.getPath();
query = uri.getQuery();
fragment = uri.getFragment();
}
else if (replType.isSourceLocation()) {
ISourceLocation rep = ((ISourceLocation) repl.getValue());
scheme = rep.getScheme();
authority = rep.hasAuthority() ? rep.getAuthority() : null;
path = rep.hasPath() ? rep.getPath() : null;
query = rep.hasQuery() ? rep.getQuery() : null;
fragment = rep.hasFragment() ? rep.getFragment() : null;
}
else {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
uriPartChanged = true;
}
else if (name.equals("fragment")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
fragment = newStringValue;
uriPartChanged = true;
}
else if (name.equals("query")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
query= newStringValue;
uriPartChanged = true;
}
else if (name.equals("user")) {
if (!replType.isString()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
URI uri = loc.getURI();
if (!ctx.getResolverRegistry().supportsHost(uri)) {
throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the user field, use authority instead.", getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
}
if (uri.getHost() != null) {
uri = URIUtil.changeUserInformation(uri, newStringValue);
}
authority = uri.getAuthority();
uriPartChanged = true;
}
else if (name.equals("port")) {
if (!replType.isInteger()) {
throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
}
URI uri = loc.getURI();
if (!ctx.getResolverRegistry().supportsHost(uri)) {
throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the port field, use authority instead.", getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
}
if (uri.getHost() != null) {
int port = Integer.parseInt(((IInteger) repl.getValue()).getStringRepresentation());
uri = URIUtil.changePort(uri, port);
}
authority = uri.getAuthority();
uriPartChanged = true;
}
else if (name.equals("length")){
if (!replType.isInteger()) {
throw new UnexpectedType(getTypeFactory().integerType(), replType, ctx.getCurrentAST());
}
iLength = ((IInteger) replValue).intValue();
if (iLength < 0) {
throw RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
}
}
else if (name.equals("offset")){
if (!replType.isInteger()) {
throw new UnexpectedType(getTypeFactory().integerType(), replType, ctx.getCurrentAST());
}
iOffset = ((IInteger) replValue).intValue();
if (iOffset < 0) {
RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
}
}
else if (name.equals("begin")) {
if (!replType.isSubtypeOf(intTuple)) {
throw new UnexpectedType(intTuple, replType, ctx.getCurrentAST());
}
iBeginLine = ((IInteger) ((ITuple) replValue).get(0)).intValue();
iBeginColumn = ((IInteger) ((ITuple) replValue).get(1)).intValue();
if (iBeginColumn < 0 || iBeginLine < 0) {
throw RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
}
}
else if (name.equals("end")) {
if (!replType.isSubtypeOf(intTuple)) {
throw new UnexpectedType(intTuple, replType, ctx.getCurrentAST());
}
iEndLine = ((IInteger) ((ITuple) replValue).get(0)).intValue();
iEndColumn = ((IInteger) ((ITuple) replValue).get(1)).intValue();
if (iEndLine < 0 || iEndColumn < 0) {
throw RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
}
}
else {
// TODO: is this the right exception? How so "undeclared"?
throw new UndeclaredField(name, getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
}
ISourceLocation newLoc = loc;
if (uriPartChanged) {
newLoc = getValueFactory().sourceLocation(scheme, authority, path, query, fragment);