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.GlobalDescr;
022: import org.drools.lang.descr.ImportDescr;
023: import org.drools.lang.descr.PackageDescr;
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 PackageHandler extends BaseAbstractHandler implements Handler {
035: PackageHandler(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(null);
041:
042: this .validPeers = new HashSet();
043: this .validPeers.add(null);
044:
045: this .allowNesting = false;
046: }
047: }
048:
049: public Object start(final String uri, final String localName,
050: final Attributes attrs) throws SAXException {
051: this .xmlPackageReader.startConfiguration(localName, attrs);
052:
053: final String ruleSetName = attrs.getValue("name");
054:
055: if (ruleSetName == null || ruleSetName.trim().equals("")) {
056: throw new SAXParseException(
057: "<package> requires a 'name' attribute",
058: this .xmlPackageReader.getLocator());
059: }
060:
061: final PackageDescr packageDescr = new PackageDescr(ruleSetName
062: .trim());
063:
064: this .xmlPackageReader.setPackageDescr(packageDescr);
065: return packageDescr;
066: }
067:
068: public Object end(final String uri, final String localName)
069: throws SAXException {
070: final PackageDescr packageDescr = this .xmlPackageReader
071: .getPackageDescr();
072: final Configuration config = this .xmlPackageReader
073: .endConfiguration();
074:
075: final Configuration[] imports = config.getChildren("import");
076:
077: for (int i = 0, length = imports.length; i < length; i++) {
078: final String importEntry = imports[i].getAttribute("name");
079:
080: if (importEntry == null || importEntry.trim().equals("")) {
081: throw new SAXParseException("<import> cannot be blank",
082: this .xmlPackageReader.getLocator());
083: }
084: packageDescr.addImport(new ImportDescr(importEntry));
085: }
086:
087: final Configuration[] globals = config.getChildren("global");
088:
089: for (int i = 0, length = globals.length; i < length; i++) {
090: final String identifier = globals[i]
091: .getAttribute("identifier");
092:
093: if (identifier == null || identifier.trim().equals("")) {
094: throw new SAXParseException(
095: "<global> must have an identifier",
096: this .xmlPackageReader.getLocator());
097: }
098:
099: final String type = globals[i].getAttribute("type");
100: if (type == null || type.trim().equals("")) {
101: throw new SAXParseException(
102: "<global> must have specify a type",
103: this .xmlPackageReader.getLocator());
104: }
105: final GlobalDescr global = new GlobalDescr(identifier, type);
106: packageDescr.addGlobal(global);
107: }
108:
109: return null;
110: }
111:
112: public Class generateNodeFor() {
113: return PackageDescr.class;
114: }
115: }
|