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: package org.apache.synapse.mediators.xquery;
020:
021: import net.sf.saxon.javax.xml.xquery.XQItemType;
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.OMNamespace;
024: import org.apache.axiom.om.xpath.AXIOMXPath;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.synapse.Mediator;
028: import org.apache.synapse.config.xml.AbstractMediatorSerializer;
029: import org.apache.synapse.config.xml.SynapseXPathSerializer;
030: import org.apache.synapse.config.xml.XMLConfigConstants;
031: import org.apache.synapse.util.xpath.SynapseXPath;
032:
033: import javax.xml.namespace.QName;
034: import java.util.List;
035:
036: /**
037: * Serialize the given XQuery mediator into a XML
038: * <p/>
039: * <pre>
040: * <xquery key="string" [target="xpath"]>
041: * <variable name="string" type="string" [key="string"] [expression="xpath"]
042: * [value="string"]/>?
043: * </xquery>
044: * </pre>
045: */
046: public class XQueryMediatorSerializer extends
047: AbstractMediatorSerializer {
048:
049: private static final Log log = LogFactory
050: .getLog(XQueryMediatorSerializer.class);
051:
052: private static final OMNamespace XQUERY_NS = fac.createOMNamespace(
053: XMLConfigConstants.SYNAPSE_NAMESPACE, "xquery");
054:
055: public OMElement serializeMediator(OMElement parent, Mediator m) {
056:
057: if (!(m instanceof XQueryMediator)) {
058: handleException("Invalid Mediator has passed to serializer");
059: }
060: XQueryMediator queryMediator = (XQueryMediator) m;
061:
062: OMElement xquery = fac.createOMElement("xquery", synNS);
063: String key = queryMediator.getQueryKey();
064: if (key != null) {
065: xquery.addAttribute(fac.createOMAttribute("key", nullNS,
066: key));
067:
068: }
069:
070: saveTracingState(xquery, queryMediator);
071:
072: AXIOMXPath targetXPath = queryMediator.getTarget();
073: if (targetXPath != null
074: && !XQueryMediator.DEFAULT_XPATH.equals(targetXPath
075: .toString())) {
076:
077: xquery.addAttribute(fac.createOMAttribute("target", nullNS,
078: targetXPath.toString()));
079: serializeNamespaces(xquery, targetXPath);
080: }
081:
082: List pros = queryMediator.getDataSourceProperties();
083: if (pros != null && !pros.isEmpty()) {
084: OMElement dataSource = fac.createOMElement("dataSource",
085: synNS);
086: serializeProperties(dataSource, pros);
087: xquery.addChild(dataSource);
088: }
089:
090: List list = queryMediator.getVariables();
091: if (list != null && !list.isEmpty()) {
092: for (int i = 0; i < list.size(); i++) {
093: Object o = list.get(i);
094: if (o instanceof MediatorBaseVariable) {
095: MediatorBaseVariable variable = (MediatorBaseVariable) o;
096: QName name = variable.getName();
097: Object value = variable.getValue();
098: if (name != null && value != null) {
099: OMElement baseElement = fac.createOMElement(
100: "variable", synNS);
101: baseElement.addAttribute(fac.createOMAttribute(
102: "name", nullNS, name.getLocalPart()));
103: baseElement.addAttribute(fac.createOMAttribute(
104: "value", nullNS, (String) value));
105: String type = null;
106: int varibelType = variable.getType();
107: if (XQItemType.XQBASETYPE_INT == varibelType) {
108: type = "INT";
109: } else if (XQItemType.XQBASETYPE_INTEGER == varibelType) {
110: type = "INTEGER";
111: } else if (XQItemType.XQBASETYPE_BOOLEAN == varibelType) {
112: type = "BOOLEAN";
113: } else if (XQItemType.XQBASETYPE_BYTE == varibelType) {
114: type = "BYTE";
115: } else if (XQItemType.XQBASETYPE_DOUBLE == varibelType) {
116: type = "DOUBLE";
117: } else if (XQItemType.XQBASETYPE_SHORT == varibelType) {
118: type = "SHORT";
119: } else if (XQItemType.XQBASETYPE_LONG == varibelType) {
120: type = "LONG";
121: } else if (XQItemType.XQBASETYPE_FLOAT == varibelType) {
122: type = "FLOAT";
123: } else if (XQItemType.XQBASETYPE_STRING == varibelType) {
124: type = "STRING";
125: } else if (XQItemType.XQITEMKIND_DOCUMENT == varibelType) {
126: type = "DOCUMENT";
127: } else if (XQItemType.XQITEMKIND_DOCUMENT_ELEMENT == varibelType) {
128: type = "DOCUMENT_ELEMENT";
129: } else if (XQItemType.XQITEMKIND_ELEMENT == varibelType) {
130: type = "ELEMENT";
131: } else {
132: handleException("Unknown Type "
133: + varibelType);
134: }
135: if (type != null) {
136: baseElement.addAttribute(fac
137: .createOMAttribute("type", nullNS,
138: type));
139:
140: }
141: xquery.addChild(baseElement);
142: }
143: } else if (o instanceof MediatorCustomVariable) {
144: MediatorCustomVariable variable = (MediatorCustomVariable) o;
145: QName name = variable.getName();
146: if (name != null) {
147: OMElement customElement = fac.createOMElement(
148: "variable", synNS);
149: customElement.addAttribute(fac
150: .createOMAttribute("name", nullNS, name
151: .getLocalPart()));
152: String regkey = variable.getRegKey();
153: if (regkey != null) {
154: customElement.addAttribute(fac
155: .createOMAttribute("key", nullNS,
156: regkey));
157: }
158: AXIOMXPath expression = variable
159: .getExpression();
160: if (expression != null
161: && !XQueryMediator.DEFAULT_XPATH
162: .equals(expression.toString())) {
163:
164: customElement.addAttribute(fac
165: .createOMAttribute("expression",
166: nullNS, expression
167: .toString()));
168: serializeNamespaces(customElement,
169: expression);
170: }
171: String type = null;
172: int varibelType = variable.getType();
173: if (XQItemType.XQITEMKIND_DOCUMENT == varibelType) {
174: type = "DOCUMENT";
175: } else if (XQItemType.XQITEMKIND_DOCUMENT_ELEMENT == varibelType) {
176: type = "DOCUMENT_ELEMENT";
177: } else if (XQItemType.XQITEMKIND_ELEMENT == varibelType) {
178: type = "ELEMENT";
179: } else if (XQItemType.XQBASETYPE_INT == varibelType) {
180: type = "INT";
181: } else if (XQItemType.XQBASETYPE_INTEGER == varibelType) {
182: type = "INTEGER";
183: } else if (XQItemType.XQBASETYPE_BOOLEAN == varibelType) {
184: type = "BOOLEAN";
185: } else if (XQItemType.XQBASETYPE_BYTE == varibelType) {
186: type = "BYTE";
187: } else if (XQItemType.XQBASETYPE_DOUBLE == varibelType) {
188: type = "DOUBLE";
189: } else if (XQItemType.XQBASETYPE_SHORT == varibelType) {
190: type = "SHORT";
191: } else if (XQItemType.XQBASETYPE_LONG == varibelType) {
192: type = "LONG";
193: } else if (XQItemType.XQBASETYPE_FLOAT == varibelType) {
194: type = "FLOAT";
195: } else if (XQItemType.XQBASETYPE_STRING == varibelType) {
196: type = "STRING";
197: } else {
198: handleException("Unknown Type "
199: + varibelType);
200: }
201: if (type != null) {
202: customElement.addAttribute(fac
203: .createOMAttribute("type", nullNS,
204: type));
205:
206: }
207: xquery.addChild(customElement);
208: }
209: }
210: }
211: }
212: if (parent != null) {
213: parent.addChild(xquery);
214: }
215: return xquery;
216: }
217:
218: public String getMediatorClassName() {
219: return XQueryMediator.class.getName();
220: }
221:
222: }
|