use of org.nhindirect.policy.PolicyParseException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser method buildX509Field.
/**
* Builds a certificate reference expression that is an {@link X509Field}.
* @param token The token used to build the field.
* @return An {@link X509Field} object that represents the token. Returns null if the token does not represent an {@link X509Field}.
* @throws PolicyParseException
*/
protected PolicyExpression buildX509Field(String token) throws PolicyParseException {
X509Field<?> retVal = null;
final X509FieldType fieldType = X509FieldType.fromToken(token);
if (fieldType != null) {
try {
Class<? extends X509Field<?>> fieldRefClass = fieldType.getReferenceClass();
if (fieldRefClass == null)
throw new PolicyParseException("X509Field with token name " + token + " has not been implemented yet.");
retVal = fieldRefClass.newInstance();
} catch (PolicyParseException ex) {
throw ex;
}///CLOVER:OFF
catch (Exception e) {
throw new PolicyParseException("Error building X509Field", e);
}
///CLOVER:ON
}
return retVal;
}
use of org.nhindirect.policy.PolicyParseException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser method buildExtensionField.
/**
* Builds a certificate reference expression that is an {@link ExtensionField}.
* @param token The token used to build the field.
* @return An {@link ExtensionField} object that represents the token. Returns null if the token does not represent an {@link ExtensionField}.
* @throws PolicyParseException
*/
protected PolicyExpression buildExtensionField(String token) throws PolicyParseException {
ExtensionField<?> retVal = null;
final ExtensionIdentifier fieldType = ExtensionIdentifier.fromToken(token);
if (fieldType != null) {
try {
boolean required = token.endsWith("+");
final Class<? extends ExtensionField<?>> fieldRefClass = fieldType.getReferenceClass(token);
if (fieldRefClass == null)
throw new PolicyParseException("ExtensionField with token name " + token + " has not been implemented yet.");
final Constructor<?> cons = fieldRefClass.getConstructor(Boolean.TYPE);
retVal = (ExtensionField<?>) cons.newInstance(required);
} catch (PolicyParseException ex) {
throw ex;
}///CLOVER:OFF
catch (Exception e) {
throw new PolicyParseException("Error building ExtensionField", e);
}
///CLOVER:ON
}
return retVal;
}
use of org.nhindirect.policy.PolicyParseException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser method parseToTokens.
/**
* Parses an input stream of the SimpleTextV1 lexicon into a vector of tokens. Tokens are inserted into the vector
* as they encountered in the stream.
* @param stream The input stream.
* @return An ordered vector of tokens parsed from the input stream.
* @throws PolicyParseException
*/
protected Vector<TokenTypeAssociation> parseToTokens(InputStream stream) throws PolicyParseException {
final Vector<TokenTypeAssociation> tokens = new Vector<TokenTypeAssociation>();
try {
final InputStreamReader isr = new InputStreamReader(stream);
StringWriter writer = new StringWriter();
boolean holdMode = false;
TokenType holdType = null;
for (int i; (i = isr.read()) > 0; ) {
writer.write(i);
final String checkForTokenString = writer.toString();
// check to see if we have an exact match to a token
TokenType exactMatchToken = tokenMap.get(checkForTokenString);
if (exactMatchToken != null) {
// may have a partial string of an operator with more characters
if (exactMatchToken == TokenType.OPERATOR_EXPRESSION || exactMatchToken == TokenType.CERTIFICATE_REFERENCE_EXPRESSION) {
holdType = exactMatchToken;
// go into hold mode so we can check the next character
holdMode = true;
} else {
// not an operator, so go ahead and mark it as a complete token
tokens.add(new TokenTypeAssociation(checkForTokenString, exactMatchToken));
writer = new StringWriter();
}
} else {
if (holdMode) {
// we know that the checkForTokenString is comprised of an exact match at the beginning of the string
// up to the last character in the string
// break the string into the known token and the start of the next token
final String operatorToken = checkForTokenString.substring(0, (checkForTokenString.length() - 1));
final String nextToken = checkForTokenString.substring((checkForTokenString.length() - 1));
// add the token to the token vector
tokens.add(new TokenTypeAssociation(operatorToken, holdType));
// reset the writer
writer = new StringWriter();
// check if the nextToken string matches a string
exactMatchToken = tokenMap.get(nextToken);
if (exactMatchToken != null) {
tokens.add(new TokenTypeAssociation(nextToken, exactMatchToken));
} else
// not a reserved token, so queue up the nextToken to continue on with the parsing
writer.write(nextToken);
holdMode = false;
holdType = null;
} else {
// check to see if the checkForTokenString now contains a reserved token
for (String key : tokenMap.keySet()) {
int idx = checkForTokenString.indexOf(key);
if (idx >= 0) {
// found one... need to break the string into a literal and the new token
final String firstToken = checkForTokenString.substring(0, idx).trim();
final String secondToken = checkForTokenString.substring(idx).trim();
// if the first token is all white space, don't add it as a token
if (!firstToken.isEmpty())
tokens.add(new TokenTypeAssociation(firstToken, TokenType.LITERAL_EXPRESSION));
// reset the writer
writer = new StringWriter();
// check if the second token (which we know is a reserved token)
// is an operator...
exactMatchToken = tokenMap.get(secondToken);
if (exactMatchToken == TokenType.OPERATOR_EXPRESSION || exactMatchToken == TokenType.CERTIFICATE_REFERENCE_EXPRESSION) {
// go into hold mode
holdMode = true;
holdType = exactMatchToken;
writer.write(secondToken);
} else {
// the token is not an operator, so add it the token vector
tokens.add(new TokenTypeAssociation(secondToken, exactMatchToken));
}
break;
}
}
}
}
}
// now that we have completed traversing the expression lexicon, if there is anything left over in the writer then
// add it as a token
final String remainingString = writer.toString().trim();
if (!remainingString.isEmpty()) {
final TokenType exactMatchToken = tokenMap.get(remainingString);
final TokenType addTokenType = (exactMatchToken != null) ? exactMatchToken : TokenType.LITERAL_EXPRESSION;
tokens.add(new TokenTypeAssociation(remainingString, addTokenType));
}
} catch (IOException e) {
throw new PolicyParseException("Error parsing: " + e.getMessage(), e);
}
return tokens;
}
Aggregations