1
15 package gate.creole;
16
17 import java.io.InputStream;
18 import java.net.URL;
19 import java.util.*;
20
21 import org.jdom.JDOMException;
22 import org.jdom.Namespace;
23 import org.jdom.input.SAXBuilder;
24
25 import gate.Resource;
26
27
31 public class AnnotationSchema extends AbstractLanguageResource{
32 public static final String FILE_URL_PARAM_NAME = "xmlFileUrl";
33
34
35 private static final boolean DEBUG = false;
36
37
38 private static Map xSchema2JavaMap;
39
40
41 private static Map java2xSchemaMap;
42
43
46 private static void setUpStaticData()
47 throws ResourceInstantiationException
48 {
49 xSchema2JavaMap = new HashMap();
50 java2xSchemaMap = new HashMap();
51
52 xSchema2JavaMap.put("string", String.class.getName());
53 xSchema2JavaMap.put("integer", Integer.class.getName());
54 xSchema2JavaMap.put("int", Integer.class.getName() );
55 xSchema2JavaMap.put("boolean", Boolean.class.getName());
56 xSchema2JavaMap.put("float", Float.class.getName());
57 xSchema2JavaMap.put("double", Double.class.getName());
58 xSchema2JavaMap.put("short", Short.class.getName());
59 xSchema2JavaMap.put("byte", Byte.class.getName());
60
61 java2xSchemaMap.put(String.class.getName(), "string");
62 java2xSchemaMap.put(Integer.class.getName(), "integer");
63 java2xSchemaMap.put(Boolean.class.getName(), "boolean");
64 java2xSchemaMap.put(Float.class.getName(), "float");
65 java2xSchemaMap.put(Double.class.getName(), "double");
66 java2xSchemaMap.put(Short.class.getName(), "short");
67 java2xSchemaMap.put(Byte.class.getName(), "byte");
68 }
70
71 protected String annotationName = null;
72
73
74 public String getAnnotationName(){
75 return annotationName;
76 }
78
79 public void setAnnotationName(String annotationName) {
80 this.annotationName = annotationName;
81 }
83
84 protected Set featureSchemaSet = null;
85
86
87 public AnnotationSchema(){
88 }
90
91 public Set getFeatureSchemaSet(){
92 return featureSchemaSet;
93 }
95
96 public void setFeatureSchemaSet(Set featureSchemaSet) {
97 this.featureSchemaSet = featureSchemaSet;
98 }
100
103 public FeatureSchema getFeatureSchema(String featureName) {
104 if(featureSchemaSet == null) return null;
105 Iterator fsIterator = featureSchemaSet.iterator();
106 while (fsIterator.hasNext()) {
107 FeatureSchema fs = (FeatureSchema) fsIterator.next();
108 if (fs.getFeatureName().equals(featureName) )
109 return fs;
110 }
111 return null;
112 }
114
117 public Resource init() throws ResourceInstantiationException {
118 if(xSchema2JavaMap == null || java2xSchemaMap == null)
120 setUpStaticData();
121
122 if(xmlFileUrl != null) fromXSchema(xmlFileUrl);
124
125 return this;
126 }
128
129 protected URL xmlFileUrl;
130
131
134 protected Namespace namespace;
135
136
137 public void setXmlFileUrl(URL xmlFileUrl) { this.xmlFileUrl = xmlFileUrl; }
138
139
140 public URL getXmlFileUrl() { return xmlFileUrl; }
141
142
145 public void fromXSchema(URL anXSchemaURL)
146 throws ResourceInstantiationException {
147 org.jdom.Document jDom = null;
148 SAXBuilder saxBuilder = new SAXBuilder(false);
149 try {
150 try{
151 jDom = saxBuilder.build(anXSchemaURL);
152 }catch(JDOMException je){
153 throw new ResourceInstantiationException(je);
154 }
155 } catch (java.io.IOException ex) {
156 throw new ResourceInstantiationException(ex);
157 }
158 workWithJDom(jDom);
159 }
161
164 public void fromXSchema(InputStream anXSchemaInputStream)
165 throws ResourceInstantiationException {
166 org.jdom.Document jDom = null;
167 SAXBuilder saxBuilder = new SAXBuilder(false);
168 try {
169 try{
170 jDom = saxBuilder.build(anXSchemaInputStream);
171 }catch(JDOMException je){
172 throw new ResourceInstantiationException(je);
173 }
174 } catch (java.io.IOException ex) {
175 throw new ResourceInstantiationException(ex);
176 }
177 workWithJDom(jDom);
178 }
180
187 private void workWithJDom(org.jdom.Document jDom){
188 org.jdom.Element rootElement = jDom.getRootElement();
190 namespace = rootElement.getNamespace();
191 List rootElementChildrenList = rootElement.getChildren("element", namespace);
193 Iterator rootElementChildrenIterator = rootElementChildrenList.iterator();
194 while (rootElementChildrenIterator.hasNext()){
195 org.jdom.Element childElement =
196 (org.jdom.Element) rootElementChildrenIterator.next();
197 createAnnotationSchemaObject(childElement);
198 } }
201
204 private void createAnnotationSchemaObject(org.jdom.Element anElement){
205 annotationName = anElement.getAttributeValue("name");
208 if (annotationName == null)
209 annotationName = "UnknownElement";
210 org.jdom.Element complexTypeElement = anElement.getChild("complexType",
212 namespace);
213 if (complexTypeElement != null){
214 List complexTypeCildrenList = complexTypeElement.getChildren("attribute",
215 namespace);
216 Iterator complexTypeCildrenIterator = complexTypeCildrenList.iterator();
217 if (complexTypeCildrenIterator.hasNext())
218 featureSchemaSet = new HashSet();
219 while (complexTypeCildrenIterator.hasNext()) {
220 org.jdom.Element childElement =
221 (org.jdom.Element) complexTypeCildrenIterator.next();
222 createAndAddFeatureSchemaObject(childElement);
223 } } }
227
231 public void createAndAddFeatureSchemaObject(org.jdom.Element
232 anAttributeElement) {
233 String featureName = null;
234 String featureType = null;
235 String featureUse = null;
236 String featureValue = null;
237 Set featurePermissibleValuesSet = null;
238
239 featureName = anAttributeElement.getAttributeValue("name");
242 if (featureName == null)
243 featureName = "UnknownFeature";
244
245 featureType = anAttributeElement.getAttributeValue("type");
247 if (featureType != null)
248 featureType = (String) xSchema2JavaMap.get(featureType);
250
251 featureUse = anAttributeElement.getAttributeValue("use");
253 if (featureUse == null)
254 featureUse = "optional";
256
257 featureValue = anAttributeElement.getAttributeValue("value");
259 if (featureValue == null)
260 featureValue = "";
261
262 org.jdom.Element simpleTypeElement =
264 anAttributeElement.getChild("simpleType",
265 namespace);
266
267 if (simpleTypeElement != null) {
269 org.jdom.Element restrictionElement =
270 simpleTypeElement.getChild("restriction",
271 namespace);
272 if (restrictionElement != null) {
273 featureType = restrictionElement.getAttributeValue("base");
275
276 if (featureType == null)
279 featureType = (String) xSchema2JavaMap.get("string");
281 else
282 featureType = (String) xSchema2JavaMap.get(featureType);
284
285 List enumerationElementChildrenList =
287 restrictionElement.getChildren("enumeration",
288 namespace);
289 Iterator enumerationChildrenIterator =
290 enumerationElementChildrenList.iterator();
291
292 if (enumerationChildrenIterator.hasNext())
294 featurePermissibleValuesSet = new HashSet();
295 while (enumerationChildrenIterator.hasNext()) {
296 org.jdom.Element enumerationElement =
297 (org.jdom.Element) enumerationChildrenIterator.next();
298 String permissibleValue =
299 enumerationElement.getAttributeValue("value");
300 featurePermissibleValuesSet.add(permissibleValue);
302 } } }
306 if (simpleTypeElement == null && featureType == null )
309 featureType = (String) xSchema2JavaMap.get("string");
310
311 FeatureSchema featureSchema = new FeatureSchema(
313 featureName,
314 featureType,
315 featureValue,
316 featureUse,
317 featurePermissibleValuesSet);
318 featureSchemaSet.add(featureSchema);
319 }
321
324 public String toXSchema(){
325 StringBuffer schemaString = new StringBuffer();
326 schemaString.append("<?xml version=\"1.0\"?>\n" +
327 "<schema xmlns=\"http://www.w3.org/2000/10/XMLSchema\">\n"+
328 " <element name=\"" + annotationName + "\"");
329
330 if (featureSchemaSet == null)
331 schemaString.append("/>\n");
332 else {
333 schemaString.append(">\n <complexType>\n");
334 Iterator featureSchemaSetIterator = featureSchemaSet.iterator();
335 while (featureSchemaSetIterator.hasNext()){
336 FeatureSchema fs = (FeatureSchema) featureSchemaSetIterator.next();
337 schemaString.append(" " + fs.toXSchema(java2xSchemaMap));
338 } schemaString.append(" </complexType>\n");
340 schemaString.append(" </element>\n");
341 } schemaString.append("</schema>\n");
343 return schemaString.toString();
344 }}
347
348