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:
032: /**
033: * @author mproctor
034: *
035: * TODO To change the template for this generated type comment go to Window -
036: * Preferences - Java - Code Style - Code Templates
037: */
038: class RestrictionConnectiveHandler extends BaseAbstractHandler
039: implements Handler {
040:
041: public final static String AND = "and-restriction-connective";
042: public final static String OR = "or-restriction-connective";
043:
044: RestrictionConnectiveHandler(final XmlPackageReader xmlPackageReader) {
045: this .xmlPackageReader = xmlPackageReader;
046:
047: if ((this .validParents == null) && (this .validPeers == null)) {
048: this .validParents = new HashSet();
049: this .validParents.add(FieldConstraintDescr.class);
050:
051: this .validPeers = new HashSet();
052: this .validPeers.add(null);
053:
054: this .validPeers.add(LiteralRestrictionDescr.class);
055: this .validPeers.add(ReturnValueRestrictionDescr.class);
056: this .validPeers.add(VariableRestrictionDescr.class);
057: this .validPeers.add(RestrictionConnectiveDescr.class);
058: this .validPeers
059: .add(QualifiedIdentifierRestrictionDescr.class);
060:
061: this .allowNesting = true;
062: }
063: }
064:
065: public Object start(final String uri, final String localName,
066: final Attributes attrs) throws SAXException {
067: this .xmlPackageReader.startConfiguration(localName, attrs);
068:
069: RestrictionConnectiveDescr connectiveDescr = null;
070: if (localName.equals(RestrictionConnectiveHandler.OR)) {
071: connectiveDescr = new RestrictionConnectiveDescr(
072: RestrictionConnectiveDescr.OR);
073: } else if (localName.equals(RestrictionConnectiveHandler.AND)) {
074: connectiveDescr = new RestrictionConnectiveDescr(
075: RestrictionConnectiveDescr.AND);
076: }
077:
078: return connectiveDescr;
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 RestrictionConnectiveDescr connectiveDescr = (RestrictionConnectiveDescr) this .xmlPackageReader
087: .getCurrent();
088:
089: final LinkedList parents = this .xmlPackageReader.getParents();
090: final int size = parents.size();
091: final ListIterator ite = parents.listIterator(parents.size());
092:
093: ite.previous();
094:
095: final Object obj = ite.previous();
096:
097: if (obj instanceof FieldConstraintDescr) {
098: final FieldConstraintDescr fieldConstriantDescr = (FieldConstraintDescr) obj;
099: fieldConstriantDescr.addRestriction(connectiveDescr);
100: } else if (obj instanceof RestrictionConnectiveDescr) {
101: final RestrictionConnectiveDescr restconective = (RestrictionConnectiveDescr) obj;
102: restconective.addRestriction(connectiveDescr);
103: }
104:
105: return null;
106: }
107:
108: public Class generateNodeFor() {
109: return RestrictionConnectiveDescr.class;
110: }
111: }
|