Rev 2347 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* Copyright (C) 2008-2012 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* Author: Steve Ratcliffe
* Create date: 03-Nov-2008
*/
package uk.me.parabola.mkgmap.osmstyle.eval;
import uk.me.parabola.log.Logger;
import uk.me.parabola.mkgmap.osmstyle.function.FunctionFactory;
import uk.me.parabola.mkgmap.osmstyle.function.StyleFunction;
import uk.me.parabola.mkgmap.reader.osm.Element;
import uk.me.parabola.mkgmap.scan.SyntaxException;
/**
* A base class that can be used as the superclass of an operation.
*
* @author Steve Ratcliffe
*/
public abstract class AbstractOp
implements Op
{
private static final Logger log =
Logger.
getLogger(AbstractOp.
class);
protected Op first
;
private char type
;
public static Op createOp
(String value
) {
char c = value.
charAt(0);
Op op
;
switch (c
) {
case EQUALS: op =
new EqualsOp
(); break;
case AND:
if (value.
length() > 1)
throw new SyntaxException
(String.
format("Use '&' instead of '%s'", value
));
op =
new AndOp
();
break;
case OR:
if (value.
length() > 1)
throw new SyntaxException
(String.
format("Use '|' instead of '%s'", value
));
op =
new OrOp
();
break;
case REGEX: op =
new RegexOp
(); break;
case OPEN_PAREN: op =
new OpenOp
(); break;
case CLOSE_PAREN: op =
new CloseOp
(); break;
case '>':
if (value.
equals(">="))
op =
new GTEOp
();
else
op =
new GTOp
();
break;
case '<':
if (value.
equals("<="))
op =
new LTEOp
();
else
op =
new LTOp
();
break;
case '!':
if (value.
equals("!="))
op =
new NotEqualOp
();
else
op =
new NotOp
();
break;
default:
throw new SyntaxException
("Unrecognised operation " + c
);
}
return op
;
}
protected String getTagValue
(Element el,
String key
) {
if (key.
endsWith("()")) {
String functionName = key.
substring(0, key.
length()-
2);
StyleFunction function = FunctionFactory.
getCachedFunction(functionName
);
if (function==
null) {
log.
error("Unknown style function "+ functionName
);
return null;
}
return function.
calcValue(el
);
} else {
return el.
getTag(key
);
}
}
/**
* Does this operation have a higher priority that the other one?
* @param other The other operation.
*/
public boolean hasHigherPriority
(Op other
) {
return priority
() > other.
priority();
}
public Op getFirst
() {
return first
;
}
public void setFirst
(Op first
) {
this.
first = first
;
}
public char getType
() {
return type
;
}
public String getTypeString
() {
return String.
valueOf(getType
());
}
void setType
(char type
) {
this.
type = type
;
}
public String value
() {
return first.
toString();
}
public boolean isType
(char value
) {
return type == value
;
}
}