# Unhandled exception parsing ADL

**URL:** <https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106>\
**Category:** Reference Implementation: Java (archive)\
**Created:** [23 October 2008 08:28 UTC](https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106 "2008-10-23T08:28:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Flinton](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Adam\_Flinton](https://discourse.openehr.org/u/Adam_Flinton)\
**Post date:** [23 October 2008 08:28 UTC](https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106/1 "2008-10-23T08:28:06Z")

</div>

I've built an ant task for generating XML from ADL & running it against  
out adl archetypes dies when an unhandled error pops up.

Exception in thread "main" se.acode.openehr.parser.TokenMgrError:  
Lexical error at line 1, column 1. Encountered: "\u00ef" (239), after : ""  
&nbsp;&nbsp;&nbsp;&nbsp;at  
se.acode.openehr.parser.ADLParserTokenManager.getNextToken(ADLParserTokenManager.java:27554)  
&nbsp;&nbsp;&nbsp;&nbsp;at  
se.acode.openehr.parser.ADLParser.jj\_consume\_token(ADLParser.java:7061)  
&nbsp;&nbsp;&nbsp;&nbsp;at se.acode.openehr.parser.ADLParser.archetype(ADLParser.java:214)  
&nbsp;&nbsp;&nbsp;&nbsp;at se.acode.openehr.parser.ADLParser.parse(ADLParser.java:101)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final public Archetype archetype() throws ParseException, Exception {  
\<cut\>  
&nbsp;&nbsp;&nbsp;&nbsp;jj\_consume\_token(SYM\_ARCHETYPE);  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final private Token jj\_consume\_token(int kind) throws ParseException {  
&nbsp;&nbsp;&nbsp;&nbsp;Token oldToken;  
&nbsp;&nbsp;&nbsp;&nbsp;if ((oldToken = token).next != null) token = token.next;  
&nbsp;&nbsp;&nbsp;&nbsp;else token = token.next = token\_source.getNextToken();  
&nbsp;&nbsp;&nbsp;  
\<cut\>

public Token getNextToken()

\<cut\>  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new TokenMgrError(EOFSeen, curLexState, error\_line,  
error\_column, error\_after, curChar, TokenMgrError.LEXICAL\_ERROR);

I.e. the Token jj\_consume\_token() method only throws a parse exception  
where getNextToken() :

A) Throws a TokenMgrError within the body of the method  
B) Doesn't then throw it at the method level (e.g. public Token  
getNextToken() throws TokenMgrError {} )  
C) Token jj\_consume\_token() doesn't then either catch or throw the  
TokenMgrError.

Adam

---

<div class="post-metadata">

**Author:** ![Adam\_Flinton](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Adam\_Flinton](https://discourse.openehr.org/u/Adam_Flinton)\
**Post date:** [3 November 2008 11:07 UTC](https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106/2 "2008-11-03T11:07:23Z")

</div>

The problem resolves to the (optional) Byte Order Mark for UTF-8.  
Various times people have opened ADL files with text editors & some  
introduce the BOM upon saving.

I have written a little class which strips the BOM but given the parser  
looks to be generated I am unsure how to integrate it.

The byte array to check for/starts with is:

&nbsp;&nbsp;&nbsp;&nbsp;private static final byte UTF8\_BOM\_1 = (byte) 0xef;  
&nbsp;&nbsp;&nbsp;&nbsp;private static final byte UTF8\_BOM\_2 = (byte) 0xbb;  
&nbsp;&nbsp;&nbsp;&nbsp;private static final byte UTF8\_BOM\_3 = (byte) 0xbf;

...

final byte[] utf8Bom = {UTF8\_BOM\_1, UTF8\_BOM\_2, UTF8\_BOM\_3};

And then this needs to be removed prior to parsing

Any takers?

Any idea at which level this would be best integrated so that I can do  
it & then send the changed files to this list or indeed how do I get  
commit rights etc?

Adam

Adam Flinton wrote:

---

<div class="post-metadata">

**Author:** ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)\
**Post date:** [3 November 2008 11:16 UTC](https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106/3 "2008-11-03T11:16:22Z")

</div>

Hi Adam,

there is a parser rule in the adl.jj that ignores the BOM (ll. 224):

\< \* \> SKIP : /\* WHITE SPACE \*/  
{  
&nbsp;&nbsp;" "

> "\t"  
> "\n"  
> "\r"  
> "\f"  
> "\ufeff" /\* UTF-8 Byte Order Mark \*/

}

Not sure why this doesn't work for you?

Cheers  
Sebastian

Adam Flinton wrote:

---

<div class="post-metadata">

**Author:** ![Adam\_Flinton](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Adam\_Flinton](https://discourse.openehr.org/u/Adam_Flinton)\
**Post date:** [3 November 2008 12:27 UTC](https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106/4 "2008-11-03T12:27:16Z")

</div>

Sebastian Garde wrote:

> Hi Adam,
> 
> there is a parser rule in the adl.jj that ignores the BOM (ll. 224):
> 
> \< \* \> SKIP : /\* WHITE SPACE \*/  
> {  
> " "  
> \> "\t"  
> \> "\n"  
> \> "\r"  
> \> "\f"  
> \> "\ufeff" /\* UTF-8 Byte Order Mark \*/  
> }
> 
> Not sure why this doesn't work for you?

Good question...... However it doesn't seem to....Have you tried firing  
in ADL which have a BOM?

Also... are you sure "\ufeff" is correct?

Not:

"\u00ef" (i.e. decimal 239).

Also the BOM is:

> 239 187 191 i.e. ||EF BB BF & not just 239|

I've integrated the fix into my own ant task though :

&nbsp;&nbsp;&nbsp;&nbsp;// For BOM removal  
&nbsp;&nbsp;&nbsp;&nbsp;private static final byte UTF8\_BOM\_1 = (byte) 0xef;  
&nbsp;&nbsp;&nbsp;&nbsp;private static final byte UTF8\_BOM\_2 = (byte) 0xbb;  
&nbsp;&nbsp;&nbsp;&nbsp;private static final byte UTF8\_BOM\_3 = (byte) 0xbf;  
&nbsp;&nbsp;&nbsp;&nbsp;final byte[] utf8Bom = { UTF8\_BOM\_1, UTF8\_BOM\_2, UTF8\_BOM\_3 };  
&nbsp;&nbsp;&nbsp;&nbsp;byte[] bomBuffer = new byte[utf8Bom.length];  
&nbsp;&nbsp;&nbsp;&nbsp;long skipL = utf8Bom.length;

\<cut\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream test = getInputStream(inputFile);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream in = getInputStream(inputFile);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputStream out = getOutputStream(outputFile);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADL2XMLConvertor conv = new ADL2XMLConvertor();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//System.out.println("PCDTO convert() OK to here  
inputFile = "+inputFile);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean skip = checkRemoveBOM(test);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("skip = "+skip);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(skip){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("skipL = "+skipL);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in.skip(skipL);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = conv.convert(in, out);

\<cut\>

&nbsp;&nbsp;&nbsp;&nbsp;public boolean checkRemoveBOM(InputStream in) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean containsBom = false;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nRead = in.read(bomBuffer, 0, bomBuffer.length);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (nRead != -1) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (java.util.Arrays.equals(bomBuffer, utf8Bom)) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// System.out.write(bomBuffer, 0, nRead);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Contains BOM");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;containsBom = true;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (java.io.IOException e) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println("I/O error occurred: " + e.getMessage());  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return containsBom;  
&nbsp;&nbsp;&nbsp;&nbsp;}

& this works so I can get on with the generation to XML which is the  
main thing for me.

I have an XML diff task & the Ocean ADL2XMl converter so I am about to  
run some test runs & then eventually run against the NHS ADL which  
is in:

[http://svn.openehr.org/knowledge/archetypes/dev-uk-nhs/adl](http://svn.openehr.org/knowledge/archetypes/dev-uk-nhs/adl)

So I may be coming back with a list of diffs.

TIA

Adam

---

<div class="post-metadata">

**Author:** ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)\
**Post date:** [3 November 2008 12:51 UTC](https://discourse.openehr.org/t/unhandled-exception-parsing-adl/16106/5 "2008-11-03T12:51:51Z")

</div>

Hi Adam,

This should be an easy fix. Just include your BOM1,2,3 as part of the SKIP block. Can you forward some archetype that has these BOMs for testcase?

Cheers,  
Rong
