1 64 65 package com.jcorporate.expresso.core.misc; 66 67 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 68 import org.apache.log4j.Logger; 69 import org.w3c.dom.Document ; 70 import org.w3c.dom.NamedNodeMap ; 71 import org.w3c.dom.Node ; 72 import org.w3c.dom.NodeList ; 73 import org.xml.sax.ErrorHandler ; 74 import org.xml.sax.InputSource ; 75 import org.xml.sax.SAXException ; 76 import org.xml.sax.SAXParseException ; 77 78 import javax.xml.parsers.DocumentBuilder ; 79 import javax.xml.parsers.DocumentBuilderFactory ; 80 import java.io.CharArrayReader ; 81 82 83 90 public class StringDOMParser 91 implements ErrorHandler { 92 static Logger log = Logger.getLogger("expresso.core.misc.StringDOMParser"); 93 94 public StringDOMParser() { 95 } 96 97 105 public Document parseString(String data) { 106 return parseString(data.toCharArray()); 107 } 108 109 117 public Document parseString(char[] data) { 118 try { 119 InputSource inputSource = new InputSource (new CharArrayReader (data)); 120 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 121 dbf.setNamespaceAware(true); 122 dbf.setValidating(false); 123 124 DocumentBuilder db = dbf.newDocumentBuilder(); 125 db.setErrorHandler(this); 126 127 Document doc = db.parse(inputSource); 128 129 return doc; 130 } catch (Exception e) { 131 log.error("Error Parsing XML Document.", e); 132 133 return null; 134 } 135 } 136 144 public void warning(SAXParseException ex) { 145 146 log.warn(getLocationString(ex) + ": " + ex.getMessage()); 147 } 148 149 154 public void error(SAXParseException ex) { 155 log.error(getLocationString(ex) + ": " + ex.getMessage()); 156 } 157 158 164 public void fatalError(SAXParseException ex) 165 throws SAXException { 166 log.error(getLocationString(ex) + ": " + ex.getMessage()); 167 throw ex; 168 } 169 170 176 private String getLocationString(SAXParseException ex) { 177 FastStringBuffer str = new FastStringBuffer(128); 178 String systemId = ex.getSystemId(); 179 180 if (systemId != null) { 181 int index = systemId.lastIndexOf('/'); 182 183 if (index != -1) { 184 systemId = systemId.substring(index + 1); 185 } 186 187 str.append(systemId); 188 } 189 190 str.append(':'); 191 str.append(ex.getLineNumber()); 192 str.append(':'); 193 str.append(ex.getColumnNumber()); 194 195 return str.toString(); 196 } 198 204 public void dumpDOM(Node n) { 205 if (n == null) { 206 return; 207 } 208 209 FastStringBuffer fsb = new FastStringBuffer(512); 210 fsb.append('\n'); 211 fsb = this.buildDOMString(n, fsb, 0); 212 log.info(fsb.toString()); 213 } 214 215 223 private FastStringBuffer buildDOMString(Node n, FastStringBuffer buffer, 224 int nestingLevel) { 225 if (n == null) { 226 return buffer; 227 } 228 229 buffer.append(this.padWithTabs(nestingLevel)); 233 buffer.append("Node "); 234 235 if (n.getNodeName() != null) { 236 buffer.append("Name= "); 237 buffer.append(n.getNodeName()); 238 } 239 if (n.getNodeValue() != null) { 240 buffer.append(" Value= "); 241 buffer.append(n.getNodeValue()); 242 } 243 244 buffer.append('\n'); 245 buffer.append(this.padWithTabs(nestingLevel)); 246 247 NamedNodeMap nm = n.getAttributes(); 251 252 if (nm != null) { 253 buffer.append("Attributes: "); 254 255 for (int i = 0; i < nm.getLength(); i++) { 256 Node temp = nm.item(i); 257 buffer.append(temp.getNodeName() + "="); 258 buffer.append(temp.getNodeValue() + " "); 259 } 260 } else { 261 buffer.append("<No Attributes>"); 262 } 263 264 buffer.append('\n'); 265 266 NodeList nl = n.getChildNodes(); 270 271 if (nl == null) { 272 return buffer; 273 } 274 275 buffer.append(this.padWithTabs(nestingLevel)); 276 277 for (int i = 0; i < nl.getLength(); i++) { 278 buffer = buildDOMString(nl.item(i), buffer, nestingLevel + 1); 279 } 280 281 buffer.append('\n'); 282 283 return buffer; 284 } 285 286 private String padWithTabs(int numTabs) { 287 FastStringBuffer fsb = new FastStringBuffer(numTabs); 288 289 for (int i = 0; i < numTabs; i++) { 290 fsb.append('\t'); 291 } 292 293 return fsb.toString(); 294 } 295 } | Popular Tags |