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 EvalHandler extends BaseAbstractHandler implements Handler {
042: EvalHandler(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:
068: final EvalDescr evalDescr = new EvalDescr();
069:
070: return evalDescr;
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 EvalDescr evalDescr = (EvalDescr) this .xmlPackageReader
079: .getCurrent();
080:
081: final String expression = config.getText();
082:
083: if (expression == null || expression.trim().equals("")) {
084: throw new SAXParseException(
085: "<eval> must have some content",
086: this .xmlPackageReader.getLocator());
087: }
088:
089: evalDescr.setContent(expression);
090:
091: final LinkedList parents = this .xmlPackageReader.getParents();
092: final ListIterator it = parents.listIterator(parents.size());
093: it.previous();
094: final Object parent = it.previous();
095:
096: final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
097: parentDescr.addDescr(evalDescr);
098:
099: return null;
100: }
101:
102: public Class generateNodeFor() {
103: return EvalDescr.class;
104: }
105: }
|