001: package org.drools.xml;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.HashSet;
020: import java.util.LinkedList;
021: import java.util.ListIterator;
022:
023: import org.drools.lang.descr.AndDescr;
024: import org.drools.lang.descr.ConditionalElementDescr;
025: import org.drools.lang.descr.EvalDescr;
026: import org.drools.lang.descr.ExistsDescr;
027: import org.drools.lang.descr.ForallDescr;
028: import org.drools.lang.descr.NotDescr;
029: import org.drools.lang.descr.OrDescr;
030: import org.drools.lang.descr.PatternDescr;
031: import org.drools.lang.descr.QueryDescr;
032: import org.drools.lang.descr.RuleDescr;
033: import org.xml.sax.Attributes;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * @author mproctor
038: *
039: * TODO To change the template for this generated type comment go to Window -
040: * Preferences - Java - Code Style - Code Templates
041: */
042: class AndHandler extends BaseAbstractHandler implements Handler {
043: AndHandler(final XmlPackageReader xmlPackageReader) {
044: this .xmlPackageReader = xmlPackageReader;
045:
046: if ((this .validParents == null) && (this .validPeers == null)) {
047: this .validParents = new HashSet();
048: this .validParents.add(QueryDescr.class);
049: this .validParents.add(RuleDescr.class);
050: this .validParents.add(OrDescr.class);
051: this .validParents.add(LiteralRestrictionHandler.class);
052:
053: this .validPeers = new HashSet();
054: this .validPeers.add(null);
055: this .validPeers.add(AndDescr.class);
056: this .validPeers.add(OrDescr.class);
057: this .validPeers.add(NotDescr.class);
058: this .validPeers.add(ExistsDescr.class);
059: this .validPeers.add(EvalDescr.class);
060: this .validPeers.add(PatternDescr.class);
061: this .validPeers.add(ForallDescr.class);
062:
063: this .allowNesting = true;
064: }
065: }
066:
067: public Object start(final String uri, final String localName,
068: final Attributes attrs) throws SAXException {
069: this .xmlPackageReader.startConfiguration(localName, attrs);
070: final AndDescr andDescr = new AndDescr();
071:
072: return andDescr;
073: }
074:
075: public Object end(final String uri, final String localName)
076: throws SAXException {
077: final Configuration config = this .xmlPackageReader
078: .endConfiguration();
079:
080: final AndDescr andDescr = (AndDescr) this .xmlPackageReader
081: .getCurrent();
082:
083: final LinkedList parents = this .xmlPackageReader.getParents();
084: final ListIterator it = parents.listIterator(parents.size());
085: it.previous();
086: final Object parent = it.previous();
087:
088: if (parent instanceof RuleDescr || parent instanceof QueryDescr) {
089: final RuleDescr ruleDescr = (RuleDescr) parent;
090: ruleDescr.setLhs(andDescr);
091: } else if (parent instanceof ConditionalElementDescr) {
092: final ConditionalElementDescr ceDescr = (ConditionalElementDescr) parent;
093: ceDescr.addDescr(andDescr);
094: }
095:
096: return null;
097: }
098:
099: public Class generateNodeFor() {
100: return AndDescr.class;
101: }
102: }
|