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