01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.drools.model;
18:
19: import javax.jbi.messaging.NormalizedMessage;
20: import javax.xml.namespace.NamespaceContext;
21: import javax.xml.transform.Source;
22: import javax.xml.transform.dom.DOMSource;
23:
24: import org.w3c.dom.Element;
25:
26: import org.apache.servicemix.expression.JAXPBooleanXPathExpression;
27: import org.apache.servicemix.expression.JAXPStringXPathExpression;
28: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
29:
30: public class Message {
31:
32: private static final SourceTransformer TRANFORMER = new SourceTransformer();
33:
34: private final NormalizedMessage message;
35: private final NamespaceContext namespaceContext;
36:
37: public Message(NormalizedMessage message,
38: NamespaceContext namespaceContext) {
39: this .message = message;
40: // Make sure message is re-readable
41: this .namespaceContext = namespaceContext;
42: Source content = message.getContent();
43: if (content != null) {
44: try {
45: content = new DOMSource(TRANFORMER
46: .toDOMElement(content));
47: message.setContent(content);
48: } catch (Exception e) {
49: throw new RuntimeException(e);
50: }
51: }
52:
53: }
54:
55: public NormalizedMessage getInternalMessage() {
56: return this .message;
57: }
58:
59: public boolean xpath(String xpath) throws Exception {
60: JAXPBooleanXPathExpression expression = new JAXPBooleanXPathExpression(
61: xpath);
62: if (this .namespaceContext != null) {
63: expression.setNamespaceContext(this .namespaceContext);
64: }
65: Boolean b = (Boolean) expression.evaluate(null, message);
66: return b.booleanValue();
67: }
68:
69: public String valueOf(String xpath) throws Exception {
70: JAXPStringXPathExpression expression = new JAXPStringXPathExpression(
71: xpath);
72: if (this .namespaceContext != null) {
73: expression.setNamespaceContext(this .namespaceContext);
74: }
75: return (String) expression.evaluate(null, message);
76: }
77:
78: public Object getProperty(String name) {
79: return message.getProperty(name);
80: }
81:
82: public void setProperty(String name, Object value) {
83: message.setProperty(name, value);
84: }
85:
86: public Element getContent() {
87: return (Element) ((DOMSource) message.getContent()).getNode();
88: }
89:
90: }
|