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.Element;
024: import org.geotools.xml.schema.ElementGrouping;
025: import org.geotools.xml.schema.Sequence;
026: import org.xml.sax.Attributes;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * SequenceHandler purpose.
031: *
032: * <p>
033: * represents a sequence element
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/SequenceHandler.java $
039: * @version $Id: SequenceHandler.java 20747 2006-07-29 01:12:15Z jeichar $
040: */
041: public class SequenceHandler extends ElementGroupingHandler {
042: /** 'sequence' */
043: public final static String LOCALNAME = "sequence";
044: private String id;
045: private int maxOccurs;
046: private int minOccurs;
047: private List children; // element, group, choice, sequence or any
048: private DefaultSequence cache = null;
049:
050: /**
051: * @see java.lang.Object#hashCode()
052: */
053: public int hashCode() {
054: return (LOCALNAME.hashCode() * ((id == null) ? 1 : id
055: .hashCode()))
056: + ((children == null) ? 2 : children.hashCode());
057: }
058:
059: /**
060: * @see org.geotools.xml.XSIElementHandler#getHandler(java.lang.String,
061: * java.lang.String)
062: */
063: public XSIElementHandler getHandler(String namespaceURI,
064: String localName) {
065: logger.finest("Getting Handler for " + localName + " :: "
066: + namespaceURI);
067:
068: if (namespaceURI.equalsIgnoreCase(namespaceURI)) {
069: // child types
070: //
071: // any
072: if (AnyHandler.LOCALNAME.equalsIgnoreCase(localName)) {
073: if (children == null) {
074: children = new LinkedList();
075: }
076:
077: AnyHandler ah = new AnyHandler();
078: children.add(ah);
079:
080: return ah;
081: }
082:
083: // choice
084: if (ChoiceHandler.LOCALNAME.equalsIgnoreCase(localName)) {
085: if (children == null) {
086: children = new LinkedList();
087: }
088:
089: ChoiceHandler ah = new ChoiceHandler();
090: children.add(ah);
091:
092: return ah;
093: }
094:
095: // element
096: if (ElementTypeHandler.LOCALNAME
097: .equalsIgnoreCase(localName)) {
098: if (children == null) {
099: children = new LinkedList();
100: }
101:
102: ElementTypeHandler ah = new ElementTypeHandler();
103: children.add(ah);
104:
105: return ah;
106: }
107:
108: // group
109: if (GroupHandler.LOCALNAME.equalsIgnoreCase(localName)) {
110: if (children == null) {
111: children = new LinkedList();
112: }
113:
114: GroupHandler ah = new GroupHandler();
115: children.add(ah);
116:
117: return ah;
118: }
119:
120: // sequence
121: if (LOCALNAME.equalsIgnoreCase(localName)) {
122: if (children == null) {
123: children = new LinkedList();
124: }
125:
126: SequenceHandler ah = new SequenceHandler();
127: children.add(ah);
128:
129: return ah;
130: }
131: }
132:
133: return null;
134: }
135:
136: /**
137: * @see org.geotools.xml.XSIElementHandler#startElement(java.lang.String,
138: * java.lang.String, org.xml.sax.Attributes)
139: */
140: public void startElement(String namespaceURI, String localName,
141: Attributes atts) {
142: // id
143: id = atts.getValue("", "id");
144:
145: if (id == null) {
146: id = atts.getValue(namespaceURI, "id");
147: }
148:
149: // maxOccurs
150: String maxOccurs1 = atts.getValue("", "maxOccurs");
151:
152: if (maxOccurs1 == null) {
153: maxOccurs1 = atts.getValue(namespaceURI, "maxOccurs");
154: }
155:
156: // minOccurs
157: String minOccurs1 = atts.getValue("", "minOccurs");
158:
159: if (minOccurs1 == null) {
160: minOccurs1 = atts.getValue(namespaceURI, "minOccurs");
161: }
162:
163: if ((minOccurs1 != null) && !"".equalsIgnoreCase(minOccurs1)) {
164: this .minOccurs = Integer.parseInt(minOccurs1);
165: } else {
166: this .minOccurs = 1;
167: }
168:
169: if ((maxOccurs1 != null) && !"".equalsIgnoreCase(maxOccurs1)) {
170: if ("unbounded".equalsIgnoreCase(maxOccurs1)) {
171: this .maxOccurs = ElementGrouping.UNBOUNDED;
172: } else {
173: this .maxOccurs = Integer.parseInt(maxOccurs1);
174: }
175: } else {
176: this .maxOccurs = 1;
177: }
178: }
179:
180: /**
181: * @see org.geotools.xml.XSIElementHandler#getLocalName()
182: */
183: public String getLocalName() {
184: return LOCALNAME;
185: }
186:
187: /**
188: * @see org.geotools.xml.XSIHandlers.ElementGroupingHandler#compress(org.geotools.xml.XSIHandlers.SchemaHandler)
189: */
190: protected ElementGrouping compress(SchemaHandler parent)
191: throws SAXException {
192:
193: synchronized (this ) {
194: if (cache != null)
195: return cache;
196: cache = new DefaultSequence();
197: }
198:
199: cache.id = id;
200: cache.minOccurs = minOccurs;
201: cache.maxOccurs = maxOccurs;
202:
203: logger.finest(id + " :: This Sequence has "
204: + ((children == null) ? 0 : children.size())
205: + " children");
206:
207: if (children != null) {
208: cache.children = new ElementGrouping[children.size()];
209:
210: // TODO compress sequences here
211: // sequqnces can be inlined here.
212: for (int i = 0; i < cache.children.length; i++)
213: cache.children[i] = ((ElementGroupingHandler) children
214: .get(i)).compress(parent);
215: }
216:
217: children = null;
218: id = null;
219:
220: return cache;
221: }
222:
223: /**
224: * @see org.geotools.xml.XSIElementHandler#getHandlerType()
225: */
226: public int getHandlerType() {
227: return SEQUENCE;
228: }
229:
230: /**
231: * @see org.geotools.xml.XSIElementHandler#endElement(java.lang.String,
232: * java.lang.String)
233: */
234: public void endElement(String namespaceURI, String localName) {
235: // do nothing
236: }
237:
238: /**
239: * <p>
240: * Default implementation of a sequence for a parsed xml sequence
241: * </p>
242: *
243: * @author dzwiers
244: *
245: * @see Sequence
246: */
247: private static class DefaultSequence implements Sequence {
248: // file visible avoids set* methods
249: ElementGrouping[] children;
250: String id;
251: int minOccurs;
252: int maxOccurs;
253:
254: /**
255: * @see org.geotools.xml.xsi.ElementGrouping#findChildElement(java.lang.String)
256: */
257: public Element findChildElement(String name) {
258: if (children == null) {
259: return null;
260: }
261:
262: for (int i = 0; i < children.length; i++) {
263: Element t = children[i].findChildElement(name);
264:
265: if (t != null) { // found it
266:
267: return t;
268: }
269: }
270:
271: return null;
272: }
273:
274: /**
275: * @see org.geotools.xml.xsi.Sequence#getChildren()
276: */
277: public ElementGrouping[] getChildren() {
278: return children;
279: }
280:
281: /**
282: * @see org.geotools.xml.xsi.Sequence#getId()
283: */
284: public String getId() {
285: return id;
286: }
287:
288: /**
289: * @see org.geotools.xml.xsi.ElementGrouping#getMaxOccurs()
290: */
291: public int getMaxOccurs() {
292: return maxOccurs;
293: }
294:
295: /**
296: * @see org.geotools.xml.xsi.ElementGrouping#getMinOccurs()
297: */
298: public int getMinOccurs() {
299: return minOccurs;
300: }
301:
302: /**
303: * @see org.geotools.xml.xsi.ElementGrouping#getGrouping()
304: */
305: public int getGrouping() {
306: return SEQUENCE;
307: }
308:
309: public Element findChildElement(String localName,
310: URI namespaceURI) {
311: if (children == null) {
312: return null;
313: }
314:
315: for (int i = 0; i < children.length; i++) {
316: Element t = children[i].findChildElement(localName,
317: namespaceURI);
318:
319: if (t != null) { // found it
320:
321: return t;
322: }
323: }
324:
325: return null;
326: }
327: }
328: }
|