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 NotHandler extends BaseAbstractHandler implements Handler {
042: NotHandler(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:
050: this .validPeers = new HashSet();
051: this .validPeers.add(null);
052: this .validPeers.add(AndDescr.class);
053: this .validPeers.add(OrDescr.class);
054: this .validPeers.add(NotDescr.class);
055: this .validPeers.add(ExistsDescr.class);
056: this .validPeers.add(EvalDescr.class);
057: this .validPeers.add(PatternDescr.class);
058: this .validPeers.add(ForallDescr.class);
059:
060: this .allowNesting = true;
061: }
062: }
063:
064: public Object start(final String uri, final String localName,
065: final Attributes attrs) throws SAXException {
066: this .xmlPackageReader.startConfiguration(localName, attrs);
067: final NotDescr notDescr = new NotDescr();
068:
069: return notDescr;
070: }
071:
072: public Object end(final String uri, final String localName)
073: throws SAXException {
074: final Configuration config = this .xmlPackageReader
075: .endConfiguration();
076:
077: final NotDescr notDescr = (NotDescr) this .xmlPackageReader
078: .getCurrent();
079:
080: if ((notDescr.getDescrs().size() != 1)
081: && (notDescr.getDescrs().get(0).getClass() != PatternDescr.class)) {
082: throw new SAXParseException(
083: "<not> can only have a single <pattern...> as a child element",
084: this .xmlPackageReader.getLocator());
085: }
086:
087: final LinkedList parents = this .xmlPackageReader.getParents();
088: final ListIterator it = parents.listIterator(parents.size());
089: it.previous();
090: final Object parent = it.previous();
091:
092: final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
093: parentDescr.addDescr(notDescr);
094:
095: return null;
096: }
097:
098: public Class generateNodeFor() {
099: return NotDescr.class;
100: }
101: }
|