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.AndDescr;
024: import org.drools.lang.descr.ConditionalElementDescr;
025: import org.drools.lang.descr.FieldBindingDescr;
026: import org.drools.lang.descr.FieldConstraintDescr;
027: import org.drools.lang.descr.OrDescr;
028: import org.drools.lang.descr.PatternDescr;
029: import org.drools.lang.descr.PredicateDescr;
030: import org.xml.sax.Attributes;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.SAXParseException;
033:
034: /**
035: * @author mproctor
036: *
037: * TODO To change the template for this generated type comment go to Window -
038: * Preferences - Java - Code Style - Code Templates
039: */
040: class FieldConstraintHandler extends BaseAbstractHandler implements
041: Handler {
042: FieldConstraintHandler(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(PatternDescr.class);
048: this .validParents.add(AndDescr.class);
049: this .validParents.add(OrDescr.class);
050:
051: this .validPeers = new HashSet();
052: this .validPeers.add(null);
053: this .validPeers.add(FieldConstraintDescr.class);
054: this .validPeers.add(PredicateDescr.class);
055: this .validPeers.add(FieldBindingDescr.class);
056:
057: this .validPeers.add(AndDescr.class);
058: this .validPeers.add(OrDescr.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:
068: final String fieldName = attrs.getValue("field-name");
069:
070: if (fieldName == null || fieldName.trim().equals("")) {
071: throw new SAXParseException(
072: "<field-constraint> requires a 'field-name' attribute",
073: this .xmlPackageReader.getLocator());
074: }
075:
076: final FieldConstraintDescr fieldConstraint = new FieldConstraintDescr(
077: fieldName);
078:
079: return fieldConstraint;
080: }
081:
082: public Object end(final String uri, final String localName)
083: throws SAXException {
084:
085: final Configuration config = this .xmlPackageReader
086: .endConfiguration();
087:
088: final FieldConstraintDescr fieldConstraintDescr = (FieldConstraintDescr) this .xmlPackageReader
089: .getCurrent();
090:
091: final LinkedList parents = this .xmlPackageReader.getParents();
092: final ListIterator it = parents.listIterator(parents.size());
093: it.previous();
094: final Object parent = it.previous();
095:
096: if (parent instanceof PatternDescr) {
097: final PatternDescr patternDescr = (PatternDescr) parent;
098: patternDescr.addConstraint(fieldConstraintDescr);
099: } else if (parent instanceof ConditionalElementDescr) {
100: final ConditionalElementDescr ceDescr = (ConditionalElementDescr) parent;
101: final FieldConstraintDescr field = (FieldConstraintDescr) this .xmlPackageReader
102: .getCurrent();
103: ceDescr.addOrMerge(field);
104: }
105:
106: return null;
107: }
108:
109: public Class generateNodeFor() {
110: return FieldConstraintDescr.class;
111: }
112: }
|