001 /*
002 * Java Genetic Algorithm Library (jenetics-2.0.2).
003 * Copyright (c) 2007-2014 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics.internal.util;
021
022 import static org.jenetics.internal.util.reflect.classOf;
023
024 import java.util.HashMap;
025 import java.util.List;
026 import java.util.Map;
027
028 import javax.xml.bind.DataBindingException;
029 import javax.xml.bind.JAXBContext;
030 import javax.xml.bind.JAXBException;
031 import javax.xml.bind.annotation.adapters.XmlAdapter;
032
033 import org.jenetics.internal.util.model.CharacterModel;
034
035 import org.jenetics.util.Function;
036 import org.jenetics.util.StaticObject;
037
038 /**
039 * JAXB helper methods.
040 *
041 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
042 * @version 1.6 — <em>$Date: 2014-04-12 $</em>
043 * @since 2.0
044 */
045 public class jaxb extends StaticObject {
046 private jaxb() {}
047
048 private static final class JAXBContextHolder {
049 private static final JAXBContext CONTEXT; static {
050 try {
051 CONTEXT = JAXBContext.newInstance(
052 "org.jenetics:org.jenetics.internal.util"
053 );
054 } catch (JAXBException e) {
055 throw new DataBindingException(
056 "Something went wrong while creating JAXBContext.", e
057 );
058 }
059 }
060 }
061
062 public static JAXBContext context() {
063 return JAXBContextHolder.CONTEXT;
064 }
065
066 private static final XmlAdapter<Object, Object> IdentityAdapter =
067 new XmlAdapter<Object, Object>() {
068 @Override public Object unmarshal(final Object value) {
069 return value;
070 }
071 @Override public Object marshal(final Object value) {
072 return value;
073 }
074 };
075
076 private static final Map<Class<?>, XmlAdapter<?, ?>> ADAPTERS = new HashMap<>();
077
078 static {
079 ADAPTERS.put(Character.class, CharacterModel.ADAPTER);
080 ADAPTERS.put(CharacterModel.class, CharacterModel.ADAPTER);
081 }
082
083 /**
084 * Return the an {@code XmlAdapter} for the given {@code vale}. If no
085 * adapter could be found, and identity adapter is returned.
086 *
087 * @param value the object for which to find an {@code XmlAdapter}
088 * @return the {@code XmlAdapter} for the given object, or an identity
089 * adapter if no one can be found.
090 */
091 @SuppressWarnings("unchecked")
092 public static XmlAdapter<Object, Object> adapterFor(final Object value) {
093 final Class<?> cls = classOf(value);
094
095 synchronized (ADAPTERS) {
096 if (!ADAPTERS.containsKey(cls)) {
097 ADAPTERS.put(cls, newXmlAdapter(cls));
098 }
099
100 return (XmlAdapter<Object, Object>) ADAPTERS.get(cls);
101 }
102 }
103
104 @SuppressWarnings("unchecked")
105 private static XmlAdapter<Object, Object> newXmlAdapter(final Class<?> cls) {
106 final List<Class<?>> classes = reflect.allDeclaredClasses(cls);
107
108 XmlAdapter<Object, Object> adapter = IdentityAdapter;
109 for (int i = 0; i < classes.size() && adapter == IdentityAdapter; ++i) {
110 if (XmlAdapter.class.isAssignableFrom(classes.get(i))) {
111 try {
112 adapter = (XmlAdapter<Object, Object>)classes.get(i).newInstance();
113 } catch (InstantiationException | IllegalAccessException e) {
114 // ignore exception
115 }
116 }
117 }
118
119 return adapter;
120 }
121
122 /**
123 * Shorthand for {@code adapterFor(value).marshal(value)}
124 */
125 public static Object marshal(final Object value) throws Exception {
126 return adapterFor(value).marshal(value);
127 }
128
129 /**
130 * Shorthand for {@code adapterFor(value).unmarshal(value)}
131 */
132 public static Object unmarshal(final Object value) throws Exception {
133 return adapterFor(value).unmarshal(value);
134 }
135
136 /**
137 * Return a marshaller function from the given
138 * {@link javax.xml.bind.annotation.adapters.XmlAdapter}.
139 *
140 * @param a the adapter used by the marshaller function.
141 * @return the marshaller function
142 */
143 public static <V, B> Function<B, V> Marshaller(final XmlAdapter<V, B> a) {
144 return new Function<B, V>() {
145 @Override
146 public V apply(final B value) {
147 try {
148 return a.marshal(value);
149 } catch (Exception e) {
150 throw new RuntimeException(e);
151 }
152 }
153 };
154 }
155
156 /**
157 * Return a unmarshaller function from the given
158 * {@link javax.xml.bind.annotation.adapters.XmlAdapter}.
159 *
160 * @param a the adapter used by the unmarshaller function.
161 * @return the unmarshaller function
162 */
163 public static <V, B> Function<V, B> Unmarshaller(final XmlAdapter<V, B> a) {
164 return new Function<V, B>() {
165 @Override
166 public B apply(final V value) {
167 try {
168 return a.unmarshal(value);
169 } catch (Exception e) {
170 throw new RuntimeException(e);
171 }
172 }
173 };
174 }
175
176 /**
177 * Return a marshaller function for the given object.
178 *
179 * @param value the value to marshal
180 * @return the marshaller function
181 */
182 public static Function<Object, Object> Marshaller(final Object value) {
183 return Marshaller(adapterFor(value));
184 }
185
186 /**
187 * Return a unmarshaller function for the given object.
188 *
189 * @param value the value to unmarshal
190 * @return the unmarshaller function
191 */
192 public static Function<Object, Object> Unmarshaller(final Object value) {
193 return Unmarshaller(jaxb.adapterFor(value));
194 }
195
196 }
|