Skip to content

Instantly share code, notes, and snippets.

@johngirvin
Created April 10, 2017 10:33
Show Gist options
  • Save johngirvin/5913c8cfc20a9ffa4ddbe334243dd2ea to your computer and use it in GitHub Desktop.
Save johngirvin/5913c8cfc20a9ffa4ddbe334243dd2ea to your computer and use it in GitHub Desktop.
Run libGDX XmlReader.rl through Ragel to produce a partial C# conversion.
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ObjectMap.Entry;
/** Lightweight XML parser. Supports a subset of XML features: elements, attributes, text, predefined entities, CDATA, mixed
* content. Namespaces are parsed as part of the element or attribute name. Prologs and doctypes are ignored. Only 8-bit character
* encodings are supported. Input is assumed to be well formed.<br>
* <br>
* The default behavior is to parse the XML into a DOM. Extends this class and override methods to perform event driven parsing.
* When this is done, the parse methods will return null.
* @author Nathan Sweet */
public class XmlReader {
private final Array<Element> elements = new Array(8);
private Element root, current;
private final StringBuilder textBuffer = new StringBuilder(64);
public Element parse (String xml) {
char[] data = xml.toCharArray();
return parse(data, 0, data.length);
}
public Element parse (Reader reader) throws IOException {
try {
char[] data = new char[1024];
int offset = 0;
while (true) {
int length = reader.read(data, offset, data.length - offset);
if (length == -1) break;
if (length == 0) {
char[] newData = new char[data.length * 2];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
} else
offset += length;
}
return parse(data, 0, offset);
} catch (IOException ex) {
throw new SerializationException(ex);
} finally {
StreamUtils.closeQuietly(reader);
}
}
public Element parse (InputStream input) throws IOException {
try {
return parse(new InputStreamReader(input, "UTF-8"));
} catch (IOException ex) {
throw new SerializationException(ex);
} finally {
StreamUtils.closeQuietly(input);
}
}
public Element parse (FileHandle file) throws IOException {
InputStream is = null;
try {
return parse(file.reader("UTF-8"));
} catch (Exception ex) {
throw new SerializationException("Error parsing file: " + file, ex);
} finally {
StreamUtils.closeQuietly(is);
}
}
public Element parse (char[] data, int offset, int length) {
int cs, p = offset, pe = length;
int s = 0;
String attributeName = null;
boolean hasBody = false;
/* #line 100 "XmlReader.cs" */
{
cs = xml_start;
}
/* #line 105 "XmlReader.cs" */
{
if ( p == pe )
goto _test_eof;
switch ( cs )
{
st1:
if ( ++p == pe )
goto _test_eof1;
case 1:
switch( data[p] ) {
case '\u0020': goto st1;
case '\u003c': goto st2;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st1;
goto st0;
st0:
cs = 0;
goto _out;
st2:
if ( ++p == pe )
goto _test_eof2;
case 2:
switch( data[p] ) {
case '\u0020': goto st2;
case '\u002f': goto st0;
case '\u003e': goto st0;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st2;
goto tr3;
tr3:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st3;
st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
/* #line 147 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto tr5;
case '\u002f': goto tr6;
case '\u003e': goto tr7;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto tr5;
goto st3;
tr5:
/* #line 100 "XmlReader.rl" */
{
char c = data[s];
if (c == '?' || c == '!') {
if (
data[s + 1] == '[' && //
data[s + 2] == 'C' && //
data[s + 3] == 'D' && //
data[s + 4] == 'A' && //
data[s + 5] == 'T' && //
data[s + 6] == 'A' && //
data[s + 7] == '['
) {
s += 8;
p = s + 2;
while (data[p - 2] != ']' || data[p - 1] != ']' || data[p] != '>')
p++;
text(new String(data, s, p - s - 2));
} else if (c == '!' && data[s + 1] == '-' && data[s + 2] == '-') {
p = s + 3;
while (data[p] != '-' || data[p + 1] != '-' || data[p + 2] != '>')
p++;
p += 2;
} else
while (data[p] != '>') p++;
{if (true) goto st15;}
}
hasBody = true;
open(new String(data, s, p - s));
}
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
/* #line 193 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st4;
case '\u002f': goto st11;
case '\u003d': goto st0;
case '\u003e': goto tr11;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st4;
goto tr8;
tr8:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st5;
st5:
if ( ++p == pe )
goto _test_eof5;
case 5:
/* #line 212 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto tr13;
case '\u002f': goto st0;
case '\u003d': goto tr14;
case '\u003e': goto st0;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto tr13;
goto st5;
tr13:
/* #line 141 "XmlReader.rl" */
{
attributeName = new String(data, s, p - s);
}
goto st6;
st6:
if ( ++p == pe )
goto _test_eof6;
case 6:
/* #line 233 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st6;
case '\u003d': goto st7;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st6;
goto st0;
tr14:
/* #line 141 "XmlReader.rl" */
{
attributeName = new String(data, s, p - s);
}
goto st7;
st7:
if ( ++p == pe )
goto _test_eof7;
case 7:
/* #line 252 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st7;
case '\u0022': goto st8;
case '\u0027': goto st13;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st7;
goto st0;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
if ( data[p] == 34u )
goto tr20;
goto tr19;
tr19:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
/* #line 277 "XmlReader.cs" */
if ( data[p] == 34u )
goto tr22;
goto st9;
tr20:
/* #line 99 "XmlReader.rl" */
{ s = p; }
/* #line 144 "XmlReader.rl" */
{
attribute(attributeName, new String(data, s, p - s));
}
goto st10;
tr22:
/* #line 144 "XmlReader.rl" */
{
attribute(attributeName, new String(data, s, p - s));
}
goto st10;
st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
/* #line 299 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st4;
case '\u002f': goto st11;
case '\u003e': goto tr11;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st4;
goto st0;
tr6:
/* #line 100 "XmlReader.rl" */
{
char c = data[s];
if (c == '?' || c == '!') {
if (
data[s + 1] == '[' && //
data[s + 2] == 'C' && //
data[s + 3] == 'D' && //
data[s + 4] == 'A' && //
data[s + 5] == 'T' && //
data[s + 6] == 'A' && //
data[s + 7] == '['
) {
s += 8;
p = s + 2;
while (data[p - 2] != ']' || data[p - 1] != ']' || data[p] != '>')
p++;
text(new String(data, s, p - s - 2));
} else if (c == '!' && data[s + 1] == '-' && data[s + 2] == '-') {
p = s + 3;
while (data[p] != '-' || data[p + 1] != '-' || data[p + 2] != '>')
p++;
p += 2;
} else
while (data[p] != '>') p++;
{if (true) goto st15;}
}
hasBody = true;
open(new String(data, s, p - s));
}
goto st11;
st11:
if ( ++p == pe )
goto _test_eof11;
case 11:
/* #line 345 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto tr23;
case '\u003e': goto tr24;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto tr23;
goto st0;
tr23:
/* #line 129 "XmlReader.rl" */
{
hasBody = false;
close();
{if (true) goto st15;}
}
goto st12;
st12:
if ( ++p == pe )
goto _test_eof12;
case 12:
/* #line 366 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st12;
case '\u003e': goto tr11;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st12;
goto st0;
tr7:
/* #line 100 "XmlReader.rl" */
{
char c = data[s];
if (c == '?' || c == '!') {
if (
data[s + 1] == '[' && //
data[s + 2] == 'C' && //
data[s + 3] == 'D' && //
data[s + 4] == 'A' && //
data[s + 5] == 'T' && //
data[s + 6] == 'A' && //
data[s + 7] == '['
) {
s += 8;
p = s + 2;
while (data[p - 2] != ']' || data[p - 1] != ']' || data[p] != '>')
p++;
text(new String(data, s, p - s - 2));
} else if (c == '!' && data[s + 1] == '-' && data[s + 2] == '-') {
p = s + 3;
while (data[p] != '-' || data[p + 1] != '-' || data[p + 2] != '>')
p++;
p += 2;
} else
while (data[p] != '>') p++;
{if (true) goto st15;}
}
hasBody = true;
open(new String(data, s, p - s));
}
/* #line 138 "XmlReader.rl" */
{
if (hasBody) {if (true) goto st15;}
}
goto st34;
tr11:
/* #line 138 "XmlReader.rl" */
{
if (hasBody) {if (true) goto st15;}
}
goto st34;
tr24:
/* #line 129 "XmlReader.rl" */
{
hasBody = false;
close();
{if (true) goto st15;}
}
/* #line 138 "XmlReader.rl" */
{
if (hasBody) {if (true) goto st15;}
}
goto st34;
st34:
if ( ++p == pe )
goto _test_eof34;
case 34:
/* #line 433 "XmlReader.cs" */
if ( data[p] == 32u )
goto st34;
if ( 9u <= data[p] && data[p] <= 13u )
goto st34;
goto st0;
st13:
if ( ++p == pe )
goto _test_eof13;
case 13:
if ( data[p] == 39u )
goto tr20;
goto tr26;
tr26:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st14;
st14:
if ( ++p == pe )
goto _test_eof14;
case 14:
/* #line 454 "XmlReader.cs" */
if ( data[p] == 39u )
goto tr22;
goto st14;
st15:
if ( ++p == pe )
goto _test_eof15;
case 15:
switch( data[p] ) {
case '\u0020': goto st15;
case '\u003c': goto st17;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st15;
goto tr28;
tr28:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st16;
st16:
if ( ++p == pe )
goto _test_eof16;
case 16:
/* #line 478 "XmlReader.cs" */
if ( data[p] == 60u )
goto tr32;
goto st16;
tr32:
/* #line 147 "XmlReader.rl" */
{
int end = p;
while (end != s) {
switch (data[end - 1]) {
case ' ':
case '\t':
case '\n':
case '\r':
end--;
continue;
}
break;
}
int current = s;
boolean entityFound = false;
while (current != end) {
if (data[current++] != '&') continue;
int entityStart = current;
while (current != end) {
if (data[current++] != ';') continue;
textBuffer.append(data, s, entityStart - s - 1);
String name = new String(data, entityStart, current - entityStart - 1);
String value = entity(name);
textBuffer.append(value != null ? value : name);
s = current;
entityFound = true;
break;
}
}
if (entityFound) {
if (s < end) textBuffer.append(data, s, end - s);
text(textBuffer.toString());
textBuffer.setLength(0);
} else
text(new String(data, s, end - s));
}
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
/* #line 525 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st17;
case '\u002f': goto st30;
case '\u003e': goto st0;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st17;
goto tr33;
tr33:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st18;
st18:
if ( ++p == pe )
goto _test_eof18;
case 18:
/* #line 543 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto tr36;
case '\u002f': goto tr37;
case '\u003e': goto tr38;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto tr36;
goto st18;
tr36:
/* #line 100 "XmlReader.rl" */
{
char c = data[s];
if (c == '?' || c == '!') {
if (
data[s + 1] == '[' && //
data[s + 2] == 'C' && //
data[s + 3] == 'D' && //
data[s + 4] == 'A' && //
data[s + 5] == 'T' && //
data[s + 6] == 'A' && //
data[s + 7] == '['
) {
s += 8;
p = s + 2;
while (data[p - 2] != ']' || data[p - 1] != ']' || data[p] != '>')
p++;
text(new String(data, s, p - s - 2));
} else if (c == '!' && data[s + 1] == '-' && data[s + 2] == '-') {
p = s + 3;
while (data[p] != '-' || data[p + 1] != '-' || data[p + 2] != '>')
p++;
p += 2;
} else
while (data[p] != '>') p++;
{if (true) goto st15;}
}
hasBody = true;
open(new String(data, s, p - s));
}
goto st19;
st19:
if ( ++p == pe )
goto _test_eof19;
case 19:
/* #line 589 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st19;
case '\u002f': goto st26;
case '\u003d': goto st0;
case '\u003e': goto tr42;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st19;
goto tr39;
tr39:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st20;
st20:
if ( ++p == pe )
goto _test_eof20;
case 20:
/* #line 608 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto tr44;
case '\u002f': goto st0;
case '\u003d': goto tr45;
case '\u003e': goto st0;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto tr44;
goto st20;
tr44:
/* #line 141 "XmlReader.rl" */
{
attributeName = new String(data, s, p - s);
}
goto st21;
st21:
if ( ++p == pe )
goto _test_eof21;
case 21:
/* #line 629 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st21;
case '\u003d': goto st22;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st21;
goto st0;
tr45:
/* #line 141 "XmlReader.rl" */
{
attributeName = new String(data, s, p - s);
}
goto st22;
st22:
if ( ++p == pe )
goto _test_eof22;
case 22:
/* #line 648 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st22;
case '\u0022': goto st23;
case '\u0027': goto st32;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st22;
goto st0;
st23:
if ( ++p == pe )
goto _test_eof23;
case 23:
if ( data[p] == 34u )
goto tr51;
goto tr50;
tr50:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st24;
st24:
if ( ++p == pe )
goto _test_eof24;
case 24:
/* #line 673 "XmlReader.cs" */
if ( data[p] == 34u )
goto tr53;
goto st24;
tr51:
/* #line 99 "XmlReader.rl" */
{ s = p; }
/* #line 144 "XmlReader.rl" */
{
attribute(attributeName, new String(data, s, p - s));
}
goto st25;
tr53:
/* #line 144 "XmlReader.rl" */
{
attribute(attributeName, new String(data, s, p - s));
}
goto st25;
st25:
if ( ++p == pe )
goto _test_eof25;
case 25:
/* #line 695 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st19;
case '\u002f': goto st26;
case '\u003e': goto tr42;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st19;
goto st0;
tr37:
/* #line 100 "XmlReader.rl" */
{
char c = data[s];
if (c == '?' || c == '!') {
if (
data[s + 1] == '[' && //
data[s + 2] == 'C' && //
data[s + 3] == 'D' && //
data[s + 4] == 'A' && //
data[s + 5] == 'T' && //
data[s + 6] == 'A' && //
data[s + 7] == '['
) {
s += 8;
p = s + 2;
while (data[p - 2] != ']' || data[p - 1] != ']' || data[p] != '>')
p++;
text(new String(data, s, p - s - 2));
} else if (c == '!' && data[s + 1] == '-' && data[s + 2] == '-') {
p = s + 3;
while (data[p] != '-' || data[p + 1] != '-' || data[p + 2] != '>')
p++;
p += 2;
} else
while (data[p] != '>') p++;
{if (true) goto st15;}
}
hasBody = true;
open(new String(data, s, p - s));
}
goto st26;
st26:
if ( ++p == pe )
goto _test_eof26;
case 26:
/* #line 741 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto tr54;
case '\u003e': goto tr55;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto tr54;
goto st0;
tr54:
/* #line 129 "XmlReader.rl" */
{
hasBody = false;
close();
{if (true) goto st15;}
}
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
/* #line 762 "XmlReader.cs" */
switch( data[p] ) {
case '\u0020': goto st27;
case '\u003e': goto tr42;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st27;
goto st0;
tr38:
/* #line 100 "XmlReader.rl" */
{
char c = data[s];
if (c == '?' || c == '!') {
if (
data[s + 1] == '[' && //
data[s + 2] == 'C' && //
data[s + 3] == 'D' && //
data[s + 4] == 'A' && //
data[s + 5] == 'T' && //
data[s + 6] == 'A' && //
data[s + 7] == '['
) {
s += 8;
p = s + 2;
while (data[p - 2] != ']' || data[p - 1] != ']' || data[p] != '>')
p++;
text(new String(data, s, p - s - 2));
} else if (c == '!' && data[s + 1] == '-' && data[s + 2] == '-') {
p = s + 3;
while (data[p] != '-' || data[p + 1] != '-' || data[p + 2] != '>')
p++;
p += 2;
} else
while (data[p] != '>') p++;
{if (true) goto st15;}
}
hasBody = true;
open(new String(data, s, p - s));
}
/* #line 138 "XmlReader.rl" */
{
if (hasBody) {if (true) goto st15;}
}
goto st28;
tr42:
/* #line 138 "XmlReader.rl" */
{
if (hasBody) {if (true) goto st15;}
}
goto st28;
tr55:
/* #line 129 "XmlReader.rl" */
{
hasBody = false;
close();
{if (true) goto st15;}
}
/* #line 138 "XmlReader.rl" */
{
if (hasBody) {if (true) goto st15;}
}
goto st28;
st28:
if ( ++p == pe )
goto _test_eof28;
case 28:
/* #line 829 "XmlReader.cs" */
if ( data[p] == 60u )
goto st29;
goto st0;
st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
switch( data[p] ) {
case '\u0020': goto st29;
case '\u002f': goto st30;
default: break;
}
if ( 9u <= data[p] && data[p] <= 13u )
goto st29;
goto st0;
st30:
if ( ++p == pe )
goto _test_eof30;
case 30:
if ( data[p] == 62u )
goto st0;
goto st31;
st31:
if ( ++p == pe )
goto _test_eof31;
case 31:
if ( data[p] == 62u )
goto tr59;
goto st31;
tr59:
/* #line 134 "XmlReader.rl" */
{
close();
{if (true) goto st15;}
}
goto st35;
st35:
if ( ++p == pe )
goto _test_eof35;
case 35:
/* #line 870 "XmlReader.cs" */
goto st0;
st32:
if ( ++p == pe )
goto _test_eof32;
case 32:
if ( data[p] == 39u )
goto tr51;
goto tr60;
tr60:
/* #line 99 "XmlReader.rl" */
{ s = p; }
goto st33;
st33:
if ( ++p == pe )
goto _test_eof33;
case 33:
/* #line 887 "XmlReader.cs" */
if ( data[p] == 39u )
goto tr53;
goto st33;
default: break;
}
_test_eof1: cs = 1; goto _test_eof;
_test_eof2: cs = 2; goto _test_eof;
_test_eof3: cs = 3; goto _test_eof;
_test_eof4: cs = 4; goto _test_eof;
_test_eof5: cs = 5; goto _test_eof;
_test_eof6: cs = 6; goto _test_eof;
_test_eof7: cs = 7; goto _test_eof;
_test_eof8: cs = 8; goto _test_eof;
_test_eof9: cs = 9; goto _test_eof;
_test_eof10: cs = 10; goto _test_eof;
_test_eof11: cs = 11; goto _test_eof;
_test_eof12: cs = 12; goto _test_eof;
_test_eof34: cs = 34; goto _test_eof;
_test_eof13: cs = 13; goto _test_eof;
_test_eof14: cs = 14; goto _test_eof;
_test_eof15: cs = 15; goto _test_eof;
_test_eof16: cs = 16; goto _test_eof;
_test_eof17: cs = 17; goto _test_eof;
_test_eof18: cs = 18; goto _test_eof;
_test_eof19: cs = 19; goto _test_eof;
_test_eof20: cs = 20; goto _test_eof;
_test_eof21: cs = 21; goto _test_eof;
_test_eof22: cs = 22; goto _test_eof;
_test_eof23: cs = 23; goto _test_eof;
_test_eof24: cs = 24; goto _test_eof;
_test_eof25: cs = 25; goto _test_eof;
_test_eof26: cs = 26; goto _test_eof;
_test_eof27: cs = 27; goto _test_eof;
_test_eof28: cs = 28; goto _test_eof;
_test_eof29: cs = 29; goto _test_eof;
_test_eof30: cs = 30; goto _test_eof;
_test_eof31: cs = 31; goto _test_eof;
_test_eof35: cs = 35; goto _test_eof;
_test_eof32: cs = 32; goto _test_eof;
_test_eof33: cs = 33; goto _test_eof;
_test_eof: {}
_out: {}
}
/* #line 194 "XmlReader.rl" */
if (p < pe) {
int lineNumber = 1;
for (int i = 0; i < p; i++)
if (data[i] == '\n') lineNumber++;
throw new SerializationException("Error parsing XML on line " + lineNumber + " near: "
+ new String(data, p, Math.min(32, pe - p)));
} else if (elements.size != 0) {
Element element = elements.peek();
elements.clear();
throw new SerializationException("Error parsing XML, unclosed element: " + element.getName());
}
Element root = this.root;
this.root = null;
return root;
}
/* #line 953 "XmlReader.cs" */
const int xml_start = 1;
const int xml_first_final = 34;
const int xml_error = 0;
const int xml_en_elementBody = 15;
const int xml_en_main = 1;
/* #line 213 "XmlReader.rl" */
protected void open (String name) {
Element child = new Element(name, current);
Element parent = current;
if (parent != null) parent.addChild(child);
elements.add(child);
current = child;
}
protected void attribute (String name, String value) {
current.setAttribute(name, value);
}
protected String entity (String name) {
if (name.equals("lt")) return "<";
if (name.equals("gt")) return ">";
if (name.equals("amp")) return "&";
if (name.equals("apos")) return "'";
if (name.equals("quot")) return "\"";
if (name.startsWith("#x")) return Character.toString((char)Integer.parseInt(name.substring(2), 16));
return null;
}
protected void text (String text) {
String existing = current.getText();
current.setText(existing != null ? existing + text : text);
}
protected void close () {
root = elements.pop();
current = elements.size > 0 ? elements.peek() : null;
}
static public class Element {
private final String name;
private ObjectMap<String, String> attributes;
private Array<Element> children;
private String text;
private Element parent;
public Element (String name, Element parent) {
this.name = name;
this.parent = parent;
}
public String getName () {
return name;
}
public ObjectMap<String, String> getAttributes () {
return attributes;
}
/** @throws GdxRuntimeException if the attribute was not found. */
public String getAttribute (String name) {
if (attributes == null) throw new GdxRuntimeException("Element " + this.name + " doesn't have attribute: " + name);
String value = attributes.get(name);
if (value == null) throw new GdxRuntimeException("Element " + this.name + " doesn't have attribute: " + name);
return value;
}
public String getAttribute (String name, String defaultValue) {
if (attributes == null) return defaultValue;
String value = attributes.get(name);
if (value == null) return defaultValue;
return value;
}
public void setAttribute (String name, String value) {
if (attributes == null) attributes = new ObjectMap(8);
attributes.put(name, value);
}
public int getChildCount () {
if (children == null) return 0;
return children.size;
}
/** @throws GdxRuntimeException if the element has no children. */
public Element getChild (int i) {
if (children == null) throw new GdxRuntimeException("Element has no children: " + name);
return children.get(i);
}
public void addChild (Element element) {
if (children == null) children = new Array(8);
children.add(element);
}
public String getText () {
return text;
}
public void setText (String text) {
this.text = text;
}
public void removeChild (int index) {
if (children != null) children.removeIndex(index);
}
public void removeChild (Element child) {
if (children != null) children.removeValue(child, true);
}
public void remove () {
parent.removeChild(this);
}
public Element getParent () {
return parent;
}
public String toString () {
return toString("");
}
public String toString (String indent) {
StringBuilder buffer = new StringBuilder(128);
buffer.append(indent);
buffer.append('<');
buffer.append(name);
if (attributes != null) {
for (Entry<String, String> entry : attributes.entries()) {
buffer.append(' ');
buffer.append(entry.key);
buffer.append("=\"");
buffer.append(entry.value);
buffer.append('\"');
}
}
if (children == null && (text == null || text.length() == 0))
buffer.append("/>");
else {
buffer.append(">\n");
String childIndent = indent + '\t';
if (text != null && text.length() > 0) {
buffer.append(childIndent);
buffer.append(text);
buffer.append('\n');
}
if (children != null) {
for (Element child : children) {
buffer.append(child.toString(childIndent));
buffer.append('\n');
}
}
buffer.append(indent);
buffer.append("</");
buffer.append(name);
buffer.append('>');
}
return buffer.toString();
}
/** @param name the name of the child {@link Element}
* @return the first child having the given name or null, does not recurse */
public Element getChildByName (String name) {
if (children == null) return null;
for (int i = 0; i < children.size; i++) {
Element element = children.get(i);
if (element.name.equals(name)) return element;
}
return null;
}
/** @param name the name of the child {@link Element}
* @return the first child having the given name or null, recurses */
public Element getChildByNameRecursive (String name) {
if (children == null) return null;
for (int i = 0; i < children.size; i++) {
Element element = children.get(i);
if (element.name.equals(name)) return element;
Element found = element.getChildByNameRecursive(name);
if (found != null) return found;
}
return null;
}
/** @param name the name of the children
* @return the children with the given name or an empty {@link Array} */
public Array<Element> getChildrenByName (String name) {
Array<Element> result = new Array<Element>();
if (children == null) return result;
for (int i = 0; i < children.size; i++) {
Element child = children.get(i);
if (child.name.equals(name)) result.add(child);
}
return result;
}
/** @param name the name of the children
* @return the children with the given name or an empty {@link Array} */
public Array<Element> getChildrenByNameRecursively (String name) {
Array<Element> result = new Array<Element>();
getChildrenByNameRecursively(name, result);
return result;
}
private void getChildrenByNameRecursively (String name, Array<Element> result) {
if (children == null) return;
for (int i = 0; i < children.size; i++) {
Element child = children.get(i);
if (child.name.equals(name)) result.add(child);
child.getChildrenByNameRecursively(name, result);
}
}
/** @throws GdxRuntimeException if the attribute was not found. */
public float getFloatAttribute (String name) {
return Float.parseFloat(getAttribute(name));
}
public float getFloatAttribute (String name, float defaultValue) {
String value = getAttribute(name, null);
if (value == null) return defaultValue;
return Float.parseFloat(value);
}
/** @throws GdxRuntimeException if the attribute was not found. */
public int getIntAttribute (String name) {
return Integer.parseInt(getAttribute(name));
}
public int getIntAttribute (String name, int defaultValue) {
String value = getAttribute(name, null);
if (value == null) return defaultValue;
return Integer.parseInt(value);
}
/** @throws GdxRuntimeException if the attribute was not found. */
public boolean getBooleanAttribute (String name) {
return Boolean.parseBoolean(getAttribute(name));
}
public boolean getBooleanAttribute (String name, boolean defaultValue) {
String value = getAttribute(name, null);
if (value == null) return defaultValue;
return Boolean.parseBoolean(value);
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public String get (String name) {
String value = get(name, null);
if (value == null) throw new GdxRuntimeException("Element " + this.name + " doesn't have attribute or child: " + name);
return value;
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public String get (String name, String defaultValue) {
if (attributes != null) {
String value = attributes.get(name);
if (value != null) return value;
}
Element child = getChildByName(name);
if (child == null) return defaultValue;
String value = child.getText();
if (value == null) return defaultValue;
return value;
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public int getInt (String name) {
String value = get(name, null);
if (value == null) throw new GdxRuntimeException("Element " + this.name + " doesn't have attribute or child: " + name);
return Integer.parseInt(value);
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public int getInt (String name, int defaultValue) {
String value = get(name, null);
if (value == null) return defaultValue;
return Integer.parseInt(value);
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public float getFloat (String name) {
String value = get(name, null);
if (value == null) throw new GdxRuntimeException("Element " + this.name + " doesn't have attribute or child: " + name);
return Float.parseFloat(value);
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public float getFloat (String name, float defaultValue) {
String value = get(name, null);
if (value == null) return defaultValue;
return Float.parseFloat(value);
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public boolean getBoolean (String name) {
String value = get(name, null);
if (value == null) throw new GdxRuntimeException("Element " + this.name + " doesn't have attribute or child: " + name);
return Boolean.parseBoolean(value);
}
/** Returns the attribute value with the specified name, or if no attribute is found, the text of a child with the name.
* @throws GdxRuntimeException if no attribute or child was not found. */
public boolean getBoolean (String name, boolean defaultValue) {
String value = get(name, null);
if (value == null) return defaultValue;
return Boolean.parseBoolean(value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment