001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.io;
034:
035: import org.xml.sax.Attributes;
036:
037: import com.vividsolutions.jump.feature.AttributeType;
038:
039: /**
040: * This is a helper class to store information about a JCS Column for the GML Parser ({@link GMLReader}). <br>
041: *
042: * Also has a function for checking if an XML tag matches this column specification.
043: */
044: public class ColumnDescription {
045: public static final int VALUE_IS_BODY = 1;
046: public static final int VALUE_IS_ATT = 2;
047: String columnName; //jcs column name
048: String tagName; //XML tag this is a part of
049: boolean tagNeedsAttribute = false; //true if the tag containing the value has a certain attribute in it
050: String attributeName; //name of this attribute
051: boolean tagAttributeNeedsValue = false; // true if the tag/attribute needs a value
052: String attributeValue; // actual value of that attribute
053: int valueType = VALUE_IS_BODY; //either VALUE_IS_BODY or VALUE_IS_ATT
054: String valueAttribute; //which attribute the value is in (only for VALUE_IS_ATT)
055: AttributeType type;
056:
057: //**constructor**/
058: public ColumnDescription() {
059: }
060:
061: /**
062: * Sets the [JCS] type of this column
063: *
064: * @param t JCS type that this column will contain (null means 'STRING')
065: **/
066: public void setType(AttributeType t) {
067: //<<TODO:DESIGN>> Shouldn't we use the Assert class to stipulate that
068: //t must not be null? [Jon Aquino]
069: if (t == null) {
070: type = AttributeType.STRING;
071: } else {
072: type = t;
073: }
074: }
075:
076: /**
077: * Returns the [JCS] type of this column
078: * cf. setType()
079: **/
080: public AttributeType getType() {
081: return type;
082: }
083:
084: /**
085: * Set the name of this column.
086: * @param colname name of the column
087: **/
088: public void setColumnName(String colname) {
089: columnName = colname;
090: }
091:
092: /**
093: * Sets the name of the XML tag that this column will be extracted from.
094: * @param tagname name of the XML tag
095: **/
096: public void setTagName(String tagname) {
097: tagName = tagname;
098: }
099:
100: /**
101: * Sets the name of the attribute (and its value) that the xml tag that this column will be extracted from.
102: *<pre>
103: * For example, the XML '<value type=name> DAVE </value>' would described by:
104: * setTagName('value');
105: * setTagAttribute('type','name');
106: *</pre>
107: *
108: * @param attName name of the XML attribute name
109: *@param attValue its value
110: **/
111: public void setTagAttribute(String attName, String attValue) {
112: attributeName = attName;
113: attributeValue = attValue;
114: tagNeedsAttribute = true;
115: tagAttributeNeedsValue = true;
116: }
117:
118: /**
119: * Sets the name of the attribute (with no value) that the xml tag that this column will be extracted from.
120: *<PRE>
121: * For example, the XML '<value name=david></value>' would described by:
122: * setTagName('value');
123: * setTagAttribute('name');
124: *</PRE>
125: *
126: * @param attName name of the XML attribute name
127: **/
128: public void setTagAttribute(String attName) {
129: attributeName = attName;
130: tagNeedsAttribute = true;
131: }
132:
133: /**
134: * Sets the name of the attribute that the actual column's value will be found.
135: *<PRE>
136: * For example, the XML '<value name=david></value>' would described by:
137: * setTagName('value');
138: * setTagAttribute('name');
139: * setValueAttribute('name');
140: *</PRE>
141: *
142: * NOTE: not calling this function will mean to get the column's value from the BODY
143: * of the tag.
144: *
145: *@param attName name of the attribute that the column's value will be extracted from
146: */
147: public void setValueAttribute(String attName) {
148: valueAttribute = attName;
149: valueType = VALUE_IS_ATT;
150: }
151:
152: /**
153: * Given a set of XML attributes associated with an XML tag, find the index of
154: * a given attribute in a case insensitive way.
155: *
156: * This is the case insensitive version of the standard function. Returns -1 if
157: * the attribute cannot be found
158: *
159: * @param atts XML attributes for a tag
160: * @param att_name name of the attribute
161: **/
162: int lookupAttribute(Attributes atts, String att_name) {
163: int t;
164:
165: for (t = 0; t < atts.getLength(); t++) {
166: if (atts.getQName(t).equalsIgnoreCase(att_name)) {
167: return t;
168: }
169: }
170:
171: return -1;
172: }
173:
174: /**
175: * Given an xml tag (its name and attributes), see if it matches this column description<br>
176: * If it doesnt, return 0 <br>
177: * If it does, return either VALUE_IS_BODY or VALUE_IS_ATTRIBUTE<br>
178: * @param XMLtagName name of the xml tag
179: * @param xmlAtts list of the xml attributes for the tag (cf. xerces or SAX)
180: */
181: public int match(String XMLtagName, Attributes xmlAtts) {
182: int attindex;
183:
184: if (XMLtagName.compareToIgnoreCase(tagName) == 0) {
185: //tags match
186: if (tagNeedsAttribute) {
187: //attindex = xmlAtts.getIndex(attributeName);
188: attindex = lookupAttribute(xmlAtts, attributeName);
189:
190: if (attindex == -1) {
191: return 0; //doesnt have the required attribute
192: }
193:
194: if (tagAttributeNeedsValue) {
195: if (xmlAtts.getValue(attindex).compareToIgnoreCase(
196: attributeValue) != 0) {
197: return 0; // attribute doesnt have the correct value
198: }
199: }
200: }
201:
202: return valueType;
203: }
204:
205: return 0; // not the right tag
206: }
207: }
|