KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > util > XMLSupport


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 */

18
19 package org.apache.batik.dom.util;
20
21 import org.apache.batik.util.XMLConstants;
22 import org.w3c.dom.Attr JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25
26 /**
27  * This class provides support for XML features.
28  *
29  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
30  * @version $Id: XMLSupport.java,v 1.9 2004/10/30 18:38:04 deweese Exp $
31  */

32
33 public class XMLSupport implements XMLConstants {
34
35     /**
36      * This class does not need to be instanciated.
37      */

38     protected XMLSupport() {
39     }
40
41     /**
42      * Returns the xml:lang attribute value of the given element.
43      */

44     public static String JavaDoc getXMLLang(Element JavaDoc elt) {
45     Attr JavaDoc attr = elt.getAttributeNodeNS(XML_NAMESPACE_URI, "lang");
46     if (attr != null) {
47         return attr.getNodeValue();
48     }
49     for (Node JavaDoc n = elt.getParentNode(); n != null; n = n.getParentNode()) {
50         if (n.getNodeType() == Node.ELEMENT_NODE) {
51         attr = ((Element JavaDoc)n).getAttributeNodeNS(XML_NAMESPACE_URI,
52                                                        "lang");
53         if (attr != null) {
54             return attr.getNodeValue();
55         }
56         }
57     }
58     return "en";
59     }
60
61     /**
62      * Returns the xml:space attribute value of the given element.
63      */

64     public static String JavaDoc getXMLSpace(Element JavaDoc elt) {
65     Attr JavaDoc attr = elt.getAttributeNodeNS(XML_NAMESPACE_URI, "space");
66     if (attr != null) {
67         return attr.getNodeValue();
68     }
69     for (Node JavaDoc n = elt.getParentNode(); n != null; n = n.getParentNode()) {
70         if (n.getNodeType() == Node.ELEMENT_NODE) {
71         attr = ((Element JavaDoc)n).getAttributeNodeNS(XML_NAMESPACE_URI,
72                                                        "space");
73         if (attr != null) {
74             return attr.getNodeValue();
75         }
76         }
77     }
78     return "default";
79     }
80
81     /**
82      * Strips the white spaces in the given string according to the xml:space
83      * attribute recommended behaviour when it has the 'default' value.
84      */

85     public static String JavaDoc defaultXMLSpace(String JavaDoc data) {
86     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
87     boolean space = false;
88     for (int i = 0; i < data.length(); i++) {
89         char c = data.charAt(i);
90         switch (c) {
91         case 10:
92         case 13:
93         space = false;
94         break;
95         case ' ':
96         case '\t':
97         if (!space) {
98             result.append(' ');
99             space = true;
100         }
101         break;
102         default:
103         result.append(c);
104         space = false;
105         }
106     }
107     return result.toString().trim();
108     }
109
110     /**
111      * Strips the white spaces in the given string according to the xml:space
112      * attribute recommended behaviour when it has the 'preserve' value.
113      */

114     public static String JavaDoc preserveXMLSpace(String JavaDoc data) {
115     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
116     for (int i = 0; i < data.length(); i++) {
117         char c = data.charAt(i);
118         switch (c) {
119         case 10:
120         case 13:
121         case '\t':
122         result.append(' ');
123         break;
124         default:
125         result.append(c);
126         }
127     }
128     return result.toString();
129     }
130 }
131
Popular Tags