1   /*
2    *  FeatureSchema.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Cristian URSU, 27/Sept/2000
12   *
13   *  $Id: FeatureSchema.java,v 1.12 2001/04/25 12:23:50 cursu Exp $
14   */
15  
16  package gate.creole;
17  
18  import java.util.*;
19  import java.io.*;
20  
21  import gate.util.*;
22  
23  /**
24    * This class describes a schema for a feature. It is used as part of
25    * {@link gate.creole.AnnotationSchema} class.
26    */
27  public class FeatureSchema implements Serializable {
28  
29    /** Debug flag */
30    private static final boolean DEBUG = false;
31  
32    /** The name of this feature. */
33    String featureName = null;
34  
35    /** The class name of the feature value*/
36    String featureValueClassName = null;
37  
38    /** The value of the feature. This must be read only when "use" is default
39      * or fixed.
40      */
41    String featureValue = null;
42  
43    /** The use of that feature can be one of:
44      *  prohibited | optional | required | default | fixed : optional
45      */
46    String featureUse = null;
47  
48    /** The default or fixed value for that feature*/
49  
50    /** Permisible value set, if appropriate. */
51    Set featurePermissibleValuesSet = null;
52  
53    /** Construction given a name of an feature and a feature value class name
54     */
55    public FeatureSchema( String aFeatureName,
56                          String aFeatureValueClassName,
57                          String aFeatureValue,
58                          String aFeatureUse,
59                          Set    aFeaturePermissibleValuesSet ){
60  
61      featureName                 = aFeatureName;
62      featureValueClassName       = aFeatureValueClassName;
63      featureValue                = aFeatureValue;
64      featureUse                  = aFeatureUse;
65      featurePermissibleValuesSet = aFeaturePermissibleValuesSet;
66    }
67  
68    /** Tests whether the values are an enumeration or not. */
69    public boolean isEnumeration() {
70      return featurePermissibleValuesSet != null;
71    }// isEnumeration()
72  
73    /** Get the feature name */
74    public String getFeatureName() {
75      return featureName;
76    }// getFeatureName()
77  
78  
79    /** Get the feature value class name */
80    public String getValueClassName() {
81      return featureValueClassName;
82    }// getValueClassName()
83  
84    /** Returns the permissible values as a Set*/
85    public Set getPermissibleValues() {
86      return featurePermissibleValuesSet;
87    }// getPermissibleValues()
88  
89  
90    /** Adds all values from the given set as permissible values for
91      * the given feature. No check is performed to see if the
92      * class name of the feature value is the same as the
93      * the elements of the given set. Returns true if the set has been assigned.
94      */
95    public boolean setPermissibleValues(Set aPermisibleValuesSet) {
96      featurePermissibleValuesSet.clear();
97      return featurePermissibleValuesSet.addAll(aPermisibleValuesSet);
98    }// setPermissibleValues()
99  
100   /** Adds a value to the enumeration of permissible value for an
101     * feature of this type. Returns false, i.e. fails, if the
102     * class name of the feature value does not match the class name
103     * of the given object
104     *
105     * @param obj the object representing a permissible value. If null then
106     *  simply returns with false.
107     */
108   public boolean addPermissibleValue(Object obj) {
109     if(obj == null) return false;
110     if (! obj.getClass().getName().equals(featureValueClassName))
111         return false;
112     if (featurePermissibleValuesSet == null)
113         featurePermissibleValuesSet = new HashSet();
114     return featurePermissibleValuesSet.add(obj);
115   }// addPermissibleValue()
116 
117   /** This method transforms a feature to its XSchema representation. It is used
118     * in toXSchema().
119     *
120     * @param aJava2XSchemaMap a Java map object that will be serialized in XSchema
121     * @return a String containing the XSchema representation
122     */
123   public String toXSchema(Map aJava2XSchemaMap){
124 
125     StringBuffer schemaString = new StringBuffer();
126     schemaString.append("<attribute name=\"" + featureName + "\" ");
127     schemaString.append("use=\"" + featureUse + "\"");
128 
129     // If there are no permissible values that means that the type must
130     // be specified as an attribute for the attribute element
131     if (!isEnumeration())
132       schemaString.append(" type=\"" +
133           (String) aJava2XSchemaMap.get(featureValueClassName) + "\"/>\n");
134     else {
135       schemaString.append(">\n <simpleType>\n");
136       schemaString.append("  <restriction base=\"" + featureValueClassName +
137                                                                      "\">\n");
138       Iterator featurePermissibleValuesSetIterator =
139                                featurePermissibleValuesSet.iterator();
140 
141       while (featurePermissibleValuesSetIterator.hasNext()){
142         String featurePermissibleValue =
143                     (String) featurePermissibleValuesSetIterator.next();
144         schemaString.append("   <enumeration value=\"" +
145                             featurePermissibleValue + "\"/>\n");
146       }// end while
147 
148       schemaString.append("  </restriction>\n");
149       schemaString.append(" </simpleType>\n");
150       schemaString.append("</attribute>\n");
151 
152     }// end if else
153 
154     return schemaString.toString();
155   } // end toXSchema
156 
157   /** This method returns the value of the feature.
158     * If featureUse is something else than "default" or "fixed" it will return
159     * the empty string "".
160     */
161   public String getFeatureValue(){
162     if (isDefault() || isFixed())
163       return featureValue;
164     else
165       return "";
166   } // getFeatureValue
167 
168   /** This method sets the value of the feature.
169     * @param aFeatureValue a String representing the value of a feature.
170     */
171   public void setFeatureValue(String aFeatureValue){
172     featureValue = aFeatureValue;
173   } // setFeatureValue
174 
175   /**
176     * This method is used to check if the feature is required.
177     * @return true if the feature is required. Otherwhise returns false
178     */
179   public boolean isRequired(){
180     return "required".equals(featureUse);
181   } // isRequired
182 
183   /** This method is used to check if the feature is default.
184     * Default is used if the feature was omitted.
185     * @return true if the feature is default. Otherwhise returns false
186     */
187   public boolean isDefault(){
188     return "default".equals(featureUse);
189   } // isDefault
190 
191   /** This method is used to check if the feature, is fixed.
192     * @return true if the feature is fixed. Otherwhise returns false
193     */
194   public boolean isFixed(){
195     return "fixed".equals(featureUse);
196   } // isFixed
197 
198   /** This method is used to check if the feature is optional.
199     * @return true if the optional is fixed. Otherwhise returns false
200     */
201   public boolean isOptional(){
202     return "optional".equals(featureUse);
203   } // isOptional
204 
205   /** This method is used to check if the feature is prohibited.
206     * @return true if the prohibited is fixed. Otherwhise returns false
207     */
208   public boolean isProhibited(){
209     return "prohibited".equals(featureUse);
210   } // isProhibited
211 
212 } // FeatureSchema
213