View Javadoc

1   /*
2    * Copyright  1999-2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  
18  package org.apache.axis.message.addressing.util;
19  
20  import javax.xml.soap.SOAPElement;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Node;
27  import org.w3c.dom.NodeList;
28  import org.w3c.dom.Text;
29  
30  /***
31   * Class TextExtractor
32   * 
33   * @author Davanum Srinivas (dims@yahoo.com)
34   */
35  public class TextExtractor {
36  
37      public static QName getQName(String value, SOAPElement elem) {
38          int p = value.indexOf(':');
39          String prefix = (p == -1) ? "" : value.substring(0, p);
40          String ns = elem.getNamespaceURI(prefix);
41          return new QName(ns, value.substring(p+1));
42      }
43  
44      public static String getText(Node node) {
45          if (node == null) {
46              return "";
47          }
48  
49          if (node instanceof Text) {
50              return node.getNodeValue().trim();
51          }
52  
53          StringBuffer result = new StringBuffer();
54          NodeList list = node.getChildNodes();
55          for (int i=0;i<list.getLength();i++) {
56              Node child = list.item(i);
57              if (child instanceof Text) {
58                  result.append(child.getNodeValue());
59              } else {
60                  // until first non text node
61                  break;
62              }
63          }
64          return result.toString().trim();
65      }
66  
67      public static String getText(SOAPElement element) {
68          String value = element.getValue();
69          return (value == null) ? "" : value.trim();
70      }
71      
72  }