getting user defined error messages using antlr3 - antlr

numberrange returns [String value]
: numberrangesub
{
String numberRange = ($numberrangesub.text);
String [] v = numberRange.split(",");
if ( Integer.parseInt(v[0].trim()) < Integer.parseInt(v[1].trim())) $value =numberRange;
else throw new RecognitionException();
}
;
Please observe the above ANTLR code. In this I want to throw a user friendly error message like "from value should be less than to value in BETWEEN clause".
I am expecting like this RecognitionException("from value should be less than to value in BETWEEN clause"); But antlr did not accept like as above.
In java class where I am calling the generated java class by Antlr. I am handling like as follows.
try
{
parser.numberRangeCheck();
}
catch (RecognitionException e)
{
throw createException("Invalid Business logic syntax at " + parser.getErrorHeader(e) + ", " + parser.getErrorMessage(e, null), Level.INFO, logger);
}
Any help will be appriciated.

Why not simply throw a RuntimeException with your custom error message?
// ...
else throw new RuntimeException("from value should be less than to value in BETWEEN clause");
// ...

As Terrance wrote in "The Definitive ANTLR Reference" error chapter excerpt:
To avoid forcing English-only error messages and to generally make
things as flexible as possible, the recognizer does not create exception
objects with string messages. Instead, it tracks the information necessary to generate an error.
So there is no error message supplied to RecognitionError's constructor. But you can define additional field of your recognizer to hold user-friendly error message shown on RecognitionError handling:
numberrange returns [String value]
: numberrangesub
{
String numberRange = ($numberrangesub.text);
String [] v = numberRange.split(",");
if ( Integer.parseInt(v[0].trim()) < Integer.parseInt(v[1].trim()))
$value = numberRange;
else {
this.errorMessage = "from value should be less than to value in BETWEEN clause";
throw new RecognitionException(this.input);
}
}
;
And then override the getErrorMessage method:
public String getErrorMessage(RecognitionException e, String[] tokenNames) {
String msg = this.errorMessage;
// ...
}
This works similar to paraphrase mechanism explained in the same excerpt.

Related

Jeval EvaluationException: Invalid use of quotes

When I try to evaluate an expression I get this error: net.sourceforge.jeval.EvaluationException: Invalid use of quotes.
To be clearer. I read my rules from a .txt files where after I put them in a hasmap.
I have tried:
#{mentor} = Chung
#{mentor} = "Chung"
#{mentor} == "Chung"
#{mentor} == 'Chung'
but still gives another error or same error.
What am I doing wrong?
List<String> approvalMentors = new ArrayList<>();
Evaluator evaluator = new Evaluator();
evaluator.putVariable("mentor", String.valueOf(graduationApprovalRequest.getMentor()));
for (HashMap.Entry<String, String> entry : this.schoolRules.entrySet()) {
try {
if(evaluator.evaluate(entry.getValue()).equals("1.0")){
approvalMentors.add(entry.getKey());
}
} catch (EvaluationException ex) {
Logger.getLogger(SchoolRules.class.getName()).log(Level.SEVERE, null, ex);
}
}
return approvalMentors;
.txt File I read my rules from
#{mentor} = 'Chung'
JEval internal validation logic assumes each "operand" (a part of the conditional expression) should be either a built-in function, a number, a special symbol, OR a quoted string. If there is even a single SPACE between the == and double-quote symbols, the check for correctness of quoted string fails. Each such operand should either begin AND end with a quote(s), or do not have the quote(s) at all. The latter is the case with numbers, for example.

ImmutablePropertyException periodically when changing enum field's value via Rational Team Concert API

Hitting this issue with changing certain enumeration-based fields in my new RTC work item for a RTC API tool I'm working on.
Basically, I get an ImmutablePropertyException the first time I change the field, but the next time it works without an exception.
Want to get rid of the exceptions. I'm using a value RTC is actually returning to me as a valid enum value for the field.
Assigning RTC work item field: odc.impact a field value of ->
Integrity [odc.impact.literal.l4]
EXCEPTION: Could not assign value,
even though it was found in the enumeration list: [Unassigned,
Installability, Standards, Integrity]
com.ibm.team.repository.common.internal.ImmutablePropertyException at
com.ibm.team.repository.common.internal.util.ItemUtil$ProtectAdapter.notifyChanged(ItemUtil.java:2070)
at
org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:380)
at
com.ibm.team.repository.common.model.impl.StringExtensionEntryImpl.setTypedValue(StringExtensionEntryImpl.java:178)
at
com.ibm.team.repository.common.model.impl.StringExtensionEntryImpl.setValue(StringExtensionEntryImpl.java:360)
at org.eclipse.emf.common.util.BasicEMap.putEntry(BasicEMap.java:303)
at org.eclipse.emf.common.util.BasicEMap.put(BasicEMap.java:584) at
org.eclipse.emf.common.util.BasicEMap$DelegatingMap.put(BasicEMap.java:799)
at
com.ibm.team.repository.common.model.impl.ItemImpl.setStringExtension(ItemImpl.java:1228)
at
com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setEnumeration(WorkItemImpl.java:3779)
at
com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setValue(WorkItemImpl.java:2915)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
at java.lang.reflect.Method.invoke(Method.java:620) at
com.ibm.team.repository.common.internal.util.ItemStore$ItemInvocationHandler.invoke(ItemStore.java:597)
at com.sun.proxy.$Proxy18.setValue(Unknown Source) at
com.rtc.vda.WorkItemInitialization.setAttributeValueEx(WorkItemInitialization.java:237)
at
com.rtc.vda.WorkItemInitialization.setAttributeValue(WorkItemInitialization.java:210)
at
com.rtc.vda.WorkItemInitialization.execute(WorkItemInitialization.java:186)
at
com.ibm.team.workitem.client.WorkItemOperation.execute(WorkItemOperation.java:85)
at
com.ibm.team.workitem.client.WorkItemOperation.doRun(WorkItemOperation.java:272)
at
com.ibm.team.workitem.client.WorkItemOperation.run(WorkItemOperation.java:242)
at
com.ibm.team.workitem.client.WorkItemOperation.run(WorkItemOperation.java:189)
at com.rtc.vda.RTCUtilities.createWorkItem(RTCUtilities.java:191) at
com.rtc.vda.RTCMain.main(RTCMain.java:178)
Assigning: odc.impact -> Integrity [odc.impact.literal.l4]
This is the code snippet to set the enum value:
public boolean setAttributeValueEx (IWorkItem w, String attributeKey, String valueName) {
// (REO) Get the attribute
IAttribute a = customAttributesMap.get(attributeKey);
// (REO) Buffer of valid values for error reporting
StringBuffer b = new StringBuffer();
try {
// (REO) Get the enumeration for this attribute from the repository (DO NOT CACHE IT OR YOU WILL HAVE PROBLEMS)
IWorkItemClient workItemClient = (IWorkItemClient) rtcParameters.getTeamRepository().getClientLibrary(IWorkItemClient.class);
IEnumeration<? extends ILiteral> rtcAttrEnumeration = workItemClient.resolveEnumeration(a, curMonitor);
// (REO) Find an enum value that matches this string and assign it
for (ILiteral literal : rtcAttrEnumeration.getEnumerationLiterals()) {
String vName = literal.getName();
String vId = literal.getIdentifier2().getStringIdentifier();
b.append(",");
b.append(vName);
if (valueName.equalsIgnoreCase(vName)) {
String msg2 = "Assigning: " + a.getIdentifier() + " -> " + vName + " [" + vId + "]";
RTCMain.out(msg2);
w.setValue(a, literal.getIdentifier2()); // (REO) SOURCE OF PERIODIC EXCEPTION
return true;
}
}
} catch (Exception e) {
RTCMain.out("EXCEPTION: Could not assign value, even though it was found in the enumeration list:\n\t[" + b + "]");
e.printStackTrace();
RTCMain.out("");
return false;
}
RTCMain.out("VALUE NOT FOUND: Valid values are:" + b);
return false;
}
Anyone know why I'm getting the periodic ImmutablePropertyException for only some of the fields, and why it goes away on the second call?
Thanks!
You just need to use the workingCopy.getWorkItem() object passed in to the execute() call rather than a cached version in a member variable. The attributes on the workingCopy object are not immutable and work fine.
public class WorkItemCreator extends WorkItemOperation {
...
#Override
protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem newWorkItem = workingCopy.getWorkItem();
// Set attribute values on newWorkItem to avoid ImmutablePropertyExceptions

NHibernate Criteria Filtering

I use this code to filter database records
if (!string.IsNullOrEmpty(_searchCriteria.MessageType))
{
var messageType = (AutotransferMessageType)Enum.Parse(typeof(AutotransferMessageType), _searchCriteria.MessageType, true);
if (Enum.IsDefined(typeof(AutotransferMessageType), messageType))
{
criteriaQuery.CreateAlias("AutotransferInputRecord", "AutotransferInputRecord")
.Add(
Restrictions.Eq(
"AutotransferInputRecord." + AutotransferLogSearchCriteria.MessageTypePropertyName,
messageType));
}
else
{
criteriaQuery.Add(Restrictions.IsNull("AutotransferInputRecord"));
}
}
AutotransferMessageType is enumerable type
public enum AutotransferMessageType
{
[DisplayName("MT202")]
[DatabaseName("MT202")]
MT202,
[DisplayName("MT210")]
[DatabaseName("MT210")]
MT210,
//...
}
My filter outputs the results when I enter MT202, for example. (It's the right behavior).
When I input just number, for example, 202, I get no results (It's the right behavior too).
But when I try to input some line, "mt", for example, I get error
Unexpected application error has been occured:
'Requested value 'mt' was not found.'
How to make the filter do not show any results when I input a line?
Your error is coming from the line that parses the enum. Use Enum.TryParse instead:
AutotransferMessageType msgEnum;
var enumPrasedOk = Enum.TryParse(_searchCriteria.MessageType, true, out msgEnum);
if(enumPrasedOk){
//Do something
}else{
//Handle case where enum was not found for some reason (if need be)
}
Also please note that you can not look up the enum this way using it's description (in your case they are the same so it is ok).

How can we extract all messages from processing report in json-schema validator

I am using json-schema validator for validating json data. if any error occur it will generate a report. But I want to show the error to the user the report is too big so I want to show only error messages.
This is my report
----------reports-------------
com.github.fge.jsonschema.report.ListProcessingReport: failure
--- BEGIN MESSAGES ---
error: instance failed to match at least one required schema among 4
level: "error"
schema: {"loadingURI":"#","pointer":"/properties/question-groups/items"}
instance: {"pointer":"/question-groups/0"}
domain: "validation"
keyword: "anyOf"
nrSchemas: 4
reports: {"/properties/question-groups/items/anyOf/0":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/multiple-choice/properties/evaluation-key/properties/options/items"},"instance":{"pointer":"/question-groups/0/evaluation-key/options/0"},"domain":"validation","keyword":"allOf","message":"instance failed to match all required schemas (matched only 0 out of 1)","matched":0,"nrSchemas":1,"reports":{"/definitions/multiple-choice/properties/evaluation-key/properties/options/items/allOf/0":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/multiple-choice/properties/evaluation-key/properties/options/items/allOf/0/properties/score"},"instance":{"pointer":"/question-groups/0/evaluation-key/options/0/score"},"domain":"validation","keyword":"type","message":"instance type (string) does not match any allowed primitive type (allowed: [\"integer\"])","found":"string","expected":["integer"]}]}}],"/properties/question-groups/items/anyOf/1":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/text/properties/evaluation-key"},"instance":{"pointer":"/question-groups/0/evaluation-key"},"domain":"validation","keyword":"additionalProperties","message":"object instance has properties which are not allowed by the schema: [\"options\"]","unwanted":["options"]},{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/text/properties/evaluation-key"},"instance":{"pointer":"/question-groups/0/evaluation-key"},"domain":"validation","keyword":"required","message":"object has missing required properties ([\"scorers\"])","required":["scorers"],"missing":["scorers"]},{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/text/properties/type"},"instance":{"pointer":"/question-groups/0/type"},"domain":"validation","keyword":"enum","message":"instance value (\"multiple-choice\") not found in enum (possible values: [\"text\"])","value":"multiple-choice","enum":["text"]}],"/properties/question-groups/items/anyOf/2":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/numeric/properties/evaluation-key"},"instance":{"pointer":"/question-groups/0/evaluation-key"},"domain":"validation","keyword":"additionalProperties","message":"object instance has properties which are not allowed by the schema: [\"options\"]","unwanted":["options"]},{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/numeric/properties/evaluation-key"},"instance":{"pointer":"/question-groups/0/evaluation-key"},"domain":"validation","keyword":"required","message":"object has missing required properties ([\"scorers\"])","required":["scorers"],"missing":["scorers"]},{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/numeric/properties/type"},"instance":{"pointer":"/question-groups/0/type"},"domain":"validation","keyword":"enum","message":"instance value (\"multiple-choice\") not found in enum (possible values: [\"numeric\"])","value":"multiple-choice","enum":["numeric"]}],"/properties/question-groups/items/anyOf/3":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/table"},"instance":{"pointer":"/question-groups/0"},"domain":"validation","keyword":"anyOf","message":"instance failed to match at least one required schema among 2","nrSchemas":2,"reports":{"/definitions/table/anyOf/0":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/table/anyOf/0"},"instance":{"pointer":"/question-groups/0"},"domain":"validation","keyword":"required","message":"object has missing required properties ([\"cells\"])","required":["cells","evaluation-key","group-id","question-text","type"],"missing":["cells"]}],"/definitions/table/anyOf/1":[{"level":"error","schema":{"loadingURI":"#","pointer":"/definitions/table/anyOf/1"},"instance":{"pointer":"/question-groups/0"},"domain":"validation","keyword":"required","message":"object has missing required properties ([\"cells\",\"matching-unit\"])","required":["cells","evaluation-key","group-id","matching-unit","question-text","type"],"missing":["cells","matching-unit"]}]}}]}
--- END MESSAGES ---
How can I get only error messages, any suggestions will be help full.
Thank you
somu
You can read the report as a JsonNode and get what ever parameter you need from that.
ProcessingReport report;
ProcessingMessage message;
report = schema.validate(jsonData);
Iterator itr = report.iterator();
while(itr.hasNext())
{
message = (ProcessingMessage) itr.next();
System.out.println("Message" + message.asJson().get("message").asText());
System.out.println("Reports" + message.asJson().get("reports").asText());
}
You should iterate over all messages and choose only errors comparing getLogLevel() with LogLevel.ERROR. That works for me
Iterator<ProcessingMessage> itr = report.iterator();
while(itr.hasNext())
{
ProcessingMessage message = (ProcessingMessage) itr.next();
if(message.getLogLevel().equals(LogLevel.ERROR)){
System.out.println(message.toString());
}
}
You can use the following method
String getErrorsList(ProcessingReport report, boolean onlyErrors) {
StringBuilder jsonValidationErrors = new StringBuilder();
for (ProcessingMessage processingMessage : report) {
if(onlyErrors && LogLevel.ERROR.equals(processingMessage.getLogLevel())) {
jsonValidationErrors.append(processingMessage.getMessage()).append("\n\r");
} else if(!onlyErrors) {
jsonValidationErrors.append(processingMessage.getMessage()).append("\n\r");
}
}
return jsonValidationErrors.toString();
}

How to get rid of StringTemplate warning "\n in string"

I'm using StringTemplate 4 to generate some Java source files.
The templates are really simple, e.g.:
QueryHandler(method) ::="
public class Obtenir<method.name>Handler extends QueryHandler\<List\<<method.name>Db>> implements IQueryHandler\<List\<<method.name>>>{
private IQuery\<List\<<method.name>Db>> query;
private <method.name>Converter converter;
#Inject
public Obtenir<method.name>Handler(IQuery\<List\<<method.name>Db>> query, <method.name>Converter converter, IStatisticsCollecter theStatsCollecter){
super(theStatsCollecter);
if(query == null){
throw new IllegalArgumentException(\"The query argument cannot be null\");
}
if(converter == null){
throw new IllegalArgumentException(\"Illegal argument for converter(null)\");
}
this.query = query;
this.converter = converter;
}
public List\<<method.name>> handle(Object... params) throws JdbcException {
final String method = \"obtenir<method.name>\";
DaoQueryStatusCallable status = new DaoQueryStatusCallable();
List\<<method.name>Db> result = invoke(query, status, method);
return converter.convert(result);
}
}
"
The code is even simpler:
STGroup group = new STGroupFile("src/main/resources/QueryHandler.stg");
ST wsTemplate = group.getInstanceOf("QueryHandler");
wsTemplate.add("method", m);
System.out.println(wsTemplate.render());
The template lines are separated by Unix EOLs (\n).
When I execute the code, StringTemplate is emitting a warning "QueryHandler.stg 1:25: \n in string".
The result is correct, but I'd still like to get rid of this message.
Anybody ever had this problem and knows how to solve it?
t() ::= "..." is meant only for single lines. Please use
t() ::= <<
...
>>
to get multi-line templates.
Ter