1 56 57 package org.jdom.contrib.helpers; 58 59 import org.jdom.*; 60 61 66 public class TextHelper { 67 73 public static String normalize(String text) { 74 char[] chars = text.toCharArray(); 75 char[] newChars = new char[chars.length]; 76 boolean white = true; 77 int pos = 0; 78 for (int i = 0; i < chars.length; i++) { 79 char c = chars[i]; 80 if (c == ' ' || c == '\r' || c == '\n' || c == '\t') { 81 if (!white) { 82 newChars[pos++] = ' '; 83 white = true; 84 } 85 } 86 else { 87 newChars[pos++] = c; 88 white = false; 89 } 90 } 91 if (white && pos > 0) { 92 pos--; 93 } 94 return new String (newChars, 0, pos); 95 } 96 97 110 public static String getChildText(Element parent, String name) { 111 Element child = parent.getChild(name); 112 if (child == null) { 113 return null; 114 } 115 return child.getText(); 116 } 117 118 132 public static String getChildText(Element parent, String name, Namespace ns) { 133 Element child = parent.getChild(name, ns); 134 if (child == null) { 135 return null; 136 } 137 return child.getText(); 138 } 139 140 152 public static String getChildTextTrim(Element parent, String name) { 153 Element child = parent.getChild(name); 154 if (child == null) { 155 return null; 156 } else { 157 return child.getText().trim(); 158 } 159 } 160 161 175 public static String getChildTextTrim(Element parent, String name, Namespace ns) { 176 Element child = parent.getChild(name, ns); 177 if (child == null) { 178 return null; 179 } else { 180 return child.getText().trim(); 181 } 182 } 183 184 196 public static String getChildTextNormalize(Element parent, String name) { 197 Element child = parent.getChild(name); 198 if (child == null) { 199 return null; 200 } else { 201 return normalize(child.getText()); 202 } 203 } 204 205 219 public static String getChildTextNormalize(Element parent, String name, Namespace ns) { 220 Element child = parent.getChild(name, ns); 221 if (child == null) { 222 return null; 223 } else { 224 return normalize(child.getText()); 225 } 226 } 227 } 228 229 | Popular Tags |