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: * @author fmeyer
036: *
037: */
038: class LiteralRestrictionHandler extends BaseAbstractHandler implements
039: Handler {
040: LiteralRestrictionHandler(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: this .validPeers.add(null);
050:
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: "<literal-restriction> requires an 'evaluator' attribute",
070: this .xmlPackageReader.getLocator());
071: }
072:
073: final String text = attrs.getValue("value");
074: if (text == null || text.trim().equals("")) {
075: throw new SAXParseException(
076: "<literal-restriction> requires an 'value' attribute",
077: this .xmlPackageReader.getLocator());
078: }
079:
080: final LiteralRestrictionDescr literalDescr = new LiteralRestrictionDescr(
081: evaluator, text);
082:
083: return literalDescr;
084: }
085:
086: public Object end(final String uri, final String localName)
087: throws SAXException {
088: final Configuration config = this .xmlPackageReader
089: .endConfiguration();
090:
091: final LiteralRestrictionDescr literalDescr = (LiteralRestrictionDescr) this .xmlPackageReader
092: .getCurrent();
093:
094: final LinkedList parents = this .xmlPackageReader.getParents();
095: final ListIterator it = parents.listIterator(parents.size());
096: it.previous();
097:
098: final Object parent = it.previous();
099:
100: if (parent instanceof FieldConstraintDescr) {
101: final FieldConstraintDescr fieldConstriantDescr = (FieldConstraintDescr) parent;
102: fieldConstriantDescr.addRestriction(literalDescr);
103: } else if (parent instanceof RestrictionConnectiveDescr) {
104: final RestrictionConnectiveDescr restrictionDescr = (RestrictionConnectiveDescr) parent;
105: restrictionDescr.addRestriction(literalDescr);
106: }
107: return null;
108: }
109:
110: public Class generateNodeFor() {
111: return LiteralRestrictionDescr.class;
112: }
113: }
|