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.OMElement;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.synapse.Mediator;
026: import org.apache.synapse.SynapseException;
027: import org.apache.synapse.mediators.filters.SwitchMediator;
028:
029: import java.util.Iterator;
030:
031: /**
032: * <pre>
033: * <switch source="xpath">
034: * <case regex="string">
035: * mediator+
036: * </case>+
037: * <default>
038: * mediator+
039: * </default>?
040: * </switch>
041: * </pre>
042: */
043: public class SwitchMediatorSerializer extends
044: AbstractMediatorSerializer {
045:
046: public OMElement serializeMediator(OMElement parent, Mediator m) {
047:
048: if (!(m instanceof SwitchMediator)) {
049: handleException("Unsupported mediator passed in for serialization : "
050: + m.getType());
051: }
052:
053: SwitchMediator mediator = (SwitchMediator) m;
054: OMElement switchMed = fac.createOMElement("switch", synNS);
055: saveTracingState(switchMed, mediator);
056:
057: if (mediator.getSource() != null) {
058: SynapseXPathSerializer.serializeXPath(mediator.getSource(),
059: switchMed, "source");
060:
061: } else {
062: handleException("Invalid switch mediator. Source required");
063: }
064:
065: Iterator iter = mediator.getCases().iterator();
066: while (iter.hasNext()) {
067: OMElement caseElem = fac.createOMElement("case", synNS);
068: SwitchCase aCase = ((SwitchCase) iter.next());
069: if (aCase.getRegex() != null) {
070: caseElem.addAttribute(fac.createOMAttribute("regex",
071: nullNS, aCase.getRegex().pattern()));
072: } else {
073: handleException("Invalid switch case. Regex required");
074: }
075: AnonymousListMediator caseMediator = aCase
076: .getCaseMediator();
077: if (caseMediator != null) {
078: new AnonymousListMediatorSerializer()
079: .serializeMediator(caseElem, caseMediator);
080: switchMed.addChild(caseElem);
081: }
082: }
083: SwitchCase defaultCase = mediator.getDefaultCase();
084: if (defaultCase != null) {
085: OMElement caseDefaultElem = fac.createOMElement("default",
086: synNS);
087: AnonymousListMediator caseDefaultMediator = defaultCase
088: .getCaseMediator();
089: if (caseDefaultMediator != null) {
090: new AnonymousListMediatorSerializer()
091: .serializeMediator(caseDefaultElem,
092: caseDefaultMediator);
093: switchMed.addChild(caseDefaultElem);
094: }
095: }
096: if (parent != null) {
097: parent.addChild(switchMed);
098: }
099: return switchMed;
100: }
101:
102: public String getMediatorClassName() {
103: return SwitchMediator.class.getName();
104: }
105: }
|