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:
021: import org.drools.lang.descr.AndDescr;
022: import org.drools.lang.descr.AttributeDescr;
023: import org.drools.lang.descr.FunctionDescr;
024: import org.drools.lang.descr.PackageDescr;
025: import org.drools.lang.descr.QueryDescr;
026: import org.drools.lang.descr.RuleDescr;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.SAXParseException;
030:
031: /**
032: * @author mproctor
033: *
034: * TODO To change the template for this generated type comment go to Window -
035: * Preferences - Java - Code Style - Code Templates
036: */
037: class RuleHandler extends BaseAbstractHandler implements Handler {
038: RuleHandler(final XmlPackageReader xmlPackageReader) {
039: this .xmlPackageReader = xmlPackageReader;
040:
041: if ((this .validParents == null) && (this .validPeers == null)) {
042: this .validParents = new HashSet();
043: this .validParents.add(PackageDescr.class);
044:
045: this .validPeers = new HashSet();
046: this .validPeers.add(null);
047: this .validPeers.add(FunctionDescr.class);
048: this .validPeers.add(RuleDescr.class);
049: this .validPeers.add(QueryDescr.class);
050:
051: this .allowNesting = false;
052: }
053: }
054:
055: public Object start(final String uri, final String localName,
056: final Attributes attrs) throws SAXException {
057: this .xmlPackageReader.startConfiguration(localName, attrs);
058:
059: final String ruleName = attrs.getValue("name");
060:
061: if (ruleName == null || ruleName.trim().equals("")) {
062: throw new SAXParseException(
063: "<rule> requires a 'name' attribute",
064: this .xmlPackageReader.getLocator());
065: }
066:
067: final RuleDescr ruleDescr = new RuleDescr(ruleName.trim());
068:
069: return ruleDescr;
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 RuleDescr ruleDescr = (RuleDescr) this .xmlPackageReader
078: .getCurrent();
079:
080: final AndDescr lhs = ruleDescr.getLhs();
081:
082: if (lhs == null) {
083: throw new SAXParseException("<rule> requires a LHS",
084: this .xmlPackageReader.getLocator());
085: }
086:
087: final Configuration rhs = config.getChild("rhs");
088: if (rhs == null) {
089: throw new SAXParseException(
090: "<rule> requires a <rh> child element",
091: this .xmlPackageReader.getLocator());
092: }
093:
094: ruleDescr.setConsequence(rhs.getText());
095:
096: final Configuration[] attributes = config
097: .getChildren("rule-attribute");
098: for (int i = 0, length = attributes.length; i < length; i++) {
099: final String name = attributes[i].getAttribute("name");
100: if (name == null || name.trim().equals("")) {
101: throw new SAXParseException(
102: "<rule-attribute> requires a 'name' attribute",
103: this .xmlPackageReader.getLocator());
104: }
105:
106: final String value = attributes[i].getAttribute("value");
107:
108: ruleDescr.addAttribute(new AttributeDescr(name, value));
109: }
110:
111: this .xmlPackageReader.getPackageDescr().addRule(ruleDescr);
112:
113: return null;
114: }
115:
116: public Class generateNodeFor() {
117: return RuleDescr.class;
118: }
119: }
|