001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xml.handlers.xsi;
017:
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.geotools.xml.XSIElementHandler;
024: import org.geotools.xml.schema.Attribute;
025: import org.geotools.xml.schema.AttributeGroup;
026: import org.geotools.xml.schema.impl.AttributeGroupGT;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.SAXNotRecognizedException;
030:
031: /**
032: * AttributeGroupHandler purpose.
033: *
034: * <p>
035: * Represents an 'attributeGroup' element.
036: * </p>
037: *
038: * @author dzwiers, Refractions Research, Inc. http://www.refractions.net
039: * @author $Author:$ (last modification)
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/xsi/AttributeGroupHandler.java $
041: * @version $Id: AttributeGroupHandler.java 22266 2006-10-19 11:30:55Z acuster $
042: */
043: public class AttributeGroupHandler extends XSIElementHandler {
044: /** 'attributeGroup' */
045: public final static String LOCALNAME = "attributeGroup";
046: private static int offset = 0;
047: private String id;
048: private String name;
049: private String ref;
050: private AnyAttributeHandler anyAttribute;
051: private List attrDecs;
052: private int hashCodeOffset = getOffset();
053: private AttributeGroup cache = null;
054:
055: /*
056: * Helper method for hashCode
057: */
058: private static int getOffset() {
059: return offset++;
060: }
061:
062: /**
063: * @see java.lang.Object#hashCode()
064: */
065: public int hashCode() {
066: return (LOCALNAME.hashCode()
067: * ((id == null) ? 1 : id.hashCode())
068: * ((ref == null) ? 1 : ref.hashCode()) * ((name == null) ? 1
069: : name.hashCode()))
070: + hashCodeOffset;
071: }
072:
073: /**
074: * @see org.geotools.xml.XSIElementHandler#getHandler(java.lang.String,
075: * java.lang.String)
076: */
077: public XSIElementHandler getHandler(String namespaceURI,
078: String localName) throws SAXException {
079: if (SchemaHandler.namespaceURI.equalsIgnoreCase(namespaceURI)) {
080: // child types
081: //
082: // attribute
083: if (AttributeHandler.LOCALNAME.equalsIgnoreCase(localName)) {
084: if (attrDecs == null) {
085: attrDecs = new LinkedList();
086: }
087:
088: AttributeHandler ah = new AttributeHandler();
089: attrDecs.add(ah);
090:
091: return ah;
092: }
093:
094: // attributeGroup
095: if (AttributeGroupHandler.LOCALNAME
096: .equalsIgnoreCase(localName)) {
097: if (attrDecs == null) {
098: attrDecs = new LinkedList();
099: }
100:
101: AttributeGroupHandler ah = new AttributeGroupHandler();
102: attrDecs.add(ah);
103:
104: return ah;
105: }
106:
107: // anyAttribute
108: if (AnyAttributeHandler.LOCALNAME
109: .equalsIgnoreCase(localName)) {
110: AnyAttributeHandler sth = new AnyAttributeHandler();
111:
112: if (anyAttribute == null) {
113: anyAttribute = sth;
114: } else {
115: throw new SAXNotRecognizedException(LOCALNAME
116: + " may only have one child declaration.");
117: }
118:
119: return sth;
120: }
121: }
122:
123: return null;
124: }
125:
126: /**
127: * @see org.geotools.xml.XSIElementHandler#startElement(java.lang.String,
128: * java.lang.String, org.xml.sax.Attributes)
129: */
130: public void startElement(String namespaceURI, String localName,
131: Attributes atts) {
132: id = atts.getValue("", "id");
133:
134: if (id == null) {
135: id = atts.getValue(namespaceURI, "id");
136: }
137:
138: name = atts.getValue("", "name");
139:
140: if (name == null) {
141: name = atts.getValue(namespaceURI, "name");
142: }
143:
144: ref = atts.getValue("", "ref");
145:
146: if (ref == null) {
147: ref = atts.getValue(namespaceURI, "ref");
148: }
149: }
150:
151: /**
152: * @see org.geotools.xml.XSIElementHandler#getLocalName()
153: */
154: public String getLocalName() {
155: return LOCALNAME;
156: }
157:
158: /**
159: * returns the 'name' attribute
160: *
161: */
162: public String getName() {
163: return name;
164: }
165:
166: /**
167: * <p>
168: * Reduces the memory imprint returning a smaller object
169: * </p>
170: *
171: * @param parent
172: *
173: *
174: * @throws SAXException
175: */
176: protected AttributeGroup compress(SchemaHandler parent)
177: throws SAXException {
178: if (cache != null) {
179: return cache;
180: }
181:
182: String anyAttributeNamespace = (anyAttribute == null) ? null
183: : anyAttribute.getNamespace();
184: Attribute[] attributes = null;
185:
186: if (attrDecs != null) {
187: Iterator i = attrDecs.iterator();
188: HashSet h = new HashSet();
189:
190: while (i.hasNext()) {
191: Object o = i.next();
192:
193: if (o instanceof AttributeHandler) {
194: h.add(((AttributeHandler) o).compress(parent));
195: } else {
196: AttributeGroupHandler agh = (AttributeGroupHandler) o;
197: AttributeGroup ag = agh.compress(parent);
198:
199: if ((ag != null) && (ag.getAttributes() != null)) {
200: Attribute[] aa = ag.getAttributes();
201:
202: for (int j = 0; j < aa.length; j++)
203: h.add(aa[j]);
204: }
205: }
206: }
207:
208: attributes = (Attribute[]) h
209: .toArray(new Attribute[h.size()]);
210: }
211:
212: String name1 = this .name;
213:
214: if ((ref != null) && !"".equalsIgnoreCase(ref)) {
215: AttributeGroup ag = parent.lookUpAttributeGroup(ref);
216:
217: if (ag == null) {
218: throw new SAXException("AttributeGroup '" + ref
219: + "' was refered and not found");
220: }
221:
222: name1 = ag.getName();
223:
224: if ((anyAttribute == null)
225: || "".equalsIgnoreCase(anyAttribute.getNamespace())) {
226: anyAttributeNamespace = ag.getAnyAttributeNameSpace();
227: }
228:
229: if (attributes != null) {
230: throw new SAXException(
231: "Cannot have a ref and children for an AttributeGroup");
232: }
233:
234: attributes = ag.getAttributes();
235: }
236:
237: cache = new AttributeGroupGT(id, name1, parent
238: .getTargetNamespace(), attributes,
239: anyAttributeNamespace);
240:
241: return cache;
242: }
243:
244: /**
245: * @see org.geotools.xml.XSIElementHandler#getHandlerType()
246: */
247: public int getHandlerType() {
248: return DEFAULT;
249: }
250:
251: /**
252: * @see org.geotools.xml.XSIElementHandler#endElement(java.lang.String,
253: * java.lang.String)
254: */
255: public void endElement(String namespaceURI, String localName) {
256: // do nothing
257: }
258: }
|