KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.ws.jaxme.xs.XSAttributable;
23 import org.apache.ws.jaxme.xs.XSAttribute;
24 import org.apache.ws.jaxme.xs.XSAttributeGroup;
25 import org.apache.ws.jaxme.xs.XSObject;
26 import org.apache.ws.jaxme.xs.XSWildcard;
27 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
28 import org.apache.ws.jaxme.xs.xml.XsESchema;
29 import org.apache.ws.jaxme.xs.xml.XsGAttrDecls;
30 import org.apache.ws.jaxme.xs.xml.XsNCName;
31 import org.apache.ws.jaxme.xs.xml.XsQName;
32 import org.apache.ws.jaxme.xs.xml.XsTAttribute;
33 import org.apache.ws.jaxme.xs.xml.XsTAttributeGroup;
34 import org.apache.ws.jaxme.xs.xml.XsTAttributeGroupRef;
35 import org.apache.ws.jaxme.xs.xml.XsTWildcard;
36 import org.xml.sax.SAXException JavaDoc;
37
38
39 /**
40  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
41  */

42 public class XSAttributeGroupImpl extends XSOpenAttrsImpl implements XSAttributeGroup {
43   private final XsQName name;
44   private boolean isValidated;
45   private XSAttributable[] attributes;
46
47   protected XsTAttributeGroup getXsTAttributeGroup() {
48     return (XsTAttributeGroup) getXsObject();
49   }
50
51   protected boolean isReference() {
52     return getXsTAttributeGroup().getRef() != null;
53   }
54
55   protected XSAttributeGroupImpl(XSObject pParent, XsTAttributeGroup pBaseGroup)
56       throws SAXException {
57     super(pParent, pBaseGroup);
58     XsQName qName;
59     if (isReference()) {
60       qName = getXsTAttributeGroup().getRef();
61     } else {
62       XsNCName myName = pBaseGroup.getName();
63       if (myName == null) {
64         throw new LocSAXException("Invalid attribute group: Neither of its 'name' or 'ref' attributes are set.",
65                                      pBaseGroup.getLocator());
66       } else {
67         XsESchema schema = pBaseGroup.getXsESchema();
68         qName = new XsQName(schema.getTargetNamespace(), myName.toString(), schema.getTargetNamespacePrefix());
69       }
70     }
71     name = qName;
72   }
73
74   public XsQName getName() {
75     return name;
76   }
77
78   public void validate() throws SAXException {
79     if (isValidated) {
80       return;
81     } else {
82       isValidated = true;
83     }
84
85     if (isReference()) {
86       XSAttributeGroup referencedGroup = getXSSchema().getAttributeGroup(getName());
87       if (referencedGroup == null) {
88         throw new LocSAXException("Invalid attribute group: Unknown attribute group " + name + " referenced",
89                                    getLocator());
90       }
91       referencedGroup.validate();
92       attributes = referencedGroup.getAttributes();
93     } else {
94       XsTAttributeGroup attributeGroup = (XsTAttributeGroup) getXsObject();
95       attributes = getAttributes(this, attributeGroup);
96     }
97   }
98
99   public XSAttributable[] getAttributes() {
100     return attributes;
101   }
102
103   protected static XSAttributable[] getAttributes(XSObjectImpl pObject,
104                                                   XsGAttrDecls pAttrDecls) throws SAXException {
105     List JavaDoc attributes = new ArrayList JavaDoc();
106     Object JavaDoc[] allAttributes = pAttrDecls.getAllAttributes();
107     for (int i = 0; i < allAttributes.length; i++) {
108       Object JavaDoc o = allAttributes[i];
109       if (o == null) {
110         throw new NullPointerException JavaDoc("Null attribute detected.");
111       } else if (o instanceof XsTAttribute) {
112         XsTAttribute xsTAttr = (XsTAttribute) o;
113         if (XsTAttribute.PROHIBITED.equals(xsTAttr.getUse())) {
114             continue;
115         }
116         XSAttribute attribute = pObject.getXSSchema().getXSObjectFactory().newXSAttribute(pObject, xsTAttr);
117         attribute.validate();
118         attributes.add(attribute);
119       } else if (o instanceof XsTAttributeGroupRef) {
120         XsTAttributeGroupRef agRef = (XsTAttributeGroupRef) o;
121         XsQName ref = agRef.getRef();
122         if (ref == null) {
123           throw new LocSAXException("Invalid attribute group: Missing 'ref' attribute", pObject.getLocator());
124         }
125         XSAttributeGroup attributeGroup = pObject.getXSSchema().getAttributeGroup(ref);
126         if (attributeGroup == null) {
127           throw new LocSAXException("Unknown attribute group name: " + ref, pObject.getLocator());
128         }
129         attributeGroup.validate();
130         XSAttributable[] agAttributes = attributeGroup.getAttributes();
131         for (int j = 0; j < agAttributes.length; j++) {
132           attributes.add(agAttributes[j]);
133         }
134       } else if (o instanceof XsTAttributeGroup) {
135         XsTAttributeGroup ag = (XsTAttributeGroup) o;
136         XSAttributable[] agAttributes = getAttributes(pObject, ag);
137         for (int j = 0; j < agAttributes.length; j++) {
138           attributes.add(agAttributes[j]);
139         }
140       } else if (o instanceof XsTWildcard) {
141         XSWildcard wildcard = pObject.getXSSchema().getXSObjectFactory().newXSWildcard(pObject, (XsTWildcard) o);
142         wildcard.validate();
143         attributes.add(wildcard);
144       } else {
145         throw new IllegalStateException JavaDoc("Unknown attribute type: " + o.getClass().getName());
146       }
147     }
148     return (XSAttributable[]) attributes.toArray(new XSAttributable[attributes.size()]);
149   }
150 }
151
Popular Tags