KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > UnmarshallerFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import org.jboss.xb.binding.parser.JBossXBParser;
28
29 /**
30  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
31  * @version <tt>$Revision: 1958 $</tt>
32  */

33 public abstract class UnmarshallerFactory
34 {
35    protected Map JavaDoc features;
36    protected Boolean JavaDoc validation = Boolean.TRUE;
37    protected Boolean JavaDoc namespaces = Boolean.TRUE;
38
39    public static UnmarshallerFactory newInstance()
40    {
41       return new UnmarshallerFactoryImpl();
42    }
43
44    public abstract Unmarshaller newUnmarshaller();
45
46    public void setFeature(String JavaDoc name, Object JavaDoc value)
47    {
48       Boolean JavaDoc bValue;
49       if(value == null)
50       {
51          bValue = null;
52       }
53       else if(value instanceof String JavaDoc)
54       {
55          bValue = Boolean.valueOf((String JavaDoc)value);
56       }
57       else if(value instanceof Boolean JavaDoc)
58       {
59          bValue = (Boolean JavaDoc)value;
60       }
61       else
62       {
63          throw new JBossXBRuntimeException(
64             "Allowed feature values are null, 'true, 'false', Boolean.TRUE, Boolean.FALSE. Passed in value: " + value
65          );
66       }
67
68       if(Unmarshaller.VALIDATION.equals(name))
69       {
70          validation = bValue;
71       }
72       else if(Unmarshaller.NAMESPACES.equals(name))
73       {
74          namespaces = bValue;
75       }
76       else
77       {
78          if(features == null)
79          {
80             features = new HashMap JavaDoc();
81          }
82          features.put(name, value);
83       }
84    }
85
86    // Inner
87

88    static class UnmarshallerFactoryImpl
89       extends UnmarshallerFactory
90    {
91       public Unmarshaller newUnmarshaller()
92       {
93          UnmarshallerImpl unmarshaller;
94          try
95          {
96             unmarshaller = new UnmarshallerImpl();
97          }
98          catch(JBossXBException e)
99          {
100             throw new JBossXBRuntimeException(e.getMessage(), e);
101          }
102
103          JBossXBParser parser = unmarshaller.getParser();
104          if(validation != null)
105          {
106             parser.setFeature(Unmarshaller.VALIDATION, validation.booleanValue());
107          }
108
109          if(namespaces != null)
110          {
111             parser.setFeature(Unmarshaller.NAMESPACES, namespaces.booleanValue());
112          }
113
114          if(features != null)
115          {
116             for(Iterator JavaDoc i = features.entrySet().iterator(); i.hasNext();)
117             {
118                Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
119                if(entry.getValue() != null)
120                {
121                   Boolean JavaDoc value = (Boolean JavaDoc)entry.getValue();
122                   parser.setFeature((String JavaDoc)entry.getKey(), value.booleanValue());
123                }
124             }
125          }
126
127          //parser.setFeature(Unmarshaller.SCHEMA_VALIDATION, true);
128
//parser.setFeature(Unmarshaller.SCHEMA_FULL_CHECKING, true);
129

130          try
131          {
132             parser.setFeature(Unmarshaller.DYNAMIC_VALIDATION, true);
133          }
134          catch(JBossXBRuntimeException e)
135          {
136             // dynamic_validation is a required xerces-specific feature
137
}
138
139          return unmarshaller;
140       }
141    }
142 }
143
Popular Tags