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