Search in sources :

Example 1 with BinaryDigest

use of com.dexels.navajo.document.types.BinaryDigest in project navajo by Dexels.

the class BinaryStoreValue method evaluate.

@Override
public Object evaluate() {
    String tenant = super.getInstance();
    String resourceName = getStringOperand(0);
    BinaryDigest binaryDigest = getBinaryDigestOperand(1);
    BinaryStore os = BinaryStoreFactory.getInstance().getBinaryStore(resourceName, tenant);
    return os.resolve(binaryDigest);
}
Also used : BinaryDigest(com.dexels.navajo.document.types.BinaryDigest) BinaryStore(com.dexels.navajo.resource.binarystore.BinaryStore)

Example 2 with BinaryDigest

use of com.dexels.navajo.document.types.BinaryDigest 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();
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Percentage(com.dexels.navajo.document.types.Percentage) Date(java.util.Date) ClockTime(com.dexels.navajo.document.types.ClockTime) PropertyTypeException(com.dexels.navajo.document.PropertyTypeException) NavajoException(com.dexels.navajo.document.NavajoException) ParseException(java.text.ParseException) ExpressionChangedException(com.dexels.navajo.document.ExpressionChangedException) IOException(java.io.IOException) Date(java.util.Date) StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) Money(com.dexels.navajo.document.types.Money) BinaryDigest(com.dexels.navajo.document.types.BinaryDigest) Coordinate(com.dexels.navajo.document.types.Coordinate) ParseException(java.text.ParseException) NumberFormat(java.text.NumberFormat)

Example 3 with BinaryDigest

use of com.dexels.navajo.document.types.BinaryDigest in project navajo by Dexels.

the class MD5Sum method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    String output = "unknown";
    if (getOperand(0) == null) {
        return Integer.valueOf(0);
    }
    if (getOperand(0) instanceof Binary) {
        Binary binaryFile = (Binary) getOperand(0);
        return binaryFile.getHexDigest();
    }
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    if (getOperand(0) instanceof Message) {
        Message m = (Message) getOperand(0);
        StringWriter sw = new StringWriter();
        m.write(sw);
        md5.update(sw.toString().getBytes());
    } else {
        md5.update((getOperand(0) + "").getBytes());
    }
    byte[] array = md5.digest();
    if (getOperands().size() > 1 && getOperand(1) instanceof Boolean && (boolean) getOperand(1)) {
        // return hex representation
        return new BinaryDigest(array).hex();
    }
    BigInteger bigInt = new BigInteger(1, array);
    output = bigInt.toString(16);
    return output;
}
Also used : Message(com.dexels.navajo.document.Message) StringWriter(java.io.StringWriter) BinaryDigest(com.dexels.navajo.document.types.BinaryDigest) BigInteger(java.math.BigInteger) Binary(com.dexels.navajo.document.types.Binary) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

BinaryDigest (com.dexels.navajo.document.types.BinaryDigest)3 ExpressionChangedException (com.dexels.navajo.document.ExpressionChangedException)1 Message (com.dexels.navajo.document.Message)1 NavajoException (com.dexels.navajo.document.NavajoException)1 PropertyTypeException (com.dexels.navajo.document.PropertyTypeException)1 Binary (com.dexels.navajo.document.types.Binary)1 ClockTime (com.dexels.navajo.document.types.ClockTime)1 Coordinate (com.dexels.navajo.document.types.Coordinate)1 Money (com.dexels.navajo.document.types.Money)1 NavajoExpression (com.dexels.navajo.document.types.NavajoExpression)1 Percentage (com.dexels.navajo.document.types.Percentage)1 StopwatchTime (com.dexels.navajo.document.types.StopwatchTime)1 BinaryStore (com.dexels.navajo.resource.binarystore.BinaryStore)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 BigInteger (java.math.BigInteger)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1