01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.synapse.config.xml;
21:
22: import org.apache.axiom.om.OMElement;
23: import org.apache.axiom.om.OMNamespace;
24: import org.apache.commons.logging.Log;
25: import org.apache.commons.logging.LogFactory;
26: import org.apache.synapse.SynapseException;
27: import org.apache.synapse.util.xpath.SynapseXPath;
28:
29: /**
30: *
31: */
32: public class SynapseXPathSerializer {
33:
34: private static final Log log = LogFactory
35: .getLog(SynapseXPathSerializer.class);
36:
37: public static OMElement serializeXPath(SynapseXPath xpath,
38: OMElement elem, String attribName) {
39:
40: OMNamespace nullNS = elem.getOMFactory().createOMNamespace(
41: XMLConfigConstants.NULL_NAMESPACE, "");
42:
43: if (xpath != null) {
44:
45: elem.addAttribute(elem.getOMFactory().createOMAttribute(
46: attribName, nullNS, xpath.toString()));
47:
48: serializeNamespaces(elem, xpath);
49:
50: } else {
51: handleException("Couldn't find the xpath in the SynapseXPath");
52: }
53:
54: return elem;
55: }
56:
57: private static void serializeNamespaces(OMElement elem,
58: SynapseXPath xpath) {
59:
60: for (Object o : xpath.getNamespaces().keySet()) {
61: String prefix = (String) o;
62: String uri = xpath.getNamespaceContext()
63: .translateNamespacePrefixToUri(prefix);
64: if (!XMLConfigConstants.SYNAPSE_NAMESPACE.equals(uri)) {
65: elem.declareNamespace(uri, prefix);
66: }
67: }
68: }
69:
70: private static void handleException(String message) {
71: log.error(message);
72: throw new SynapseException(message);
73: }
74: }
|