use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.
the class GetHash method execute.
/**
* Hashes the string contents (assumed to be UTF-8) using the SHA-256 algorithm.
*/
@Override
public void execute(Context context) {
String baseString = context.getStringArgument(0);
BEnumerator algorithm = (BEnumerator) context.getRefArgument(0);
String hashAlgorithm;
// todo document the supported algorithm
switch(algorithm.getName()) {
case "SHA1":
hashAlgorithm = "SHA-1";
break;
case "SHA256":
hashAlgorithm = "SHA-256";
break;
case "MD5":
hashAlgorithm = "MD5";
break;
default:
throw new BallerinaException("Unsupported algorithm " + algorithm + " for HMAC calculation");
}
String result;
try {
MessageDigest messageDigest;
messageDigest = MessageDigest.getInstance(hashAlgorithm);
messageDigest.update(baseString.getBytes("UTF-8"));
byte[] bytes = messageDigest.digest();
final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
final int byteVal = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[byteVal >>> 4];
hexChars[j * 2 + 1] = hexArray[byteVal & 0x0F];
}
result = new String(hexChars);
} catch (NoSuchAlgorithmException e) {
throw new BallerinaException("Error while calculating HMAC for " + algorithm + ": " + e.getMessage(), context);
} catch (UnsupportedEncodingException e) {
throw new BallerinaException("Error while encoding" + e.getMessage(), context);
}
context.setReturnValues(new BString(result));
}
use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.
the class FormatTo method execute.
@Override
public void execute(Context context) {
BStruct timeStruct = ((BStruct) context.getRefArgument(0));
BEnumerator pattern = (BEnumerator) context.getRefArgument(1);
switch(pattern.getName()) {
case "RFC_1123":
ZonedDateTime zonedDateTime = getZonedDateTime(timeStruct);
String formattedDateTime = zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME);
context.setReturnValues(new BString(formattedDateTime));
break;
default:
throw new BallerinaException("failed to format date/time: unrecognized time format");
}
}
use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.
the class AbstractSQLAction method getParameterDirection.
private int getParameterDirection(BStruct parameter) {
int direction = 0;
BEnumerator dirEnum = (BEnumerator) parameter.getRefField(2);
if (dirEnum != null) {
String sqlType = dirEnum.getName();
switch(sqlType) {
case Constants.QueryParamDirection.DIR_OUT:
direction = Constants.QueryParamDirection.OUT;
break;
case Constants.QueryParamDirection.DIR_INOUT:
direction = Constants.QueryParamDirection.INOUT;
break;
}
}
return direction;
}
use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.
the class ProgramFileReader method readEnumInfoEntries.
private void readEnumInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
int enumCount = dataInStream.readShort();
for (int i = 0; i < enumCount; i++) {
// Create enum info entry
int enumNameCPIndex = dataInStream.readInt();
int flags = dataInStream.readInt();
UTF8CPEntry enumNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(enumNameCPIndex);
String enumName = enumNameUTF8Entry.getValue();
EnumInfo enumInfo = new EnumInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), enumNameCPIndex, enumName, flags);
packageInfo.addEnumInfo(enumName, enumInfo);
// Set enum type
BEnumType enumType = new BEnumType(enumName, packageInfo.getPkgPath());
enumInfo.setType(enumType);
int enumeratorCount = dataInStream.readShort();
BEnumerator[] enumerators = new BEnumerator[enumeratorCount];
for (int j = 0; j < enumeratorCount; j++) {
int enumeratorNameCPIndex = dataInStream.readInt();
UTF8CPEntry enumeratorNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(enumeratorNameCPIndex);
String enumeratorName = enumeratorNameUTF8Entry.getValue();
BEnumerator enumerator = new BEnumerator(enumeratorName, enumType);
enumerators[j] = enumerator;
}
enumType.setEnumerators(enumerators);
// Read attributes of the struct info
readAttributeInfoEntries(dataInStream, packageInfo, enumInfo);
}
}
use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.
the class AbstractSQLAction method getSQLType.
private String getSQLType(BStruct parameter) {
String sqlType = "";
BEnumerator typeEnum = (BEnumerator) parameter.getRefField(0);
if (typeEnum != null) {
sqlType = typeEnum.getName();
}
return sqlType;
}
Aggregations