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.FunctionDescr;
022: import org.drools.lang.descr.PackageDescr;
023: import org.drools.lang.descr.RuleDescr;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.SAXParseException;
027:
028: /**
029: * @author mproctor
030: *
031: * TODO To change the template for this generated type comment go to Window -
032: * Preferences - Java - Code Style - Code Templates
033: */
034: class LhsHandler extends BaseAbstractHandler implements Handler {
035: LhsHandler(final XmlPackageReader xmlPackageReader) {
036: this .xmlPackageReader = xmlPackageReader;
037:
038: if ((this .validParents == null) && (this .validPeers == null)) {
039: this .validParents = new HashSet();
040: this .validParents.add(PackageDescr.class);
041:
042: this .validPeers = new HashSet();
043: this .validPeers.add(null);
044: this .validPeers.add(FunctionDescr.class);
045: this .validPeers.add(RuleDescr.class);
046:
047: this .allowNesting = false;
048: }
049: }
050:
051: public Object start(final String uri, final String localName,
052: final Attributes attrs) throws SAXException {
053: this .xmlPackageReader.startConfiguration(localName, attrs);
054: return null;
055: }
056:
057: public Object end(final String uri, final String localName)
058: throws SAXException {
059: final Configuration config = this .xmlPackageReader
060: .endConfiguration();
061:
062: final String name = config.getAttribute("name");
063: if (name == null || name.trim().equals("")) {
064: throw new SAXParseException(
065: "<function> requires a 'name' attribute",
066: this .xmlPackageReader.getLocator());
067: }
068:
069: final String returnType = config.getAttribute("return-type");
070: if (returnType == null || returnType.trim().equals("")) {
071: throw new SAXParseException(
072: "<function> requires a 'return-type' attribute",
073: this .xmlPackageReader.getLocator());
074: }
075:
076: final FunctionDescr functionDescr = new FunctionDescr(name,
077: returnType);
078:
079: final Configuration[] parameters = config
080: .getChildren("parameter");
081:
082: for (int i = 0, length = parameters.length; i < length; i++) {
083: final String identifier = parameters[i]
084: .getAttribute("identifier");
085: if (name == null || identifier.trim().equals("")) {
086: throw new SAXParseException(
087: "function's <parameter> requires an 'identifier' attribute",
088: this .xmlPackageReader.getLocator());
089: }
090:
091: final String type = parameters[i].getText();
092: if (type == null || type.trim().equals("")) {
093: throw new SAXParseException(
094: "function's <parameter> must specify a 'type'",
095: this .xmlPackageReader.getLocator());
096: }
097:
098: functionDescr.addParameter(type, identifier);
099: }
100:
101: // we allow empty, "", bodies - but make sure that we atleast have a body element
102: final Configuration body = config.getChild("body");
103: if (body == null) {
104: throw new SAXParseException("function must have a <body>",
105: this .xmlPackageReader.getLocator());
106: }
107:
108: functionDescr.setText(body.getText());
109:
110: final PackageDescr packageDescr = this .xmlPackageReader
111: .getPackageDescr();
112:
113: packageDescr.addFunction(functionDescr);
114:
115: return functionDescr;
116: }
117:
118: public Class generateNodeFor() {
119: return FunctionDescr.class;
120: }
121: }
|