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: class FieldBindingHandler extends BaseAbstractHandler implements
35: Handler {
36: FieldBindingHandler(final XmlPackageReader xmlPackageReader) {
37: this .xmlPackageReader = xmlPackageReader;
38:
39: if ((this .validParents == null) && (this .validPeers == null)) {
40: this .validParents = new HashSet();
41: this .validParents.add(PatternDescr.class);
42:
43: this .validPeers = new HashSet();
44: this .validPeers.add(null);
45: this .validPeers.add(FieldConstraintDescr.class);
46: this .validPeers.add(PredicateDescr.class);
47: this .validPeers.add(FieldBindingDescr.class);
48: this .allowNesting = false;
49: }
50: }
51:
52: public Object start(final String uri, final String localName,
53: final Attributes attrs) throws SAXException {
54: this .xmlPackageReader.startConfiguration(localName, attrs);
55:
56: final String identifier = attrs.getValue("identifier");
57: if (identifier == null || identifier.trim().equals("")) {
58: throw new SAXParseException(
59: "<field-binding> requires an 'identifier' attribute",
60: this .xmlPackageReader.getLocator());
61: }
62:
63: final String fieldName = attrs.getValue("field-name");
64: if (fieldName == null || fieldName.trim().equals("")) {
65: throw new SAXParseException(
66: "<field-binding> requires a 'field-name' attribute",
67: this .xmlPackageReader.getLocator());
68: }
69:
70: final FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
71: fieldName, identifier);
72:
73: return fieldBindingDescr;
74: }
75:
76: public Object end(final String uri, final String localName)
77: throws SAXException {
78: final Configuration config = this .xmlPackageReader
79: .endConfiguration();
80:
81: final FieldBindingDescr fieldBindingDescr = (FieldBindingDescr) this .xmlPackageReader
82: .getCurrent();
83:
84: final LinkedList parents = this .xmlPackageReader.getParents();
85: final ListIterator it = parents.listIterator(parents.size());
86: it.previous();
87: final PatternDescr patternDescr = (PatternDescr) it.previous();
88:
89: patternDescr.addConstraint(fieldBindingDescr);
90:
91: return null;
92: }
93:
94: public Class generateNodeFor() {
95: return FieldBindingDescr.class;
96: }
97: }
|