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 org.geotools.xml.XSIElementHandler;
019: import org.geotools.xml.schema.Attribute;
020: import org.geotools.xml.schema.SimpleType;
021: import org.geotools.xml.schema.impl.AttributeGT;
022: import org.xml.sax.Attributes;
023: import org.xml.sax.SAXException;
024: import org.xml.sax.SAXNotRecognizedException;
025:
026: /**
027: * AttributeHandler purpose.
028: *
029: * <p>
030: * Represents an 'attribute' element
031: * </p>
032: *
033: * <p>
034: * Example Use:
035: * <pre><code>
036: *
037: * AttributeHandler x = new AttributeHandler(...);
038: *
039: * </code></pre>
040: * </p>
041: *
042: * @author dzwiers, Refractions Research, Inc. http://www.refractions.net
043: * @author $Author:$ (last modification)
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/xsi/AttributeHandler.java $
045: * @version $Id: AttributeHandler.java 22266 2006-10-19 11:30:55Z acuster $
046: */
047: public class AttributeHandler extends XSIElementHandler {
048: /** 'attribute' */
049: public final static String LOCALNAME = "attribute";
050:
051: /** OPTIONAL */
052: public static final int OPTIONAL = 0;
053:
054: /** PROHIBITED */
055: public static final int PROHIBITED = 1;
056:
057: /** REQUIRED */
058: public static final int REQUIRED = 2;
059: private static int offset = 0;
060: private String id;
061: private String name;
062: private String type;
063: private String ref;
064: private String def;
065: private String fixed;
066: private int use;
067: private SimpleTypeHandler simpleType;
068: private int hashCodeOffset = getOffset();
069: private Attribute cache = null;
070:
071: /*
072: * hashCode() helper
073: */
074: private static int getOffset() {
075: return offset++;
076: }
077:
078: /**
079: * @see java.lang.Object#hashCode()
080: */
081: public int hashCode() {
082: return (LOCALNAME.hashCode()
083: * ((id == null) ? 1 : id.hashCode())
084: * ((type == null) ? 1 : type.hashCode()) * ((name == null) ? 1
085: : name.hashCode()))
086: + hashCodeOffset;
087: }
088:
089: /**
090: * @see org.geotools.xml.XSIElementHandler#getHandler(java.lang.String,
091: * java.lang.String)
092: */
093: public XSIElementHandler getHandler(String namespaceURI,
094: String localName) throws SAXException {
095: if (SchemaHandler.namespaceURI.equalsIgnoreCase(namespaceURI)) {
096: // child types
097: //
098: // simpleType
099: if (SimpleTypeHandler.LOCALNAME.equalsIgnoreCase(localName)) {
100: SimpleTypeHandler sth = new SimpleTypeHandler();
101:
102: if (simpleType == null) {
103: simpleType = sth;
104: } else {
105: throw new SAXNotRecognizedException(
106: "Extension may only have one 'simpleType' or 'complexType' declaration.");
107: }
108:
109: return sth;
110: }
111: }
112:
113: return null;
114: }
115:
116: /**
117: * @see org.geotools.xml.XSIElementHandler#startElement(java.lang.String,
118: * java.lang.String, org.xml.sax.Attributes)
119: */
120: public void startElement(String namespaceURI, String localName,
121: Attributes atts) {
122: id = atts.getValue("", "id");
123:
124: if (id == null) {
125: id = atts.getValue(namespaceURI, "id");
126: }
127:
128: String name1 = atts.getValue("", "name");
129:
130: if (name1 == null) {
131: name1 = atts.getValue(namespaceURI, "name");
132: }
133:
134: String ref1 = atts.getValue("", "ref");
135:
136: if (ref1 == null) {
137: ref1 = atts.getValue(namespaceURI, "ref");
138: }
139:
140: String type1 = atts.getValue("", "type");
141:
142: if (type1 == null) {
143: type1 = atts.getValue(namespaceURI, "type");
144: }
145:
146: this .name = name1;
147: this .type = type1;
148: this .ref = ref1;
149:
150: def = atts.getValue("", "default");
151:
152: if (def == null) {
153: def = atts.getValue(namespaceURI, "default");
154: }
155:
156: fixed = atts.getValue("", "fixed");
157:
158: if (fixed == null) {
159: fixed = atts.getValue(namespaceURI, "fixed");
160: }
161:
162: // form -- Ignore
163: String use1 = atts.getValue("", "use");
164:
165: if (use1 == null) {
166: use1 = atts.getValue(namespaceURI, "use");
167: }
168:
169: this .use = findUse(use1);
170: }
171:
172: /**
173: * @see org.geotools.xml.XSIElementHandler#getLocalName()
174: */
175: public String getLocalName() {
176: return LOCALNAME;
177: }
178:
179: /**
180: * <p>
181: * Convert the 'use' attribute to an int mask
182: * </p>
183: *
184: * @param use
185: *
186: */
187: public static int findUse(String use) {
188: if ("optional".equalsIgnoreCase(use)) {
189: return OPTIONAL;
190: }
191:
192: if ("prohibited".equalsIgnoreCase(use)) {
193: return PROHIBITED;
194: }
195:
196: if ("required".equalsIgnoreCase(use)) {
197: return REQUIRED;
198: }
199:
200: return -1;
201: }
202:
203: /**
204: * <p>
205: * converts an int mask representing use to the string representation
206: * </p>
207: *
208: * @param use
209: *
210: */
211: public static String writeUse(int use) {
212: switch (use) {
213: case OPTIONAL:
214: return "optional";
215:
216: case PROHIBITED:
217: return "prohibited";
218:
219: case REQUIRED:
220: return "required";
221:
222: default:
223: return null;
224: }
225: }
226:
227: /**
228: * <p>
229: * Returns the attribute name
230: * </p>
231: *
232: */
233: public String getName() {
234: return name;
235: }
236:
237: /**
238: * <p>
239: * creates a smaller simpler version
240: * </p>
241: *
242: * @param parent
243: *
244: *
245: * @throws SAXException
246: */
247: protected Attribute compress(SchemaHandler parent)
248: throws SAXException {
249: if (cache != null) {
250: return cache;
251: }
252:
253: // a.form = form; TODO add form support?
254: SimpleType st = null;
255: String name1 = this .name;
256: String def1 = this .def;
257: String fixed1 = this .fixed;
258: int use1 = this .use;
259:
260: if (simpleType != null) {
261: st = simpleType.compress(parent);
262: } else {
263: if ((ref != null) && !"".equalsIgnoreCase(ref)) {
264: Attribute refA = parent.lookUpAttribute(ref);
265:
266: if (refA == null) {
267: throw new SAXException("Attribute '" + ref
268: + "' was refered and not found");
269: }
270:
271: st = refA.getSimpleType();
272: name1 = refA.getName();
273: use1 = use1 | refA.getUse();
274:
275: if ((def1 == null) || "".equalsIgnoreCase(def1)) {
276: def1 = refA.getDefault();
277: }
278:
279: if ((fixed1 == null) || "".equalsIgnoreCase(fixed1)) {
280: fixed1 = refA.getFixed();
281: }
282: } else if ((type != null) && (!"".equalsIgnoreCase(type))) {
283: // look it up --- find it
284: st = parent.lookUpSimpleType(type);
285: }
286: }
287:
288: cache = new AttributeGT(id, name1, parent.getTargetNamespace(),
289: st, use1, def1, fixed1, false);
290:
291: id = type = ref = null;
292:
293: return cache;
294: }
295:
296: /**
297: * @see org.geotools.xml.XSIElementHandler#getHandlerType()
298: */
299: public int getHandlerType() {
300: return DEFAULT;
301: }
302:
303: /**
304: * @see org.geotools.xml.XSIElementHandler#endElement(java.lang.String,
305: * java.lang.String)
306: */
307: public void endElement(String namespaceURI, String localName) {
308: // do nothing
309: }
310: }
|