View Javadoc
1   /*
2    * Copyright 2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.metadata.jaxbadapters;
17  
18  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Name;
19  import org.openehealth.ipf.commons.ihe.xds.core.metadata.XpnName;
20  
21  import javax.xml.bind.annotation.adapters.XmlAdapter;
22  
23  /**
24   * A JAXB {@link XmlAdapter} that allows the Name class to be serialized.
25   * <p/>
26   * The original implementation of the {@link Name} class was concrete and had
27   * no subclasses. The XML serialization of a {@link Name} used to look like this:
28   * <pre>
29   * {@code
30   *
31   * <name>
32   *    <given>John</given>
33   *    <family>Doe</family>
34   * </name>
35   * }
36   * </pre>
37   * <p/>
38   * When the {@link Name} class was made abstract, the XML above could no
39   * longer be serialized. This adapter allows the XML shown above to be
40   * serialized without change.
41   * @author Michael Ottati
42   */
43  public class NameAdapter extends XmlAdapter<XpnName, Name> {
44      @Override
45      public Name unmarshal(XpnName v) throws Exception {
46          return v;
47      }
48  
49      @Override
50      public XpnName marshal(Name v) throws Exception {
51  
52          return new XpnName( v.getFamilyName(),
53                              v.getGivenName(),
54                              v.getSecondAndFurtherGivenNames(),
55                              v.getSuffix(),
56                              v.getPrefix(),
57                              v.getDegree()
58          );
59      }
60  }