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