KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > expression > XMLBeansStringXPathExpression


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.expression;
18
19 import org.apache.xmlbeans.XmlCursor;
20 import org.apache.xmlbeans.XmlObject;
21 import org.apache.xmlbeans.XmlOptions;
22
23 /**
24  * An {@link Expression} which evaluates an XPath expression using <a HREF="http://xmlbeans.apache.org/">XMLBeans</a> and
25  * returns the String value.
26  *
27  * @version $Revision: 426415 $
28  */

29 public class XMLBeansStringXPathExpression extends XMLBeansXPathExpression {
30     public XMLBeansStringXPathExpression(String JavaDoc xpath) {
31         super(xpath);
32     }
33
34     protected Object JavaDoc evaluateXPath(XmlObject object, String JavaDoc xpath, XmlOptions options) {
35         XmlObject[] xmlObjects = object.selectPath(xpath);
36         if (xmlObjects == null || xmlObjects.length == 0) {
37             return "";
38         }
39         else if (xmlObjects.length == 1) {
40             return asString(xmlObjects[0]);
41         }
42         else {
43             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
44             for (int i = 0; i < xmlObjects.length; i++) {
45                 XmlObject xmlObject = xmlObjects[i];
46                 buffer.append(asString(xmlObject));
47             }
48             return buffer.toString();
49         }
50     }
51
52     protected String JavaDoc asString(XmlObject xmlObject) {
53         XmlCursor cursor = xmlObject.newCursor();
54         return cursor.getTextValue();
55     }
56 }
57
Popular Tags