Changeset 105:8ddcbae1273d

Show
Ignore:
Timestamp:
07/10/08 14:49:52 (5 years ago)
Author:
fbatard
Branch:
default
convert_revision:
svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@106
Message:

New Default Parser for the observation response with XMLBeans

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • org/w3c/unicorn/response/parser/DefaultParser.java

    r89 r105  
    11package org.w3c.unicorn.response.parser; 
    22 
     3import java.io.IOException; 
    34import java.io.InputStream; 
    45import java.util.ArrayList; 
     6import java.util.Calendar; 
     7import java.util.GregorianCalendar; 
    58import java.util.List; 
    69 
    7 import javax.xml.bind.JAXBContext; 
    8 import javax.xml.bind.JAXBException; 
    9 import javax.xml.bind.Unmarshaller; 
     10import javax.xml.datatype.DatatypeConfigurationException; 
     11import javax.xml.datatype.DatatypeFactory; 
     12import javax.xml.datatype.XMLGregorianCalendar; 
    1013 
    1114import org.apache.commons.logging.Log; 
    1215import 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; 
     16import org.apache.xmlbeans.XmlCursor; 
     17import org.apache.xmlbeans.XmlException; 
    2018import org.w3c.unicorn.response.A; 
    2119import org.w3c.unicorn.response.Code; 
     
    2422import org.w3c.unicorn.response.Info; 
    2523import org.w3c.unicorn.response.Longmessage; 
    26 import org.w3c.unicorn.response.Warning; 
    2724import org.w3c.unicorn.response.Response; 
    2825import org.w3c.unicorn.response.Result; 
     26import org.w3c.unicorn.response.Warning; 
    2927import org.w3c.unicorn.util.LocalizedString; 
    3028 
     
    3735                        .getLog("org.w3c.unicorn.response.parser.DefaultParser"); 
    3836 
    39         private static JAXBContext aJAXBContext = null; 
    40  
    41         private static Unmarshaller aUnmarshaller = null; 
    42  
    43         static { 
    44                 try { 
    45                         aJAXBContext = JAXBContext 
    46                                         .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  
    5437        /** 
    5538         * Parses the input and returns the response. 
     
    5841         */ 
    5942        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                 
    6855        } 
    6956 
     
    7562         * @return The new list of localized strings. 
    7663         */ 
    77         private List<LocalizedString> swapListMessage(List<String> x, String lang) { 
     64        private List<LocalizedString> swapListMessage(String[] x, String lang) { 
    7865                List<LocalizedString> y = new ArrayList<LocalizedString>(); 
    7966                for (Object ox : x) { 
     
    9986                                LocalizedString coy = new LocalizedString(cox, lang); 
    10087                                y.add(coy); 
    101                         } else if (ox instanceof org.w3c.unicorn.generated.observationresponse.A) { 
    102                                 org.w3c.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; 
    10390                                A coy = new A(); 
    10491                                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)); 
    106144                                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; 
    109148                                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)); 
    111203                                y.add(coy); 
    112                         } else if (ox instanceof org.w3c.unicorn.generated.observationresponse.Img) { 
    113                                 org.w3c.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; 
    114206                                Img coy = new Img(); 
    115207                                coy.setAlt(cox.getAlt()); 
     
    132224         */ 
    133225        private Longmessage swap( 
    134                         org.w3c.unicorn.generated.observationresponse.Longmessage x, 
     226                        org.w3.unicorn.observationresponse.LongmessageDocument.Longmessage x, 
    135227                        String lang) { 
    136228                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)); 
    138283                return y; 
    139284        } 
     
    147292         */ 
    148293        private List<Longmessage> swapListLongmessage( 
    149                         List<org.w3c.unicorn.generated.observationresponse.Longmessage> x, 
     294                        org.w3.unicorn.observationresponse.LongmessageDocument.Longmessage[] x, 
    150295                        String lang) { 
    151296                List<Longmessage> y = new ArrayList<Longmessage>(); 
    152297                for (Object ox : x) { 
    153                         org.w3c.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; 
    154299                        Longmessage coy = swap(cox, lang); 
    155300                        y.add(coy); 
     
    165310         */ 
    166311        private Warning swap( 
    167                         org.w3c.unicorn.generated.observationresponse.Warning x, String lang) { 
     312                        org.w3.unicorn.observationresponse.WarningDocument.Warning x, String lang) { 
    168313                Warning y = new Warning(); 
    169314                y.setLine(x.getLine()); 
     
    171316                y.setContext(x.getContext()); 
    172317                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)); 
    175320                return y; 
    176321        } 
     
    182327         * @return The swapped error. 
    183328         */ 
    184         private Error swap(org.w3c.unicorn.generated.observationresponse.Error x, 
     329        private Error swap(org.w3.unicorn.observationresponse.ErrorDocument.Error x, 
    185330                        String lang) { 
    186331                Error y = new Error(); 
     
    189334                y.setErrortype(x.getErrortype()); 
    190335                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)); 
    193338                return y; 
    194339        } 
     
    200345         * @return The swapped info. 
    201346         */ 
    202         private Info swap(org.w3c.unicorn.generated.observationresponse.Info x, 
     347        private Info swap(org.w3.unicorn.observationresponse.InfoDocument.Info x, 
    203348                        String lang) { 
    204349                Info y = new Info(); 
     
    206351                y.setColumn(x.getColumn()); 
    207352                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)); 
    210355                return y; 
    211356        } 
     
    217362         * @return The swapped response. 
    218363         */ 
    219         private Response swap(Observationresponse or) { 
     364        private Response swap(org.w3.unicorn.observationresponse.ObservationresponseDocument ord) { 
    220365                Response res = new Response(); 
     366                org.w3.unicorn.observationresponse.ObservationresponseDocument.Observationresponse or=ord.getObservationresponse(); 
    221367                res.setUri(or.getUri()); 
    222368                res.setCheckedby(or.getCheckedby()); 
    223369                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()); 
    226381 
    227382                // Fill res.result 
    228                 org.w3c.unicorn.generated.observationresponse.Result rrr = or 
    229                                 .getResult(); 
     383                org.w3.unicorn.observationresponse.ResultDocument.Result rrr = or.getResult(); 
    230384                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()) { 
    234388                                        String lang = warnings.getLang(); 
    235389                                        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()) { 
    238391                                                r.getWarnings().add(swap(w, lang)); 
    239392                                        } 
     
    242395                        } 
    243396 
    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()) { 
    247400                                        String lang = errors.getLang(); 
    248401                                        Result r = new Result(errors.getLang(), wl.getUri()); 
    249                                         for (org.w3c.unicorn.generated.observationresponse.Error w : wl 
    250                                                         .getError()) { 
     402                                        for (org.w3.unicorn.observationresponse.ErrorDocument.Error w : wl 
     403                                                        .getErrorArray()) { 
    251404                                                r.getErrors().add(swap(w, lang)); 
    252405                                        } 
     
    255408                        } 
    256409 
    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) { 
    259412                                String lang = informations.getLang(); 
    260                                 for (Infolist wl : informations.getInfolist()) { 
     413                                for (org.w3.unicorn.observationresponse.InfolistDocument.Infolist wl : informations.getInfolistArray()) { 
    261414                                        Result r = new Result(informations.getLang(), wl.getUri()); 
    262                                         for (org.w3c.unicorn.generated.observationresponse.Info w : wl 
    263                                                         .getInfo()) { 
     415                                        for (org.w3.unicorn.observationresponse.InfoDocument.Info w : wl 
     416                                                        .getInfoArray()) { 
    264417                                                r.getInfos().add(swap(w, lang)); 
    265418                                        }