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.FieldBindingDescr;
24: import org.drools.lang.descr.FieldConstraintDescr;
25: import org.drools.lang.descr.PatternDescr;
26: import org.drools.lang.descr.PredicateDescr;
27: import org.xml.sax.Attributes;
28: import org.xml.sax.SAXException;
29: import org.xml.sax.SAXParseException;
30:
31: /**
32: * @author mproctor
33: *
34: * TODO To change the template for this generated type comment go to Window -
35: * Preferences - Java - Code Style - Code Templates
36: */
37: class PredicateHandler extends BaseAbstractHandler implements Handler {
38: PredicateHandler(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(PatternDescr.class);
44:
45: this .validPeers = new HashSet();
46: this .validPeers.add(null);
47: this .validPeers.add(FieldConstraintDescr.class);
48: this .validPeers.add(PredicateDescr.class);
49: this .validPeers.add(FieldBindingDescr.class);
50: this .allowNesting = false;
51: }
52: }
53:
54: public Object start(final String uri, final String localName,
55: final Attributes attrs) throws SAXException {
56: this .xmlPackageReader.startConfiguration(localName, attrs);
57:
58: final PredicateDescr predicateDescr = new PredicateDescr();
59:
60: return predicateDescr;
61: }
62:
63: public Object end(final String uri, final String localName)
64: throws SAXException {
65: final Configuration config = this .xmlPackageReader
66: .endConfiguration();
67:
68: final PredicateDescr predicateDescr = (PredicateDescr) this .xmlPackageReader
69: .getCurrent();
70:
71: final String expression = config.getText();
72:
73: if (expression == null || expression.trim().equals("")) {
74: throw new SAXParseException(
75: "<predicate> must have some content",
76: this .xmlPackageReader.getLocator());
77: }
78:
79: predicateDescr.setContent(expression);
80:
81: final LinkedList parents = this .xmlPackageReader.getParents();
82: final ListIterator it = parents.listIterator(parents.size());
83: it.previous();
84: final PatternDescr patternDescr = (PatternDescr) it.previous();
85:
86: patternDescr.addConstraint(predicateDescr);
87:
88: return null;
89: }
90:
91: public Class generateNodeFor() {
92: return PredicateDescr.class;
93: }
94: }
|