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.net.URISyntaxException;
020:
021: import org.geotools.xml.XSIElementHandler;
022: import org.geotools.xml.schema.Any;
023: import org.geotools.xml.schema.Element;
024: import org.geotools.xml.schema.ElementGrouping;
025: import org.xml.sax.Attributes;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * AnyHandler purpose.
030: *
031: * <p>
032: * Represents an 'any' 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/AnyHandler.java $
038: * @version $Id: AnyHandler.java 22266 2006-10-19 11:30:55Z acuster $
039: */
040: public class AnyHandler extends ElementGroupingHandler {
041: /** 'any' */
042: public final static String LOCALNAME = "any";
043:
044: /** strict */
045: public static final int STRICT = 0;
046:
047: /** lax */
048: public static final int LAX = 1;
049:
050: /** skip */
051: public static final int SKIP = 2;
052: private String id;
053: private URI namespace;
054: private int minOccurs;
055: private int maxOccurs;
056:
057: // private int processContents;
058: private DefaultAny cache = null;
059:
060: /**
061: * @see java.lang.Object#hashCode()
062: */
063: public int hashCode() {
064: return (LOCALNAME.hashCode() * ((id == null) ? 1 : id
065: .hashCode()))
066: + (minOccurs * maxOccurs);
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) {
075: return null;
076: }
077:
078: /**
079: * @see org.geotools.xml.XSIElementHandler#startElement(java.lang.String,
080: * java.lang.String, org.xml.sax.Attributes)
081: */
082: public void startElement(String namespaceURI, String localName,
083: Attributes atts) throws SAXException {
084: id = atts.getValue("", "id");
085:
086: if (id == null) {
087: id = atts.getValue(namespaceURI, "id");
088: }
089:
090: String min = atts.getValue("", "minOccurs");
091:
092: if (min == null) {
093: min = atts.getValue(namespaceURI, "minOccurs");
094: }
095:
096: String max = atts.getValue("", "maxOccurs");
097:
098: if (max == null) {
099: max = atts.getValue(namespaceURI, "maxOccurs");
100: }
101:
102: String namespace1 = atts.getValue("", "namespace");
103:
104: if (namespace1 == null) {
105: namespace1 = atts.getValue(namespaceURI, "namespace");
106: }
107:
108: try {
109: if (namespace1 != null) {
110: if (namespace1.toLowerCase().equals("##any")) {
111: this .namespace = Any.ALL;
112: } else {
113: if (namespace1.toLowerCase().equals("##other")) {
114: // TODO improve this
115: this .namespace = Any.ALL;
116: } else {
117: if (namespace1.toLowerCase().equals(
118: "##targetNamespace")) {
119: try {
120: this .namespace = new URI(namespaceURI);
121: } catch (URISyntaxException e) {
122: logger.warning(e.toString());
123: this .namespace = new URI(namespace1);
124: }
125: } else {
126: this .namespace = new URI(namespace1);
127: }
128: }
129: }
130: }
131: } catch (URISyntaxException e) {
132: logger.warning(e.toString());
133: throw new SAXException(e);
134: }
135:
136: if ((null == min) || "".equalsIgnoreCase(min)) {
137: minOccurs = 1;
138: } else {
139: minOccurs = Integer.parseInt(min);
140: }
141:
142: if ((null == max) || "".equalsIgnoreCase(max)) {
143: maxOccurs = 1;
144: } else {
145: if ("unbounded".equalsIgnoreCase(max)) {
146: maxOccurs = ElementGrouping.UNBOUNDED;
147: } else {
148: maxOccurs = Integer.parseInt(max);
149: }
150: }
151: }
152:
153: /**
154: * @see org.geotools.xml.XSIElementHandler#getLocalName()
155: */
156: public String getLocalName() {
157: return LOCALNAME;
158: }
159:
160: /**
161: * <p>
162: * maps strings -> int constants for the 'process' attribute
163: * </p>
164: *
165: * @param process
166: *
167: *
168: * @throws SAXException
169: */
170: public static int findProcess(String process) throws SAXException {
171: if ((process == null) || "".equalsIgnoreCase(process)) {
172: return STRICT;
173: }
174:
175: if ("lax".equalsIgnoreCase(process)) {
176: return LAX;
177: }
178:
179: if ("skip".equalsIgnoreCase(process)) {
180: return SKIP;
181: }
182:
183: if ("strict".equalsIgnoreCase(process)) {
184: return STRICT;
185: }
186:
187: throw new SAXException("Unknown Process Type: '" + process
188: + "'");
189: }
190:
191: /**
192: * <p>
193: * reverses the findProcess method, converting from integers to String for
194: * the process attribute.
195: * </p>
196: *
197: * @param process
198: *
199: */
200: public static String writeProcess(int process) {
201: switch (process) {
202: case LAX:
203: return "lax";
204:
205: case SKIP:
206: return "skip";
207:
208: case STRICT:
209: default:
210: return "strict";
211: }
212: }
213:
214: /**
215: * @see org.geotools.xml.XSIHandlers.ElementGroupingHandler#compress(org.geotools.xml.XSIHandlers.SchemaHandler)
216: */
217: protected ElementGrouping compress(SchemaHandler parent) {
218: synchronized (this ) {
219: if (cache != null)
220: return cache;
221: cache = new DefaultAny();
222: }
223: cache.id = id;
224: cache.namespace = namespace;
225: cache.minOccurs = minOccurs;
226: cache.maxOccurs = maxOccurs;
227:
228: id = null;
229: namespace = null;
230:
231: return cache;
232: }
233:
234: /**
235: * @see org.geotools.xml.XSIElementHandler#getHandlerType()
236: */
237: public int getHandlerType() {
238: return DEFAULT;
239: }
240:
241: /**
242: * @see org.geotools.xml.XSIElementHandler#endElement(java.lang.String,
243: * java.lang.String)
244: */
245: public void endElement(String namespaceURI, String localName) {
246: // do nothing
247: }
248:
249: /**
250: * <p>
251: * Any instance implementation
252: * </p>
253: *
254: * @author dzwiers
255: *
256: * @see Any
257: */
258: private static class DefaultAny implements Any {
259: String id;
260: URI namespace;
261: int maxOccurs;
262: int minOccurs;
263:
264: public Element findChildElement(String name) {
265: //TODO look up namespace Schema and do this correctly
266: return null;
267: }
268:
269: /**
270: * @see org.geotools.xml.xsi.Any#getId()
271: */
272: public String getId() {
273: return id;
274: }
275:
276: /**
277: * @see org.geotools.xml.xsi.ElementGrouping#getMaxOccurs()
278: */
279: public int getMaxOccurs() {
280: return maxOccurs;
281: }
282:
283: /**
284: * @see org.geotools.xml.xsi.ElementGrouping#getMinOccurs()
285: */
286: public int getMinOccurs() {
287: return minOccurs;
288: }
289:
290: /**
291: * @see org.geotools.xml.xsi.Any#getNamespace()
292: */
293: public URI getNamespace() {
294: return namespace;
295: }
296:
297: /**
298: * @see org.geotools.xml.xsi.ElementGrouping#getGrouping()
299: */
300: public int getGrouping() {
301: return ANY;
302: }
303:
304: public Element findChildElement(String localName,
305: URI namespaceURI) {
306: //TODO look up namespace Schema and do this correctly
307: return null;
308: }
309: }
310: }
|