01: package org.drools.xml;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.HashSet;
20: import java.util.LinkedList;
21: import java.util.ListIterator;
22:
23: import org.drools.lang.descr.AndDescr;
24: import org.drools.lang.descr.ConditionalElementDescr;
25: import org.drools.lang.descr.EvalDescr;
26: import org.drools.lang.descr.ExistsDescr;
27: import org.drools.lang.descr.ForallDescr;
28: import org.drools.lang.descr.NotDescr;
29: import org.drools.lang.descr.OrDescr;
30: import org.drools.lang.descr.PatternDescr;
31: import org.xml.sax.Attributes;
32: import org.xml.sax.SAXException;
33:
34: /**
35: * @author mproctor
36: */
37: class OrHandler extends BaseAbstractHandler implements Handler {
38: OrHandler(final XmlPackageReader xmlPackageReader) {
39: this .xmlPackageReader = xmlPackageReader;
40:
41: if ((this .validParents == null) && (this .validPeers == null)) {
42: this .validParents = new HashSet();
43: this .validParents.add(AndDescr.class);
44: this .validParents.add(PatternDescr.class);
45:
46: this .validPeers = new HashSet();
47: this .validPeers.add(null);
48: this .validPeers.add(AndDescr.class);
49: this .validPeers.add(OrDescr.class);
50: this .validPeers.add(NotDescr.class);
51: this .validPeers.add(ExistsDescr.class);
52: this .validPeers.add(EvalDescr.class);
53: this .validPeers.add(PatternDescr.class);
54: this .validPeers.add(ForallDescr.class);
55:
56: this .allowNesting = true;
57: }
58: }
59:
60: public Object start(final String uri, final String localName,
61: final Attributes attrs) throws SAXException {
62: this .xmlPackageReader.startConfiguration(localName, attrs);
63: final OrDescr orDescr = new OrDescr();
64:
65: return orDescr;
66: }
67:
68: public Object end(final String uri, final String localName)
69: throws SAXException {
70: final Configuration config = this .xmlPackageReader
71: .endConfiguration();
72:
73: final OrDescr orDescr = (OrDescr) this .xmlPackageReader
74: .getCurrent();
75:
76: final LinkedList parents = this .xmlPackageReader.getParents();
77: final ListIterator it = parents.listIterator(parents.size());
78: it.previous();
79: final Object parent = it.previous();
80:
81: if (parent instanceof ConditionalElementDescr) {
82: final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
83: parentDescr.addDescr(orDescr);
84: } else if (parent instanceof PatternDescr) {
85: final PatternDescr parentDescr = (PatternDescr) parent;
86: parentDescr.addConstraint(orDescr);
87: }
88:
89: return null;
90: }
91:
92: public Class generateNodeFor() {
93: return OrDescr.class;
94: }
95: }
|