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.AccumulateDescr;
024: import org.drools.lang.descr.BaseDescr;
025: import org.drools.lang.descr.PatternDescr;
026: import org.xml.sax.Attributes;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.SAXParseException;
029:
030: /**
031: * @author fernandomeyer
032: */
033:
034: public class AccumulateHelperHandler extends BaseAbstractHandler
035: implements Handler {
036:
037: AccumulateHelperHandler(final XmlPackageReader xmlPackageReader) {
038: this .xmlPackageReader = xmlPackageReader;
039:
040: if ((this .validParents == null) && (this .validPeers == null)) {
041: this .validParents = new HashSet();
042: this .validParents.add(AccumulateDescr.class);
043:
044: this .validPeers = new HashSet();
045: this .validPeers.add(null);
046:
047: this .validPeers.add(PatternDescr.class);
048: this .validPeers.add(BaseDescr.class);
049:
050: this .allowNesting = true;
051: }
052: }
053:
054: public Object end(final String uri, final String localName)
055: throws SAXException {
056:
057: final Configuration config = this .xmlPackageReader
058: .endConfiguration();
059: final BaseDescr baseDescr = (BaseDescr) this .xmlPackageReader
060: .getCurrent();
061:
062: final String expression = config.getText();
063:
064: if (expression == null) {
065: throw new SAXParseException("<" + localName
066: + "> must have some content", this .xmlPackageReader
067: .getLocator());
068: }
069:
070: final LinkedList parents = this .xmlPackageReader.getParents();
071: final ListIterator ite = parents.listIterator(parents.size());
072: ite.previous();
073: final Object parent = ite.previous();
074:
075: final AccumulateDescr accumulate = (AccumulateDescr) parent;
076:
077: if (localName.equals("init"))
078: accumulate.setInitCode(expression.trim());
079: else if (localName.equals("action"))
080: accumulate.setActionCode(expression.trim());
081: else if (localName.equals("result"))
082: accumulate.setResultCode(expression.trim());
083: else if (localName.equals("reverse"))
084: accumulate.setReverseCode(expression.trim());
085: else if (localName.equals("external-function")) {
086: accumulate.setExternalFunction(true);
087: accumulate.setFunctionIdentifier(config
088: .getAttribute("evaluator"));
089: accumulate.setExpression(config.getAttribute("expression"));
090: }
091:
092: return null;
093: }
094:
095: public Class generateNodeFor() {
096: return BaseDescr.class;
097: }
098:
099: public Object start(final String uri, final String localName,
100: final Attributes attrs) throws SAXException {
101:
102: this .xmlPackageReader.startConfiguration(localName, attrs);
103:
104: return new BaseDescr();
105: }
106:
107: }
|