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 fmeyer
035: */
036:
037: class QualifiedIdentifierRestrictionHandler extends BaseAbstractHandler
038: implements Handler {
039: QualifiedIdentifierRestrictionHandler(
040: final XmlPackageReader xmlPackageReader) {
041: this .xmlPackageReader = xmlPackageReader;
042:
043: if ((this .validParents == null) && (this .validPeers == null)) {
044: this .validParents = new HashSet();
045: this .validParents.add(FieldConstraintDescr.class);
046: this .validParents.add(RestrictionConnectiveDescr.class);
047:
048: this .validPeers = new HashSet();
049:
050: this .validPeers.add(null);
051: this .validPeers.add(LiteralRestrictionDescr.class);
052: this .validPeers.add(ReturnValueRestrictionDescr.class);
053: this .validPeers.add(VariableRestrictionDescr.class);
054: this .validPeers.add(RestrictionConnectiveDescr.class);
055: this .validPeers
056: .add(QualifiedIdentifierRestrictionDescr.class);
057:
058: this .allowNesting = false;
059: }
060: }
061:
062: public Object start(final String uri, final String localName,
063: final Attributes attrs) throws SAXException {
064: this .xmlPackageReader.startConfiguration(localName, attrs);
065:
066: final String evaluator = attrs.getValue("evaluator");
067: if (evaluator == null || evaluator.trim().equals("")) {
068: throw new SAXParseException(
069: "<bound-variable> requires an 'evaluator' attribute",
070: this .xmlPackageReader.getLocator());
071: }
072:
073: // qualifiedIdentifierRestricionDescr.set
074:
075: final QualifiedIdentifierRestrictionDescr qualifiedIdentifierRestricionDescr = new QualifiedIdentifierRestrictionDescr(
076: evaluator, null);
077:
078: return qualifiedIdentifierRestricionDescr;
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 QualifiedIdentifierRestrictionDescr qualifiedIdentifierRestricionDescr = (QualifiedIdentifierRestrictionDescr) this .xmlPackageReader
087: .getCurrent();
088:
089: final String expression = config.getText();
090:
091: if (expression == null || expression.trim().equals("")) {
092: throw new SAXParseException(
093: "<qualified-identifier-restriction> must have an expression content",
094: this .xmlPackageReader.getLocator());
095: }
096:
097: qualifiedIdentifierRestricionDescr.setText(expression);
098:
099: final LinkedList parents = this .xmlPackageReader.getParents();
100: final ListIterator it = parents.listIterator(parents.size());
101: it.previous();
102:
103: final Object parent = it.previous();
104:
105: if (parent instanceof FieldConstraintDescr) {
106: final FieldConstraintDescr fieldConstraintDescr = (FieldConstraintDescr) parent;
107: fieldConstraintDescr
108: .addRestriction(qualifiedIdentifierRestricionDescr);
109: } else if (parent instanceof RestrictionConnectiveDescr) {
110: final RestrictionConnectiveDescr restrictionConDescr = (RestrictionConnectiveDescr) parent;
111: restrictionConDescr
112: .addRestriction(qualifiedIdentifierRestricionDescr);
113: }
114: return null;
115: }
116:
117: public Class generateNodeFor() {
118: return QualifiedIdentifierRestrictionDescr.class;
119: }
120: }
|