1 39 40 package parser.dom; 41 42 import java.io.File ; 43 import java.io.ByteArrayInputStream ; 44 import java.io.FileInputStream ; 45 import java.io.IOException ; 46 47 import javax.xml.parsers.DocumentBuilder ; 48 import org.w3c.dom.Document ; 49 import com.sun.japex.*; 50 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 51 import org.w3c.dom.NamedNodeMap ; 52 import org.w3c.dom.Node ; 53 import org.w3c.dom.NodeList ; 54 import org.xml.sax.InputSource ; 55 56 public class XercesDOMParserDriver extends JapexDriverBase { 57 58 ByteArrayInputStream _inputStream; 59 60 DocumentBuilder _docBuilder; 61 DOMParser _parser; 62 63 public void initializeDriver() { 64 try { 65 _parser = new DOMParser(); 66 _parser.setFeature("http://xml.org/sax/features/namespaces", true); 67 _parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 try { Thread.currentThread().sleep(5000); } catch (Exception ee) {} 71 } 72 } 73 74 public void prepare(TestCase testCase) { 75 String xmlFile = testCase.getParam("xmlfile"); 76 if (xmlFile == null) { 77 throw new RuntimeException ("xmlfile not specified"); 78 } 79 try { 81 FileInputStream fis = new FileInputStream (new File (xmlFile)); 83 byte[] xmlFileByteArray = com.sun.japex.Util.streamToByteArray(fis); 84 _inputStream = new ByteArrayInputStream (xmlFileByteArray); 85 fis.close(); 86 } 87 catch (IOException e) { 88 e.printStackTrace(); 89 } 90 91 } 92 93 public void warmup(TestCase testCase) { 94 try { 95 _inputStream.reset(); 96 _parser.parse(new InputSource (_inputStream)); 97 Document d = _parser.getDocument(); 98 } 100 catch (Exception e) { 101 e.printStackTrace(); 102 } 103 } 104 105 public void run(TestCase testCase) { 106 try { 107 _inputStream.reset(); 108 _parser.parse(new InputSource (_inputStream)); 109 Document d = _parser.getDocument(); 110 } 112 catch (Exception e) { 113 e.printStackTrace(); 114 } 115 } 116 117 public void finish(TestCase testCase) { 118 } 119 120 public void terminateDriver() { 121 } 122 123 public final void traverse(Node n) { 124 Object o = n.getNodeValue(); 125 126 if (n.hasAttributes()) { 127 NamedNodeMap nnm = n.getAttributes(); 128 for (int i = 0; i < nnm.getLength(); i++) { 129 Node an = nnm.item(i); 130 o = an.getNodeValue(); 131 } 132 } 133 134 if (n.hasChildNodes()) { 135 NodeList nl = n.getChildNodes(); 136 for (int i = 0; i < nl.getLength(); i++) { 137 traverse(nl.item(i)); 138 } 139 } 140 } 141 } 142 | Popular Tags |