blob: 0017f195424b400e15ffabf8593e4d89ec14b325 [file] [log] [blame]
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Stuart McCullochc792b372008-02-17 16:12:24 +000019package org.apache.felix.obrplugin;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000020
21
22import org.w3c.dom.Node;
23import org.w3c.dom.NodeList;
24
25
26/**
27 * Provide XML helper methods to support pre-Java5 runtimes
28 *
29 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
30 */
31public class XmlHelper
32{
33 /**
34 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
35 */
36 public static String getTextContent( Node node )
37 {
38 switch ( node.getNodeType() )
39 {
40 case Node.ELEMENT_NODE:
41 case Node.ATTRIBUTE_NODE:
42 case Node.ENTITY_NODE:
43 case Node.ENTITY_REFERENCE_NODE:
44 case Node.DOCUMENT_FRAGMENT_NODE:
45 return mergeTextContent( node.getChildNodes() );
46 case Node.TEXT_NODE:
47 case Node.CDATA_SECTION_NODE:
48 case Node.COMMENT_NODE:
49 case Node.PROCESSING_INSTRUCTION_NODE:
50 return node.getNodeValue();
51 case Node.DOCUMENT_NODE:
52 case Node.DOCUMENT_TYPE_NODE:
53 case Node.NOTATION_NODE:
54 default:
55 return null;
56 }
57 }
58
59
60 /**
61 * based on the following quote from public Java5 javadoc of org.w3c.dom.Node.getTextContent method:
62 *
63 * "concatenation of the textContent attribute value of every child node, excluding COMMENT_NODE and
64 * PROCESSING_INSTRUCTION_NODE nodes. This is the empty string if the node has no children"
65 */
66 private static String mergeTextContent( NodeList nodes )
67 {
68 StringBuffer buf = new StringBuffer();
69 for ( int i = 0; i < nodes.getLength(); i++ )
70 {
71 Node n = nodes.item( i );
72 final String text;
73
74 switch ( n.getNodeType() )
75 {
76 case Node.COMMENT_NODE:
77 case Node.PROCESSING_INSTRUCTION_NODE:
78 text = null;
79 break;
80 default:
81 text = getTextContent( n );
82 break;
83 }
84
85 if ( text != null )
86 {
87 buf.append( text );
88 }
89 }
90 return buf.toString();
91 }
92
93
94 /**
95 * based on public Java5 javadoc of org.w3c.dom.Node.setTextContent method
96 */
97 public static void setTextContent( Node node, final String text )
98 {
99 while ( node.hasChildNodes() )
100 {
101 node.removeChild( node.getFirstChild() );
102 }
103
104 if ( text != null && text.length() > 0 )
105 {
106 Node textNode = node.getOwnerDocument().createTextNode( text );
107 node.appendChild( textNode );
108 }
109 }
110}