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:
020: import org.geotools.xml.XSIElementHandler;
021: import org.geotools.xml.schema.Element;
022: import org.geotools.xml.schema.ElementGrouping;
023: import org.geotools.xml.schema.Group;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.SAXNotRecognizedException;
027:
028: /**
029: * GroupHandler purpose.
030: *
031: * <p>
032: * Representa a 'group' element
033: * </p>
034: *
035: * @author dzwiers, Refractions Research, Inc. http://www.refractions.net
036: * @author $Author:$ (last modification)
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/xsi/GroupHandler.java $
038: * @version $Id: GroupHandler.java 22266 2006-10-19 11:30:55Z acuster $
039: */
040: public class GroupHandler extends ElementGroupingHandler {
041: /** 'group' */
042: public final static String LOCALNAME = "group";
043: private static int offset = 0;
044: private String id;
045: private String name;
046: private String ref = null;
047: private int maxOccurs = 1;
048: private int minOccurs = 1;
049: private ElementGroupingHandler child; // one of 'all', 'choice', or 'sequence'
050: private int hashCodeOffset = getOffset();
051: private DefaultGroup cache = null;
052:
053: /*
054: * helper for hashCode()
055: */
056: private static int getOffset() {
057: return offset++;
058: }
059:
060: /**
061: * @see java.lang.Object#hashCode()
062: */
063: public int hashCode() {
064: return (LOCALNAME.hashCode() * ((name == null) ? 1 : name
065: .hashCode()))
066: + hashCodeOffset;
067: }
068:
069: /**
070: * @see org.geotools.xml.XSIElementHandler#getHandler(java.lang.String,
071: * java.lang.String)
072: */
073: public XSIElementHandler getHandler(String namespaceURI,
074: String localName) throws SAXException {
075: if (SchemaHandler.namespaceURI.equalsIgnoreCase(namespaceURI)) {
076: // child types
077: //
078: // all
079: if (AllHandler.LOCALNAME.equalsIgnoreCase(localName)) {
080: AllHandler sth = new AllHandler();
081:
082: if (child == null) {
083: child = sth;
084: } else {
085: throw new SAXNotRecognizedException(LOCALNAME
086: + " may only have one child.");
087: }
088:
089: return sth;
090: }
091:
092: // choice
093: if (ChoiceHandler.LOCALNAME.equalsIgnoreCase(localName)) {
094: ChoiceHandler sth = new ChoiceHandler();
095:
096: if (child == null) {
097: child = sth;
098: } else {
099: throw new SAXNotRecognizedException(LOCALNAME
100: + " may only have one child.");
101: }
102:
103: return sth;
104: }
105:
106: // sequence
107: if (SequenceHandler.LOCALNAME.equalsIgnoreCase(localName)) {
108: SequenceHandler sth = new SequenceHandler();
109:
110: if (child == null) {
111: child = sth;
112: } else {
113: throw new SAXNotRecognizedException(LOCALNAME
114: + " may only have one child.");
115: }
116:
117: return sth;
118: }
119: }
120:
121: return null;
122: }
123:
124: /**
125: * @see org.geotools.xml.XSIElementHandler#startElement(java.lang.String,
126: * java.lang.String, org.xml.sax.Attributes)
127: */
128: public void startElement(String namespaceURI, String localName,
129: Attributes atts) {
130: id = atts.getValue("", "id");
131:
132: if (id == null) {
133: id = atts.getValue(namespaceURI, "id");
134: }
135:
136: String max = atts.getValue("", "maxOccurs");
137:
138: if (max == null) {
139: max = atts.getValue(namespaceURI, "maxOccurs");
140: }
141:
142: String min = atts.getValue("", "minOccurs");
143:
144: if (min == null) {
145: min = atts.getValue(namespaceURI, "minOccurs");
146: }
147:
148: name = atts.getValue("", "name");
149:
150: if (name == null || "".equals(name)) {
151: name = atts.getValue(namespaceURI, "name");
152: }
153:
154: ref = atts.getValue("", "ref");
155:
156: if (ref == null || "".equals(ref)) {
157: ref = atts.getValue(namespaceURI, "ref"); // mutally exclusive with
158: }
159:
160: // name ...
161: if ((min != null) && !"".equalsIgnoreCase(min)) {
162: minOccurs = Integer.parseInt(min);
163: }
164:
165: if ((max != null) && !"".equalsIgnoreCase(max)) {
166: if ("unbounded".equalsIgnoreCase(max)) {
167: maxOccurs = ElementGrouping.UNBOUNDED;
168: } else {
169: maxOccurs = Integer.parseInt(max);
170: }
171: }
172: }
173:
174: /**
175: * @see org.geotools.xml.XSIElementHandler#getLocalName()
176: */
177: public String getLocalName() {
178: return LOCALNAME;
179: }
180:
181: /**
182: * <p>
183: * returns the group's name
184: * </p>
185: *
186: */
187: public String getName() {
188: return name;
189: }
190:
191: /**
192: * @see org.geotools.xml.XSIHandlers.ElementGroupingHandler#compress(org.geotools.xml.XSIHandlers.SchemaHandler)
193: */
194: protected ElementGrouping compress(SchemaHandler parent)
195: throws SAXException {
196:
197: synchronized (this ) {
198: if (cache != null)
199: return cache;
200: cache = new DefaultGroup();
201: }
202:
203: cache.id = this .id;
204: cache.name = this .name;
205: cache.namespace = parent.getTargetNamespace();
206: cache.min = this .minOccurs;
207: cache.max = this .maxOccurs;
208: cache.child = (this .child == null) ? null : this .child
209: .compress(parent); // deal with all/choice/sequnce
210: if (ref != null) {
211: Group g = parent.lookUpGroup(ref);
212: if (g != null) {
213: if ((id == null) || "".equalsIgnoreCase(id)) {
214: id = g.getId();
215: }
216:
217: cache.min = g.getMinOccurs();
218: cache.max = g.getMaxOccurs();
219: cache.name = g.getName();
220: cache.namespace = g.getNamespace();
221:
222: cache.child = (g.getChild() == null) ? cache.child : g
223: .getChild();
224: }
225: }
226:
227: child = null;
228:
229: return cache;
230: }
231:
232: /**
233: * @see org.geotools.xml.XSIElementHandler#getHandlerType()
234: */
235: public int getHandlerType() {
236: return DEFAULT;
237: }
238:
239: /**
240: * @see org.geotools.xml.XSIElementHandler#endElement(java.lang.String,
241: * java.lang.String)
242: */
243: public void endElement(String namespaceURI, String localName) {
244: // do nothing
245: }
246:
247: protected static class DefaultGroup implements Group {
248: protected ElementGrouping child;
249: protected String id;
250: protected int min, max;
251: protected String name;
252: protected URI namespace;
253:
254: /**
255: * TODO summary sentence for getChild ...
256: *
257: * @see org.geotools.xml.schema.Group#getChild()
258: */
259: public ElementGrouping getChild() {
260: return child;
261: }
262:
263: /**
264: * TODO summary sentence for getId ...
265: *
266: * @see org.geotools.xml.schema.Group#getId()
267: */
268: public String getId() {
269: return id;
270: }
271:
272: /**
273: * TODO summary sentence for getMaxOccurs ...
274: *
275: * @see org.geotools.xml.schema.Group#getMaxOccurs()
276: */
277: public int getMaxOccurs() {
278: return max;
279: }
280:
281: /**
282: * TODO summary sentence for getMinOccurs ...
283: *
284: * @see org.geotools.xml.schema.Group#getMinOccurs()
285: */
286: public int getMinOccurs() {
287: return min;
288: }
289:
290: /**
291: * TODO summary sentence for getName ...
292: *
293: * @see org.geotools.xml.schema.Group#getName()
294: */
295: public String getName() {
296: return name;
297: }
298:
299: /**
300: * TODO summary sentence for getNamespace ...
301: *
302: * @see org.geotools.xml.schema.Group#getNamespace()
303: */
304: public URI getNamespace() {
305: return namespace;
306: }
307:
308: /**
309: * TODO summary sentence for getGrouping ...
310: *
311: * @see org.geotools.xml.schema.ElementGrouping#getGrouping()
312: */
313: public int getGrouping() {
314: return GROUP;
315: }
316:
317: /**
318: * TODO summary sentence for findChildElement ...
319: *
320: * @see org.geotools.xml.schema.ElementGrouping#findChildElement(java.lang.String)
321: * @param arg1
322: */
323: public Element findChildElement(String arg1) {
324: return child == null ? null : child.findChildElement(arg1);
325: }
326:
327: public Element findChildElement(String localName,
328: URI namespaceURI) {
329: return child == null ? null : child.findChildElement(
330: localName, namespaceURI);
331: }
332:
333: }
334: }
|