001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.drools.model;
018
019 import javax.jbi.messaging.NormalizedMessage;
020 import javax.xml.namespace.NamespaceContext;
021 import javax.xml.transform.Source;
022 import javax.xml.transform.dom.DOMSource;
023
024 import org.w3c.dom.Element;
025
026 import org.apache.servicemix.expression.JAXPBooleanXPathExpression;
027 import org.apache.servicemix.expression.JAXPStringXPathExpression;
028 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
029
030 public class Message {
031
032 private static final SourceTransformer TRANFORMER = new SourceTransformer();
033
034 private final NormalizedMessage message;
035 private final NamespaceContext namespaceContext;
036
037 public Message(NormalizedMessage message, NamespaceContext namespaceContext) {
038 this.message = message;
039 // Make sure message is re-readable
040 this.namespaceContext = namespaceContext;
041 Source content = message.getContent();
042 if (content != null) {
043 try {
044 content = new DOMSource(TRANFORMER.toDOMElement(content));
045 message.setContent(content);
046 } catch (Exception e) {
047 throw new RuntimeException(e);
048 }
049 }
050
051 }
052
053 public NormalizedMessage getInternalMessage() {
054 return this.message;
055 }
056
057 public boolean xpath(String xpath) throws Exception {
058 JAXPBooleanXPathExpression expression = new JAXPBooleanXPathExpression(xpath);
059 if (this.namespaceContext != null) {
060 expression.setNamespaceContext(this.namespaceContext);
061 }
062 Boolean b = (Boolean) expression.evaluate(null, message);
063 return b.booleanValue();
064 }
065
066 public String valueOf(String xpath) throws Exception {
067 JAXPStringXPathExpression expression = new JAXPStringXPathExpression(xpath);
068 if (this.namespaceContext != null) {
069 expression.setNamespaceContext(this.namespaceContext);
070 }
071 return (String)expression.evaluate(null, message);
072 }
073
074
075 public Object getProperty(String name) {
076 return message.getProperty(name);
077 }
078
079 public void setProperty(String name, Object value) {
080 message.setProperty(name, value);
081 }
082
083 public Element getContent() {
084 return (Element) ((DOMSource) message.getContent()).getNode();
085 }
086
087 }