001 /*
002 * FeatureSchema.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Cristian URSU, 27/Sept/2000
013 *
014 * $Id: FeatureSchema.java 13142 2010-10-14 08:49:36Z markagreenwood $
015 */
016
017 package gate.creole;
018
019 import java.io.Serializable;
020 import java.util.*;
021
022 /**
023 * This class describes a schema for a feature. It is used as part of
024 * {@link gate.creole.AnnotationSchema} class.
025 */
026 public class FeatureSchema implements Serializable {
027
028 /** Debug flag */
029 private static final boolean DEBUG = false;
030
031 /** The name of this feature. */
032 String featureName = null;
033
034 /** The class name of the feature value */
035 Class<?> featureValueClass = null;
036
037 /**
038 * The value of the feature. This must be read only when "use" is
039 * default or fixed.
040 */
041 String featureValue = null;
042
043 /**
044 * The use of that feature can be one of: prohibited | optional |
045 * required | default | fixed : optional
046 */
047 String featureUse = null;
048
049 /** The default or fixed value for that feature */
050
051 /** Permisible value set, if appropriate. */
052 Set featurePermissibleValuesSet = null;
053
054 /**
055 * Construction given a name of an feature and a feature value class
056 * name
057 */
058 public FeatureSchema(String aFeatureName, Class<?> aFeatureValueClass,
059 String aFeatureValue, String aFeatureUse,
060 Set aFeaturePermissibleValuesSet) {
061
062 featureName = aFeatureName;
063 featureValueClass = aFeatureValueClass;
064 featureValue = aFeatureValue;
065 featureUse = aFeatureUse;
066 featurePermissibleValuesSet = aFeaturePermissibleValuesSet;
067 }
068
069 /** Tests whether the values are an enumeration or not. */
070 public boolean isEnumeration() {
071 return featurePermissibleValuesSet != null;
072 }// isEnumeration()
073
074 /** Get the feature name */
075 public String getFeatureName() {
076 return featureName;
077 }// getFeatureName()
078
079 /**
080 * @return the featureValueClass
081 */
082 public Class<?> getFeatureValueClass() {
083 return featureValueClass;
084 }
085
086 /** Returns the permissible values as a Set */
087 public Set getPermittedValues() {
088 return featurePermissibleValuesSet;
089 }// getPermissibleValues()
090
091 /**
092 * Adds all values from the given set as permissible values for the
093 * given feature. No check is performed to see if the class name of
094 * the feature value is the same as the the elements of the given set.
095 * Returns true if the set has been assigned.
096 */
097 public boolean setPermissibleValues(Set aPermisibleValuesSet) {
098 featurePermissibleValuesSet.clear();
099 return featurePermissibleValuesSet.addAll(aPermisibleValuesSet);
100 }// setPermissibleValues()
101
102 /**
103 * Adds a value to the enumeration of permissible value for an feature
104 * of this type. Returns false, i.e. fails, if the class name of the
105 * feature value does not match the class name of the given object
106 *
107 * @param obj the object representing a permissible value. If null
108 * then simply returns with false.
109 */
110 public boolean addPermissibleValue(Object obj) {
111 if(obj == null) return false;
112 if(!obj.getClass().getName().equals(featureValueClass)) return false;
113 if(featurePermissibleValuesSet == null)
114 featurePermissibleValuesSet = new HashSet();
115 return featurePermissibleValuesSet.add(obj);
116 }// addPermissibleValue()
117
118 /**
119 * This method transforms a feature to its XSchema representation. It
120 * is used in toXSchema().
121 *
122 * @param aJava2XSchemaMap a Java map object that will be serialized
123 * in XSchema
124 * @return a String containing the XSchema representation
125 */
126 public String toXSchema(Map aJava2XSchemaMap) {
127
128 StringBuffer schemaString = new StringBuffer();
129 schemaString.append(" <attribute name=\"" + featureName + "\" ");
130 schemaString.append("use=\"" + featureUse + "\"");
131
132 // If there are no permissible values that means that the type must
133 // be specified as an attribute for the attribute element
134 if(!isEnumeration())
135 schemaString.append(" type=\""
136 + (String)aJava2XSchemaMap.get(featureValueClass) + "\"/>\n");
137 else {
138 schemaString.append(">\n <simpleType>\n");
139 schemaString.append(" <restriction base=\""
140 + (String)aJava2XSchemaMap.get(featureValueClass) + "\">\n");
141 Iterator featurePermissibleValuesSetIterator = featurePermissibleValuesSet
142 .iterator();
143
144 while(featurePermissibleValuesSetIterator.hasNext()) {
145 String featurePermissibleValue = (String)featurePermissibleValuesSetIterator
146 .next();
147 schemaString.append(" <enumeration value=\""
148 + featurePermissibleValue + "\"/>\n");
149 }// end while
150
151 schemaString.append(" </restriction>\n");
152 schemaString.append(" </simpleType>\n");
153 schemaString.append(" </attribute>\n");
154
155 }// end if else
156
157 return schemaString.toString();
158 } // end toXSchema
159
160 /**
161 * This method returns the value of the feature. If featureUse is
162 * something else than "default" or "fixed" it will return the empty
163 * string "".
164 */
165 public String getFeatureValue() {
166 if(isDefault() || isFixed())
167 return featureValue;
168 else return "";
169 } // getFeatureValue
170
171 /**
172 * This method returns the value of the feature regardless of the
173 * current value of featureUse.
174 */
175 public String getRawFeatureValue() {
176 return featureValue;
177 } // getRawFeatureValue
178
179 /**
180 * This method sets the value of the feature.
181 *
182 * @param aFeatureValue a String representing the value of a feature.
183 */
184 public void setFeatureValue(String aFeatureValue) {
185 featureValue = aFeatureValue;
186 } // setFeatureValue
187
188 /**
189 * This method is used to check if the feature is required.
190 *
191 * @return true if the feature is required. Otherwhise returns false
192 */
193 public boolean isRequired() {
194 return "required".equals(featureUse);
195 } // isRequired
196
197 /**
198 * This method is used to check if the feature is default. Default is
199 * used if the feature was omitted.
200 *
201 * @return true if the feature is default. Otherwhise returns false
202 */
203 public boolean isDefault() {
204 return "default".equals(featureUse);
205 } // isDefault
206
207 /**
208 * This method is used to check if the feature, is fixed.
209 *
210 * @return true if the feature is fixed. Otherwhise returns false
211 */
212 public boolean isFixed() {
213 return "fixed".equals(featureUse);
214 } // isFixed
215
216 /**
217 * This method is used to check if the feature is optional.
218 *
219 * @return true if the optional is fixed. Otherwhise returns false
220 */
221 public boolean isOptional() {
222 return "optional".equals(featureUse);
223 } // isOptional
224
225 /**
226 * This method is used to check if the feature is prohibited.
227 *
228 * @return true if the prohibited is fixed. Otherwhise returns false
229 */
230 public boolean isProhibited() {
231 return "prohibited".equals(featureUse);
232 } // isProhibited
233
234 } // FeatureSchema
|