001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. 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,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.config.xml;
021:
022: import org.apache.axiom.om.OMAttribute;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.synapse.Mediator;
025: import org.apache.synapse.SynapseConstants;
026: import org.apache.synapse.mediators.filters.FilterMediator;
027: import org.jaxen.JaxenException;
028:
029: import javax.xml.namespace.QName;
030: import java.util.regex.Pattern;
031: import java.util.regex.PatternSyntaxException;
032:
033: /**
034: * <p>Creates a filter mediator instance with the default behavior</p>
035: *
036: * <pre>
037: * <filter (source="xpath" regex="string") | xpath="xpath">
038: * mediator+
039: * </filter>
040: * </pre>
041: *
042: * <p>or if the filter medaitor needs to support the else behavior as well (i.e. a set of mediators
043: * to be executed when the filter evaluates to false).</p>
044: *
045: * <pre>
046: * <filter (source="xpath" regex="string") | xpath="xpath">
047: * <then [sequence="string"]>
048: * mediator+
049: * </then>
050: * <else [sequence="string"]>
051: * mediator+
052: * </else>
053: * </filter>
054: * </pre>
055: */
056: public class FilterMediatorFactory extends AbstractListMediatorFactory {
057:
058: private static final QName FILTER_Q = new QName(
059: SynapseConstants.SYNAPSE_NAMESPACE, "filter");
060: private static final QName THEN_Q = new QName(
061: SynapseConstants.SYNAPSE_NAMESPACE, "then");
062: private static final QName ELSE_Q = new QName(
063: SynapseConstants.SYNAPSE_NAMESPACE, "else");
064:
065: public Mediator createMediator(OMElement elem) {
066:
067: FilterMediator filter = new FilterMediator();
068:
069: OMAttribute attXpath = elem.getAttribute(ATT_XPATH);
070: OMAttribute attSource = elem.getAttribute(ATT_SOURCE);
071: OMAttribute attRegex = elem.getAttribute(ATT_REGEX);
072:
073: if (attXpath != null) {
074: if (attXpath.getAttributeValue() != null
075: && attXpath.getAttributeValue().trim().length() == 0) {
076:
077: handleException("Invalid attribute value specified for xpath");
078:
079: } else {
080:
081: try {
082: filter.setXpath(SynapseXPathFactory
083: .getSynapseXPath(elem, ATT_XPATH));
084: } catch (JaxenException e) {
085: handleException(
086: "Invalid XPath expression for attribute xpath : "
087: + attXpath.getAttributeValue(), e);
088: }
089:
090: }
091:
092: } else if (attSource != null && attRegex != null) {
093:
094: if ((attSource.getAttributeValue() != null && attSource
095: .getAttributeValue().trim().length() == 0)
096: || (attRegex.getAttributeValue() != null && attRegex
097: .getAttributeValue().trim().length() == 0)) {
098:
099: handleException("Invalid attribute values for source and/or regex specified");
100:
101: } else {
102:
103: try {
104: filter.setSource(SynapseXPathFactory
105: .getSynapseXPath(elem, ATT_SOURCE));
106: } catch (JaxenException e) {
107:
108: handleException(
109: "Invalid XPath expression for attribute source : "
110: + attSource.getAttributeValue(), e);
111: }
112:
113: try {
114: filter.setRegex(Pattern.compile(attRegex
115: .getAttributeValue()));
116: } catch (PatternSyntaxException pse) {
117:
118: handleException(
119: "Invalid Regular Expression for attribute regex : "
120: + attRegex.getAttributeValue(), pse);
121: }
122: }
123:
124: } else {
125:
126: handleException("An xpath or (source, regex) attributes are required for a filter");
127: }
128:
129: // after successfully creating the mediator
130: // set its common attributes such as tracing etc
131: processTraceState(filter, elem);
132:
133: OMElement thenElem = elem.getFirstChildWithName(THEN_Q);
134:
135: if (thenElem != null) {
136:
137: filter.setThenElementPresent(true);
138: OMAttribute sequenceAttr = thenElem
139: .getAttribute(ATT_SEQUENCE);
140:
141: if (sequenceAttr != null
142: && sequenceAttr.getAttributeValue() != null) {
143:
144: filter.setThenKey(sequenceAttr.getAttributeValue());
145:
146: } else {
147: addChildren(thenElem, filter);
148: }
149:
150: OMElement elseElem = elem.getFirstChildWithName(ELSE_Q);
151:
152: if (elseElem != null) {
153:
154: sequenceAttr = elseElem.getAttribute(ATT_SEQUENCE);
155:
156: if (sequenceAttr != null
157: && sequenceAttr.getAttributeValue() != null) {
158:
159: filter.setElseKey(sequenceAttr.getAttributeValue());
160:
161: } else {
162:
163: AnonymousListMediator listMediator = AnonymousListMediatorFactory
164: .createAnonymousListMediator(elseElem);
165: filter.setElseMediator(listMediator);
166: }
167: }
168:
169: } else {
170:
171: filter.setThenElementPresent(false);
172: addChildren(elem, filter);
173: }
174:
175: return filter;
176: }
177:
178: public QName getTagQName() {
179: return FILTER_Q;
180: }
181: }
|