KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > XMLBinding


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.xml;
10
11 import java.io.IOException JavaDoc;
12 import javolution.Javolution;
13 import javolution.JavolutionError;
14 import javolution.lang.ClassInitializer;
15 import javolution.lang.Reflection;
16 import javolution.lang.Reusable;
17 import javolution.text.Appendable;
18 import javolution.text.CharArray;
19 import javolution.text.Text;
20 import javolution.util.FastMap;
21 import javolution.util.FastMap.Entry;
22 import javolution.xml.stream.XMLStreamException;
23 import javolution.xml.stream.XMLStreamReaderImpl;
24 import javolution.xml.stream.XMLStreamWriterImpl;
25 import j2me.lang.CharSequence;
26 import j2me.util.Collection;
27 import j2me.util.Iterator;
28 import j2me.util.Map;
29
30 /**
31  * <p> This class represents the binding between Java classes and
32  * their XML representation ({@link XMLFormat}); the binding may be shared
33  * among multiple {@link XMLObjectReader}/ {@link XMLObjectWriter}
34  * instances (thread-safe).</p>
35  *
36  * <p> Custom XML bindings can also be used to alias class names and
37  * ensure that the XML representation is:<ul>
38  * <li> Impervious to obfuscation.</li>
39  * <li> Unnaffected by any class refactoring.</li>
40  * <li> Can be mapped to multiple implementations. For example:[code]
41  *
42  * // Creates a binding to serialize Swing components into high-level XML
43  * // and deserialize the same XML into SWT components.
44  * XMLBinding swingBinding = new XMLBinding();
45  * swingBinding.setAlias(javax.swing.JButton.class, "Button");
46  * swingBinding.setAlias(javax.swing.JTable.class, "Table");
47  * ...
48  * XMLBinding swtBinding = new XMLBinding();
49  * swtBinding.setAlias(org.eclipse.swt.widgets.Button.class, "Button");
50  * swtBinding.setAlias(org.eclipse.swt.widgets.Table.class, "Table");
51  * ...
52  *
53  * // Writes Swing Desktop to XML.
54  * XMLObjectWriter writer = new XMLObjectWriter().setBinding(swingBinding);
55  * writer.setOutput(new FileOutputStream("C:/desktop.xml"));
56  * writer.write(swingDesktop, "Desktop", SwingDesktop.class);
57  * writer.close();
58  *
59  * // Reads back high-level XML to a SWT implementation!
60  * XMLObjectReader reader = new XMLObjectReader().setXMLBinding(swtBinding);
61  * reader.setInput(new FileInputStream("C:/desktop.xml"));
62  * SWTDesktop swtDesktop = reader.read("Desktop", SWTDesktop.class);
63  * reader.close();
64  * [/code]</li>
65  * </ul></p>
66  *
67  * <p> More advanced bindings can also be created through sub-classing.[code]
68  *
69  * // XML binding using reflection.
70  * public ReflectionBinding extends XMLBinding {
71  * public <T> XMLFormat<T> getFormat(Class<T> cls) {
72  * Field[] fields = clt.getDeclaredFields();
73  * return new XMLReflectionFormat<T>(fields);
74  * }
75  * }
76  *
77  * // XML binding read from DTD input source.
78  * public DTDBinding extends XMLBinding {
79  * public DTDBinding(InputStream dtd) {
80  * ...
81  * }
82  * }
83  *
84  * // XML binding overriding statically bounded formats.
85  * public MyBinding extends XMLBinding {
86  * // Non-static formats use the XMLFormat no-arg constructor.
87  * XMLFormat<String> _myStringFormat = new XMLFormat<String>() {...}
88  * XMLFormat<Collection> _myCollectionFormat = new XMLFormat<Collection>() {...}
89  * public <T> XMLFormat<T> getFormat(Class<T> cls) {
90  * if (String.class.equals(cls))
91  * return _myStringFormat;
92  * if (Collection.class.isAssignableFrom(cls))
93  * return _myCollectionFormat;
94  * return super.getFormat(cls);
95  * }
96  * }
97  * [/code]
98  *
99  * <p> The default XML binding supports all static XML formats
100  * (those created using {@link XMLFormat#XMLFormat(Class)}
101  * constructor) which also include formats for the following
102  * non-Javolution types:<ul>
103  * <li><code>java.lang.Object</code> (empty element)</li>
104  * <li><code>java.lang.Class</code></li>
105  * <li><code>java.lang.String</code></li>
106  * <li><code>java.lang.Appendable</code></li>
107  * <li><code>java.util.Collection</code></li>
108  * <li><code>java.util.Map</code></li>
109  * <li><code>java.lang.Object[]</code></li>
110  * <li> all primitive types wrappers (e.g.
111  * <code>Boolean, Integer ...</code>)</li>
112  * </ul></p>
113  *
114  * <p> Finally, the XML binding itself defines a default {@link #XML XML} format
115  * allowing saving/retrieving of the binding from XML files.</p>
116  *
117  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
118  * @version 4.0, September 4, 2006
119  */

120 public class XMLBinding implements Reusable {
121
122     /**
123      * Holds the static mapping format.
124      */

125     private static FastMap STATIC_MAPPING = new FastMap().setShared(true);
126
127     /**
128      * Holds the default instance used by readers/writers.
129      */

130     static final XMLBinding DEFAULT = new XMLBinding();
131
132     /**
133      * Holds the XML representation of this binding (class/alias mapping
134      * and class attribute values).
135      */

136     protected static XMLFormat XML = new XMLFormat() {
137
138         public void read(InputElement xml, Object JavaDoc obj)
139                 throws XMLStreamException {
140             XMLBinding binding = (XMLBinding) obj;
141             binding._classAttributeName = xml.getAttribute(
142                     "classAttributeName", (String JavaDoc) null);
143             binding._classAttributeURI = xml.getAttribute("classAttributeURI",
144                     (String JavaDoc) null);
145             FastMap fm = binding._aliasToClass = (FastMap) xml.get(
146                     "AliasToClass", FastMap.class);
147             // Builds reverse mapping automatically.
148
for (Entry e = fm.head(), t = fm.tail(); (e = (Entry) e.getNext()) != t;) {
149                 binding._classToAlias.put(e.getValue(), e.getKey());
150             }
151         }
152
153         public void write(Object JavaDoc obj, OutputElement xml)
154                 throws XMLStreamException {
155             XMLBinding binding = (XMLBinding) obj;
156             xml.setAttribute("classAttributeName", binding._classAttributeName);
157             xml.setAttribute("classAttributeURI", binding._classAttributeURI);
158             xml.add(binding._aliasToClass, "AliasToClass", FastMap.class);
159         }
160     };
161
162     /**
163      * Holds the local name of the class attribute.
164      */

165     private String JavaDoc _classAttributeName = "class";
166
167     /**
168      * Holds the URI of the class attribute if any.
169      */

170     private String JavaDoc _classAttributeURI = null;
171
172     /**
173      * Holds the class to name mapping.
174      */

175     private FastMap _classToAlias = new FastMap();
176
177     /**
178      * Holds the name to class mapping.
179      */

180     private FastMap _aliasToClass = new FastMap();
181
182     /**
183      * Default constructor.
184      */

185     public XMLBinding() {
186     }
187
188     /**
189      * Sets the alias of the specified class. Classes may have multiple
190      * aliases but any given alias maps to a single class.
191      *
192      * @param cls the class being aliased.
193      * @param alias the alias for the specified class.
194      */

195     public void setAlias(Class JavaDoc cls, String JavaDoc alias) {
196         Class JavaDoc prevForAlias = (Class JavaDoc) _aliasToClass.put(alias, cls);
197         // Removes any previous class to given alias mapping.
198
if (prevForAlias != null) {
199             _classToAlias.put(prevForAlias, null);
200         }
201         _classToAlias.put(cls, alias);
202     }
203
204     /**
205      * Sets the name of the attribute holding the classname/alias
206      * (by default<code>"class"</code>).
207      * If the local name is <code>null</code> then the class attribute
208      * is never read/written (which may prevent unmarshalling).
209      *
210      * @param name the local name of the attribute or <code>null</code>.
211      */

212     public void setClassAttribute(String JavaDoc name) {
213         _classAttributeName = name;
214         _classAttributeURI = null;
215     }
216
217     /**
218      * Sets the local name and namespace URI of the attribute holding the
219      * classname/alias (by default<code>"class"</code> and no namespace URI).
220      * If the local name is <code>null</code> then the class attribute
221      * is never read/written (which may prevent unmarshalling).
222      *
223      * @param localName the local name of the attribute or <code>null</code>.
224      * @param uri the URI of the attribute or <code>null</code> if the
225      * class attribute has no namespace URI.
226      */

227     public void setClassAttribute(String JavaDoc localName, String JavaDoc uri) {
228         _classAttributeName = localName;
229         _classAttributeURI = uri;
230     }
231
232     /**
233      * Returns the XML format for the specified class/interface.
234      * The default implementation returns the most
235      * specialized static format compatible with the specified class.
236      *
237      * @param cls the class for which the XML format is returned.
238      * @return the XML format for the specified class.
239      */

240     public/*<T>*/XMLFormat/*<T>*/getFormat(Class JavaDoc/*<T>*/cls) {
241         Object JavaDoc xmlFormat = STATIC_MAPPING.get(cls);
242         return (xmlFormat != null) ? (XMLFormat) xmlFormat : searchFormat(cls);
243     }
244
245     private XMLFormat searchFormat(Class JavaDoc cls) {
246         // First initialize class to ensure static format creation.
247
ClassInitializer.initialize(cls);
248
249         // Then search best match.
250
XMLFormat bestMatchFormat = null;
251         for (int i = 0, j = XMLFormat._ClassInstancesLength; i < j; i++) {
252             XMLFormat xmlFormat = XMLFormat._ClassInstances[i];
253             if (xmlFormat._class.isAssignableFrom(cls)) { // Compatible.
254
if ((bestMatchFormat == null)
255                         || (bestMatchFormat._class
256                                 .isAssignableFrom(xmlFormat._class))) {
257                     bestMatchFormat = xmlFormat;
258                 }
259             }
260         }
261         if (bestMatchFormat == null)
262             throw new JavolutionError("Cannot find format for " + cls);
263
264         STATIC_MAPPING.put(cls, bestMatchFormat);
265         return bestMatchFormat;
266     }
267
268     /**
269      * Returns the name identifying the specified class (value of
270      * {@link #setClassAttribute class attribute} during marshalling).
271      * The default implementation returns the class alias (if any) or
272      * <code>cls.getName()</code>.
273      *
274      * @param cls the class for which a name identifier is returned.
275      * @return the alias or name for the class.
276      */

277     protected String JavaDoc getName(Class JavaDoc cls) {
278         String JavaDoc alias = (String JavaDoc) _classToAlias.get(cls);
279         return (alias != null) ? alias : cls.getName();
280     }
281
282     /**
283      * Returns the class identified by the specified name (value of
284      * {@link #setClassAttribute class attribute} during unmarshalling).
285      * The default implementation returns an aliased class or
286      * <code>Class.forName(name.toString())</code>.
287      *
288      * @param name the class name identifier.
289      * @return the class for the specified name.
290      * @throws ClassNotFoundException
291      */

292     protected Class JavaDoc getClass(CharArray name) throws ClassNotFoundException JavaDoc {
293         Class JavaDoc cls = (Class JavaDoc) _aliasToClass.get(name);
294         return (cls != null) ? cls : Reflection.getClass(name);
295     }
296
297     /**
298      * Returns the local name identifying the specified class (the element local
299      * name during marshalling). The default implementation returns
300      * <code>this.getName(cls)</code>.
301      *
302      * @param cls the class for which the local name is returned.
303      * @return the local name of the specified class.
304      */

305     protected String JavaDoc getLocalName(Class JavaDoc cls) {
306         return this.getName(cls);
307     }
308
309     /**
310      * Returns the URI identifying the specified class (the element namespace
311      * URI during marshalling). The default implementation returns
312      * <code>null</code> (no namespace URI).
313      *
314      * @param cls the class for which the namespace URI is returned.
315      * @return the URI for the specified class or <code>null</code> if none.
316      */

317     protected String JavaDoc getURI(Class JavaDoc cls) {
318         return null;
319     }
320
321     /**
322      * Returns the class identified by the specified local name and URI
323      * (the element local name and URI during unmarshalling). The default
324      * implementation returns <code>getClass(localName)</code>.
325      *
326      * @param localName the class local name identifier.
327      * @param uri the class URI identifier (can be <code>null</code>).
328      * @return the corresponding class.
329      * @throws ClassNotFoundException
330      */

331     protected Class JavaDoc getClass(CharArray localName, CharArray uri)
332             throws ClassNotFoundException JavaDoc {
333         return getClass(localName);
334     }
335
336     // Implements Reusable.
337
public void reset() {
338         _aliasToClass.reset();
339         _classToAlias.reset();
340     }
341
342     /**
343      * Reads the class from the class attribute of current XML element.
344      *
345      * @param reader the reader to be used.
346      * @return the corresponding class.
347      * @throws XMLStreamException
348      */

349     final Class JavaDoc readClassAttribute(XMLStreamReaderImpl reader)
350             throws XMLStreamException {
351         if (_classAttributeName == null)
352             throw new XMLStreamException(
353                     "Binding has no class attribute defined, cannot retrieve class");
354         CharArray className = reader.getAttributeValue(
355                 toCsq(_classAttributeURI), toCsq(_classAttributeName));
356         if (className == null)
357             throw new XMLStreamException(
358                     "Cannot retrieve class (class attribute not found)");
359         try {
360             return getClass(className);
361         } catch (ClassNotFoundException JavaDoc e) {
362             throw new XMLStreamException(e);
363         }
364     }
365
366     /**
367      * Writes the class to the class attribute of the current XML element.
368      *
369      * @param writer the writer to be used.
370      * @param cls the class being written.
371      * @throws XMLStreamException
372      */

373     final void writeClassAttribute(XMLStreamWriterImpl writer, Class JavaDoc cls)
374             throws XMLStreamException {
375
376         if (_classAttributeName != null) {
377             String JavaDoc value = getName(cls);
378             if (_classAttributeURI == null) {
379                 writer.writeAttribute(toCsq(_classAttributeName), toCsq(value));
380             } else {
381                 writer.writeAttribute(toCsq(_classAttributeURI),
382                         toCsq(_classAttributeName), toCsq(value));
383             }
384         }
385     }
386
387     /**
388      * Holds the static XML format for <code>Object.class</code> instances
389      * (default format when a more specialized format does not exist).
390      * The XML representation consists of an empty element with no attribute.
391      */

392     static final XMLFormat/*<Object>*/OBJECT_XML = new XMLFormat(new Object JavaDoc()
393             .getClass()) {
394
395         public void read(javolution.xml.XMLFormat.InputElement xml, Object JavaDoc obj) {
396             // Do nothing.
397
}
398
399         public void write(Object JavaDoc obj, javolution.xml.XMLFormat.OutputElement xml) {
400             // Do nothing
401
}
402     };
403
404     /**
405      * Holds the default XML representation for <code>java.lang.Class</code>
406      * instances. This representation consists of a <code>"name"</code>
407      * attribute holding the class name.
408      */

409     static final XMLFormat/*<Class>*/CLASS_XML = new XMLFormat("".getClass()
410             .getClass()) {
411
412         public boolean isReferenceable() {
413             return false; // Always by value (immutable).
414
}
415
416         public Object JavaDoc newInstance(Class JavaDoc cls,
417                 javolution.xml.XMLFormat.InputElement xml)
418                 throws XMLStreamException {
419             CharArray name = xml.getAttribute("name");
420             if (name == null)
421                 throw new XMLStreamException("Attribute 'name' missing");
422             Class JavaDoc clazz;
423             try {
424                 clazz = Reflection.getClass(name);
425             } catch (ClassNotFoundException JavaDoc e) {
426                 throw new XMLStreamException(e);
427             }
428             return clazz;
429         }
430
431         public void read(javolution.xml.XMLFormat.InputElement xml, Object JavaDoc obj)
432                 throws XMLStreamException {
433             // Do nothing.
434
}
435
436         public void write(Object JavaDoc obj, javolution.xml.XMLFormat.OutputElement xml)
437                 throws XMLStreamException {
438             xml.setAttribute("name", ((Class JavaDoc) obj).getName());
439         }
440     };
441
442     /**
443      * Holds the default XML representation for <code>java.lang.String</code>
444      * instances. This representation consists of a <code>"value"</code>
445      * attribute holding the string.
446      */

447     static final XMLFormat/*<String>*/STRING_XML = new XMLFormat("".getClass()) {
448
449         public Object JavaDoc newInstance(Class JavaDoc cls,
450                 javolution.xml.XMLFormat.InputElement xml)
451                 throws XMLStreamException {
452             return xml.getAttribute("value", "");
453         }
454
455         public void read(InputElement xml, Object JavaDoc obj)
456                 throws XMLStreamException {
457             // Do nothing.
458
}
459
460         public void write(Object JavaDoc obj, OutputElement xml)
461                 throws XMLStreamException {
462             xml.setAttribute("value", (String JavaDoc) obj);
463         }
464     };
465
466     /**
467      * Holds the default XML representation for <code>java.lang.Appendable</code>
468      * instances. This representation consists of a <code>"value"</code> attribute
469      * holding the characters.
470      */

471     static final XMLFormat/*<Appendable>*/APPENDABLE_XML = new XMLFormat(
472             Javolution.j2meGetClass("javolution.text.Appendable")) {
473
474         public void read(InputElement xml, Object JavaDoc obj)
475                 throws XMLStreamException {
476             CharSequence JavaDoc csq = xml.getAttribute("value");
477             if (csq != null) {
478                 try {
479                     ((Appendable JavaDoc) obj).append(csq);
480                 } catch (IOException JavaDoc e) {
481                     throw new XMLStreamException(e);
482                 }
483             }
484         }
485
486         public void write(Object JavaDoc obj, OutputElement xml)
487                 throws XMLStreamException {
488             if (obj instanceof CharSequence JavaDoc) {
489                 xml.setAttribute("value", (CharSequence JavaDoc) obj);
490             } else {
491                 xml.setAttribute("value", obj.toString());
492             }
493         }
494     };
495
496     /**
497      * Holds the default XML representation for <code>java.util.Collection</code>
498      * instances. This representation consists of nested XML elements one for
499      * each element of the collection. The elements' order is defined by
500      * the collection iterator order. Collections are deserialized using their
501      * default constructor.
502      */

503     static final XMLFormat/*<Collection>*/COLLECTION_XML = new XMLFormat(
504             Javolution.j2meGetClass("j2me.util.Collection")) {
505
506         public void read(InputElement xml, Object JavaDoc obj)
507                 throws XMLStreamException {
508             Collection collection = (Collection) obj;
509             while (xml.hasNext()) {
510                 collection.add(xml.getNext());
511             }
512         }
513
514         public void write(Object JavaDoc obj, OutputElement xml)
515                 throws XMLStreamException {
516             Collection collection = (Collection) obj;
517             for (Iterator i = collection.iterator(); i.hasNext();) {
518                 xml.add(i.next());
519             }
520         }
521     };
522
523     /**
524      * Holds the default XML representation for <code>java.util.Map</code>
525      * instances. This representation consists of key/value pair as nested
526      * XML elements. For example:[code]
527      * <javolution.util.FastMap>
528      * <Key class="java.lang.String" value="ONE"/>
529      * <Value class="java.lang.Integer" value="1"/>
530      * <Key class="java.lang.String" value="TWO"/>
531      * <Value class="java.lang.Integer" value="2"/>
532      * <Key class="java.lang.String" value="THREE"/>
533      * <Value class="java.lang.Integer" value="3"/>
534      * </javolution.util.FastMap>[/code]
535      *
536      * The elements' order is defined by the map's entries iterator order.
537      * Maps are deserialized using their default constructor.
538      */

539     static final XMLFormat/*<Map>*/MAP_XML = new XMLFormat(Javolution
540             .j2meGetClass("j2me.util.Map")) {
541
542         public void read(InputElement xml, Object JavaDoc obj)
543                 throws XMLStreamException {
544             final Map map = (Map) obj;
545             while (xml.hasNext()) {
546                 Object JavaDoc key = xml.get("Key");
547                 Object JavaDoc value = xml.get("Value");
548                 map.put(key, value);
549             }
550         }
551
552         public void write(Object JavaDoc obj, OutputElement xml)
553                 throws XMLStreamException {
554             final Map map = (Map) obj;
555             for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
556                 Map.Entry entry = (Map.Entry) it.next();
557                 xml.add(entry.getKey(), "Key");
558                 xml.add(entry.getValue(), "Value");
559             }
560         }
561     };
562
563     /**
564      * Holds the default XML representation for <code>java.lang.Object[]</code>
565      * instances. This representation consists of nested XML elements one for
566      * each element of the array.
567      /*@JVM-1.4+@
568      static final XMLFormat
569      /**/

570     /*<Object[]>*//*@JVM-1.4+@
571      OBJECT_ARRAY_XML = new XMLFormat(
572      new Object[0].getClass()) {
573      
574      public Object newInstance(Class cls, javolution.xml.XMLFormat.InputElement xml) throws XMLStreamException {
575      Class componentType;
576      try {
577      componentType = Reflection.getClass(xml.getAttribute("componentType"));
578      } catch (ClassNotFoundException e) {
579      throw new XMLStreamException(e);
580      }
581      int length = xml.getAttribute("length", 0);
582      return java.lang.reflect.Array.newInstance(componentType, length);
583      }
584
585      public void read(InputElement xml, Object obj) throws XMLStreamException {
586      Object[] array = (Object[]) obj;
587      for (int i=0; i < array.length; i++) {
588      array[i] = xml.getNext();
589      }
590      }
591
592      public void write(Object obj, OutputElement xml) throws XMLStreamException {
593      Object[] array = (Object[]) obj;
594      xml.setAttribute("componentType", array.getClass().getComponentType().getName());
595      xml.setAttribute("length", array.length);
596      for (int i=0; i < array.length; i++) {
597      xml.add(array[i]);
598      }
599      }
600      };
601      /**/

602
603     /**
604      * Holds the default XML representation for <code>java.lang.Boolean</code>.
605      */

606     static final XMLFormat/*<Boolean>*/BOOLEAN_XML = new XMLFormat(
607             new Boolean JavaDoc(true).getClass()) {
608
609         public boolean isReferenceable() {
610             return false; // Always by value (immutable).
611
}
612
613         public Object JavaDoc newInstance(Class JavaDoc cls,
614                 javolution.xml.XMLFormat.InputElement xml)
615                 throws XMLStreamException {
616             return new Boolean JavaDoc(xml.getAttribute("value", false));
617         }
618
619         public void read(InputElement xml, Object JavaDoc obj)
620                 throws XMLStreamException {
621             // Do nothing.
622
}
623
624         public void write(Object JavaDoc obj, OutputElement xml)
625                 throws XMLStreamException {
626             xml.setAttribute("value", ((Boolean JavaDoc) obj).booleanValue());
627         }
628     };
629
630     /**
631      * Holds the default XML representation for <code>java.lang.Byte</code>.
632      */

633     static final XMLFormat/*<Byte>*/BYTE_XML = new XMLFormat(
634             new Byte JavaDoc((byte) 0).getClass()) {
635
636         public boolean isReferenceable() {
637             return false; // Always by value (immutable).
638
}
639
640         public Object JavaDoc newInstance(Class JavaDoc cls,
641                 javolution.xml.XMLFormat.InputElement xml)
642                 throws XMLStreamException {
643             return new Byte JavaDoc((byte) xml.getAttribute("value", 0));
644         }
645
646         public void read(InputElement xml, Object JavaDoc obj)
647                 throws XMLStreamException {
648             // Do nothing.
649
}
650
651         public void write(Object JavaDoc obj, OutputElement xml)
652                 throws XMLStreamException {
653             xml.setAttribute("value", ((Byte JavaDoc) obj).byteValue());
654         }
655     };
656
657     /**
658      * Holds the default XML representation for <code>java.lang.Character</code>.
659      */

660     static final XMLFormat/*<Character>*/CHARACTER_XML = new XMLFormat(
661             new Character JavaDoc(' ').getClass()) {
662
663         public boolean isReferenceable() {
664             return false; // Always by value (immutable).
665
}
666
667         public Object JavaDoc newInstance(Class JavaDoc cls,
668                 javolution.xml.XMLFormat.InputElement xml)
669                 throws XMLStreamException {
670             CharSequence JavaDoc csq = xml.getAttribute("value");
671             if ((csq == null) || (csq.length() != 1))
672                 throw new XMLStreamException(
673                         "Missing or invalid value attribute");
674             return new Character JavaDoc(csq.charAt(0));
675         }
676
677         public void read(InputElement xml, Object JavaDoc obj)
678                 throws XMLStreamException {
679             // Do nothing.
680
}
681
682         public void write(Object JavaDoc obj, OutputElement xml)
683                 throws XMLStreamException {
684             xml.setAttribute("value", Text.valueOf(((Character JavaDoc) obj)
685                     .charValue()));
686         }
687     };
688
689     /**
690      * Holds the default XML representation for <code>java.lang.Short</code>.
691      */

692     static final XMLFormat/*<Short>*/SHORT_XML = new XMLFormat(new Short JavaDoc(
693             (short) 0).getClass()) {
694
695         public boolean isReferenceable() {
696             return false; // Always by value (immutable).
697
}
698
699         public Object JavaDoc newInstance(Class JavaDoc cls,
700                 javolution.xml.XMLFormat.InputElement xml)
701                 throws XMLStreamException {
702             return new Short JavaDoc((short) xml.getAttribute("value", 0));
703         }
704
705         public void read(InputElement xml, Object JavaDoc obj)
706                 throws XMLStreamException {
707             // Do nothing.
708
}
709
710         public void write(Object JavaDoc obj, OutputElement xml)
711                 throws XMLStreamException {
712             xml.setAttribute("value", ((Short JavaDoc) obj).shortValue());
713         }
714     };
715
716     /**
717      * Holds the default XML representation for <code>java.lang.Integer</code>.
718      */

719     static final XMLFormat/*<Integer>*/INTEGER_XML = new XMLFormat(
720             new Integer JavaDoc(0).getClass()) {
721
722         public boolean isReferenceable() {
723             return false; // Always by value (immutable).
724
}
725
726         public Object JavaDoc newInstance(Class JavaDoc cls,
727                 javolution.xml.XMLFormat.InputElement xml)
728                 throws XMLStreamException {
729             return new Integer JavaDoc(xml.getAttribute("value", 0));
730         }
731
732         public void read(InputElement xml, Object JavaDoc obj)
733                 throws XMLStreamException {
734             // Do nothing.
735
}
736
737         public void write(Object JavaDoc obj, OutputElement xml)
738                 throws XMLStreamException {
739             xml.setAttribute("value", ((Integer JavaDoc) obj).intValue());
740         }
741     };
742
743     /**
744      * Holds the default XML representation for <code>java.lang.Long</code>.
745      */

746     static final XMLFormat/*<Long>*/LONG_XML = new XMLFormat(new Long JavaDoc(0)
747             .getClass()) {
748
749         public boolean isReferenceable() {
750             return false; // Always by value (immutable).
751
}
752
753         public Object JavaDoc newInstance(Class JavaDoc cls,
754                 javolution.xml.XMLFormat.InputElement xml)
755                 throws XMLStreamException {
756             return new Long JavaDoc(xml.getAttribute("value", 0L));
757         }
758
759         public void read(InputElement xml, Object JavaDoc obj)
760                 throws XMLStreamException {
761             // Do nothing.
762
}
763
764         public void write(Object JavaDoc obj, OutputElement xml)
765                 throws XMLStreamException {
766             xml.setAttribute("value", ((Long JavaDoc) obj).longValue());
767         }
768     };
769
770     /**
771      * Holds the default XML representation for <code>java.lang.Float</code>.
772      /*@JVM-1.1+@
773      static final XMLFormat
774      /**/

775     /*<Float>*//*@JVM-1.1+@
776      FLOAT_XML = new XMLFormat(new Float(0f).getClass()) {
777      
778      public boolean isReferenceable() {
779      return false; // Always by value (immutable).
780      }
781      
782      public Object newInstance(Class cls, javolution.xml.XMLFormat.InputElement xml) throws XMLStreamException {
783      return new Float(xml.getAttribute("value", 0f));
784      }
785
786      public void read(InputElement xml, Object obj) throws XMLStreamException {
787      // Do nothing.
788      }
789
790      public void write(Object obj, OutputElement xml) throws XMLStreamException {
791      xml.setAttribute("value", ((Float) obj).floatValue());
792      }
793      };
794      /**/

795
796     /**
797      * Holds the default XML representation for <code>java.lang.Double</code>.
798      /*@JVM-1.1+@
799      static final XMLFormat
800      /**/

801     /*<Double>*//*@JVM-1.1+@
802      DOUBLE_XML = new XMLFormat(new Double(0.0).getClass()) {
803      public boolean isReferenceable() {
804      return false; // Always by value (immutable).
805      }
806      
807      public Object newInstance(Class cls, javolution.xml.XMLFormat.InputElement xml) throws XMLStreamException {
808      return new Double(xml.getAttribute("value", 0.0));
809      }
810
811      public void read(InputElement xml, Object obj) throws XMLStreamException {
812      // Do nothing.
813      }
814
815      public void write(Object obj, OutputElement xml) throws XMLStreamException {
816      xml.setAttribute("value", ((Double) obj).doubleValue());
817      }
818      };
819      /**/

820
821     private static CharSequence JavaDoc toCsq/**/(Object JavaDoc str) {
822         return Javolution.j2meToCharSeq(str);
823     }
824
825 }
Popular Tags