KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > mps > XPathContentMessagePropertyValue


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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.components.mps;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.messaging.NormalizedMessage;
21
22 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
23 import org.w3c.dom.Document JavaDoc;
24
25 import org.apache.xpath.CachedXPathAPI;
26 import org.apache.xpath.objects.XObject;
27
28 /**
29  * Get the Value of a property from the message given the XPath Statement.
30  * @author rbuckland
31  *
32  */

33 public class XPathContentMessagePropertyValue implements PropertyValue {
34     
35     public final static String JavaDoc XML_ELEMENT_NAME = "xpath-expression";
36     
37     private String JavaDoc xpath;
38     
39     public XPathContentMessagePropertyValue(String JavaDoc xpath) {
40         this.xpath = xpath;
41     }
42
43     /**
44      * Get a value give the XPath statement preset in our Ctor.
45      * Return null if the value was empty or not there
46      */

47     public String JavaDoc getPropertyValue(NormalizedMessage msg) throws JBIException {
48         String JavaDoc resultValue = null;
49         if (msg.getContent() != null) {
50             CachedXPathAPI xpathApi = new CachedXPathAPI();
51             try {
52                 Document JavaDoc doc = new SourceTransformer().toDOMDocument(msg);
53                 XObject result = xpathApi.eval(doc,xpath);
54
55                 resultValue = result.toString();
56                 if ("".equals(resultValue)) {
57                     resultValue = null;
58                 }
59             } catch (Exception JavaDoc e) {
60                 throw new JBIException("Could not get value from message via xpath " + xpath, e);
61             }
62         }
63         return resultValue;
64
65     }
66     
67 }
68
Popular Tags