1 19 20 package de.gulden.util.xml; 21 22 import de.gulden.util.Toolbox; 23 import de.gulden.util.xml.NodeListImpl; 24 import java.io.*; 25 import java.util.*; 26 import java.util.Collection ; 27 import javax.xml.parsers.*; 28 import org.w3c.dom.*; 29 30 36 public class XMLToolbox { 37 38 42 45 protected static DocumentBuilder parseXMLDocumentBuilder; 46 47 48 52 55 public static DocumentBuilder createDefaultDocumentBuilder() { 56 try { 57 javax.xml.parsers.DocumentBuilderFactory factory=javax.xml.parsers.DocumentBuilderFactory.newInstance(); 58 return factory.newDocumentBuilder(); 59 } catch (Exception e) { 60 System.err.println("fatal error: cannot create default DocumentBuilder"); 61 System.err.println(e.getClass().getName()+" - "+e.getMessage()); 62 e.printStackTrace(System.err); 63 System.exit(2); 64 return null; 65 } 66 } 67 68 71 public static DocumentBuilder createDefaultDocumentBuilder(boolean validating, boolean namespaceAware, boolean ignoringElementContentWhitespace, boolean ignoringComments, boolean expandEntityReferences, boolean coalescing) { 72 try { 73 javax.xml.parsers.DocumentBuilderFactory factory=javax.xml.parsers.DocumentBuilderFactory.newInstance(); 74 factory.setValidating(validating); 75 factory.setNamespaceAware(namespaceAware); 76 factory.setIgnoringElementContentWhitespace(ignoringElementContentWhitespace); 77 factory.setIgnoringComments(ignoringComments); 78 factory.setExpandEntityReferences(expandEntityReferences); 79 factory.setCoalescing(coalescing); 80 return factory.newDocumentBuilder(); 81 } catch (Exception e) { 82 System.err.println("fatal error: cannot create default DocumentBuilder"); 83 System.err.println(e.getClass().getName()+" - "+e.getMessage()); 84 e.printStackTrace(System.err); 85 System.exit(2); 86 return null; 87 } 88 } 89 90 93 public static Element getChild(Element e, String tagname) { 94 Node n=e.getFirstChild(); 95 while (n!=null) 96 { 97 if (n instanceof Element) 98 { 99 Element c=(Element)n; 100 if (c.getTagName().equals(tagname)) 101 { 102 return c; 103 } 104 } 105 n=n.getNextSibling(); 106 } 107 return null; 108 } 109 110 113 public static Element getChild(Element e, String tagname, String attributeName, String attributeValue) { 114 Node n=e.getFirstChild(); 115 while (n!=null) { 116 if (n instanceof Element) { 117 Element c=(Element)n; 118 if (c.getTagName().equals(tagname)) { 119 if (c.hasAttribute(attributeName) && c.getAttribute(attributeName).equals(attributeValue)) { 120 return c; 121 } 122 } 123 } 124 n=n.getNextSibling(); 125 } 126 return null; 127 } 128 129 132 public static Element getChildRequired(Element e, String tagname) throws XMLException { 133 Element c=getChild(e,tagname); 134 if (c==null) 135 { 136 throw new XMLException("child tag '<"+tagname+"'> required",e); 137 } 138 return c; 139 } 140 141 144 public static String getAttributeRequired(Element e, String att) throws XMLException { 145 String a=e.getAttribute(att); 146 if ((a==null)||(a.length()==0)) 147 { 148 throw new XMLException("attribute '"+att+"' required",e); 149 } 150 return a; 151 } 152 153 156 public static NodeListCollection getChildren(Element e, String tagname) { 157 NodeListImpl nl=new NodeListImpl(); 158 Node n=e.getFirstChild(); 159 while (n!=null) 160 { 161 if (n instanceof Element) 162 { 163 Element c=(Element)n; 164 if (c.getTagName().equals(tagname)) 165 { 166 nl.add(c); 167 } 168 } 169 n=n.getNextSibling(); 170 } 171 return nl; 172 } 173 174 public static boolean isYesAttribute(Element e, String attr) { 175 return getBooleanAttribute(e,attr,false); 176 } 177 178 181 public static String getText(Element e) { 182 Node n=e.getFirstChild(); 183 if ((n!=null)&&(n instanceof CharacterData)) 184 { 185 String t=((CharacterData)n).getData(); 186 return t; 187 } 188 else 189 { 190 return null; 191 } 192 } 193 194 197 public static String getChildText(Element e, String tagname) { 198 Element child=getChild(e,tagname); 199 if (child!=null) 200 { 201 return getText(child); 202 } 203 else 204 { 205 return null; 206 } 207 } 208 209 public static void requireTagName(Element element, String name) throws XMLException { 210 if (!element.getTagName().equals(name)) { 211 throw new XMLException("illegal XML tag - expected <"+name+">",element); 212 } 213 } 214 215 218 public static Element getFirstChild(Element e) { 219 return getSelfOrFollowingElement(e.getFirstChild()); 220 } 221 222 225 public static NodeListCollection getChildren(Element e) { 226 NodeListCollection c=new NodeListImpl(); 227 Element ee=getFirstChild(e); 228 while (ee!=null) { 229 c.add(ee); 230 ee=getFollowingElement(ee); 231 } 232 return c; 233 } 234 235 238 public static Element getSelfOrFollowingElement(Node n) { 239 while ((n!=null)&&(!(n instanceof Element))) { 240 n=n.getNextSibling(); 241 } 242 return (Element)n; 243 } 244 245 248 public static Element getFollowingElement(Node n) { 249 return getNextSibling(n); 251 } 252 253 256 public static String getLangstring(Element element, String countryCode) { 257 if (element!=null) { org.w3c.dom.NodeList nl=getChildren(element,"langstring"); 259 if (nl.getLength()>0) { 260 org.w3c.dom.Element defaultElement=null; 261 for (int i=0;i<nl.getLength();i++) { 262 Element e=(Element)nl.item(i); 263 String c=e.getAttribute("lang"); 264 if ((c.equals(""))&&(defaultElement==null)) { 265 defaultElement=e; 266 if (countryCode==null) { 267 return getText(e); } 269 } else if ((countryCode!=null)&&(countryCode.equalsIgnoreCase(c))) { return getText(e); } 272 } 273 if (defaultElement!=null) { 275 return getText(defaultElement); } else { 278 if (countryCode==null) { return getText((Element)nl.item(0)); } else { 282 return null; } 284 } 285 } else { 286 return null; 287 } 288 } else { 289 return null; 290 } 291 } 292 293 296 public static String getLangstring(Element element) { 297 return getLangstring(element,null); } 299 300 public static String translateJavaNameToXMLName(String name) { 301 int pos=0; 302 while ((pos<name.length())&&(Character.isUpperCase(name.charAt(pos)))) { 304 pos++; 305 } 306 while ((pos<name.length())&&(Character.isLowerCase(name.charAt(pos)))) { 308 pos++; 309 } 310 if (pos>0) { 312 if (pos<name.length()) { 313 return name.substring(0,pos).toLowerCase()+"-"+translateJavaNameToXMLName(name.substring(pos)); 314 } else { 315 return name.toLowerCase(); 316 } 317 } else { 318 return ""; 319 } 320 } 321 322 public static String translateXMLNameToJavaName(String name) { 323 StringTokenizer st=new StringTokenizer(name,"-",false); 325 StringBuffer sb=new StringBuffer (st.nextToken()); 326 while (st.hasMoreTokens()) { 327 String t=st.nextToken(); 328 sb.append(de.gulden.util.Toolbox.capitalize(t)); 329 } 330 return sb.toString(); 331 } 332 333 336 public static Element getNextSibling(Node n) { 337 return getSelfOrFollowingElement(n.getNextSibling()); 338 } 339 340 343 public static NodeListCollection getChildren(Element e, String [] tagnames) { 344 NodeListImpl nl=new NodeListImpl(); 345 Node n=e.getFirstChild(); 346 while (n!=null) { 347 if (n instanceof Element) { 348 Element c=(Element)n; 349 if (Toolbox.arrayContains(tagnames,c.getTagName())) { 350 nl.add(c); 351 } 352 } 353 n=n.getNextSibling(); 354 } 355 return nl; 356 } 357 358 361 public static Element parseXML(String s) throws XMLException { 362 try { 363 if (parseXMLDocumentBuilder==null) { 364 parseXMLDocumentBuilder=createDefaultDocumentBuilder(); 365 if (parseXMLDocumentBuilder==null) { 366 throw new XMLException("Internal error: cannot parse XML - could not create document builder"); 367 } 368 } 369 java.io.StringBufferInputStream in=new java.io.StringBufferInputStream (s); 370 org.w3c.dom.Document doc=parseXMLDocumentBuilder.parse(in); 371 Element xml=doc.getDocumentElement(); 372 return xml; 373 } catch (java.io.IOException ioe) { 374 throw new XMLException("cannot parse XML",ioe); 375 } catch (org.xml.sax.SAXException saxe) { 376 throw new XMLException("cannot parse XML",saxe); 377 } 378 } 379 380 public static String formatXML(Node xml, boolean indenting, boolean preserveSpace) { 381 try { 384 java.io.StringWriter out=new java.io.StringWriter (); 385 org.apache.xml.serialize.OutputFormat outputFormat=new org.apache.xml.serialize.OutputFormat(); 386 outputFormat.setIndenting(indenting); 387 outputFormat.setPreserveSpace(preserveSpace); 388 org.apache.xml.serialize.XMLSerializer serializer=new org.apache.xml.serialize.XMLSerializer(out,outputFormat); 389 org.apache.xml.serialize.DOMSerializer domSerializer=serializer.asDOMSerializer(); 390 if (xml instanceof Element) { 391 domSerializer.serialize((Element)xml); 392 } else if (xml instanceof Document) { 393 domSerializer.serialize((Document)xml); 394 } else { 395 throw new Error ("internal error: can only xml-serialize objects of type Element or Document"); 396 } 397 return out.toString(); 398 } catch (java.io.IOException ioe) { 399 return null; 400 } 401 } 402 403 406 public static boolean getBooleanAttribute(Element e, String attr, boolean deflt) { 407 String a=e.getAttribute(attr); 408 if (Toolbox.empty(a)) { 409 return deflt; 410 } else { 411 a=a.trim().toLowerCase(); 412 if (deflt==true) { 413 return !Toolbox.isNo(a); 414 } else { 415 return Toolbox.isYes(a); 416 } 417 } 418 } 419 420 426 public static String getAttribute(Element e, String name, String deflt) { 427 boolean has=e.hasAttribute(name); 428 if (has) { 429 return e.getAttribute(name); 430 } else { 431 return deflt; 432 } 433 } 434 435 454 public static String getAttribute(Element e, String name) { 455 return getAttribute(e,name,null); 456 } 457 458 public static String xmlEscape(String s) { 459 StringBuffer sb=new StringBuffer (); 461 int len=s.length(); 462 int lastPos=0; 463 int nextOpen=s.indexOf('<'); 464 int nextClose=s.indexOf('>'); 465 boolean opens=(nextOpen!=-1); 466 boolean closes=(nextClose!=-1); 467 do { 468 if ( (opens && !closes) || ( opens && (nextOpen<nextClose)) ) { 469 String part=s.substring(lastPos,nextOpen); 470 sb.append(part); 471 sb.append("<"); 472 lastPos=nextOpen+1; 473 nextOpen=s.indexOf('<',lastPos); 474 opens=(nextOpen!=-1); 475 } else if (closes) { String part=s.substring(lastPos,nextClose); 477 sb.append(part); 478 sb.append(">"); 479 lastPos=nextClose+1; 480 nextClose=s.indexOf('>',lastPos); 481 closes=(nextClose!=-1); 482 } 483 } while (opens||closes); 484 if (lastPos<len) { 485 String rest=s.substring(lastPos); 486 sb.append(rest); 487 } 488 return sb.toString(); 489 } 490 491 public static String xmlEscapeAll(String s) { 492 char[] c={ '<', '>', '\"', '\'', '\n', '\r' }; 493 String [] r={ "<", ">", """, "'", "
", "" }; 494 return Toolbox.replaceCharsWithStrings(s,c,r); 495 } 496 497 } | Popular Tags |