1 18 19 package org.apache.batik.dom.util; 20 21 import org.apache.batik.util.XMLConstants; 22 import org.w3c.dom.Attr ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 26 32 33 public class XMLSupport implements XMLConstants { 34 35 38 protected XMLSupport() { 39 } 40 41 44 public static String getXMLLang(Element elt) { 45 Attr attr = elt.getAttributeNodeNS(XML_NAMESPACE_URI, "lang"); 46 if (attr != null) { 47 return attr.getNodeValue(); 48 } 49 for (Node n = elt.getParentNode(); n != null; n = n.getParentNode()) { 50 if (n.getNodeType() == Node.ELEMENT_NODE) { 51 attr = ((Element )n).getAttributeNodeNS(XML_NAMESPACE_URI, 52 "lang"); 53 if (attr != null) { 54 return attr.getNodeValue(); 55 } 56 } 57 } 58 return "en"; 59 } 60 61 64 public static String getXMLSpace(Element elt) { 65 Attr attr = elt.getAttributeNodeNS(XML_NAMESPACE_URI, "space"); 66 if (attr != null) { 67 return attr.getNodeValue(); 68 } 69 for (Node n = elt.getParentNode(); n != null; n = n.getParentNode()) { 70 if (n.getNodeType() == Node.ELEMENT_NODE) { 71 attr = ((Element )n).getAttributeNodeNS(XML_NAMESPACE_URI, 72 "space"); 73 if (attr != null) { 74 return attr.getNodeValue(); 75 } 76 } 77 } 78 return "default"; 79 } 80 81 85 public static String defaultXMLSpace(String data) { 86 StringBuffer result = new StringBuffer (); 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 114 public static String preserveXMLSpace(String data) { 115 StringBuffer result = new StringBuffer (); 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 |