KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > impl > XSAttributeImpl


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.impl;
18
19 import org.apache.ws.jaxme.xs.XSAnnotation;
20 import org.apache.ws.jaxme.xs.XSAttribute;
21 import org.apache.ws.jaxme.xs.XSObject;
22 import org.apache.ws.jaxme.xs.XSType;
23 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
24 import org.apache.ws.jaxme.xs.types.XSAnySimpleType;
25 import org.apache.ws.jaxme.xs.xml.XsAnyURI;
26 import org.apache.ws.jaxme.xs.xml.XsEAnnotation;
27 import org.apache.ws.jaxme.xs.xml.XsESchema;
28 import org.apache.ws.jaxme.xs.xml.XsFormChoice;
29 import org.apache.ws.jaxme.xs.xml.XsNCName;
30 import org.apache.ws.jaxme.xs.xml.XsQName;
31 import org.apache.ws.jaxme.xs.xml.XsTAttribute;
32 import org.apache.ws.jaxme.xs.xml.XsTLocalSimpleType;
33 import org.xml.sax.SAXException JavaDoc;
34
35
36 /**
37  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
38  */

39 public class XSAttributeImpl extends XSOpenAttrsImpl implements XSAttribute {
40   private final XsQName name;
41   private final XsEAnnotation xsAnnotation;
42   private final boolean isGlobal;
43   private XSAnnotation[] annotations;
44   private boolean isValidated;
45   private XSType type;
46
47   protected XsTAttribute getXsTAttribute() {
48     return (XsTAttribute) getXsObject();
49   }
50
51   protected boolean isReference() {
52     return getXsTAttribute().getRef() != null;
53   }
54
55   protected boolean isInnerSimpleType() {
56     return getXsTAttribute().getSimpleType() != null;
57   }
58
59   protected XSAttributeImpl(XSObject pParent, XsTAttribute pBaseAttribute) throws SAXException {
60     super(pParent, pBaseAttribute);
61     if (isReference()) {
62       this.name = pBaseAttribute.getRef();
63       isGlobal = pBaseAttribute.isGlobal();
64     } else {
65       XsNCName myName = pBaseAttribute.getName();
66       if (myName == null) {
67         throw new LocSAXException("Invalid attribute: Neither of its 'name' or 'ref' attributes are set.",
68                                      pBaseAttribute.getLocator());
69       }
70       XsESchema schema = pBaseAttribute.getXsESchema();
71       XsAnyURI namespace;
72       String JavaDoc namespacePrefix;
73       boolean qualified = isGlobal = pBaseAttribute.isGlobal();
74       if (!qualified) {
75         XsFormChoice form = pBaseAttribute.getForm();
76         if (form == null) {
77           form = schema.getAttributeFormDefault();
78         }
79         qualified = XsFormChoice.QUALIFIED.equals(form);
80       }
81       if (qualified) {
82         namespace = schema.getTargetNamespace();
83         namespacePrefix = schema.getTargetNamespacePrefix();
84       } else {
85         namespace = null;
86         namespacePrefix = null;
87       }
88       this.name = new XsQName(namespace, myName.toString(), namespacePrefix);
89     }
90
91     xsAnnotation = pBaseAttribute.getAnnotation();
92   }
93
94   public boolean isGlobal() {
95     return isGlobal;
96   }
97
98   public XsQName getName() {
99     return name;
100   }
101
102   public XSType getType() {
103     return type;
104   }
105
106   public XSAnnotation[] getAnnotations() {
107     return annotations;
108   }
109
110   protected boolean isValidated() {
111     return isValidated;
112   }
113
114   public void validate() throws SAXException {
115     if (isValidated()) {
116       return;
117     } else {
118       isValidated = true;
119     }
120
121     if (xsAnnotation == null) {
122       annotations = new XSAnnotation[0];
123     } else {
124       XSAnnotation ann = getXSSchema().getXSObjectFactory().newXSAnnotation(this, xsAnnotation);
125       annotations = new XSAnnotation[]{ ann };
126       ann.validate();
127     }
128
129     XSType myType;
130     if (isReference()) {
131       XSAttribute attribute = getXSSchema().getAttribute(getName());
132       attribute.validate();
133       if (attribute == null) {
134         throw new LocSAXException("Invalid attribute reference: No type named " + getName() + " defined.",
135                                      getXsTAttribute().getLocator());
136       }
137       myType = attribute.getType();
138       if (myType == null) {
139           throw new IllegalStateException JavaDoc("The referenced attributes type must not be null.");
140       }
141     } else if (isInnerSimpleType()) {
142       XsTLocalSimpleType innerSimpleType = getXsTAttribute().getSimpleType();
143       myType = getXSSchema().getXSObjectFactory().newXSType(this, innerSimpleType);
144       myType.validate();
145     } else {
146       XsQName typeName = getXsTAttribute().getType();
147       if (typeName == null) {
148         typeName = XSAnySimpleType.getInstance().getName();
149       }
150       myType = getXSSchema().getType(typeName);
151       if (myType == null) {
152         throw new LocSAXException("Invalid attribute: The referenced type " + typeName + " is not defined in the schema.",
153                                      getXsTAttribute().getLocator());
154       }
155     }
156     this.type = myType;
157   }
158
159   public boolean isOptional() {
160     XsTAttribute.Use use = getXsTAttribute().getUse();
161     if (XsTAttribute.PROHIBITED.equals(use)) {
162       throw new IllegalStateException JavaDoc("This attribute is prohibited.");
163     }
164     return XsTAttribute.OPTIONAL.equals(use);
165   }
166
167   public String JavaDoc getDefault() {
168     return getXsTAttribute().getDefault();
169   }
170
171   public String JavaDoc getFixed() {
172     return getXsTAttribute().getFixed();
173   }
174 }
175
Popular Tags