| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileOutputStream; |
| 37 | | | import java.io.IOException; |
| 38 | | | import javax.xml.transform.stream.StreamResult; |
| 39 | | | import org.xml.sax.Attributes; |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | @author |
| 45 | | | |
| 46 | | | public final class XmlUtil |
| 47 | | | { |
| 48 | | | |
| 49 | | | |
| 50 | | | {@link } |
| 51 | | | |
| 52 | 100 | | public static final Attributes EMPTY_ATTRIBUTES = new EmptyAttribute(); |
| 53 | | | |
| 54 | | | private static final int INDENT = 2; |
| 55 | | | private static final String SPACES = " "; |
| 56 | | | |
| 57 | | | |
| 58 | | | private XmlUtil () |
| 59 | 0 | | { |
| 60 | | | |
| 61 | 0 | | } |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | @see |
| 66 | | | |
| 67 | 100 | | private static final class EmptyAttribute |
| 68 | | | implements Attributes |
| 69 | | | { |
| 70 | | | {@inheritDoc} |
| 71 | | | public int getLength () |
| 72 | | | { |
| 73 | 0 | | return 0; |
| 74 | | | } |
| 75 | | | |
| 76 | | | {@inheritDoc} |
| 77 | | | public String getLocalName (int index) |
| 78 | | | { |
| 79 | 0 | | return null; |
| 80 | | | } |
| 81 | | | |
| 82 | | | {@inheritDoc} |
| 83 | | | public String getQName (int index) |
| 84 | | | { |
| 85 | 0 | | return null; |
| 86 | | | } |
| 87 | | | |
| 88 | | | {@inheritDoc} |
| 89 | | | public String getType (int index) |
| 90 | | | { |
| 91 | 0 | | return null; |
| 92 | | | } |
| 93 | | | |
| 94 | | | {@inheritDoc} |
| 95 | | | public String getURI (int index) |
| 96 | | | { |
| 97 | 0 | | return null; |
| 98 | | | } |
| 99 | | | |
| 100 | | | {@inheritDoc} |
| 101 | | | public String getValue (int index) |
| 102 | | | { |
| 103 | 0 | | return null; |
| 104 | | | } |
| 105 | | | |
| 106 | | | {@inheritDoc} |
| 107 | | | public int getIndex (String qName) |
| 108 | | | { |
| 109 | 0 | | return -1; |
| 110 | | | } |
| 111 | | | |
| 112 | | | {@inheritDoc} |
| 113 | | | public String getType (String qName) |
| 114 | | | { |
| 115 | 0 | | return null; |
| 116 | | | } |
| 117 | | | |
| 118 | | | {@inheritDoc} |
| 119 | | | public String getValue (String qName) |
| 120 | | | { |
| 121 | 0 | | return null; |
| 122 | | | } |
| 123 | | | |
| 124 | | | {@inheritDoc} |
| 125 | | | public int getIndex (String uri, String localName) |
| 126 | | | { |
| 127 | 0 | | return -1; |
| 128 | | | } |
| 129 | | | |
| 130 | | | {@inheritDoc} |
| 131 | | | public String getType (String uri, String localName) |
| 132 | | | { |
| 133 | 0 | | return null; |
| 134 | | | } |
| 135 | | | |
| 136 | | | {@inheritDoc} |
| 137 | | | public String getValue (String uri, String localName) |
| 138 | | | { |
| 139 | 0 | | return null; |
| 140 | | | } |
| 141 | | | } |
| 142 | | | |
| 143 | | | |
| 144 | | | |
| 145 | | | |
| 146 | | | @param |
| 147 | | | @return |
| 148 | | | |
| 149 | | | |
| 150 | | | public static String attributeEscape (String attribute) |
| 151 | | | { |
| 152 | 0 | | final StringBuffer sb = new StringBuffer(); |
| 153 | 0 | | if (attribute != null) |
| 154 | | | { |
| 155 | | | char c; |
| 156 | 0 | | final int l = attribute.length(); |
| 157 | 0 | | for (int i = 0; i < l; i++) |
| 158 | | | { |
| 159 | 0 | | c = attribute.charAt(i); |
| 160 | 0 | | switch (c) |
| 161 | | | { |
| 162 | | | case '<': |
| 163 | 0 | | sb.append("<"); |
| 164 | 0 | | break; |
| 165 | | | case '>': |
| 166 | 0 | | sb.append(">"); |
| 167 | 0 | | break; |
| 168 | | | case '\'': |
| 169 | 0 | | sb.append("'"); |
| 170 | 0 | | break; |
| 171 | | | case '"': |
| 172 | 0 | | sb.append("""); |
| 173 | 0 | | break; |
| 174 | | | case '&': |
| 175 | 0 | | sb.append("&"); |
| 176 | 0 | | break; |
| 177 | | | default : |
| 178 | 0 | | if (c > Byte.MAX_VALUE |
| 179 | | | || Character.isISOControl(c)) |
| 180 | | | { |
| 181 | 0 | | sb.append("&#x"); |
| 182 | 0 | | sb.append(Integer.toHexString(c)); |
| 183 | 0 | | sb.append(';'); |
| 184 | | | } |
| 185 | | | else |
| 186 | | | { |
| 187 | 0 | | sb.append(c); |
| 188 | | | } |
| 189 | | | } |
| 190 | | | } |
| 191 | | | } |
| 192 | 0 | | return sb.toString(); |
| 193 | | | } |
| 194 | | | |
| 195 | | | |
| 196 | | | |
| 197 | | | |
| 198 | | | @param |
| 199 | | | @return |
| 200 | | | |
| 201 | | | |
| 202 | | | public static String escape (String text) |
| 203 | | | { |
| 204 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 205 | 100 | | if (text != null) |
| 206 | | | { |
| 207 | | | char c; |
| 208 | 100 | | final int l = text.length(); |
| 209 | 100 | | for (int i = 0; i < l; i++) |
| 210 | | | { |
| 211 | 100 | | c = text.charAt(i); |
| 212 | 100 | | switch (c) |
| 213 | | | { |
| 214 | | | case '<': |
| 215 | 0 | | sb.append("<"); |
| 216 | 0 | | break; |
| 217 | | | case '>': |
| 218 | 0 | | sb.append(">"); |
| 219 | 0 | | break; |
| 220 | | | case '&': |
| 221 | 0 | | sb.append("&"); |
| 222 | 0 | | break; |
| 223 | | | default : |
| 224 | 100 | | if (c > Byte.MAX_VALUE) |
| 225 | | | { |
| 226 | 0 | | sb.append("&#x"); |
| 227 | 0 | | sb.append(Integer.toHexString(c)); |
| 228 | 0 | | sb.append(';'); |
| 229 | | | } |
| 230 | | | else |
| 231 | | | { |
| 232 | 100 | | sb.append(c); |
| 233 | | | } |
| 234 | | | } |
| 235 | | | } |
| 236 | | | } |
| 237 | 100 | | return sb.toString(); |
| 238 | | | } |
| 239 | | | |
| 240 | | | |
| 241 | | | |
| 242 | | | |
| 243 | | | |
| 244 | | | @param |
| 245 | | | @return |
| 246 | | | |
| 247 | | | |
| 248 | | (1) | public static String formatXml (String org) |
| 249 | | | { |
| 250 | 100 | | String result = org; |
| 251 | | | try |
| 252 | | | { |
| 253 | 100 | | final String in = org.trim(); |
| 254 | 100 | | if (in.charAt(0) == '<') |
| 255 | | | { |
| 256 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 257 | 100 | | boolean nestedTag = false; |
| 258 | 100 | | int indent = 0; |
| 259 | 100 | | for (int t = 0; t < in.length(); t++) |
| 260 | | | { |
| 261 | 100 | | char c = in.charAt(t); |
| 262 | | | |
| 263 | 100 | | switch (c) |
| 264 | | | { |
| 265 | | | case '<': |
| 266 | 100 | | t++; |
| 267 | 100 | | c = in.charAt(t); |
| 268 | 100 | | switch (c) |
| 269 | | | { |
| 270 | | | case '/': |
| 271 | 100 | | if (!nestedTag) |
| 272 | | | { |
| 273 | 0 | | indent -= INDENT; |
| 274 | 0 | | sb.append("</"); |
| 275 | | | } |
| 276 | | | else |
| 277 | | | { |
| 278 | 100 | | sb.append('\n'); |
| 279 | 100 | | indent -= INDENT; |
| 280 | 100 | | indent(indent, sb); |
| 281 | 100 | | sb.append("</"); |
| 282 | | | } |
| 283 | 100 | | nestedTag = true; |
| 284 | 100 | | break; |
| 285 | | | case '?': |
| 286 | | | case '!': |
| 287 | 0 | | if (t != 1) |
| 288 | | | { |
| 289 | 0 | | sb.append('\n'); |
| 290 | | | } |
| 291 | 0 | | sb.append('<'); |
| 292 | 0 | | sb.append(c); |
| 293 | 0 | | break; |
| 294 | | | default: |
| 295 | 100 | | nestedTag = false; |
| 296 | 100 | | if (sb.length() > 0) |
| 297 | | | { |
| 298 | 100 | | sb.append('\n'); |
| 299 | | | } |
| 300 | 100 | | indent(indent, sb); |
| 301 | 100 | | sb.append('<'); |
| 302 | 100 | | sb.append(c); |
| 303 | 100 | | indent += INDENT; |
| 304 | 100 | | break; |
| 305 | | | } |
| 306 | | | break; |
| 307 | | | case '/': |
| 308 | 100 | | sb.append(c); |
| 309 | 100 | | if (in.charAt(t + 1) == '>') |
| 310 | | | { |
| 311 | 100 | | indent -= INDENT; |
| 312 | 100 | | nestedTag = true; |
| 313 | | | } |
| 314 | | | break; |
| 315 | | | case '\n': |
| 316 | | | case '\r': |
| 317 | 0 | | break; |
| 318 | | | case '>': |
| 319 | | | default: |
| 320 | 100 | | sb.append(c); |
| 321 | | | break; |
| 322 | | | } |
| 323 | | | } |
| 324 | 100 | | result = sb.toString(); |
| 325 | | | } |
| 326 | | | } |
| 327 | 0 | | catch (Exception ex) |
| 328 | | | { |
| 329 | 0 | | result = org; |
| 330 | | (2) | |
| 331 | 100 | | } |
| 332 | 100 | | return result; |
| 333 | | | } |
| 334 | | | |
| 335 | | | |
| 336 | | | |
| 337 | | | |
| 338 | | | |
| 339 | | | |
| 340 | | | |
| 341 | | | <pre> |
| 342 | | (3) | |
| 343 | | (4) | |
| 344 | | (5) | |
| 345 | | (6) | |
| 346 | | (7) | |
| 347 | | | |
| 348 | | | </pre> |
| 349 | | | |
| 350 | | | @param |
| 351 | | | @return |
| 352 | | | @throws |
| 353 | | | |
| 354 | | (8) | public static StreamResult createStreamResult(File outFile) |
| 355 | | | throws IOException |
| 356 | | | { |
| 357 | 100 | | final StreamResult result = new StreamResult(outFile); |
| 358 | | | |
| 359 | | | |
| 360 | 100 | (9) | result.setOutputStream(new FileOutputStream(outFile)); |
| 361 | 100 | | return result; |
| 362 | | | } |
| 363 | | | |
| 364 | | | private static void indent (final int i, StringBuffer b) |
| 365 | | | { |
| 366 | 100 | | if (i > SPACES.length()) |
| 367 | | | { |
| 368 | 0 | | b.append(SPACES); |
| 369 | 0 | | indent(i - SPACES.length(), b); |
| 370 | | | } |
| 371 | | | else |
| 372 | | | { |
| 373 | 100 | | b.append(SPACES.substring(0, i)); |
| 374 | | | } |
| 375 | 100 | | } |
| 376 | | | |
| 377 | | | |
| 378 | | | } |