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.AccumulateDescr;
024: import org.drools.lang.descr.AndDescr;
025: import org.drools.lang.descr.CollectDescr;
026: import org.drools.lang.descr.ConditionalElementDescr;
027: import org.drools.lang.descr.EvalDescr;
028: import org.drools.lang.descr.ExistsDescr;
029: import org.drools.lang.descr.ForallDescr;
030: import org.drools.lang.descr.NotDescr;
031: import org.drools.lang.descr.OrDescr;
032: import org.drools.lang.descr.PatternDescr;
033: import org.drools.lang.descr.PatternDestinationDescr;
034: import org.xml.sax.Attributes;
035: import org.xml.sax.SAXException;
036: import org.xml.sax.SAXParseException;
037:
038: /**
039: * @author mproctor
040: *
041: * TODO To change the template for this generated type comment go to Window -
042: * Preferences - Java - Code Style - Code Templates
043: */
044: class PatternHandler extends BaseAbstractHandler implements Handler {
045: PatternHandler(final XmlPackageReader xmlPackageReader) {
046: this .xmlPackageReader = xmlPackageReader;
047:
048: if ((this .validParents == null) && (this .validPeers == null)) {
049: this .validParents = new HashSet();
050: this .validParents.add(AndDescr.class);
051: this .validParents.add(OrDescr.class);
052: this .validParents.add(NotDescr.class);
053: this .validParents.add(ExistsDescr.class);
054: this .validParents.add(CollectDescr.class);
055: this .validParents.add(ForallDescr.class);
056: this .validParents.add(AccumulateDescr.class);
057:
058: this .validPeers = new HashSet();
059: this .validPeers.add(null);
060: this .validPeers.add(AndDescr.class);
061: this .validPeers.add(OrDescr.class);
062: this .validPeers.add(NotDescr.class);
063: this .validPeers.add(ExistsDescr.class);
064: this .validPeers.add(EvalDescr.class);
065: this .validPeers.add(PatternDescr.class);
066: this .validPeers.add(ForallDescr.class);
067:
068: this .allowNesting = true;
069: }
070: }
071:
072: public Object start(final String uri, final String localName,
073: final Attributes attrs) throws SAXException {
074: this .xmlPackageReader.startConfiguration(localName, attrs);
075:
076: final String objectType = attrs.getValue("object-type");
077:
078: if (objectType == null || objectType.trim().equals("")) {
079: throw new SAXParseException(
080: "<pattern> requires an 'object-type' attribute",
081: this .xmlPackageReader.getLocator());
082: }
083:
084: PatternDescr patternDescr = null;
085:
086: final String identifier = attrs.getValue("identifier");
087: if (identifier == null || identifier.trim().equals("")) {
088: patternDescr = new PatternDescr(objectType);
089: } else {
090: patternDescr = new PatternDescr(objectType, identifier);
091: }
092:
093: return patternDescr;
094: }
095:
096: public Object end(final String uri, final String localName)
097: throws SAXException {
098:
099: final Configuration config = this .xmlPackageReader
100: .endConfiguration();
101: final PatternDescr patternDescr = (PatternDescr) this .xmlPackageReader
102: .getCurrent();
103:
104: final LinkedList parents = this .xmlPackageReader.getParents();
105: final ListIterator ite = parents.listIterator(parents.size());
106: ite.previous();
107: final Object parent = ite.previous();
108:
109: if (parent instanceof PatternDestinationDescr) {
110: final PatternDestinationDescr parentDescr = (PatternDestinationDescr) parent;
111: parentDescr.setInputPattern(patternDescr);
112: } else {
113: final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
114: parentDescr.addDescr(patternDescr);
115: }
116: return null;
117: }
118:
119: public Class generateNodeFor() {
120: return PatternDescr.class;
121: }
122: }
|