Response Parser
Groovy (beta)
code posted
by
raistlin77
created at 13 Jan 21:06
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
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; } } |
6.59 KB in 26 ms with coderay