View Javadoc

1   package zeus.ontology;
2   
3   import zeus.concepts.OntologyDb;
4   
5   import java.util.List;
6   import java.util.Vector;
7   import java.util.Arrays;
8   import java.util.Iterator;
9   
10  import org.xml.sax.*;
11  import org.xml.sax.helpers.DefaultHandler;
12  
13  public class TypesHandler extends DefaultHandler {
14  
15    private List types;
16  
17    private String name;
18    private String type;
19    private String value;
20  
21    private int unionTracker;
22  
23    public TypesHandler() {
24      types = new Vector();
25      unionTracker = 0;
26    }
27  
28    /***
29     * Add the derived restrictions to the db
30     */
31    public void addRestrictions(OntologyDb db) {
32      List names = Arrays.asList(db.getAllRestrictionNames());
33  
34      for(Iterator i = types.iterator() ; i.hasNext() ; ) {
35        String[] restriction = (String[])i.next();
36        String itemName = restriction[0];
37        
38        if(names.contains(itemName)) {
39  	continue;
40        }
41  
42        db.addRestrictions(new String[][] {restriction});
43      }
44  
45      //Consider moving ".*//.type" restrictions into the attributes
46    }
47  
48    private void addRestriction() {
49  
50      if(name == null || name.equals("") || type == null || type.equals("") ||
51         value == null || value.equals("")) {
52        clear();
53        return;
54      }
55  
56      String[] restriction = new String[] {name, type, value};
57      types.add(restriction);
58      clear();
59    }
60  
61    public void startElement(String namespace, String sName, String qName,
62  			   Attributes attributes) throws SAXException {
63  
64      String element = sName;
65      if(element.equals("")) {
66        element = qName;
67      }
68  
69      if(element.matches("(?i).*:union")) {
70        unionTracker++;
71      }
72      else if(element.matches("(?i).*:simpleType")) {
73        //Get name from attributes
74        for(int index = 0 ; index < attributes.getLength() ; index++) {
75  
76  	if(attributes.getQName(index).equalsIgnoreCase("name")) {
77  	  name = attributes.getValue(index);
78  	  break;
79  	}
80        }
81      }
82      else if(name != null && !name.equals("")) {
83        if(element.matches("(?i).*:restriction")) {
84  	for(int index = 0 ; index < attributes.getLength() ; index++) {
85  	  if(attributes.getQName(index).equalsIgnoreCase("base")) {
86  	    parseType(attributes.getValue(index));
87  	    break;
88  	  }
89  	}
90        }
91        else if(type != null && !type.equals("")) {
92  	parseRestriction(element, attributes);
93        }
94        
95      }
96    }
97  
98    public void endElement(String namespace, String sName, String qName)
99      throws SAXException {
100 
101     String element = sName;
102     if(element.equals("")) {
103       element = qName;
104     }
105 
106     if(element.matches("(?i).*:simpletype") && unionTracker == 0) {
107       addRestriction();
108     }
109     else if(element.matches("(?i).*:union")) {
110       unionTracker--;
111     }
112   }
113 
114   public void characters(char[] ch, int start, int length)
115     throws SAXException {
116 
117   }
118 
119   private void parseRestriction(String element, Attributes attributes) {
120     if(element.matches("(?i).*:minInclusive")) {
121       joinRestriction(">= " + attributes.getValue("value"));
122     }
123     else if(element.matches("(?i).*:maxInclusive")) {
124       joinRestriction("<= " + attributes.getValue("value"));
125     }
126     else if(element.matches("(?i).*:minExclusive")) {
127       joinRestriction("> " + attributes.getValue("value"));
128     }
129     else if(element.matches("(?i).*:maxExclusive")) {
130       joinRestriction("< " + attributes.getValue("value"));
131     }
132     else if(element.matches("(?i).*:enumeration")) {
133       disjoinRestriction(attributes.getValue("value"));
134     }
135 
136   }
137 
138   private void joinRestriction(String expression) {
139     if(unionTracker > 0) {
140       disjoinRestriction(expression);
141     }
142     else {
143       conjoinRestriction(expression);
144     }
145   }
146 
147   private void conjoinRestriction(String expression) {
148     if(value == null || value.equals("")) {
149       value = expression;
150     }
151     else {
152       value = value + " & " + expression;
153     }
154   }
155 
156   private void disjoinRestriction(String expression) {
157     if(value == null || value.equals("")) {
158       value = expression;
159     }
160     else {
161       value = value + " | " + expression;
162     }
163   }
164 
165   private void clear() {
166     name = "";
167     type = "";
168     value = "";
169   }
170 
171   private void parseType(String base) {
172 
173     if(base.matches(".*:.*")) {
174       base = base.substring(base.indexOf(":") + 1);
175     }
176 
177     if(base.toLowerCase().equals("string")) {
178       type = "String";
179     }
180     else if(base.toLowerCase().equals("integer")) {
181       type = "Integer";
182     }
183     else if(base.toLowerCase().equals("real")) {
184       type = "Real";
185     }
186     else if(base.toLowerCase().equals("boolean")) {
187       type = "Boolean";
188     }
189     else if(base.toLowerCase().equals("date")) {
190       type = "Date";
191     }
192     else if(base.toLowerCase().equals("time")) {
193       type = "Time";
194     }
195     else if(base.toLowerCase().equals("list")) {
196       type = "List";
197     }
198     else 
199       type = base;
200   }
201 }