Rewrite deserialize array function from the ChatGPT generated method.
This commit is contained in:
parent
085c04716d
commit
9c82323cfa
1 changed files with 14 additions and 32 deletions
|
@ -17,12 +17,16 @@ public class XmlRpcStreamReader {
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance ( );
|
XMLInputFactory inputFactory = XMLInputFactory.newInstance ( );
|
||||||
xmlStreamReader = inputFactory.createXMLStreamReader ( inputStream );
|
xmlStreamReader = inputFactory.createXMLStreamReader ( inputStream );
|
||||||
}
|
}
|
||||||
|
|
||||||
private String CURRENT_TAG_NAME;
|
private String CURRENT_TAG_NAME;
|
||||||
|
private int ELEM_TYPE;
|
||||||
|
|
||||||
public boolean nextTag ( ) throws XMLStreamException {
|
public boolean nextTag ( ) throws XMLStreamException {
|
||||||
while ( xmlStreamReader.hasNext ( ) ) {
|
while ( xmlStreamReader.hasNext ( ) ) {
|
||||||
int eventType = xmlStreamReader.next ( );
|
int eventType = xmlStreamReader.next ( );
|
||||||
if ( eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT ) {
|
if ( eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT ) {
|
||||||
CURRENT_TAG_NAME = getLocalName ();
|
CURRENT_TAG_NAME = getLocalName ( );
|
||||||
|
ELEM_TYPE = xmlStreamReader.getEventType ( );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,39 +141,17 @@ public class XmlRpcStreamReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object[] deserializeArray ( ) throws XMLStreamException {
|
private Object[] deserializeArray ( ) throws XMLStreamException {
|
||||||
Object[] array = new Object[ 0 ];
|
List<Object> arr = new ArrayList<> ( );
|
||||||
boolean isValueElement = false;
|
|
||||||
while ( nextTag ( ) ) {
|
while ( nextTag ( ) ) {
|
||||||
if ( xmlStreamReader.getLocalName ( ).equals ( "value" ) ) {
|
if ( CURRENT_TAG_NAME.equals ( "data" ) && ELEM_TYPE == XMLStreamConstants.END_ELEMENT ) {
|
||||||
isValueElement = true;
|
break;
|
||||||
}
|
}
|
||||||
else if ( xmlStreamReader.getLocalName ( ).equals ( "data" ) ) {
|
else if ( CURRENT_TAG_NAME.equals ( "value" ) && ELEM_TYPE == XMLStreamConstants.START_ELEMENT ) {
|
||||||
if ( isValueElement ) {
|
arr.add ( deserializeValue ( ) );
|
||||||
array = deserializeArrayData ( );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object[] deserializeArrayData ( ) throws XMLStreamException {
|
return arr.toArray ( );
|
||||||
Object[] array = new Object[ 0 ];
|
|
||||||
boolean isArrayElement = false;
|
|
||||||
while ( nextTag ( ) ) {
|
|
||||||
if ( xmlStreamReader.getLocalName ( ).equals ( "array" ) ) {
|
|
||||||
isArrayElement = true;
|
|
||||||
}
|
|
||||||
else if ( xmlStreamReader.getLocalName ( ).equals ( "value" ) ) {
|
|
||||||
if ( isArrayElement ) {
|
|
||||||
Object value = deserializeValue ( );
|
|
||||||
Object[] newArray = new Object[ array.length + 1 ];
|
|
||||||
System.arraycopy ( array , 0 , newArray , 0 , array.length );
|
|
||||||
newArray[ array.length ] = value;
|
|
||||||
array = newArray;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return array;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> deserializeStruct ( ) throws XMLStreamException {
|
private Map<String, Object> deserializeStruct ( ) throws XMLStreamException {
|
||||||
|
@ -182,13 +164,13 @@ public class XmlRpcStreamReader {
|
||||||
else if ( xmlStreamReader.getLocalName ( ).equals ( "name" ) ) {
|
else if ( xmlStreamReader.getLocalName ( ).equals ( "name" ) ) {
|
||||||
name = getElementText ( );
|
name = getElementText ( );
|
||||||
}
|
}
|
||||||
else if ( xmlStreamReader.getLocalName ( ).equals ( "value" ) && xmlStreamReader.getEventType () == XMLStreamConstants.START_ELEMENT) {
|
else if ( xmlStreamReader.getLocalName ( ).equals ( "value" ) && xmlStreamReader.getEventType ( ) == XMLStreamConstants.START_ELEMENT ) {
|
||||||
if ( name != null ) {
|
if ( name != null ) {
|
||||||
Object value = deserializeValue ( );
|
Object value = deserializeValue ( );
|
||||||
struct.put ( name , value );
|
struct.put ( name , value );
|
||||||
}
|
}
|
||||||
} else if(CURRENT_TAG_NAME.equals ( "struct" ) && xmlStreamReader.getEventType () == XMLStreamConstants.END_ELEMENT)
|
}
|
||||||
{
|
else if ( CURRENT_TAG_NAME.equals ( "struct" ) && xmlStreamReader.getEventType ( ) == XMLStreamConstants.END_ELEMENT ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue