Title / Description
Code package de.intex.xml; import it.sauronsoftware.base64.Base64 public class ResponseParser { private String lastResponse = ""; private String lastError = null; static private PROPERTIES = [:] static { PROPERTIES['LOCATION'] = [ 'CATEGORY', 'NAME' ] PROPERTIES['ARCHIVE'] = [ 'NAME' ] PROPERTIES['FIELD'] = [ 'NAME', 'TYPE', 'EASYID' ] PROPERTIES['TABFIELD'] = [ 'NAME', 'FORMAT', 'VISIBLELENGTH', 'ORDER', 'EASYID', 'SORTINDEX', 'NUMBER' ] PROPERTIES['SEARCHFIELD'] = [ 'NAME', 'NUMBER', 'EASYID', 'YPOS', 'XPOS', 'LENGTH', 'LABELPOS' ] PROPERTIES['HITLINE'] = [ 'NUMBER', 'EASYDOCREF' ] PROPERTIES['HITLIST'] = [ 'NAME', 'HITCOUNT' ] PROPERTIES['HITLINETABFIELD'] = [ 'NUMBER' ] PROPERTIES['DOCUMENT'] = [ 'FIELDCOUNT', 'ID', 'EDITED', 'CREATION', 'ARCHIVED' ] PROPERTIES['DOCUMENTFIELD'] = [ 'NAME', 'ATTRIB', 'SEGMID', 'USE=USER', 'ID', 'TYPE', 'CODE' ] PROPERTIES['GLOSSARYWORD'] = [ 'NAME' ] PROPERTIES['IMPORT'] = [ 'ARCHIVE' ] PROPERTIES['IMPORTDOCUMENT'] = [ 'EASYDOCREF' ] } /** * Konstruktur für das XMLResponseParser-Objekt. */ ResponseParser(){ /// } /** * gibt den letzen Request zurück. Nützlich für die Fehler analyse. * Da so der Fehlerhafte Request genauer betrachtet werden kann. */ public String getLastResponse(){ return lastResponse; } /* * * */ private getProperties(ln, prop, desc=false){ def ret = [:] if(desc) ret['TEXT'] = ln.text() prop.each(){ it -> ret[it] = ln.attribute( it ) } return ret; } /** * parsed den übergeben Response String. * Ist einer oder mehrere Fehler/Error vorhanden * wird lastError mit einer Fehlermeldung gesetzt. */ def parse(String response) throws ParserException { lastResponse = response; lastError = null; def tmpError = []; def xml = new XmlParser().parseText( response ) if( xml.ERROR.size() > 0 ){ xml.ERROR.each(){ tmpError << "Kommando:'${it.@COMMAND}' Fehler: '${it.@ERRORNUMBER}' '${it.text()}'" } lastError = tmpError.join("\n"); throw( new ParserException( lastError ) ) return null; } def suc = xml.SUCCESS if( suc.size() <= 0 ){ lastError = "Keine Request Informationen zur Verabeitung vorhanden" throw( new ParserException( lastError ) ) return null; } def ret = [:] suc.each(){ sucNode -> println sucNode.@COMMAND switch( sucNode.@COMMAND ){ case 'LOG': case 'LOGIN': case 'LOGOUT': case 'DELETE': ret[ sucNode.@COMMAND ] = 'OK' break; case 'GLOSSARY': case 'ARCHIVES': case 'LOCATIONS': ret[ sucNode.@COMMAND ] = [] sucNode.each(){ ln -> ret[ sucNode.@COMMAND ] << getProperties( ln, PROPERTIES[ ln.name() ], true ) } break; case 'ARCDESCRIPTION': ret[ sucNode.@COMMAND ] = [:] sucNode.ARCDESCRIPTION[0].each(){ arcDesc -> switch(arcDesc.name()){ case 'FIELDLIST': ret[ sucNode.@COMMAND ][ arcDesc.name() ] = [] arcDesc.each(){ ln -> ret[ sucNode.@COMMAND ][ arcDesc.name() ] << getProperties( ln, PROPERTIES[ ln.name() ] ) } break; case 'SEARCHMASKS': case 'HITLISTS': ret[ sucNode.@COMMAND ][ arcDesc.name() ] = [:] arcDesc.each(){ lists -> ret[ sucNode.@COMMAND ][ arcDesc.name() ][ lists.@NAME ] = [] lists.each(){ ln -> ret[ sucNode.@COMMAND ][ arcDesc.name() ][ lists.@NAME ] << getProperties(ln, PROPERTIES[ ln.name()] ) } } break; case 'STATISTICS': ret[ sucNode.@COMMAND ][ arcDesc.name() ] = [:] ['DOCCOUNT', 'INDEXCOUNT', 'ARCSIZE', 'ARCSIZETRANSFERRED', 'ARCSIZEDOCUMENTS', 'ARCSIZEQUERY', 'ARCSIZENOTICE'].each(){ ret[ sucNode.@COMMAND ][ arcDesc.name() ][ it ] = arcDesc."$it".text() } break; default: lastError = "Unbekannter Request-Typ: '${sucNode.@COMMAND}' '${arcDesc.name()}'" throw( new ParserException(lastError) ) break; } } break; case 'QUERY': ret[ sucNode.@COMMAND ] = [:] ret[ sucNode.@COMMAND ]['HITLIST'] = getProperties( sucNode.HITLIST[0], PROPERTIES['HITLIST'] ) ret[ sucNode.@COMMAND ]['HITLINE'] = [] sucNode.HITLIST[0].each(){ ln -> def tmp = getProperties(ln, PROPERTIES[ ln.name() ]) tmp['TABFIELD'] = [] ln.each(){ tab -> tmp['TABFIELD'] << getProperties(tab, PROPERTIES[ ln.name()+tab.name() ], true) } ret[ sucNode.@COMMAND ]['HITLINE'] << tmp } break; case 'IMPORT': println("-------------") println sucNode[ sucNode.@COMMAND ][0] println sucNode[ sucNode.@COMMAND ].DOCUMENT[0] println("-------------") ret[ sucNode.@COMMAND ] = getProperties( sucNode[ sucNode.@COMMAND ][0], PROPERTIES['IMPORT'] ) ret[ sucNode.@COMMAND ]['DOCUMENT'] = getProperties( sucNode[ sucNode.@COMMAND ].DOCUMENT[0], PROPERTIES['IMPORTDOCUMENT'] ) break; case 'DOCUMENT': ret[ sucNode.@COMMAND ] = getProperties( sucNode.DOCUMENT[0], PROPERTIES['DOCUMENT'] ) ret[ sucNode.@COMMAND ]['FIELD'] = [] sucNode.DOCUMENT[0].each(){ ln -> def tmp = getProperties( ln, PROPERTIES['DOCUMENTFIELD'], true ) tmp['DATA'] = [:] ln.each(){ dta -> //println tmp['TYPE'] //println tmp['CODE'] if( tmp['CODE']=='BASE64' && dta.name()=='DATA' && dta.text()!='no data requested'){ tmp['DATA'][ dta.name() ] = Base64.decode( dta.text() ) //tmp['DATA'][ dta.name() ] = 'base64' } else { tmp['DATA'][ dta.name() ] = dta.text() } } ret[ sucNode.@COMMAND ]['FIELD'] << tmp } //println sucNode break; default: lastError = "Unbekannter Request-Typ: '${sucNode.@COMMAND}'" throw( new ParserException(lastError) ) break; } } return ret; } }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code