01: package org.drools.xml;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.HashSet;
20:
21: import org.drools.lang.descr.AndDescr;
22: import org.drools.lang.descr.FunctionDescr;
23: import org.drools.lang.descr.PackageDescr;
24: import org.drools.lang.descr.QueryDescr;
25: import org.drools.lang.descr.RuleDescr;
26: import org.xml.sax.Attributes;
27: import org.xml.sax.SAXException;
28: import org.xml.sax.SAXParseException;
29:
30: /**
31: * @author mproctor
32: *
33: * TODO To change the template for this generated type comment go to Window -
34: * Preferences - Java - Code Style - Code Templates
35: */
36: class QueryHandler extends BaseAbstractHandler implements Handler {
37: QueryHandler(final XmlPackageReader xmlPackageReader) {
38: this .xmlPackageReader = xmlPackageReader;
39:
40: if ((this .validParents == null) && (this .validPeers == null)) {
41: this .validParents = new HashSet();
42: this .validParents.add(PackageDescr.class);
43:
44: this .validPeers = new HashSet();
45: this .validPeers.add(null);
46: this .validPeers.add(FunctionDescr.class);
47: this .validPeers.add(RuleDescr.class);
48: this .validPeers.add(QueryDescr.class);
49:
50: this .allowNesting = false;
51: }
52: }
53:
54: public Object start(final String uri, final String localName,
55: final Attributes attrs) throws SAXException {
56: this .xmlPackageReader.startConfiguration(localName, attrs);
57:
58: final String queryName = attrs.getValue("name");
59:
60: if (queryName == null || queryName.trim().equals("")) {
61: throw new SAXParseException(
62: "<query> requires a 'name' attribute",
63: this .xmlPackageReader.getLocator());
64: }
65:
66: final QueryDescr queryDescr = new QueryDescr(queryName.trim());
67:
68: return queryDescr;
69: }
70:
71: public Object end(final String uri, final String localName)
72: throws SAXException {
73: final Configuration config = this .xmlPackageReader
74: .endConfiguration();
75:
76: final QueryDescr queryDescr = (QueryDescr) this .xmlPackageReader
77: .getCurrent();
78:
79: final AndDescr lhs = queryDescr.getLhs();
80:
81: if (lhs == null || lhs.getDescrs().isEmpty()) {
82: throw new SAXParseException("<query> requires a LHS",
83: this .xmlPackageReader.getLocator());
84: }
85:
86: this .xmlPackageReader.getPackageDescr().addRule(queryDescr);
87:
88: return null;
89: }
90:
91: public Class generateNodeFor() {
92: return QueryDescr.class;
93: }
94: }
|