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: import java.util.LinkedList;
021: import java.util.ListIterator;
022:
023: import org.drools.lang.descr.FieldConstraintDescr;
024: import org.drools.lang.descr.LiteralRestrictionDescr;
025: import org.drools.lang.descr.QualifiedIdentifierRestrictionDescr;
026: import org.drools.lang.descr.RestrictionConnectiveDescr;
027: import org.drools.lang.descr.ReturnValueRestrictionDescr;
028: import org.drools.lang.descr.VariableRestrictionDescr;
029: import org.xml.sax.Attributes;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.SAXParseException;
032:
033: /**
034: * @author mproctor
035: *
036: * TODO To change the template for this generated type comment go to Window -
037: * Preferences - Java - Code Style - Code Templates
038: */
039: class ReturnValueRestrictionHandler extends BaseAbstractHandler
040: implements Handler {
041: ReturnValueRestrictionHandler(
042: final XmlPackageReader xmlPackageReader) {
043: this .xmlPackageReader = xmlPackageReader;
044:
045: if ((this .validParents == null) && (this .validPeers == null)) {
046: this .validParents = new HashSet();
047: this .validParents.add(FieldConstraintDescr.class);
048: this .validParents.add(RestrictionConnectiveDescr.class);
049:
050: this .validPeers = new HashSet();
051: this .validPeers.add(null);
052:
053: this .validPeers.add(LiteralRestrictionDescr.class);
054: this .validPeers.add(ReturnValueRestrictionDescr.class);
055: this .validPeers.add(VariableRestrictionDescr.class);
056: this .validPeers.add(RestrictionConnectiveDescr.class);
057: this .validPeers
058: .add(QualifiedIdentifierRestrictionDescr.class);
059:
060: this .allowNesting = false;
061: }
062: }
063:
064: public Object start(final String uri, final String localName,
065: final Attributes attrs) throws SAXException {
066: this .xmlPackageReader.startConfiguration(localName, attrs);
067: final String evaluator = attrs.getValue("evaluator");
068: if (evaluator == null || evaluator.trim().equals("")) {
069: throw new SAXParseException(
070: "<return-value-restriction> requires an 'evaluator' attribute",
071: this .xmlPackageReader.getLocator());
072: }
073:
074: final ReturnValueRestrictionDescr returnValueDescr = new ReturnValueRestrictionDescr(
075: evaluator);
076:
077: return returnValueDescr;
078: }
079:
080: public Object end(final String uri, final String localName)
081: throws SAXException {
082: final Configuration config = this .xmlPackageReader
083: .endConfiguration();
084:
085: final ReturnValueRestrictionDescr returnValueDescr = (ReturnValueRestrictionDescr) this .xmlPackageReader
086: .getCurrent();
087:
088: final String expression = config.getText();
089:
090: if (expression == null || expression.trim().equals("")) {
091: throw new SAXParseException(
092: "<return-value-restriction> must have some content",
093: this .xmlPackageReader.getLocator());
094: }
095:
096: returnValueDescr.setContent(expression);
097:
098: final LinkedList parents = this .xmlPackageReader.getParents();
099: final ListIterator it = parents.listIterator(parents.size());
100: it.previous();
101:
102: final Object parent = it.previous();
103:
104: if (parent instanceof FieldConstraintDescr) {
105: final FieldConstraintDescr fieldConstraintDescr = (FieldConstraintDescr) parent;
106: fieldConstraintDescr.addRestriction(returnValueDescr);
107: } else if (parent instanceof RestrictionConnectiveDescr) {
108: final RestrictionConnectiveDescr rcDescr = (RestrictionConnectiveDescr) parent;
109: rcDescr.addRestriction(returnValueDescr);
110: }
111: return null;
112: }
113:
114: public Class generateNodeFor() {
115: return ReturnValueRestrictionDescr.class;
116: }
117: }
|