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.antlr.runtime.ANTLRStringStream;
024: import org.antlr.runtime.CharStream;
025: import org.antlr.runtime.CommonTokenStream;
026: import org.antlr.runtime.RecognitionException;
027: import org.antlr.runtime.TokenStream;
028: import org.drools.lang.DRLLexer;
029: import org.drools.lang.DRLParser;
030: import org.drools.lang.descr.BaseDescr;
031: import org.drools.lang.descr.DeclarativeInvokerDescr;
032: import org.drools.lang.descr.FromDescr;
033: import org.xml.sax.Attributes;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.SAXParseException;
036:
037: /**
038: * @author fernandomeyer
039: */
040:
041: public class ExpressionHandler extends BaseAbstractHandler implements
042: Handler {
043:
044: ExpressionHandler(final XmlPackageReader xmlPackageReader) {
045: this .xmlPackageReader = xmlPackageReader;
046:
047: if ((this .validParents == null) && (this .validPeers == null)) {
048: this .validParents = new HashSet();
049: this .validParents.add(FromHandler.class);
050:
051: this .validPeers = new HashSet();
052: this .validPeers.add(null);
053: this .validPeers.add(BaseDescr.class);
054:
055: this .allowNesting = true;
056: }
057: }
058:
059: public Class generateNodeFor() {
060: return BaseDescr.class;
061: }
062:
063: public Object start(final String uri, final String localName,
064: final Attributes attrs) throws SAXException {
065:
066: this .xmlPackageReader.startConfiguration(localName, attrs);
067:
068: return new BaseDescr();
069: }
070:
071: public Object end(final String uri, final String localName)
072: throws SAXException {
073:
074: final Configuration config = this .xmlPackageReader
075: .endConfiguration();
076: final BaseDescr baseDescr = (BaseDescr) this .xmlPackageReader
077: .getCurrent();
078:
079: final String expression = config.getText();
080:
081: if (expression == null || expression.trim().equals("")) {
082: throw new SAXParseException("<" + localName
083: + "> must have some content", this .xmlPackageReader
084: .getLocator());
085: }
086:
087: final LinkedList parents = this .xmlPackageReader.getParents();
088: final ListIterator ite = parents.listIterator(parents.size());
089: ite.previous();
090: final Object parent = ite.previous();
091:
092: final FromDescr fromSource = (FromDescr) parent;
093: final CharStream charStream = new ANTLRStringStream(expression
094: .trim());
095: final DRLLexer lexer = new DRLLexer(charStream);
096: final TokenStream tokenStream = new CommonTokenStream(lexer);
097: final DRLParser parser = new DRLParser(tokenStream);
098:
099: try {
100: final DeclarativeInvokerDescr declarativeInvoker = parser
101: .from_source(fromSource);
102: fromSource.setDataSource(declarativeInvoker);
103: } catch (final RecognitionException e) {
104: throw new SAXParseException("<" + localName
105: + "> must have a valid expression content ",
106: this.xmlPackageReader.getLocator());
107: }
108:
109: return null;
110: }
111:
112: }
|