1 5 package org.infohazard.maverick.util; 6 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 import org.jdom.*; 11 import org.jdom.output.XMLOutputter; 12 13 18 public class XML 19 { 20 21 public final static String ATTR_VALUE = "value"; 22 23 24 public final static String TAG_PARAM = "param"; 25 26 27 public final static String ATTR_PARAM_NAME = "name"; 28 29 30 protected static XMLOutputter outputter = new XMLOutputter(); 31 32 static 33 { 34 outputter.setTextNormalize(true); 35 } 36 37 52 public static String getValue(Element node, String name) 53 { 54 String result = System.getProperty("maverick." + node.getName() + "." + name); 55 if (result != null) 56 return result; 57 58 result = node.getAttributeValue(name); 59 if (result != null) 60 return result; 61 62 Element subnode = node.getChild(name); 63 if (subnode == null) 64 return null; 65 66 return subnode.getAttributeValue(ATTR_VALUE); 67 } 68 69 76 public static String toString(Element node) 77 { 78 return escape(outputter.outputString(node)); 79 } 80 81 87 public static String escape(String in) 88 { 89 StringBuffer out = new StringBuffer (); 90 91 for (int i = 0; i < in.length(); i++) 92 { 93 char c = in.charAt(i); 94 95 switch (c) 96 { 97 case '<': 98 out.append("<"); 99 break; 100 case '>': 101 out.append(">"); 102 break; 103 case '&': 104 out.append("&"); 105 break; 106 case '"': 107 out.append("""); 108 break; 109 default: 110 out.append(c); 111 break; 112 } 113 } 114 115 return out.toString(); 116 } 117 118 126 public static Map getParams(Element node) 127 { 128 Map params = null; 129 130 Iterator it = node.getChildren(TAG_PARAM).iterator(); 131 while (it.hasNext()) 132 { 133 Element paramNode = (Element)it.next(); 134 135 String name = paramNode.getAttributeValue(ATTR_PARAM_NAME); 136 137 Object value = paramNode.getAttributeValue(ATTR_VALUE); 138 if (value == null) 139 { 140 if (paramNode.hasChildren()) 141 value = paramNode.getChildren(); 142 else 143 value = paramNode.getTextTrim(); 144 } 145 146 if (params == null) 147 params = new HashMap (); 148 149 params.put(name, value); 150 } 151 152 return params; 153 } 154 } 155 156 | Popular Tags |