KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > jaxb > impl > JAXBGlobalBindingsImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
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  */

17 package org.apache.ws.jaxme.xs.jaxb.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import javax.xml.XMLConstants JavaDoc;
24
25 import org.apache.ws.jaxme.xs.XSParser;
26 import org.apache.ws.jaxme.xs.impl.XSLogicalParser;
27 import org.apache.ws.jaxme.xs.jaxb.JAXBGlobalBindings;
28 import org.apache.ws.jaxme.xs.jaxb.JAXBJavaType;
29 import org.apache.ws.jaxme.xs.jaxb.JAXBXsObjectFactory;
30 import org.apache.ws.jaxme.xs.jaxb.JAXBXsSchema;
31 import org.apache.ws.jaxme.xs.parser.XsObjectCreator;
32 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
33 import org.apache.ws.jaxme.xs.types.XSBase64Binary;
34 import org.apache.ws.jaxme.xs.types.XSDate;
35 import org.apache.ws.jaxme.xs.types.XSDateTime;
36 import org.apache.ws.jaxme.xs.types.XSDuration;
37 import org.apache.ws.jaxme.xs.types.XSGDay;
38 import org.apache.ws.jaxme.xs.types.XSGMonth;
39 import org.apache.ws.jaxme.xs.types.XSGMonthDay;
40 import org.apache.ws.jaxme.xs.types.XSGYear;
41 import org.apache.ws.jaxme.xs.types.XSGYearMonth;
42 import org.apache.ws.jaxme.xs.types.XSHexBinary;
43 import org.apache.ws.jaxme.xs.types.XSNCName;
44 import org.apache.ws.jaxme.xs.types.XSQName;
45 import org.apache.ws.jaxme.xs.types.XSTime;
46 import org.apache.ws.jaxme.xs.xml.XsObject;
47 import org.apache.ws.jaxme.xs.xml.XsQName;
48 import org.apache.ws.jaxme.xs.xml.impl.XsObjectImpl;
49 import org.xml.sax.ContentHandler JavaDoc;
50 import org.xml.sax.Locator JavaDoc;
51 import org.xml.sax.SAXException JavaDoc;
52 import org.xml.sax.helpers.DefaultHandler JavaDoc;
53
54
55 /**
56  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
57  */

58 public class JAXBGlobalBindingsImpl extends XsObjectImpl implements JAXBGlobalBindings {
59   private static final XsQName[] forbiddenEnumQNames =
60     new XsQName[]{
61       XSQName.getInstance().getName(),
62       XSBase64Binary.getInstance().getName(),
63       XSHexBinary.getInstance().getName(),
64       XSDate.getInstance().getName(),
65       XSDateTime.getInstance().getName(),
66       XSTime.getInstance().getName(),
67       XSDuration.getInstance().getName(),
68       XSGDay.getInstance().getName(),
69       XSGMonth.getInstance().getName(),
70       XSGYear.getInstance().getName(),
71       XSGMonthDay.getInstance().getName(),
72       XSGYearMonth.getInstance().getName()
73     };
74
75   private String JavaDoc collectionType;
76   private boolean fixedAttributeAsConstantProperty, generateIsSetMethod,
77     enableFailFastCheck, choiceContentProperty,
78     javaNamingConventionsDisabled, typesafeEnumMemberNameGeneratesName,
79     isBindingStyleModelGroup;
80   private List JavaDoc javaTypes;
81   private JAXBGlobalBindings.UnderscoreBinding underscoreBinding =
82     JAXBGlobalBindings.UnderscoreBinding.AS_WORD_SEPARATOR;
83   private XsQName[] typesafeEnumBase = new XsQName[]{XSNCName.getInstance().getName()};
84
85   /** <p>Creates a new instance of JAXBGlobalBindingsImpl.</p>
86    */

87   protected JAXBGlobalBindingsImpl(XsObject pParent) {
88     super(pParent);
89   }
90
91   public void setCollectionType(String JavaDoc pType) {
92     collectionType = pType;
93   }
94
95   public String JavaDoc getCollectionType() {
96     return collectionType;
97   }
98
99   public void setFixedAttributeAsConstantProperty(boolean pFixedAttributeAsConstantProperty) {
100     fixedAttributeAsConstantProperty = pFixedAttributeAsConstantProperty;
101   }
102
103   public boolean isFixedAttributeAsConstantProperty() {
104     return fixedAttributeAsConstantProperty;
105   }
106
107   public void setGenerateIsSetMethod(boolean pGenerateIsSetMethod) {
108     generateIsSetMethod = pGenerateIsSetMethod;
109   }
110
111   public boolean isGenerateIsSetMethod() {
112     return generateIsSetMethod;
113   }
114
115   public void setEnableFailFastCheck(boolean pEnableFailFastCheck) {
116     enableFailFastCheck = pEnableFailFastCheck;
117   }
118
119   public boolean isEnableFailFastCheck() {
120     return enableFailFastCheck;
121   }
122
123   public void setChoiceContentProperty(boolean pChoiceContentProperty) {
124     choiceContentProperty = pChoiceContentProperty;
125   }
126
127   public boolean isChoiceContentProperty() {
128     return choiceContentProperty;
129   }
130
131   public void setUnderscoreBinding(JAXBGlobalBindings.UnderscoreBinding pUnderscoreBinding) {
132     underscoreBinding = pUnderscoreBinding;
133   }
134
135   public JAXBGlobalBindings.UnderscoreBinding getUnderscoreBinding() {
136     return underscoreBinding;
137   }
138
139   public void setEnableJavaNamingConventions(boolean pEnableJavaNamingConventions) {
140     javaNamingConventionsDisabled = !pEnableJavaNamingConventions;
141   }
142
143   public boolean isEnableJavaNamingConventions() {
144     return !javaNamingConventionsDisabled;
145   }
146
147   public void setTypesafeEnumBase(XsQName[] pTypes) {
148     typesafeEnumBase = pTypes;
149   }
150
151   public void setTypesafeEnumBase(String JavaDoc pTypesafeEnumBase) throws SAXException {
152     List JavaDoc list = new ArrayList JavaDoc();
153     String JavaDoc[] parts = new String JavaDoc[3];
154     for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pTypesafeEnumBase, " ,");
155          st.hasMoreTokens(); ) {
156       String JavaDoc token = st.nextToken();
157       getNamespaceSupport().processName(token, parts, false);
158       XsQName qName = new XsQName(parts[0], parts[1], XsQName.prefixOf(token));
159       for (int i = 0; i < forbiddenEnumQNames.length; i++) {
160         if (forbiddenEnumQNames[i].equals(qName)) {
161           throw new LocSAXException("The type " + qName +
162                                      " must not be specified in the typesafeEnumBase (JAXB 6.5.1)",
163                                      getLocator());
164         }
165       }
166       list.add(qName);
167     }
168     setTypesafeEnumBase((XsQName[]) list.toArray(new XsQName[list.size()]));
169   }
170
171   public XsQName[] getTypesafeEnumBase() {
172     return typesafeEnumBase;
173   }
174
175   public void setTypesafeEnumMemberName(boolean pTypesafeEnumMemberName) {
176     typesafeEnumMemberNameGeneratesName = pTypesafeEnumMemberName;
177   }
178
179   public boolean isTypesafeEnumMemberName() {
180     return typesafeEnumMemberNameGeneratesName;
181   }
182
183   public void setBindingStyle(String JavaDoc pBindingStyle) throws SAXException {
184     if ("elementBinding".equals(pBindingStyle)) {
185       isBindingStyleModelGroup = false;
186     } else if ("modelGroupBinding".equals(pBindingStyle)) {
187       isBindingStyleModelGroup = true;
188     } else {
189       throw new LocSAXException("Illegal value for 'bindingStyle': " + pBindingStyle + ", expected either of 'elementBinding' or 'modelGroupBinding'.",
190                                  getLocator());
191     }
192   }
193
194   public boolean isBindingStyleModelGroup() {
195     return isBindingStyleModelGroup;
196   }
197
198   public JAXBJavaType createJavaType() {
199     JAXBJavaType.JAXBGlobalJavaType javaTypeImpl = ((JAXBXsObjectFactory) getObjectFactory()).newJAXBGlobalJavaType(this);
200     if (javaTypes == null) {
201         javaTypes = new ArrayList JavaDoc();
202     }
203     javaTypes.add(javaTypeImpl);
204     return javaTypeImpl;
205   }
206
207   private static final JAXBJavaType.JAXBGlobalJavaType[] DEFAULT_JAXB_JAVA_TYPES =
208       new JAXBJavaType.JAXBGlobalJavaType[0];
209   public JAXBJavaType.JAXBGlobalJavaType[] getJavaType() {
210     if (javaTypes == null) {
211       return DEFAULT_JAXB_JAVA_TYPES;
212     } else {
213       return (JAXBJavaType.JAXBGlobalJavaType[]) javaTypes.toArray(new JAXBJavaType.JAXBGlobalJavaType[javaTypes.size()]);
214     }
215   }
216
217   protected XsObjectCreator[] getXsObjectCreators() {
218       return null;
219   }
220
221   protected boolean isPrefixEnabled(String JavaDoc pPrefix) {
222       // The list of extension prefixes is in the outermost schema.
223
String JavaDoc[] prefixes;
224       XSLogicalParser xsParser = getContext().getXSLogicalParser();
225       if (xsParser == null) {
226           // Just syntax parsing, take this schema as outermost instance.
227
JAXBXsSchema jaxbXsSchema = (JAXBXsSchema) getXsESchema();
228           prefixes = jaxbXsSchema.getJaxbExtensionBindingPrefixes();
229       } else {
230           prefixes = ((JAXBXsSchema) xsParser.getSyntaxSchemas()[0]).getJaxbExtensionBindingPrefixes();
231       }
232
233       for (int i = 0; i < prefixes.length; i++) {
234           if (prefixes[i].equals(pPrefix)) {
235               return true;
236           }
237       }
238       return false;
239   }
240
241   protected XsObject getBeanByParent(XsObject pParent, Locator JavaDoc pLocator, XsQName pQName) throws SAXException {
242       XsObjectCreator[] creators = getXsObjectCreators();
243       if (creators != null) {
244           for (int i = 0; i < creators.length; i++) {
245               XsObject result = creators[i].newBean(pParent, pLocator, pQName);
246               if (result != null) {
247                   return result;
248               }
249           }
250       }
251       return null;
252   }
253
254   public ContentHandler JavaDoc getChildHandler(String JavaDoc pQName, String JavaDoc pNamespaceURI,
255                                         String JavaDoc pLocalName) throws SAXException {
256     if (JAXBParser.JAXB_SCHEMA_URI.equals(pNamespaceURI) ||
257         XSParser.XML_SCHEMA_URI.equals(pNamespaceURI) ||
258         XMLConstants.XML_NS_URI.equals(pNamespaceURI)) {
259       return null;
260     }
261
262     int offset = pQName.indexOf(':');
263     if (offset > 0) {
264       String JavaDoc prefix = pQName.substring(0, offset);
265       if (isPrefixEnabled(prefix)) {
266           XsQName qName = new XsQName(pNamespaceURI, pLocalName, XsQName.prefixOf(pQName));
267           XsObject result = getBeanByParent(this, getObjectFactory().getLocator(), qName);
268           if (result != null) {
269               return getObjectFactory().newXsSAXParser(result);
270           } else {
271               return new DefaultHandler JavaDoc();
272           }
273       }
274     }
275     throw new LocSAXException("Unknown child element " + pQName + ", use the schemas jaxb:extensionBindingPrefixes attribute to ignore it.",
276                                getLocator());
277   }
278 }
279
Popular Tags