Javolution 6.0.0 java
XMLContextImpl.java
Go to the documentation of this file.
1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2012 - 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.internal;
10 
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.Map;
14 
15 import javolution.util.FastMap;
18 import javolution.xml.XMLFormat;
20 
27 @SuppressWarnings("rawtypes")
28 public final class XMLContextImpl extends XMLContext {
29 
30  private final FastMap<Class<?>, XMLFormat<?>> formats = new FastMap<Class<?>, XMLFormat<?>>();
31 
32  @Override
33  protected XMLContext inner() {
34  XMLContextImpl ctx = new XMLContextImpl();
35  ctx.formats.putAll(formats);
36  return ctx;
37  }
38 
39  @SuppressWarnings("unchecked")
40  @Override
41  protected <T> XMLFormat<T> searchFormat(Class<? extends T> type) {
42  XMLFormat xml = formats.get(type);
43  if (xml != null)
44  return xml;
45  DefaultXMLFormat format = type.getAnnotation(DefaultXMLFormat.class);
46  if (format != null) {
47  Class<? extends XMLFormat> formatClass = format.value();
48  try {
49  xml = formatClass.newInstance();
50  synchronized (formats) { // Required since possible concurrent use
51  // (getFormatInContext is not a configuration method).
52  formats.put(type, xml);
53  }
54  return xml;
55  } catch (Throwable ex) {
56  throw new RuntimeException(ex);
57  }
58  }
59  // Check predefined format as last resource.
60  if (Map.class.isAssignableFrom(type))
61  return MAP_XML;
62  if (Collection.class.isAssignableFrom(type))
63  return COLLECTION_XML;
64  return OBJECT_XML;
65  }
66 
67  @Override
68  public <T> void setFormat(Class<? extends T> type, XMLFormat<T> format) {
69  formats.put(type, format);
70  }
71 
73  // PREDEFINED FORMATS //
75 
80  private static final XMLFormat OBJECT_XML = new XMLFormat.Default();
81 
89  private static final XMLFormat COLLECTION_XML = new XMLFormat() {
90 
91  @SuppressWarnings("unchecked")
92  public void read(XMLFormat.InputElement xml, Object obj)
93  throws XMLStreamException {
94  Collection collection = (Collection) obj;
95  while (xml.hasNext()) {
96  collection.add(xml.getNext());
97  }
98  }
99 
100  public void write(Object obj, XMLFormat.OutputElement xml)
101  throws XMLStreamException {
102  Collection collection = (Collection) obj;
103  for (Iterator i = collection.iterator(); i.hasNext();) {
104  xml.add(i.next());
105  }
106  }
107 
108  };
109 
126  private static final XMLFormat MAP_XML = new XMLFormat() {
127 
128  @SuppressWarnings("unchecked")
129  public void read(XMLFormat.InputElement xml, Object obj)
130  throws XMLStreamException {
131  final Map map = (Map) obj;
132  while (xml.hasNext()) {
133  Object key = xml.get("Key");
134  Object value = xml.get("Value");
135  map.put(key, value);
136  }
137  }
138 
139  public void write(Object obj, XMLFormat.OutputElement xml)
140  throws XMLStreamException {
141  final Map map = (Map) obj;
142  for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
143  Map.Entry entry = (Map.Entry) it.next();
144  xml.add(entry.getKey(), "Key");
145  xml.add(entry.getValue(), "Value");
146  }
147  }
148 
149  };
150 
151 }
javolution
javolution.util.FastMap
Definition: FastMap.java:98
javolution.xml.XMLFormat.newInstance
T newInstance(Class<? extends T > cls, InputElement xml)
Definition: XMLFormat.java:152
javolution.xml.DefaultXMLFormat.value
Class<? extends XMLFormat<?> > value()
javolution.util.FastMap.put
V put(K key, V value)
Definition: FastMap.java:322
javolution.xml.XMLFormat.OutputElement
Definition: XMLFormat.java:622
javolution.xml.XMLContext
Definition: XMLContext.java:31
javolution.xml.stream.XMLStreamException
Definition: XMLStreamException.java:17
javolution.xml.DefaultXMLFormat
Definition: DefaultXMLFormat.java:52
javolution.xml.internal.XMLContextImpl
Definition: XMLContextImpl.java:28
javolution.xml.XMLFormat
Definition: XMLFormat.java:116
javolution.xml.internal.XMLContextImpl.searchFormat
protected< T > XMLFormat< T > searchFormat(Class<? extends T > type)
Definition: XMLContextImpl.java:41
javolution.util.FastMap.get
V get(Object key)
Definition: FastMap.java:315
javolution.xml.internal.XMLContextImpl.setFormat
public< T > void setFormat(Class<? extends T > type, XMLFormat< T > format)
Definition: XMLContextImpl.java:68
javolution.xml.internal.XMLContextImpl.formats
final FastMap< Class<?>, XMLFormat<?> > formats
Definition: XMLContextImpl.java:30
javolution.xml.internal.XMLContextImpl.inner
XMLContext inner()
Definition: XMLContextImpl.java:33
javolution.xml.XMLFormat.InputElement
Definition: XMLFormat.java:186
javolution.xml
Definition: DefaultXMLFormat.java:9
javolution.xml.stream
Definition: Location.java:9
javolution.xml.XMLFormat.Default
Definition: XMLFormat.java:986
javolution.util
Definition: FastBitSet.java:9