1 37 package org.webharvest.definition; 38 39 import java.io.InputStream ; 40 import java.util.*; 41 42 import org.apache.log4j.Logger; 43 44 public class XmlNode extends HashMap { 45 46 protected static final Logger log = Logger.getLogger(XmlNode.class); 47 48 private static final boolean IGNORE_NAMESPACE = true; 49 private static final boolean IGNORE_CASE = true; 50 51 private String name; 53 54 private XmlNode parent; 56 57 private Map attributes = new HashMap(); 59 60 private List elementList = new ArrayList(); 62 63 private String text; 65 66 72 public static XmlNode getInstance(InputStream in) { 73 return XmlParser.parse(in); 74 } 75 76 82 protected XmlNode(String name, XmlNode parent) { 83 super(); 84 85 this.name = adaptName(name); 86 this.parent = parent; 87 88 if (parent != null) { 89 parent.addElement(this); 90 } 91 } 92 93 99 private String adaptName(String s) { 100 if (IGNORE_NAMESPACE) { 101 int index = s.indexOf(':'); 102 if (index >= 0) { 103 s = s.substring(index + 1); 104 } 105 } 106 107 if (IGNORE_CASE) { 108 s = s.toLowerCase(); 109 } 110 111 return s; 112 } 113 114 117 public String getName() { 118 return name; 119 } 120 121 124 public String getText() { 125 return text; 126 } 127 128 131 public XmlNode getParent() { 132 return parent; 133 } 134 135 145 private Object getSeq(String key) { 146 StringTokenizer strTkzr = new StringTokenizer(key, "./\\[]"); 147 Object currValue = this; 148 while (strTkzr.hasMoreTokens()) { 149 String currKey = strTkzr.nextToken(); 150 if (currValue instanceof Map) { 151 currValue = ((Map)currValue).get(currKey); 152 } else if (currValue instanceof List) { 153 try { 154 List list = (List) currValue; 155 int index = Integer.parseInt(currKey); 156 157 if (index >= 0 && index < list.size()) { 158 currValue = list.get(index); 159 } 160 } catch (NumberFormatException e) { 161 return null; 162 } 163 } else { 164 return null; 165 } 166 } 167 168 return currValue; 169 } 170 171 174 public Object get(Object key) { 175 if (key == null) { 176 return null; 177 } 178 179 if (IGNORE_CASE) { 180 key = ((String )key).toLowerCase(); 181 } 182 183 String sKey = (String ) key; 184 185 if ( sKey.indexOf('/') >= 0 || sKey.indexOf('.') >= 0 || sKey.indexOf('\\') >= 0 || sKey.indexOf('[') >= 0 ) { 186 return getSeq(sKey); 187 } 188 189 if (sKey.equalsIgnoreCase("_value")) { 190 return text; 191 } else if (this.containsKey(key)) { 192 return super.get(key); 193 } else { 194 return attributes.get(key); 195 } 196 } 197 198 public String getString(Object key) { 199 return (String ) get(key); 200 } 201 202 207 public void addAttribute(String name, String value) { 208 attributes.put(adaptName(name), value); 209 } 210 211 public Map getAttributes() { 212 return this.attributes; 213 } 214 215 public String getAttribute(String attName) { 216 return (String ) this.attributes.get(attName); 217 } 218 219 223 public void addElement(XmlNode elementNode) { 224 String elementName = elementNode.getName(); 225 226 if (!this.containsKey(elementName)) { 227 this.put(elementName, new ArrayList()); 228 } 229 230 ArrayList elementsForName = (ArrayList) this.get(elementName); 231 elementsForName.add(elementNode); 232 233 elementList.add(elementNode); 234 } 235 236 240 public void addElement(String value) { 241 elementList.add(value); 242 } 243 244 245 public Object getElement(String name) { 246 return super.get(name); 247 } 248 249 253 protected void setText(String text) { 254 this.text = text; 255 } 256 257 public List getElementList() { 258 return elementList; 259 } 260 261 265 public void print() { 266 print(0); 267 } 268 269 private void print(int level) { 270 for (int i = 0; i < level; i++) { 271 System.out.print(" "); 272 } 273 System.out.print(name + ": " + attributes + ": TEXT = [" + text + "]\n"); 274 275 Iterator it = elementList.iterator(); 276 while (it.hasNext()) { 277 Object element = it.next(); 278 if (element instanceof XmlNode) { 279 XmlNode childNode = (XmlNode) element; 280 childNode.print(level + 1); 281 } else { 282 for (int i = 0; i <= level; i++) { 283 System.out.print(" "); 284 } 285 System.out.println((String )element); 286 } 287 } 288 } 289 290 } | Popular Tags |