Changeset 105:8ddcbae1273d
- Timestamp:
- 07/10/08 14:49:52 (5 years ago)
- Branch:
- default
- convert_revision:
- svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@106
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
org/w3c/unicorn/response/parser/DefaultParser.java
r89 r105 1 1 package org.w3c.unicorn.response.parser; 2 2 3 import java.io.IOException; 3 4 import java.io.InputStream; 4 5 import java.util.ArrayList; 6 import java.util.Calendar; 7 import java.util.GregorianCalendar; 5 8 import java.util.List; 6 9 7 import javax.xml. bind.JAXBContext;8 import javax.xml. bind.JAXBException;9 import javax.xml. bind.Unmarshaller;10 import javax.xml.datatype.DatatypeConfigurationException; 11 import javax.xml.datatype.DatatypeFactory; 12 import javax.xml.datatype.XMLGregorianCalendar; 10 13 11 14 import org.apache.commons.logging.Log; 12 15 import org.apache.commons.logging.LogFactory; 13 import org.w3c.unicorn.generated.observationresponse.Errorlist; 14 import org.w3c.unicorn.generated.observationresponse.Errors; 15 import org.w3c.unicorn.generated.observationresponse.Infolist; 16 import org.w3c.unicorn.generated.observationresponse.Informations; 17 import org.w3c.unicorn.generated.observationresponse.Observationresponse; 18 import org.w3c.unicorn.generated.observationresponse.Warninglist; 19 import org.w3c.unicorn.generated.observationresponse.Warnings; 16 import org.apache.xmlbeans.XmlCursor; 17 import org.apache.xmlbeans.XmlException; 20 18 import org.w3c.unicorn.response.A; 21 19 import org.w3c.unicorn.response.Code; … … 24 22 import org.w3c.unicorn.response.Info; 25 23 import org.w3c.unicorn.response.Longmessage; 26 import org.w3c.unicorn.response.Warning;27 24 import org.w3c.unicorn.response.Response; 28 25 import org.w3c.unicorn.response.Result; 26 import org.w3c.unicorn.response.Warning; 29 27 import org.w3c.unicorn.util.LocalizedString; 30 28 … … 37 35 .getLog("org.w3c.unicorn.response.parser.DefaultParser"); 38 36 39 private static JAXBContext aJAXBContext = null;40 41 private static Unmarshaller aUnmarshaller = null;42 43 static {44 try {45 aJAXBContext = JAXBContext46 .newInstance("org.w3c.unicorn.generated.observationresponse");47 aUnmarshaller = aJAXBContext.createUnmarshaller();48 } catch (final JAXBException e) {49 logger.error("JAXBException : " + e.getMessage(), e);50 e.printStackTrace();51 }52 }53 54 37 /** 55 38 * Parses the input and returns the response. … … 58 41 */ 59 42 public Response parse(InputStream inputStream) { 60 try { 61 return swap((Observationresponse) (aUnmarshaller 62 .unmarshal(inputStream))); 63 } catch (JAXBException e) { 64 logger.error("JAXBException : " + e.getMessage(), e); 65 e.printStackTrace(); 66 return null; 67 } 43 try { 44 return swap(org.w3.unicorn.observationresponse.ObservationresponseDocument.Factory.parse(inputStream)); 45 } catch (XmlException e) { 46 e.printStackTrace(); 47 logger.error("XMLBeansException : " + e.getMessage(), e); 48 return null; 49 } catch (IOException e) { 50 e.printStackTrace(); 51 logger.error("XMLBeansException : " + e.getMessage(), e); 52 return null; 53 } 54 68 55 } 69 56 … … 75 62 * @return The new list of localized strings. 76 63 */ 77 private List<LocalizedString> swapListMessage( List<String>x, String lang) {64 private List<LocalizedString> swapListMessage(String[] x, String lang) { 78 65 List<LocalizedString> y = new ArrayList<LocalizedString>(); 79 66 for (Object ox : x) { … … 99 86 LocalizedString coy = new LocalizedString(cox, lang); 100 87 y.add(coy); 101 } else if (ox instanceof org.w3 c.unicorn.generated.observationresponse.A) {102 org.w3 c.unicorn.generated.observationresponse.A cox = (org.w3c.unicorn.generated.observationresponse.A) ox;88 } else if (ox instanceof org.w3.unicorn.observationresponse.ADocument.A) { 89 org.w3.unicorn.observationresponse.ADocument.A cox = (org.w3.unicorn.observationresponse.ADocument.A) ox; 103 90 A coy = new A(); 104 91 coy.setHref(cox.getHref()); 105 coy.setContent(swap(cox.getContent(), lang)); 92 93 94 XmlCursor cursor=cox.newCursor(); 95 //System.err.println("Debut"); 96 //System.err.println(cursor.xmlText()); 97 98 ArrayList<Object> listObj = new ArrayList<Object>(); 99 100 while (cursor.hasNextToken()) { 101 cursor.toNextToken(); 102 //System.err.println(cursor.currentTokenType().intValue()==TokenType.INT_END); 103 Object current = cursor.getObject(); 104 if(current!=null) { 105 //System.err.println(current.getClass()); 106 107 if (current.getClass()== 108 org.w3.unicorn.observationresponse.impl.LongmessageDocumentImpl.LongmessageImpl.class) { 109 break; 110 } 111 // Case : A 112 if (current.getClass()== 113 org.w3.unicorn.observationresponse.impl.ADocumentImpl.AImpl.class) { 114 org.w3.unicorn.observationresponse.ADocument.A 115 a = (org.w3.unicorn.observationresponse.ADocument.A) current; 116 listObj.add(a); 117 //We skip 3 tokens to forget about the inner text in the A tag. 118 //We don't want it in the object list 119 cursor.toNextToken(); 120 cursor.toNextToken(); 121 cursor.toNextToken(); 122 } 123 // Case : Code 124 if (current.getClass()== 125 org.w3.unicorn.observationresponse.impl.CodeDocumentImpl.CodeImpl.class) { 126 org.w3.unicorn.observationresponse.CodeDocument.Code code=(org.w3.unicorn.observationresponse.CodeDocument.Code) current; 127 128 listObj.add(code); 129 } 130 // Case : Img 131 if (current.getClass()== 132 org.w3.unicorn.observationresponse.impl.ImgDocumentImpl.ImgImpl.class) { 133 org.w3.unicorn.observationresponse.ImgDocument.Img 134 img = (org.w3.unicorn.observationresponse.ImgDocument.Img) current; 135 listObj.add(img); 136 } 137 } 138 else if(cursor.isText()){ 139 listObj.add(cursor.getTextValue()); 140 } 141 142 } 143 coy.setContent(swap(listObj, lang)); 106 144 y.add(coy); 107 } else if (ox instanceof org.w3c.unicorn.generated.observationresponse.Code) { 108 org.w3c.unicorn.generated.observationresponse.Code cox = (org.w3c.unicorn.generated.observationresponse.Code) ox; 145 146 } else if (ox instanceof org.w3.unicorn.observationresponse.CodeDocument.Code) { 147 org.w3.unicorn.observationresponse.CodeDocument.Code cox = (org.w3.unicorn.observationresponse.CodeDocument.Code) ox; 109 148 Code coy = new Code(); 110 coy.setContent(swap(cox.getContent(), lang)); 149 150 151 XmlCursor cursor=cox.newCursor(); 152 //System.err.println("Debut"); 153 //System.err.println(cursor.xmlText()); 154 155 ArrayList<Object> listObj = new ArrayList<Object>(); 156 157 while (cursor.hasNextToken()) { 158 cursor.toNextToken(); 159 //System.err.println(cursor.currentTokenType().intValue()==TokenType.INT_END); 160 Object current = cursor.getObject(); 161 if(current!=null) { 162 //System.err.println(current.getClass()); 163 164 if (current.getClass()== 165 org.w3.unicorn.observationresponse.impl.LongmessageDocumentImpl.LongmessageImpl.class) { 166 break; 167 } 168 // Case : A 169 if (current.getClass()== 170 org.w3.unicorn.observationresponse.impl.ADocumentImpl.AImpl.class) { 171 org.w3.unicorn.observationresponse.ADocument.A 172 a = (org.w3.unicorn.observationresponse.ADocument.A) current; 173 listObj.add(a); 174 // We skip 3 tokens to forget about the inner text in the A tag. 175 //We don't want it in the object list 176 cursor.toNextToken(); 177 cursor.toNextToken(); 178 cursor.toNextToken(); 179 } 180 // Case : Code 181 if (current.getClass()== 182 org.w3.unicorn.observationresponse.impl.CodeDocumentImpl.CodeImpl.class) { 183 org.w3.unicorn.observationresponse.CodeDocument.Code code=(org.w3.unicorn.observationresponse.CodeDocument.Code) current; 184 185 listObj.add(code); 186 } 187 // Case : Img 188 if (current.getClass()== 189 org.w3.unicorn.observationresponse.impl.ImgDocumentImpl.ImgImpl.class) { 190 org.w3.unicorn.observationresponse.ImgDocument.Img 191 img = (org.w3.unicorn.observationresponse.ImgDocument.Img) current; 192 listObj.add(img); 193 } 194 } 195 else if(cursor.isText()){ 196 listObj.add(cursor.getTextValue()); 197 } 198 199 } 200 201 202 coy.setContent(swap(listObj, lang)); 111 203 y.add(coy); 112 } else if (ox instanceof org.w3 c.unicorn.generated.observationresponse.Img) {113 org.w3 c.unicorn.generated.observationresponse.Img cox = (org.w3c.unicorn.generated.observationresponse.Img) ox;204 } else if (ox instanceof org.w3.unicorn.observationresponse.ImgDocument.Img) { 205 org.w3.unicorn.observationresponse.ImgDocument.Img cox = (org.w3.unicorn.observationresponse.ImgDocument.Img) ox; 114 206 Img coy = new Img(); 115 207 coy.setAlt(cox.getAlt()); … … 132 224 */ 133 225 private Longmessage swap( 134 org.w3 c.unicorn.generated.observationresponse.Longmessage x,226 org.w3.unicorn.observationresponse.LongmessageDocument.Longmessage x, 135 227 String lang) { 136 228 Longmessage y = new Longmessage(); 137 y.setContent(swap(x.getContent(), lang)); 229 XmlCursor cursor=x.newCursor(); 230 //System.err.println("Debut"); 231 //System.err.println(cursor.xmlText()); 232 233 ArrayList<Object> listObj = new ArrayList<Object>(); 234 235 while (cursor.hasNextToken()) { 236 cursor.toNextToken(); 237 //System.err.println(cursor.currentTokenType().intValue()==TokenType.INT_END); 238 Object current = cursor.getObject(); 239 if(current!=null) { 240 //System.err.println(current.getClass()); 241 242 if (current.getClass()== 243 org.w3.unicorn.observationresponse.impl.LongmessageDocumentImpl.LongmessageImpl.class) { 244 break; 245 } 246 // Case : A 247 if (current.getClass()== 248 org.w3.unicorn.observationresponse.impl.ADocumentImpl.AImpl.class) { 249 org.w3.unicorn.observationresponse.ADocument.A 250 a = (org.w3.unicorn.observationresponse.ADocument.A) current; 251 listObj.add(a); 252 //We skip 3 tokens to forget about the inner text in the A tag. 253 //We don't want it in the object list 254 cursor.toNextToken(); 255 cursor.toNextToken(); 256 cursor.toNextToken(); 257 } 258 // Case : Code 259 if (current.getClass()== 260 org.w3.unicorn.observationresponse.impl.CodeDocumentImpl.CodeImpl.class) { 261 org.w3.unicorn.observationresponse.CodeDocument.Code code=(org.w3.unicorn.observationresponse.CodeDocument.Code) current; 262 263 listObj.add(code); 264 } 265 // Case : Img 266 if (current.getClass()== 267 org.w3.unicorn.observationresponse.impl.ImgDocumentImpl.ImgImpl.class) { 268 org.w3.unicorn.observationresponse.ImgDocument.Img 269 img = (org.w3.unicorn.observationresponse.ImgDocument.Img) current; 270 listObj.add(img); 271 } 272 } 273 else if(cursor.isText()){ 274 listObj.add(cursor.getTextValue()); 275 } 276 277 } 278 279 //System.err.println("List : " + listObj); 280 //System.err.println("Fin"); 281 282 y.setContent(swap(listObj, lang)); 138 283 return y; 139 284 } … … 147 292 */ 148 293 private List<Longmessage> swapListLongmessage( 149 List<org.w3c.unicorn.generated.observationresponse.Longmessage>x,294 org.w3.unicorn.observationresponse.LongmessageDocument.Longmessage[] x, 150 295 String lang) { 151 296 List<Longmessage> y = new ArrayList<Longmessage>(); 152 297 for (Object ox : x) { 153 org.w3 c.unicorn.generated.observationresponse.Longmessage cox = (org.w3c.unicorn.generated.observationresponse.Longmessage) ox;298 org.w3.unicorn.observationresponse.LongmessageDocument.Longmessage cox = (org.w3.unicorn.observationresponse.LongmessageDocument.Longmessage) ox; 154 299 Longmessage coy = swap(cox, lang); 155 300 y.add(coy); … … 165 310 */ 166 311 private Warning swap( 167 org.w3 c.unicorn.generated.observationresponse.Warning x, String lang) {312 org.w3.unicorn.observationresponse.WarningDocument.Warning x, String lang) { 168 313 Warning y = new Warning(); 169 314 y.setLine(x.getLine()); … … 171 316 y.setContext(x.getContext()); 172 317 y.setLevel(x.getLevel()); 173 y.setMessage(swapListMessage(x.getMessage (), lang));174 y.setLongmessage(swapListLongmessage(x.getLongmessage (), lang));318 y.setMessage(swapListMessage(x.getMessageArray(), lang)); 319 y.setLongmessage(swapListLongmessage(x.getLongmessageArray(), lang)); 175 320 return y; 176 321 } … … 182 327 * @return The swapped error. 183 328 */ 184 private Error swap(org.w3 c.unicorn.generated.observationresponse.Error x,329 private Error swap(org.w3.unicorn.observationresponse.ErrorDocument.Error x, 185 330 String lang) { 186 331 Error y = new Error(); … … 189 334 y.setErrortype(x.getErrortype()); 190 335 y.setContext(x.getContext()); 191 y.setMessage(swapListMessage(x.getMessage (), lang));192 y.setLongmessage(swapListLongmessage(x.getLongmessage (), lang));336 y.setMessage(swapListMessage(x.getMessageArray(), lang)); 337 y.setLongmessage(swapListLongmessage(x.getLongmessageArray(), lang)); 193 338 return y; 194 339 } … … 200 345 * @return The swapped info. 201 346 */ 202 private Info swap(org.w3 c.unicorn.generated.observationresponse.Info x,347 private Info swap(org.w3.unicorn.observationresponse.InfoDocument.Info x, 203 348 String lang) { 204 349 Info y = new Info(); … … 206 351 y.setColumn(x.getColumn()); 207 352 y.setContext(x.getContext()); 208 y.setMessage(swapListMessage(x.getMessage (), lang));209 y.setLongmessage(swapListLongmessage(x.getLongmessage (), lang));353 y.setMessage(swapListMessage(x.getMessageArray(), lang)); 354 y.setLongmessage(swapListLongmessage(x.getLongmessageArray(), lang)); 210 355 return y; 211 356 } … … 217 362 * @return The swapped response. 218 363 */ 219 private Response swap( Observationresponse or) {364 private Response swap(org.w3.unicorn.observationresponse.ObservationresponseDocument ord) { 220 365 Response res = new Response(); 366 org.w3.unicorn.observationresponse.ObservationresponseDocument.Observationresponse or=ord.getObservationresponse(); 221 367 res.setUri(or.getUri()); 222 368 res.setCheckedby(or.getCheckedby()); 223 369 res.setVersion(or.getVersion()); 224 res.setDate(or.getDate()); 225 res.setPassed(or.isPassed()); 370 XMLGregorianCalendar xmlGregorianCalendar; 371 try { 372 xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar((GregorianCalendar)or.getDate()); 373 } catch (DatatypeConfigurationException e) { 374 e.printStackTrace(); 375 logger.error("DatatypeConfigurationException erreur de date : " + e.getMessage(), e); 376 return null; 377 } 378 or.setDate(Calendar.getInstance()); 379 res.setDate(xmlGregorianCalendar); 380 res.setPassed(or.getPassed()); 226 381 227 382 // Fill res.result 228 org.w3c.unicorn.generated.observationresponse.Result rrr = or 229 .getResult(); 383 org.w3.unicorn.observationresponse.ResultDocument.Result rrr = or.getResult(); 230 384 if (rrr != null) { 231 Warnings warnings = rrr.getWarnings();232 if (warnings != null && warnings.getWarninglist () != null) {233 for ( Warninglist wl : warnings.getWarninglist()) {385 org.w3.unicorn.observationresponse.WarningsDocument.Warnings warnings = rrr.getWarnings(); 386 if (warnings != null && warnings.getWarninglistArray() != null) { 387 for (org.w3.unicorn.observationresponse.WarninglistDocument.Warninglist wl : warnings.getWarninglistArray()) { 234 388 String lang = warnings.getLang(); 235 389 Result r = new Result(lang, wl.getUri()); 236 for (org.w3c.unicorn.generated.observationresponse.Warning w : wl 237 .getWarning()) { 390 for (org.w3.unicorn.observationresponse.WarningDocument.Warning w : wl.getWarningArray()) { 238 391 r.getWarnings().add(swap(w, lang)); 239 392 } … … 242 395 } 243 396 244 Errors errors = rrr.getErrors();245 if (errors != null && errors.getErrorlist () != null) {246 for ( Errorlist wl : errors.getErrorlist()) {397 org.w3.unicorn.observationresponse.ErrorsDocument.Errors errors = rrr.getErrors(); 398 if (errors != null && errors.getErrorlistArray() != null) { 399 for (org.w3.unicorn.observationresponse.ErrorlistDocument.Errorlist wl : errors.getErrorlistArray()) { 247 400 String lang = errors.getLang(); 248 401 Result r = new Result(errors.getLang(), wl.getUri()); 249 for (org.w3 c.unicorn.generated.observationresponse.Error w : wl250 .getError ()) {402 for (org.w3.unicorn.observationresponse.ErrorDocument.Error w : wl 403 .getErrorArray()) { 251 404 r.getErrors().add(swap(w, lang)); 252 405 } … … 255 408 } 256 409 257 Informations informations = rrr.getInformations();258 if (informations != null && informations.getInfolist () != null) {410 org.w3.unicorn.observationresponse.InformationsDocument.Informations informations = rrr.getInformations(); 411 if (informations != null && informations.getInfolistArray() != null) { 259 412 String lang = informations.getLang(); 260 for ( Infolist wl : informations.getInfolist()) {413 for (org.w3.unicorn.observationresponse.InfolistDocument.Infolist wl : informations.getInfolistArray()) { 261 414 Result r = new Result(informations.getLang(), wl.getUri()); 262 for (org.w3 c.unicorn.generated.observationresponse.Info w : wl263 .getInfo ()) {415 for (org.w3.unicorn.observationresponse.InfoDocument.Info w : wl 416 .getInfoArray()) { 264 417 r.getInfos().add(swap(w, lang)); 265 418 }
