Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: sync4j/framework/core/Util.java


1   /*
2    Copyright (c) 2001-2002 sync4j project
3    All rights reserved.
4   
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8   
9    1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions, and the following disclaimer.
11  
12   2. Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions, and the disclaimer that follows
14      these conditions in the documentation and/or other materials
15      provided with the distribution.
16  
17   3. The name "sync4j" must not be used to endorse or promote products
18      derived from this software without prior written permission.
19  
20   4. Products derived from this software may not be called "sync4j", nor
21      may "sync4j" appear in their name, without prior written permission.
22  
23   In addition, we request (but do not require) that you include in the
24   end-user documentation provided with the redistribution and/or in the
25   software itself an acknowledgement equivalent to the following:
26  
27       "This product includes software developed by the
28        sync4j project."
29  
30   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
31   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33   DISCLAIMED.  IN NO EVENT SHALL THE SYNC4J AUTHORS OR THE PROJECT
34   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
37   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
40   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41   SUCH DAMAGE.
42  
43   */
44  
45  package sync4j.framework.core;
46  
47  import sync4j.framework.core.xml.*;
48  import sync4j.framework.core.RepresentationException;
49  
50  import java.util.Enumeration;
51  import java.util.Vector;
52  import java.io.*;
53  
54  /**
55   *
56   *   utilities
57   *
58   *  @author Sean C. Sullivan
59   *  @author Stefano Fornari @ Funambol
60   *  @author david.buchmann at unifr.ch
61   *
62   *  @version $Id: Util.java,v 1.7 2003/03/25 21:59:13 stefano_fornari Exp $
63   *
64   */
65  public final class Util
66  {
67      private static IXmlOperation adaptee;
68      
69      static {
70          // to switch back, uncomment and recompile...
71          //adaptee = new sync4j.framework.core.xml.jdom.XmlOperationImpl();
72          adaptee = new sync4j.framework.core.xml.nanoxml.XmlOperationImpl();
73      }
74      
75      private Util() {    
76      }
77       
78      public static Element toElement(
79              final String strXMLFragment,
80              final String strElementName)
81          throws RepresentationException
82      {
83          return adaptee.toElement(strXMLFragment, strElementName);
84      }
85  
86      public static Element extractElement(
87          final Element eParent,
88          final String strChildName,
89          final boolean bElementIsRequired)
90          throws RepresentationException
91      {
92          return adaptee.extractElement(eParent,
93                                    strChildName,
94                                    bElementIsRequired);
95      }
96      
97      public static Element extractElement(
98          final Element eParent,
99          final String strChildName,
100         final boolean bElementIsRequired,
101                 final Object ns)
102         throws RepresentationException  {
103         return adaptee.extractElement(eParent, strChildName, 
104                 bElementIsRequired, ns);
105     }
106     
107     public static String extractElementText(
108             final Element eParent           ,
109             final String  strChildName      ,
110             final boolean bElementIsRequired,
111             final Object  ns                )
112         throws RepresentationException   {
113         return adaptee.extractElementText(eParent, strChildName,
114                 bElementIsRequired, ns);
115     }
116 
117     public static String extractElementText(
118                 final Element eParent,
119                 final String strChildName,
120                 final boolean bElementIsRequired)
121         throws RepresentationException  {
122         return adaptee.extractElementText(eParent ,
123                                   strChildName,
124                                   bElementIsRequired);
125     }
126 
127     public static Namespace getNamespace(String uri) {
128         return adaptee.getNamespace(uri);
129     }
130     
131     public static Element castToElement(Object obj) {
132         return adaptee.castToElement(obj);
133     }
134     
135     static DTDVersion extractDTDVersion(
136             final Element eParent,
137             final boolean bElementIsRequired)
138             throws RepresentationException
139     {
140         // todo : re-write this - ?
141         return new DTDVersion(
142                 Util.extractElementText(
143                         eParent,
144                         "VerDTD",
145                         bElementIsRequired));
146     }
147 
148     static ProtocolVersion extractVerProto(
149             final Element eParent,
150             final boolean bElementIsRequired)
151             throws RepresentationException
152     {
153         // todo : re-write this - ?
154         return new ProtocolVersion(
155                 Util.extractElementText(
156                         eParent,
157                         "VerProto",
158                         bElementIsRequired));
159     }
160 
161     static SyncSessionIdentifier extractSessionID(
162             final Element eParent,
163             final boolean bElementIsRequired)
164             throws RepresentationException
165     {
166         // todo : re-write this - ?
167         return new SyncSessionIdentifier(
168                 Util.extractElementText(
169                         eParent,
170                         "SessionID",
171                         bElementIsRequired));
172     }
173 
174     static String extractLang(
175             final Element eParent,
176             final boolean bElementIsRequired)
177             throws RepresentationException
178     {
179         return Util.extractElementText(
180                 eParent,
181                 "Lang",
182                 bElementIsRequired);
183     }
184 
185     static String extractMessageID(
186             final Element eParent,
187             final boolean bElementIsRequired)
188             throws RepresentationException
189     {
190         return Util.extractElementText(
191                 eParent,
192                 "MsgID",
193                 bElementIsRequired);
194     }
195 
196     static SyncHeader extractSyncHeader(
197             final Element eParent)
198             throws RepresentationException
199     {
200         Element e = extractElement(
201                 eParent,
202                 "SyncHdr",
203                 true);
204         if (e == null)
205         {
206             throw new RepresentationException("missing SyncHdr");
207         }
208         return new SyncHeader(e);
209     }
210 
211     static SyncBody extractSyncBody(
212             final Element eParent)
213             throws RepresentationException
214     {
215         Element e = extractElement(
216                 eParent,
217                 "SyncBody",
218                 true);
219         if (e == null)
220         {
221             throw new RepresentationException("missing SyncBody");
222         }
223         return new SyncBody(e);
224     }
225 
226     static Target extractTarget(
227             final Element eParent,
228             final boolean bElementIsRequired)
229             throws RepresentationException
230     {
231         Element e = extractElement(
232                 eParent,
233                 "Target",
234                 bElementIsRequired);
235         if (e == null)
236         {
237             // todo : is this right?
238             return null;
239         }
240         return new Target(e);
241     }
242 
243     static Source extractSource(
244             final Element eParent,
245             final boolean bElementIsRequired)
246             throws RepresentationException
247     {
248         Element e = extractElement(
249                 eParent,
250                 "Source",
251                 bElementIsRequired);
252         if (e == null)
253         {
254             // todo : is this right?
255             return null;
256         }
257         return new Source(e);
258     }
259 
260     static Source[] extractSources(
261             final Element eParent,
262             final boolean bElementIsRequired)
263             throws RepresentationException
264     {
265         Vector vSources = new Vector();
266         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
267         {
268             Element e = (Element)enum.nextElement();
269             if (e.getName().equals("Source"))
270             {
271                 vSources.addElement(new Source(e));
272             }
273         }
274         if (vSources.size() < 1) // todo : should this be here???
275         {
276             // todo : throw exception
277         }
278 
279         /*  
280             Not compatible with j2me
281             Source[] aSources = new Source[size];
282             return (Source[]) vSources.toArray(aSources);
283         */
284         int size = vSources.size();
285         Source[] aSources = new Source[size];
286         for (int i=0; i<size; i++) {
287             aSources[i] = (Source)vSources.elementAt(i);
288         }
289         return aSources;
290     }
291 
292 
293     static String extractLocName(
294             final Element eParent,
295             final boolean bElementIsRequired)
296             throws RepresentationException
297     {
298         String strText = extractElementText(
299                 eParent,
300                 "LocName",
301                 bElementIsRequired);
302         return strText;
303     }
304 
305     static String extractRespURI(
306             final Element eParent,
307             final boolean bElementIsRequired)
308             throws RepresentationException
309     {
310         return Util.extractURI(
311                 eParent,
312                 "RespURI",
313                 bElementIsRequired);
314     }
315 
316 
317     static boolean extractNoResults(
318             final Element eParent,
319             final boolean bElementIsRequired)
320             throws RepresentationException
321     {
322         return Util.extractBooleanResult(
323                 eParent,
324                 "NoResults",
325                 bElementIsRequired);
326     }
327 
328     static boolean extractSharedMem(
329             final Element eParent,
330             final boolean bElementIsRequired)
331             throws RepresentationException
332     {
333         return Util.extractBooleanResult(
334                 eParent,
335                 "SharedMem",
336                 bElementIsRequired);
337     }
338 
339     static boolean extractArchive(
340             final Element eParent,
341             final boolean bElementIsRequired)
342             throws RepresentationException
343     {
344         return Util.extractBooleanResult(
345                 eParent,
346                 "Archive",
347                 bElementIsRequired);
348     }
349 
350 
351     static boolean extractSftDel(
352             final Element eParent,
353             final boolean bElementIsRequired)
354             throws RepresentationException
355     {
356         return Util.extractBooleanResult(
357                 eParent,
358                 "SftDel",
359                 bElementIsRequired);
360     }
361 
362 
363     static boolean extractNoResp(
364             final Element eParent,
365             final boolean bElementIsRequired)
366             throws RepresentationException
367     {
368         return Util.extractBooleanResult(
369                 eParent,
370                 "NoResp",
371                 bElementIsRequired);
372     }
373 
374     private static boolean extractBooleanResult(
375             final Element eParent,
376             final String strElementName,
377             final boolean bElementIsRequired)
378             throws RepresentationException
379     {
380         Element e = extractElement(
381                 eParent,
382                 strElementName,
383                 bElementIsRequired);
384         if (e == null)
385         {
386             return false;
387         }
388         return true;
389     }
390 
391     static Credential extractCred(
392             final Element eParent,
393             final boolean bElementIsRequired)
394             throws RepresentationException
395     {
396         Element eCred = Util.extractElement(
397                 eParent,
398                 "Cred",
399                 bElementIsRequired);
400         if (eCred == null)
401         {
402             return null;
403         }
404         return new Credential(eCred);
405     }
406 
407     static Meta extractMeta(
408             final Element eParent,
409             final boolean bElementIsRequired)
410             throws RepresentationException
411     {
412         Element eMeta = Util.extractElement(
413                 eParent,
414                 "Meta",
415                 bElementIsRequired);
416         if (eMeta == null)
417         {
418             return null;
419         }
420         return new Meta(eMeta);
421     }
422 
423 
424     static Data extractData(
425             final Element eParent,
426             final boolean bElementIsRequired)
427             throws RepresentationException
428     {
429         Element eData = Util.extractElement(
430                 eParent,
431                 "Data",
432                 bElementIsRequired);
433         if (eData == null)
434         {
435             return null;
436         }
437         return new Data(eData);
438     }
439 
440 
441     static AbstractCommand[] extractAbstractCommands(
442         final Element eParent,
443         final boolean bElementIsRequired)
444         throws RepresentationException
445     {
446         Vector vCommands = new Vector();
447         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
448         {
449             Element e = (Element)enum.nextElement();
450             final String strName = e.getName();
451             if (strName.equals(AlertCommand.COMMAND_NAME))
452             {
453               vCommands.addElement(new AlertCommand(e));
454             }
455             else if (strName.equals(AddCommand.COMMAND_NAME))
456             {
457               vCommands.addElement(new AddCommand(e));
458             }
459             else if (strName.equals(AtomicCommand.COMMAND_NAME))
460             {
461               vCommands.addElement(new AtomicCommand(e));
462             }
463             else if (strName.equals(CopyCommand.COMMAND_NAME))
464             {
465               vCommands.addElement(new CopyCommand(e));
466             }
467             else if (strName.equals(DeleteCommand.COMMAND_NAME))
468             {
469               vCommands.addElement(new DeleteCommand(e));
470             }
471             else if (strName.equals(ExecCommand.COMMAND_NAME))
472             {
473               vCommands.addElement(new ExecCommand(e));
474             }
475             else if (strName.equals(GetCommand.COMMAND_NAME))
476             {
477               vCommands.addElement(new GetCommand(e));
478             }
479             else if (strName.equals(MapCommand.COMMAND_NAME))
480             {
481               vCommands.addElement(new MapCommand(e));
482             }
483             else if (strName.equals(PutCommand.COMMAND_NAME))
484             {
485               vCommands.addElement(new PutCommand(e));
486             }
487             else if (strName.equals(ReplaceCommand.COMMAND_NAME))
488             {
489               vCommands.addElement(new ReplaceCommand(e));
490             }
491             else if (strName.equals(SearchCommand.COMMAND_NAME))
492             {
493               vCommands.addElement(new SearchCommand(e));
494             }
495             else if (strName.equals(SequenceCommand.COMMAND_NAME))
496             {
497               vCommands.addElement(new SequenceCommand(e));
498             }
499             else if (strName.equals(SyncCommand.COMMAND_NAME))
500             {
501               vCommands.addElement(new SyncCommand(e));
502             }
503             else
504             {
505                 // todo : throw exception here ?
506             }
507         }
508         if (vCommands.size() < 1)
509         {
510             // todo : throw exception here - ?
511         }
512         /* Not compatible with j2me
513         AbstractCommand[] aCommands = new AbstractCommand[vCommands.size()];
514         return (AbstractCommand[]) vCommands.toArray( ((Object[]) aCommands) );
515         */
516         int size = vCommands.size();
517         AbstractCommand[] aCommands = new AbstractCommand[size];
518         for (int i=0; i<size; i++) {
519             aCommands[i] = (AbstractCommand)vCommands.elementAt(i);
520         }
521         return aCommands;
522     }
523 
524     // corresponds to the <Cmd> element in <Status>
525     //
526     // note: "The Cmd element type specifies the name of the
527     //        SyncML command associated with the SyncML
528     //        request. The value of the element type can
529     //        also be "SyncHdr" when the CmdRef element type
530     //        has a value of '0'"
531     //
532     static String extractCommandName(
533             final Element eParent,
534             final boolean bElementIsRequired)
535             throws RepresentationException
536     {
537         String strText = extractElementText(
538                 eParent,
539                 "Cmd",
540                 bElementIsRequired);
541         return strText;
542     }
543 
544     static AbstractCommand[] extractCommands(
545         final Element eParent,
546         final boolean bElementIsRequired)
547         throws RepresentationException
548     {
549         Vector vCommands = new Vector();
550         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
551         {
552             Element e = (Element)enum.nextElement();
553             final String strName = e.getName();
554             if (strName.equals(AddCommand.COMMAND_NAME))
555             {
556               vCommands.addElement(new AddCommand(e));
557             }
558             else if (strName.equals(AlertCommand.COMMAND_NAME))
559             {
560               vCommands.addElement(new AlertCommand(e));
561             }
562             else if (strName.equals(AtomicCommand.COMMAND_NAME))
563             {
564               vCommands.addElement(new AtomicCommand(e));
565             }
566             else if (strName.equals(CopyCommand.COMMAND_NAME))
567             {
568               vCommands.addElement(new CopyCommand(e));
569             }
570             else if (strName.equals(DeleteCommand.COMMAND_NAME))
571             {
572               vCommands.addElement(new DeleteCommand(e));
573             }
574             else if (strName.equals(ExecCommand.COMMAND_NAME))
575             {
576               vCommands.addElement(new ExecCommand(e));
577             }
578             else if (strName.equals(GetCommand.COMMAND_NAME))
579             {
580               vCommands.addElement(new GetCommand(e));
581             }
582             else if (strName.equals(MapCommand.COMMAND_NAME))
583             {
584               vCommands.addElement(new MapCommand(e));
585             }
586             else if (strName.equals(PutCommand.COMMAND_NAME))
587             {
588               vCommands.addElement(new PutCommand(e));
589             }
590             else if (strName.equals(ReplaceCommand.COMMAND_NAME))
591             {
592               vCommands.addElement(new ReplaceCommand(e));
593             }
594             else if (strName.equals(ResultsCommand.COMMAND_NAME))
595             {
596               vCommands.addElement(new ResultsCommand(e));
597             }
598             else if (strName.equals(SearchCommand.COMMAND_NAME))
599             {
600               vCommands.addElement(new SearchCommand(e));
601             }
602             else if (strName.equals(SequenceCommand.COMMAND_NAME))
603             {
604               vCommands.addElement(new SequenceCommand(e));
605             }
606             else if (strName.equals(StatusCommand.COMMAND_NAME))
607             {
608               vCommands.addElement(new StatusCommand(e));
609             }
610             else if (strName.equals(SyncCommand.COMMAND_NAME))
611             {
612               vCommands.addElement(new SyncCommand(e));
613             }
614             else if (strName.equals("Final"))
615             {
616               // ignore it
617             }
618             else
619             {
620                 throw new RepresentationException(
621                     "unknown command: <"
622                     + strName
623                     + ">");
624             }
625         }
626         if (vCommands.size() < 1)
627         {
628             // todo : throw exception here - ?
629         }
630         /* Not compatible with j2me
631         AbstractCommand[] aCommands = new AbstractCommand[vCommands.size()];
632         return (AbstractCommand[]) vCommands.toArray( ((Object[]) aCommands) );
633         */
634         int size = vCommands.size();
635         AbstractCommand[] aCommands = new AbstractCommand[size];
636         for (int i=0; i<size; i++) {
637             aCommands[i] = (AbstractCommand)vCommands.elementAt(i);
638         }
639         return aCommands;
640 
641     }
642 
643     static boolean extractFinal(
644             final Element eParent,
645             final boolean bElementIsRequired)
646             throws RepresentationException
647     {
648         Element eFinal = Util.extractElement(
649                 eParent,
650                 "Final",
651                 bElementIsRequired);
652         if (eFinal == null)
653         {
654             return false;
655         }
656         return true;
657     }
658 
659 
660     static String extractFormat(
661             final Element eParent,
662             final boolean bElementIsRequired)
663             throws RepresentationException
664     {
665         return Util.extractElementText(
666                 eParent,
667                 "Format",
668                 bElementIsRequired);
669     }
670 
671     static String extractType(
672             final Element eParent,
673             final boolean bElementIsRequired)
674             throws RepresentationException
675     {
676         return Util.extractElementText(
677                 eParent,
678                 "Type",
679                 bElementIsRequired,
680                 Constants.NAMESPACE_METINF);
681     }
682 
683     static CommandIdentifier extractCommandId(
684             final Element eParent,
685             final boolean bElementIsRequired)
686             throws RepresentationException
687     {
688         // todo : doublecheck this method's behavior
689         String strText = Util.extractElementText(
690                 eParent,
691                 "CmdID",
692                 bElementIsRequired);
693         if ((bElementIsRequired == false) && (strText == null))
694         {
695             return null;
696         }
697         return new CommandIdentifier(strText);
698     }
699 
700     static int extractAlertCode(
701             final Element eParent,
702             final boolean bElementIsRequired)
703             throws RepresentationException
704     {
705         Element eData = Util.extractElement(
706                 eParent,
707                 "Data",
708                 bElementIsRequired);
709         return Integer.parseInt(eData.getText());
710     }
711 
712     static Item extractItem(
713             final Element eParent,
714             final boolean bElementIsRequired)
715             throws RepresentationException
716     {
717         Element eItem = Util.extractElement(
718                 eParent,
719                 "Item",
720                 bElementIsRequired);
721         return new Item(eItem);
722     }
723 
724     static Item[] extractItems(
725             final Element eParent,
726             final boolean bElementIsRequired)
727             throws RepresentationException
728     {
729         Vector vItems = new Vector();
730         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
731         {
732             Element e = (Element)enum.nextElement();
733             if (e.getName().equals("Item"))
734             {
735                 vItems.addElement(new Item(e));
736             }
737         }
738         if (vItems.size() < 1) // todo : should this be here???
739         {
740             // todo : throw exception
741         }
742         /* Not compatible with j2me
743             Item[] aItems = new Item[vItems.size()];
744             return (Item[]) vItems.toArray(aItems);
745         */
746         int size = vItems.size();
747         Item[] aItems = new Item[size];
748         for (int i=0; i<size; i++) {
749             aItems[i] = (Item)vItems.elementAt(i);
750         }
751         return aItems;
752     
753     }
754 
755     static MapItem[] extractMapItems(
756             final Element eParent,
757             final boolean bElementIsRequired)
758             throws RepresentationException
759     {
760         Vector vMapItems = new Vector();
761         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
762         {
763             Element e = (Element)enum.nextElement();
764             if (e.getName().equals("MapItem"))
765             {
766                 vMapItems.addElement(new MapItem(e));
767             }
768         }
769         if (vMapItems.size() < 1) // todo : should this be here???
770         {
771             // todo : throw exception
772         }
773         /* Not compatible with j2me
774         MapItem[] aMapItems = new MapItem[vMapItems.size()];
775         return (MapItem[]) vMapItems.toArray(aMapItems);
776         */
777         int size = vMapItems.size();
778         MapItem[] aMapItems = new MapItem[size];
779         for (int i=0; i<size; i++) {
780             aMapItems[i] = (MapItem)vMapItems.elementAt(i);
781         }
782         return aMapItems;
783     }
784 
785     static String extractLocURI(
786             final Element eParent,
787             final boolean bElementIsRequired)
788             throws RepresentationException
789     {
790         return extractURI(
791                 eParent,
792                 "LocURI",
793                 bElementIsRequired);
794     }
795 
796     private static String extractURI(
797             final Element eParent,
798             final String strChildName,
799             final boolean bElementIsRequired)
800             throws RepresentationException
801     {
802         Element e = extractElement(
803                 eParent,
804                 strChildName,
805                 bElementIsRequired);
806         if (e == null)
807         {
808             if (bElementIsRequired)
809             {
810                 // todo : throw exception here
811             }
812             else
813             {
814                 return null;
815             }
816         }
817         String u = extractElementText(
818                 eParent,
819                 strChildName,
820                 bElementIsRequired);
821         return u;
822     }
823 
824     static AuthenticationChallenge extractChal(
825             final Element eParent,
826             final boolean bElementIsRequired)
827             throws RepresentationException
828     {
829         Element eChal = Util.extractElement(
830                 eParent,
831                 "Chal",
832                 bElementIsRequired);
833         if (eChal == null)
834         {
835             return null;
836         }
837         return new AuthenticationChallenge(eChal);
838     }
839 
840     static TargetRef extractTargetRef(
841             final Element eParent,
842             final boolean bElementIsRequired)
843             throws RepresentationException
844     {
845         Element eTargetRef = Util.extractElement(
846                 eParent,
847                 "TargetRef",
848                 bElementIsRequired);
849         if (eTargetRef == null)
850         {
851             return null;
852         }
853         return new TargetRef(eTargetRef);
854     }
855 
856     static SourceRef extractSourceRef(
857             final Element eParent,
858             final boolean bElementIsRequired)
859             throws RepresentationException
860     {
861         Element eSourceRef = Util.extractElement(
862                 eParent,
863                 "SourceRef",
864                 bElementIsRequired);
865         if (eSourceRef == null)
866         {
867             return null;
868         }
869         return new SourceRef(eSourceRef);
870     }
871 
872     static SourceRef[] extractSourceRefs(
873             final Element eParent,
874             final boolean bElementIsRequired)
875             throws RepresentationException
876     {
877         Vector vSourceRefs = new Vector();
878         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
879         {
880             Element e = (Element)enum.nextElement();
881             if (e.getName().equals("SourceRef"))
882             {
883                 vSourceRefs.addElement(new SourceRef(e));
884             }
885         }
886         // todo : should this be here???
887         if (bElementIsRequired
888                 && (vSourceRefs.size() < 1))
889         {
890             // todo : throw exception
891         }
892         /*  
893             Not compatible with j2me
894             SourceRef[] aSourceRefs = new SourceRef[vSourceRefs.size()];
895             return (SourceRef[]) vSourceRefs.toArray(aSourceRefs);
896         */
897         int size = vSourceRefs.size();
898         SourceRef[] aSourceRefs = new SourceRef[size];
899         for (int i=0; i<size; i++) {
900             aSourceRefs[i] = (SourceRef)vSourceRefs.elementAt(i);
901         }
902         return aSourceRefs;
903 
904     }
905 
906     static TargetRef[] extractTargetRefs(
907             final Element eParent,
908             final boolean bElementIsRequired)
909             throws RepresentationException
910     {
911         Vector vTargetRefs = new Vector();
912         for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
913         {
914             Element e = (Element)enum.nextElement();
915             if (e.getName().equals("TargetRef"))
916             {
917                 vTargetRefs.addElement(new TargetRef(e));
918             }
919         }
920         // todo : should this be here???
921         if (bElementIsRequired
922                 && (vTargetRefs.size() < 1))
923         {
924             // todo : throw exception
925         }
926         /* not compatible with j2me
927             TargetRef[] aTargetRefs = new TargetRef[vTargetRefs.size()];
928             return (TargetRef[]) vTargetRefs.toArray(aTargetRefs);
929         */
930         int size = vTargetRefs.size();
931         TargetRef[] aTargetRefs = new TargetRef[size];
932         for (int i=0; i < size; i++) {
933             aTargetRefs[i] = (TargetRef)vTargetRefs.elementAt(i);
934         }
935         return aTargetRefs;
936         
937     }
938 
939     static String extractMessageRef(
940             final Element eParent,
941             final boolean bElementIsRequired)
942             throws RepresentationException
943     {
944         String strText = extractElementText(
945                 eParent,
946                 "MsgRef",
947                 bElementIsRequired);
948         return strText;
949     }
950 
951     static String extractCommandRef(
952             final Element eParent,
953             final boolean bElementIsRequired)
954             throws RepresentationException
955     {
956         String strText = extractElementText(
957                 eParent,
958                 "CmdRef",
959                 bElementIsRequired);
960         return strText;
961     }
962 
963     static String extractManufacturer(
964             final Element eParent,
965             final boolean bElementIsRequired)
966             throws RepresentationException
967     {
968         String strText = extractElementText(
969                 eParent,
970                 "Man",
971                 bElementIsRequired);
972         return strText;
973     }
974 
975     static String extractModelName(
976             final Element eParent,
977             final boolean bElementIsRequired)
978             throws RepresentationException
979     {
980         String strText = extractElementText(
981                 eParent,
982                 "Mod",
983                 bElementIsRequired);
984         return strText;
985     }
986 
987     static String extractOEM(
988             final Element eParent,
989             final boolean bElementIsRequired)
990             throws RepresentationException
991     {
992         String strText = extractElementText(
993                 eParent,
994                 "OEM",
995                 bElementIsRequired);
996         return strText;
997     }
998 
999     static String extractFirmwareVersion(
1000            final Element eParent,
1001            final boolean bElementIsRequired)
1002            throws RepresentationException
1003    {
1004        String strText = extractElementText(
1005                eParent,
1006                "FwV",
1007                bElementIsRequired);
1008        return strText;
1009    }
1010
1011    static String extractSoftwareVersion(
1012            final Element eParent,
1013            final boolean bElementIsRequired)
1014            throws RepresentationException
1015    {
1016        String strText = extractElementText(
1017                eParent,
1018                "SwV",
1019                bElementIsRequired);
1020        return strText;
1021    }
1022
1023    static String extractHardwareVersion(
1024            final Element eParent,
1025            final boolean bElementIsRequired)
1026            throws RepresentationException
1027    {
1028        String strText = extractElementText(
1029                eParent,
1030                "HwV",
1031                bElementIsRequired);
1032        return strText;
1033    }
1034
1035    static String extractDevID(
1036            final Element eParent,
1037            final boolean bElementIsRequired)
1038            throws RepresentationException
1039    {
1040        String strText = extractElementText(
1041                eParent,
1042                "DevID",
1043                bElementIsRequired);
1044        return strText;
1045    }
1046
1047
1048    static String extractDevTyp(
1049            final Element eParent,
1050            final boolean bElementIsRequired)
1051            throws RepresentationException
1052    {
1053        String strText = extractElementText(
1054                eParent,
1055                "DevTyp",
1056                bElementIsRequired);
1057        return strText;
1058    }
1059
1060    static ContentTypeInfo[] extractTxs(
1061            final Element eParent,
1062            final boolean bElementIsRequired)
1063            throws RepresentationException
1064    {
1065
1066        Vector vTxs = new Vector();
1067        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
1068        {
1069            Element e = (Element)enum.nextElement();
1070            final String strName = e.getName();
1071            if (strName.equals("Tx"))
1072            {
1073                vTxs.addElement(new ContentTypeInfo(e));
1074            }
1075        }
1076
1077        if ((bElementIsRequired == true)
1078                && (vTxs.size() == 0))
1079        {
1080            throw new RepresentationException("todo  : exception in Util.extractTxs");
1081        }
1082
1083        /* not compatible with j2me
1084            ContentTypeInfo[] aTxs = new ContentTypeInfo[vTxs.size()];
1085            return (ContentTypeInfo[]) vTxs.toArray((Object[]) aTxs);
1086        */
1087        int size = vTxs.size();
1088        ContentTypeInfo[] aTxs = new ContentTypeInfo[size];
1089        for (int i=0; i < size; i++) {
1090            aTxs[i] = (ContentTypeInfo)vTxs.elementAt(i);
1091        }
1092        return aTxs;
1093
1094    }
1095
1096    static ContentTypeInfo extractTxPref(
1097            final Element eParent,
1098            final boolean bElementIsRequired)
1099            throws RepresentationException
1100    {
1101        Element eTxPref = extractElement(
1102                eParent,
1103                "Tx-Pref",
1104                bElementIsRequired);
1105        return new ContentTypeInfo(eTxPref);
1106    }
1107
1108    static ContentTypeInfo[] extractRxs(
1109            final Element eParent,
1110            final boolean bElementIsRequired)
1111            throws RepresentationException
1112    {
1113        Vector vRxs = new Vector();
1114        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
1115        {
1116            Element e = (Element)enum.nextElement();
1117            final String strName = e.getName();
1118            if (strName.equals("Rx"))
1119            {
1120                vRxs.addElement(new ContentTypeInfo(e));
1121            }
1122        }
1123
1124        if ((bElementIsRequired == true)
1125                && (vRxs.size() == 0))
1126        {
1127            throw new RepresentationException("todo  : exception in Util.extractRxs");
1128        }
1129
1130        /* not compatible with j2me
1131            ContentTypeInfo[] aRxs = new ContentTypeInfo[vRxs.size()];
1132            return (ContentTypeInfo[]) vRxs.toArray((Object[]) aRxs);
1133        */
1134        int size = vRxs.size();
1135        ContentTypeInfo[] aRxs = new ContentTypeInfo[size];
1136        for (int i=0; i < size; i++) {
1137            aRxs[i] = (ContentTypeInfo)vRxs.elementAt(i);
1138        }
1139        return aRxs;
1140    }
1141
1142    static ContentTypeInfo extractRxPref(
1143            final Element eParent,
1144            final boolean bElementIsRequired)
1145            throws RepresentationException
1146    {
1147        Element eRxPref = extractElement(
1148                eParent,
1149                "Rx-Pref",
1150                bElementIsRequired);
1151        return new ContentTypeInfo(eRxPref);
1152    }
1153
1154    static DataStoreMemoryInfo extractDSMem(
1155            final Element eParent,
1156            final boolean bElementIsRequired)
1157            throws RepresentationException
1158    {
1159        Element eDSMem = extractElement(
1160                eParent,
1161                "DSMem",
1162                bElementIsRequired);
1163        
1164        //
1165        // If the element is not required and not in the eParent subtree, 
1166        // extractElement returns null
1167        //
1168        return (eDSMem == null) ? null : new DataStoreMemoryInfo(eDSMem);
1169    }
1170
1171    static SyncCapabilities extractSyncCap(
1172            final Element eParent,
1173            final boolean bElementIsRequired)
1174            throws RepresentationException
1175    {
1176        Element eSyncCap = extractElement(
1177                eParent,
1178                "SyncCap",
1179                bElementIsRequired);
1180        return new SyncCapabilities(eSyncCap);
1181    }
1182
1183    static SyncAnchor extractAnchor(
1184            final Element eParent,
1185            final boolean bElementIsRequired)
1186            throws RepresentationException
1187    {
1188        Element eAnchor = extractElement(
1189                eParent,
1190                "Anchor",
1191                bElementIsRequired,
1192                Constants.NAMESPACE_METINF);
1193        if (eAnchor == null)
1194        {
1195            return null;
1196        }
1197        return new SyncAnchor(eAnchor);
1198    }
1199
1200    static String extractLast(
1201            final Element eParent,
1202            final boolean bElementIsRequired)
1203            throws RepresentationException
1204    {
1205        String strText = extractElementText(
1206                eParent,
1207                "Last",
1208                bElementIsRequired);
1209        if (strText == null)
1210        {
1211            return null;
1212        }
1213        return strText;
1214    }
1215
1216    static String extractNext(
1217            final Element eParent,
1218            final boolean bElementIsRequired)
1219            throws RepresentationException
1220    {
1221        String strText = extractElementText(
1222                eParent,
1223                "Next",
1224                bElementIsRequired);
1225        if (strText == null)
1226        {
1227            return null;
1228        }
1229        return strText;
1230    }
1231
1232    static long extractMaxGUIDSize(
1233            final Element eParent,
1234            final boolean bElementIsRequired)
1235            throws RepresentationException
1236    {
1237        return extract_long(eParent,
1238                "MaxGUIDSize",
1239                bElementIsRequired,
1240                -1);
1241    }
1242
1243    static long extractSize(
1244            final Element eParent,
1245            final boolean bElementIsRequired)
1246            throws RepresentationException
1247    {
1248        return extract_long(eParent,
1249                "Size",
1250                bElementIsRequired,
1251                -1);
1252    }
1253
1254    static long extractMaxMsgSize(
1255            final Element eParent,
1256            final boolean bElementIsRequired)
1257            throws RepresentationException
1258    {
1259        return extract_long(eParent,
1260                "MaxMsgSize",
1261                bElementIsRequired,
1262                -1);
1263    }
1264
1265    static long extractMaxMem(
1266            final Element eParent,
1267            final boolean bElementIsRequired)
1268            throws RepresentationException
1269    {
1270        return extract_long(eParent,
1271                "MaxMem",
1272                bElementIsRequired,
1273                -1);
1274    }
1275
1276    static long extractMaxID(
1277            final Element eParent,
1278            final boolean bElementIsRequired)
1279            throws RepresentationException
1280    {
1281        return extract_long(eParent,
1282                "MaxID",
1283                bElementIsRequired,
1284                -1);
1285    }
1286
1287    private static long extract_long(
1288            final Element eParent,
1289            final String strElementName,
1290            final boolean bElementIsRequired,
1291            final long lDefaultValue)
1292            throws RepresentationException
1293    {
1294        long lResult = 0L;
1295
1296        try
1297        {
1298            String strText = extractElementText(
1299                    eParent,
1300                    strElementName,
1301                    bElementIsRequired);
1302            lResult = (strText == null) 
1303                        ? lDefaultValue 
1304                        : Long.parseLong(strText);
1305        }
1306        catch (Exception ex)
1307        {
1308            // todo : change exception handling?
1309            throw new RepresentationException("todo");
1310        }
1311
1312        return lResult;
1313    }
1314
1315
1316    static String extractDisplayName(
1317            final Element eParent,
1318            final boolean bElementIsRequired)
1319            throws RepresentationException
1320    {
1321        String strText = extractElementText(
1322                eParent,
1323                "DisplayName",
1324                bElementIsRequired);
1325        return strText;
1326    }
1327
1328    static String extractXNam(
1329            final Element eParent,
1330            final boolean bElementIsRequired)
1331            throws RepresentationException
1332    {
1333        String strText = extractElementText(
1334                eParent,
1335                "XNam",
1336                bElementIsRequired);
1337        return strText;
1338    }
1339
1340    static String[] extractXVals(
1341            final Element eParent,
1342            final boolean bElementIsRequired)
1343            throws RepresentationException
1344    {
1345        Vector vXVals = new Vector();
1346        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
1347        {
1348            Element e = (Element)enum.nextElement();
1349            final String strName = e.getName();
1350            if (strName.equals("XVal"))
1351            {
1352                String strText = e.getText();
1353                vXVals.addElement(strText);
1354            }
1355        }
1356
1357        if ((bElementIsRequired == true)
1358                && (vXVals.size() == 0))
1359        {
1360            throw new RepresentationException("todo  : msg here");
1361        }
1362
1363        /* not compatible with j2me
1364            String[] aXVals = new String[vXVals.size()];
1365            return (String[]) vXVals.toArray((Object[]) aXVals);
1366        */
1367        int size = vXVals.size();
1368        String[] aXVals = new String[size];
1369        for (int i=0; i < size; i++) {
1370            aXVals[i] = (String)vXVals.elementAt(i);
1371        }
1372        return aXVals;
1373    }
1374
1375    static DataStore[] extractDataStores(
1376            final Element eParent,
1377            final boolean bElementIsRequired)
1378            throws RepresentationException
1379    {
1380        Vector vDataStores = new Vector();
1381        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
1382        {
1383            Element e = (Element)enum.nextElement();
1384            final String strName = e.getName();
1385            if (strName.equals("DataStore"))
1386            {
1387                vDataStores.addElement(new DataStore(e));
1388            }
1389        }
1390
1391        if ((bElementIsRequired == true)
1392                && (vDataStores.size() == 0))
1393        {
1394            throw new RepresentationException("todo : msg here");
1395        }
1396
1397        /* not compatible with j2me
1398            DataStore[] aDataStores = new DataStore[vDataStores.size()];
1399            return (DataStore[]) vDataStores.toArray((Object[]) aDataStores);
1400        */
1401        int size = vDataStores.size();
1402        DataStore[] aDataStores = new DataStore[size];
1403        for (int i=0; i < size; i++) {
1404            aDataStores[i] = (DataStore)vDataStores.elementAt(i);
1405        }
1406        return aDataStores;
1407    }
1408
1409    static ExperimentalMetaInfo[] extractEMIs(
1410            final Element eParent,
1411            final boolean bElementIsRequired)
1412            throws RepresentationException
1413    {
1414        Vector vEMIs = new Vector();
1415        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)        {
1416            Element e = (Element)enum.nextElement();
1417            final String strName = e.getName();
1418            if (strName.equals("EMI"))
1419            {
1420                vEMIs.addElement(new ExperimentalMetaInfo(e));
1421            }
1422        }
1423
1424        if ((bElementIsRequired == true)
1425                && (vEMIs.size() == 0))
1426        {
1427            throw new RepresentationException("<EMI> element is missing");
1428        }
1429
1430        /* not compatible with j2me
1431            ExperimentalMetaInfo[] aEMIs = new ExperimentalMetaInfo[vEMIs.size()];
1432            return (ExperimentalMetaInfo[]) vEMIs.toArray((Object[]) aEMIs);
1433        */
1434        int size = vEMIs.size();
1435        ExperimentalMetaInfo[] aEMIs = new ExperimentalMetaInfo[size];
1436        for (int i=0; i < size; i++) {
1437            aEMIs[i] = (ExperimentalMetaInfo)vEMIs.elementAt(i);
1438        }
1439        return aEMIs;
1440    }
1441
1442    static ContentTypeCapability[] extractContentTypeCapabilities(
1443            final Element eParent,
1444            final boolean bElementIsRequired)
1445            throws RepresentationException
1446    {
1447        // todo : real code here
1448        ContentTypeProperty[] props = new ContentTypeProperty[1];
1449        props[0] = new ContentTypeProperty(
1450                null,
1451                null,
1452                null,
1453                null);
1454        ContentTypeCapabilityItem[] actci = new ContentTypeCapabilityItem[1];
1455        actci[0] = new ContentTypeCapabilityItem(
1456                "text/plain",
1457                props);
1458        ContentTypeCapability[] result = new ContentTypeCapability[1];
1459        result[0] = new ContentTypeCapability(actci);
1460        return result;
1461    }
1462
1463    static SyncType[] extractSyncTypes(
1464            final Element eParent,
1465            final boolean bElementIsRequired)
1466            throws RepresentationException
1467    {
1468        Vector vSyncTypes = new Vector();
1469        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
1470        {
1471            Element e = (Element)enum.nextElement();
1472            final String strName = e.getName();
1473            if (strName.equals("SyncType"))
1474            {
1475                try
1476                {
1477                    vSyncTypes.addElement(SyncType.getInstance(
1478                                            Integer.parseInt(e.getText())));
1479                }
1480                catch (Exception ex)
1481                {
1482                    throw new RepresentationException(
1483                            "todo : message here");
1484                }
1485            }
1486        }
1487
1488        /* not compatible with j2me
1489        SyncType[] aSyncTypes = new SyncType[vSyncTypes.size()];
1490        return (SyncType[]) vSyncTypes.toArray((Object[]) aSyncTypes);
1491        */
1492        int size = vSyncTypes.size();
1493        SyncType[] aSyncTypes = new SyncType[size];
1494        for (int i=0; i < size; i++) {
1495            aSyncTypes[i] = (SyncType)vSyncTypes.elementAt(i);
1496        }
1497        return aSyncTypes;
1498    }
1499
1500    static Extension[] extractExtensions(
1501            final Element eParent,
1502            final boolean bElementIsRequired)
1503            throws RepresentationException
1504    {
1505        Vector vExtensions = new Vector();
1506        for (Enumeration enum = eParent.getChildren(); enum.hasMoreElements();)
1507        {
1508            Element e = (Element)enum.nextElement();
1509            final String strName = e.getName();
1510            if (strName.equals("Ext"))
1511            {
1512                vExtensions.addElement(new Extension(e));
1513            }
1514        }
1515       
1516        int size = vExtensions.size();
1517        Extension[] aExtensions = new Extension[size];
1518        for (int i=0; i < size; i++) {
1519            aExtensions[i] = (Extension)vExtensions.elementAt(i);
1520        }
1521        return aExtensions;
1522    }
1523
1524    static String extractCTType(
1525            final Element eParent,
1526            final boolean bElementIsRequired)
1527            throws RepresentationException
1528    {
1529        String strText = extractElementText(
1530                eParent,
1531                "CTType",
1532                bElementIsRequired);
1533        return strText;
1534    }
1535
1536    static String extractVerCT(
1537            final Element eParent,
1538            final boolean bElementIsRequired)
1539            throws RepresentationException
1540    {
1541        String strText = extractElementText(
1542                eParent,
1543                "VerCT",
1544                bElementIsRequired);
1545        return strText;
1546    }
1547
1548    static String extractMark(
1549            final Element eParent,
1550            final boolean bElementIsRequired)
1551            throws RepresentationException
1552    {
1553        String strText = extractElementText(
1554                eParent,
1555                "Mark",
1556                bElementIsRequired);
1557        return strText;
1558    }
1559
1560    static String extractVersion(
1561            final Element eParent,
1562            final boolean bElementIsRequired)
1563            throws RepresentationException
1564    {
1565        String strText = extractElementText(
1566                eParent,
1567                "Version",
1568                bElementIsRequired);
1569        return strText;
1570    }
1571
1572
1573    static MemoryInfo extractMem(
1574            final Element eParent,
1575            final boolean bElementIsRequired)
1576            throws RepresentationException
1577    {
1578        Element eMem = extractElement(
1579                eParent,
1580                "Mem",
1581                bElementIsRequired);
1582        if (eMem == null)
1583        {
1584            return null;
1585        }
1586        return new MemoryInfo(eMem);
1587    }
1588
1589    static NextNonce extractNextNonce(
1590            final Element eParent,
1591            final boolean bElementIsRequired)
1592            throws RepresentationException
1593    {
1594
1595        Element eNextNonce = extractElement(
1596                eParent,
1597                "NextNonce",
1598                bElementIsRequired);
1599        if ((eNextNonce == null)
1600                && (bElementIsRequired == false))
1601        {
1602            return null;
1603        }
1604        String strText = eNextNonce.getText();
1605        // todo : throw exception if strText is null?
1606        // todo : rethink 2nd parameter:
1607        return new NextNonce(strText, true);
1608    }
1609
1610    static ContentTypeCapabilityItem[] extractContentTypeCapabilityItems(
1611            final Element eParent,
1612            final boolean bElementIsRequired)
1613            throws RepresentationException
1614    {
1615        // todo : code here
1616        return new ContentTypeCapabilityItem[1];
1617    }
1618
1619    static long extractFreeMem(
1620            final Element eParent,
1621            final boolean bElementIsRequired)
1622            throws RepresentationException
1623    {
1624        return extract_long(eParent,
1625                "FreeMem",
1626                bElementIsRequired,
1627                -1);
1628    }
1629
1630    static long extractFreeID(
1631            final Element eParent,
1632            final boolean bElementIsRequired)
1633            throws RepresentationException
1634    {
1635        return extract_long(eParent,
1636                "FreeID",
1637                bElementIsRequired,
1638                -1);
1639    }
1640
1641    static void verifyLengthGreaterThanZero(final String s)
1642    {
1643        if (s.length() < 1)
1644        {
1645            throw new IllegalArgumentException(
1646                    "String length is less than 1");
1647        }
1648    }
1649
1650    static void verifyNonNull(final Object obj)
1651    {
1652        if (obj == null)
1653        {
1654            throw new NullPointerException(
1655                    obj.getClass().toString()
1656                    + " parameter is null");
1657        }
1658    }
1659    
1660    /** convert a xml element and all its children to string and removes the
1661     *  surrounding tag
1662     * 
1663     *  @param e the element to convert
1664     *
1665     *  @return the element content as String
1666     *
1667     *  @author david.buchmann at unifr.ch
1668     */
1669    public static String contentAsString(final Element e) {
1670        String s = toString(e);
1671        s=s.substring(s.indexOf('>')+1); //remove leading tag
1672        return s.substring(0, s.lastIndexOf('<')); //remove end tag
1673    }
1674    
1675    /** convert a xml element and all its children to string.
1676     *
1677     *  @author david.buchmann at unifr.ch
1678     */
1679    public static String toString(final Element e) {
1680        if (e == null) {
1681            throw new NullPointerException("XML element is null");
1682        }
1683
1684        return adaptee.toString(e);
1685    }
1686
1687    /**
1688     * Determines if a string is empty. A string is empty when null or is trimmed
1689     * value length is 0.
1690     *
1691     * @param s the string to be checked
1692     *
1693     * @return true if the string is empty, false otherwise
1694     */
1695    static public boolean isEmpty(String s) {
1696        return ((s == null) || (s.trim().length() == 0));
1697    }
1698
1699}