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