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.xml.sax.Attributes;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.SAXParseException;
034:
035: /**
036: * @author mproctor
037: *
038: * TODO To change the template for this generated type comment go to Window -
039: * Preferences - Java - Code Style - Code Templates
040: */
041: class ExistsHandler extends BaseAbstractHandler implements Handler {
042: ExistsHandler(final XmlPackageReader xmlPackageReader) {
043: this .xmlPackageReader = xmlPackageReader;
044:
045: if ((this .validParents == null) && (this .validPeers == null)) {
046: this .validParents = new HashSet();
047: this .validParents.add(AndDescr.class);
048: this .validParents.add(OrDescr.class);
049: this .validParents.add(NotDescr.class);
050:
051: this .validPeers = new HashSet();
052: this .validPeers.add(null);
053: this .validPeers.add(AndDescr.class);
054: this .validPeers.add(OrDescr.class);
055: this .validPeers.add(NotDescr.class);
056: this .validPeers.add(ExistsDescr.class);
057: this .validPeers.add(EvalDescr.class);
058: this .validPeers.add(PatternDescr.class);
059: this .validPeers.add(ForallDescr.class);
060:
061: this .allowNesting = true;
062: }
063: }
064:
065: public Object start(final String uri, final String localName,
066: final Attributes attrs) throws SAXException {
067: this .xmlPackageReader.startConfiguration(localName, attrs);
068: final ExistsDescr existsDescr = new ExistsDescr();
069:
070: return existsDescr;
071: }
072:
073: public Object end(final String uri, final String localName)
074: throws SAXException {
075: final Configuration config = this .xmlPackageReader
076: .endConfiguration();
077:
078: final ExistsDescr existsDescr = (ExistsDescr) this .xmlPackageReader
079: .getCurrent();
080:
081: if ((existsDescr.getDescrs().size() != 1)
082: && (existsDescr.getDescrs().get(0).getClass() != PatternDescr.class)) {
083: throw new SAXParseException(
084: "<exists> can only have a single <pattern...> as a child element",
085: this .xmlPackageReader.getLocator());
086: }
087:
088: final LinkedList parents = this .xmlPackageReader.getParents();
089: final ListIterator it = parents.listIterator(parents.size());
090: it.previous();
091: final Object parent = it.previous();
092:
093: final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
094: parentDescr.addDescr(existsDescr);
095:
096: return null;
097: }
098:
099: public Class generateNodeFor() {
100: return ExistsDescr.class;
101: }
102: }
|