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.CollectDescr;
24: import org.drools.lang.descr.ConditionalElementDescr;
25: import org.drools.lang.descr.FromDescr;
26: import org.drools.lang.descr.PatternDescr;
27: import org.xml.sax.Attributes;
28: import org.xml.sax.SAXException;
29:
30: /**
31: * @author fernandomeyer
32: *
33: */
34: public class CollectHandler extends BaseAbstractHandler implements
35: Handler {
36:
37: CollectHandler(final XmlPackageReader xmlPackageReader) {
38: this .xmlPackageReader = xmlPackageReader;
39:
40: if ((this .validParents == null) && (this .validPeers == null)) {
41: this .validParents = new HashSet();
42:
43: this .validParents.add(FromDescr.class);
44:
45: this .validPeers = new HashSet();
46: this .validPeers.add(null);
47:
48: this .allowNesting = false;
49: }
50: }
51:
52: public Object start(final String uri, final String localName,
53: final Attributes attrs) throws SAXException {
54:
55: this .xmlPackageReader.startConfiguration(localName, attrs);
56: final CollectDescr collectDescr = new CollectDescr();
57: return collectDescr;
58: }
59:
60: public Object end(final String uri, final String localName)
61: throws SAXException {
62:
63: final Configuration config = this .xmlPackageReader
64: .endConfiguration();
65: final CollectDescr collectDescr = (CollectDescr) this .xmlPackageReader
66: .getCurrent();
67:
68: final LinkedList parents = this .xmlPackageReader.getParents();
69: final ListIterator ite = parents.listIterator(parents.size());
70: ite.previous();
71: final Object parent = ite.previous();
72:
73: if (parent.getClass().getName().equals(
74: FromDescr.class.getName())) {
75: final PatternDescr resultPattern = (PatternDescr) ite
76: .previous();
77: resultPattern.setSource(collectDescr);
78: } else if (parent instanceof ConditionalElementDescr) {
79: final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
80: parentDescr.addDescr(collectDescr);
81: }
82:
83: return null;
84: }
85:
86: public Class generateNodeFor() {
87: return CollectDescr.class;
88: }
89:
90: }
|