View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
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.javagen.agile.core.xml.jaxb;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Set;
21  import javax.xml.bind.annotation.adapters.XmlAdapter;
22  
23  import org.javagen.agile.core.model.Model;
24  
25  
26  public class ContextTypeAdapter extends XmlAdapter<ContextType, Map<?,?>> {
27  
28      @Override
29      public Map<?,?> unmarshal(ContextType boundType) throws Exception {
30          if (boundType==null)
31              return null;
32          Map<String, Object> map = new HashMap<String, Object>();
33          if (boundType.getEntries()!=null)
34              for(ContextEntryType entry : boundType.getEntries()) {
35                  map.put(entry.getKey(), entry.getValue());
36              }
37          if (boundType.getEntryRefs()!=null)
38              for(ContextModelEntryType entry : boundType.getEntryRefs()) {
39                  map.put(entry.getKey(), entry.getModel());
40              }
41          return map;
42      }
43  
44      @Override
45      public ContextType marshal(Map<?,?> valueType) throws Exception {
46          if (valueType==null)
47              return null;
48          Set<?> keys = valueType.keySet();
49          ContextType context = new ContextType();
50          for(Object key : keys) {
51              Object value = valueType.get(key);
52              if (value instanceof Model) {
53                  context.addEntryRef(key.toString(), (Model)value);
54              } else {
55                  context.addEntry(key.toString(), value);
56              }
57          }
58          return context;
59      }
60  
61  }