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.net.URI;
019: import java.util.LinkedList;
020: import java.util.List;
021:
022: import org.geotools.xml.XSIElementHandler;
023: import org.geotools.xml.schema.All;
024: import org.geotools.xml.schema.Element;
025: import org.geotools.xml.schema.ElementGrouping;
026: import org.xml.sax.Attributes;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * AllHandler purpose.
031: *
032: * <p>
033: * This represents an 'all' element in an xml schema.
034: * </p>
035: *
036: * @author dzwiers, Refractions Research, Inc. http://www.refractions.net
037: * @author $Author:$ (last modification)
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/xsi/AllHandler.java $
039: * @version $Id: AllHandler.java 20747 2006-07-29 01:12:15Z jeichar $
040: */
041: public class AllHandler extends ElementGroupingHandler {
042: /** 'all' */
043: public static final String LOCALNAME = "all";
044: private String id;
045: private int minOccurs;
046: private int maxOccurs;
047: private List elements;
048: private DefaultAll cache = null;
049:
050: /**
051: * @see org.geotools.xml.XSIElementHandler#getHandler(java.lang.String,
052: * java.lang.String)
053: */
054: public XSIElementHandler getHandler(String namespaceURI,
055: String localName) {
056: if (SchemaHandler.namespaceURI.equalsIgnoreCase(namespaceURI)) {
057: // child types
058: //
059: // annotation
060: // element
061: if (ElementTypeHandler.LOCALNAME
062: .equalsIgnoreCase(localName)) {
063: if (elements == null) {
064: elements = new LinkedList();
065: }
066:
067: ElementTypeHandler eh = new ElementTypeHandler();
068: elements.add(eh);
069:
070: return eh;
071: }
072: }
073:
074: return null;
075: }
076:
077: /**
078: * @see org.geotools.xml.XSIElementHandler#startElement(java.lang.String,
079: * java.lang.String, org.xml.sax.Attributes)
080: */
081: public void startElement(String namespaceURI, String localName,
082: Attributes atts) {
083: id = atts.getValue("", "id");
084:
085: if (id == null) {
086: id = atts.getValue(namespaceURI, "id");
087: }
088:
089: String minOccurs1 = atts.getValue("", "minOccurs");
090:
091: if (minOccurs1 == null) {
092: minOccurs1 = atts.getValue(namespaceURI, "minOccurs");
093: }
094:
095: if (minOccurs1 == null || minOccurs1.length() == 0) {
096: this .minOccurs = 1;
097: } else {
098: this .minOccurs = Integer.parseInt(minOccurs1);
099: }
100:
101: String maxOccurs1 = atts.getValue("", "maxOccurs");
102:
103: if (maxOccurs1 == null) {
104: maxOccurs1 = atts.getValue(namespaceURI, "maxOccurs");
105: }
106: if (maxOccurs1 == null || maxOccurs1.length() == 0) {
107: this .maxOccurs = 1;
108: } else {
109: this .maxOccurs = Integer.parseInt(maxOccurs1);
110: }
111: }
112:
113: /**
114: * @see org.geotools.xml.XSIElementHandler#getLocalName()
115: */
116: public String getLocalName() {
117: return LOCALNAME;
118: }
119:
120: /**
121: * @see org.geotools.xml.XSIHandlers.ElementGroupingHandler#compress(org.geotools.xml.XSIHandlers.SchemaHandler)
122: */
123: protected ElementGrouping compress(SchemaHandler parent)
124: throws SAXException {
125:
126: synchronized (this ) {
127: if (cache != null)
128: return cache;
129: cache = new DefaultAll();
130: }
131:
132: cache.id = id;
133: cache.maxOccurs = maxOccurs;
134: cache.minOccurs = minOccurs;
135:
136: if (elements != null) {
137: cache.elements = new Element[elements.size()];
138:
139: for (int i = 0; i < cache.elements.length; i++) {
140: cache.elements[i] = (Element) ((ElementTypeHandler) elements
141: .get(i)).compress(parent);
142: }
143: }
144:
145: id = null;
146: elements = null;
147:
148: return cache;
149: }
150:
151: /**
152: * @see java.lang.Object#hashCode()
153: */
154: public int hashCode() {
155: return (LOCALNAME.hashCode() * ((id == null) ? 1 : id
156: .hashCode()))
157: + (minOccurs * maxOccurs);
158: }
159:
160: /**
161: * @see org.geotools.xml.XSIElementHandler#getHandlerType()
162: */
163: public int getHandlerType() {
164: return DEFAULT;
165: }
166:
167: /**
168: * @see org.geotools.xml.XSIElementHandler#endElement(java.lang.String,
169: * java.lang.String)
170: */
171: public void endElement(String namespaceURI, String localName) {
172: // do nothing
173: }
174:
175: /**
176: * <p>
177: * Default implementation for when the parse is complete
178: * </p>
179: *
180: * @author dzwiers
181: *
182: * @see All
183: */
184: private static class DefaultAll implements All {
185: // file visible to avoid set* methods.
186: Element[] elements;
187: String id;
188: int maxOccurs;
189: int minOccurs;
190:
191: /**
192: * @see org.geotools.xml.xsi.ElementGrouping#findChildElement(java.lang.String)
193: */
194: public Element findChildElement(String name) {
195: if (elements == null) {
196: return null;
197: }
198:
199: for (int i = 0; i < elements.length; i++) {
200: Element t = elements[i].findChildElement(name);
201:
202: if (t != null) { // found it
203:
204: return t;
205: }
206: }
207:
208: return null;
209: }
210:
211: /**
212: * @see org.geotools.xml.xsi.All#getElements()
213: */
214: public Element[] getElements() {
215: return elements;
216: }
217:
218: /**
219: * @see org.geotools.xml.xsi.All#getId()
220: */
221: public String getId() {
222: return id;
223: }
224:
225: /**
226: * @see org.geotools.xml.xsi.ElementGrouping#getMaxOccurs()
227: */
228: public int getMaxOccurs() {
229: return maxOccurs;
230: }
231:
232: /**
233: * @see org.geotools.xml.xsi.ElementGrouping#getMinOccurs()
234: */
235: public int getMinOccurs() {
236: return minOccurs;
237: }
238:
239: /**
240: * @see org.geotools.xml.xsi.ElementGrouping#getGrouping()
241: */
242: public int getGrouping() {
243: return ALL;
244: }
245:
246: public Element findChildElement(String localName,
247: URI namespaceURI) {
248: if (elements == null) {
249: return null;
250: }
251:
252: for (int i = 0; i < elements.length; i++) {
253: Element t = elements[i].findChildElement(localName,
254: namespaceURI);
255:
256: if (t != null) { // found it
257:
258: return t;
259: }
260: }
261:
262: return null;
263: }
264: }
265: }
|