use of com.dexels.navajo.document.types.Coordinate in project navajo by Dexels.
the class TestCoordinateProperty method testPropertyMethods.
@Test
public void testPropertyMethods() throws Exception {
Coordinate c = new Coordinate(-1, 3);
assertEquals(c.toString(), "[-1.0,3.0]");
assertEquals(c.isEmpty(), false);
c.setLatitude("32");
assertEquals(c.toString(), "[-1.0,32.0]");
c.setLongitude(-11.455555);
assertEquals(c.toString(), "[-11.455555,32.0]");
try {
c.setLatitude(null);
} catch (NumberFormatException e) {
assertTrue(true);
}
try {
c.setLatitude(-900);
fail();
} catch (Exception e) {
logger.info("Error", e);
}
}
use of com.dexels.navajo.document.types.Coordinate in project navajo by Dexels.
the class TestCoordinateProperty method testValidPropertyCreation.
@Test
public void testValidPropertyCreation() {
try {
Coordinate c = new Coordinate(-1, 3);
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate("-1", 3);
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate("-1", Integer.parseInt("3"));
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate(-1.2, 3.4);
assertEquals(c.toString(), "[-1.2,3.4]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate("-1,3");
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate("[-1,3]");
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate("[-1, 3]");
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
try {
Coordinate c = new Coordinate("-1 3");
assertEquals(c.toString(), "[-1.0,3.0]");
} catch (Exception e) {
assertTrue(false);
}
}
use of com.dexels.navajo.document.types.Coordinate in project navajo by Dexels.
the class BasePropertyImpl method getTypedValue.
/**
* Get the value of a property as a Java object.
*
* @return
*/
@Override
public final Object getTypedValue() {
if (getType().equals(Property.STRING_PROPERTY)) {
return getValue();
}
if (getType().equals(Property.BOOLEAN_PROPERTY)) {
if (getValue() != null && !getValue().equals("")) {
return Boolean.valueOf(getValue().equalsIgnoreCase("true"));
} else {
return null;
}
}
if (getType().equals(EXPRESSION_LITERAL_PROPERTY)) {
if (getValue() != null && !getValue().equals("")) {
return new NavajoExpression(getValue());
} else {
return null;
}
}
if (getType().equals(EXPRESSION_PROPERTY)) {
if (evaluatedValue == null) {
evaluatedValue = getEvaluatedValue();
return evaluatedValue;
} else {
return evaluatedValue;
}
}
if (getType().equals(Property.PERCENTAGE_PROPERTY)) {
if (getValue() != null) {
return new Percentage(getValue());
} else {
return null;
}
}
if (getType().equals(Property.MONEY_PROPERTY)) {
if (getValue() == null || "".equals(getValue())) {
return new Money((Double) null, getSubType());
}
String val = getValue();
NumberFormat fn = NumberFormat.getNumberInstance(Locale.US);
Number parse;
try {
parse = fn.parse(val);
} catch (ParseException e) {
return null;
}
return new Money(parse.doubleValue(), getSubType());
} else if (getType().equals(Property.CLOCKTIME_PROPERTY)) {
if (getValue() == null || getValue().equals("")) {
return null;
}
try {
return new ClockTime(getValue(), getSubType());
} catch (Exception e) {
logger.error("Error: ", e);
}
} else if (getType().equals(Property.STOPWATCHTIME_PROPERTY)) {
try {
return new StopwatchTime(getValue(), getSubType());
} catch (Exception e) {
logger.error("Error: ", e);
}
} else if (getType().equals(Property.DATE_PROPERTY) || getType().equals(Property.TIMESTAMP_PROPERTY)) {
if (getValue() == null || getValue().equals("") || getValue().equals("null")) {
return null;
}
if (myDate != null) {
return myDate;
}
// Try in order from most specific to least specific
try {
return timestampFormat.get().parse(getValue());
} catch (Exception ex) {
try {
return dateFormat4.get().parse(getValue());
} catch (Exception ex2) {
try {
return dateFormat1.get().parse(getValue());
} catch (Exception ex3) {
try {
return dateFormat2.get().parse(getValue());
} catch (Exception ex4) {
try {
Long l = Long.parseLong(getValue());
Date d = new java.util.Date();
d.setTime(l);
return d;
} catch (Exception e5) {
logger.info("Sorry I really can't parse that date: {}", getValue());
return null;
}
}
}
}
}
} else if (getType().equals(Property.INTEGER_PROPERTY)) {
if (getValue() == null || getValue().equals("") || getValue().trim().equals("null")) {
return null;
}
try {
return Integer.valueOf(Integer.parseInt(getValue().trim()));
} catch (NumberFormatException ex3) {
logger.info("Numberformat exception...: {}", getValue().trim());
return null;
}
} else if (getType().equals(Property.LONG_PROPERTY)) {
if (getValue() == null || getValue().equals("")) {
return null;
}
try {
// Added a trim. Frank.
return Long.valueOf(Long.parseLong(getValue().trim()));
} catch (NumberFormatException ex3) {
logger.info("Numberformat exception...");
return null;
}
} else if (getType().equals(Property.FLOAT_PROPERTY)) {
if (getValue() == null || getValue().equals("")) {
return null;
}
String v = getValue();
String w = v;
// Sometimes the number formatting creates
if (v.indexOf(',') != -1) {
w = v.replaceAll(",", "");
}
Double d;
try {
d = Double.valueOf(Double.parseDouble(w));
} catch (NumberFormatException ex) {
logger.info("Can not format double with: {}", w);
return null;
}
return d;
} else if (getType().equals(Property.BINARY_PROPERTY)) {
try {
return myBinary;
} catch (Exception e) {
logger.error("Error: ", e);
}
} else if (getType().equals(Property.SELECTION_PROPERTY)) {
return getAllSelectedSelections();
} else if (getType().equals(Property.TIPI_PROPERTY)) {
return tipiProperty;
} else if (getType().equals(Property.LIST_PROPERTY)) {
if (tipiProperty != null || myValue == null) {
return tipiProperty;
}
try {
if (myValue.indexOf('[') == 0) {
// Parse back into a list
String stripped = myValue.substring(1, myValue.length() - 1);
tipiProperty = Arrays.asList(stripped.split(", "));
return tipiProperty;
} else if (myValue.length() > 0) {
logger.info("Failed to parse {} as a list!", myValue);
}
} catch (Exception e) {
logger.warn("Exception on parsing {} as a list!", myValue, e);
}
return null;
} else if (getType().equals(Property.BINARY_DIGEST_PROPERTY)) {
return new BinaryDigest(getValue());
} else if (getType().equals(Property.COORDINATE_PROPERTY)) {
try {
return new Coordinate(myValue);
} catch (Exception e) {
logger.error("Cannot create Coordinate Property: ", e);
}
}
return getValue();
}
use of com.dexels.navajo.document.types.Coordinate in project navajo by Dexels.
the class TestTMLJson method testCoordinate.
@Test
public void testCoordinate() throws Exception {
Navajo n = NavajoFactory.getInstance().createNavajo(getClass().getResourceAsStream("message2.xml"));
JSONTML json = JSONTMLFactory.getInstance();
Property prop = NavajoFactory.getInstance().createProperty(n, "coordinateProp", "", "", "");
prop.setAnyValue(new Coordinate(-12.65342223, 13.12323425));
n.getMessage("SimpleMessage").addProperty(prop);
Writer sw = new StringWriter();
json.format(n, sw, true);
String result = sw.toString();
Assert.assertEquals("{\n \"coordinateProp\" : \"[-12.65342223,13.12323425]\"\n}", result);
}
use of com.dexels.navajo.document.types.Coordinate in project navajo by Dexels.
the class ToCoordinate method evaluate.
@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
Object o = getOperand(0);
Object o1 = getOperand(1);
Coordinate cor = null;
try {
if (o != null && o1 != null) {
cor = new Coordinate(o, o1);
} else {
throw new TMLExpressionException("Error creating coordinate. No null arguments are allowed");
}
// verify coordinates
if (cor.getLongitude() < -180 || cor.getLongitude() > 180) {
throw new TMLExpressionException("Wrong Longitude. Longitude must be in [-180,180]");
}
if (cor.getLatitude() < -90 || cor.getLatitude() > 90) {
throw new TMLExpressionException("Wrong Latitute. Latitude must be in [-90,90]");
}
return cor;
} catch (Exception e) {
e.printStackTrace();
throw new TMLExpressionException(e.getMessage(), e);
}
}
Aggregations