1 39 40 package parser.dom; 41 42 import com.sun.japex.JapexDriverBase; 43 import com.sun.japex.TestCase; 44 import com.sun.org.apache.xml.internal.utils.DOMBuilder; 45 import com.sun.xml.fastinfoset.sax.Features; 46 import com.sun.xml.fastinfoset.sax.Properties; 47 import java.io.File ; 48 import java.io.ByteArrayInputStream ; 49 import java.io.FileInputStream ; 50 import javax.xml.parsers.DocumentBuilder ; 51 import javax.xml.parsers.DocumentBuilderFactory ; 52 53 import javax.xml.parsers.SAXParserFactory ; 54 import javax.xml.parsers.SAXParser ; 55 import org.xml.sax.InputSource ; 56 import org.xml.sax.XMLReader ; 57 58 59 60 public class JAXPSAXXercesDOMBuilderDriver extends JapexDriverBase { 61 ByteArrayInputStream _inputStream; 62 InputSource _inputSource; 63 64 SAXParser _parser; 65 XMLReader _reader; 66 DocumentBuilder _db; 67 68 public void initializeDriver() { 69 } 70 71 72 public void prepare(TestCase testCase) { 73 String xmlFile = testCase.getParam("xmlfile"); 74 if (xmlFile == null) { 75 throw new RuntimeException ("xmlfile not specified"); 76 } 77 78 try { 80 SAXParserFactory spf = SAXParserFactory.newInstance(); 81 spf.setNamespaceAware(true); 82 spf.setFeature(Features.NAMESPACE_PREFIXES_FEATURE, true); 83 _parser = spf.newSAXParser(); 84 _reader = _parser.getXMLReader(); 85 86 FileInputStream fis = new FileInputStream (new File (xmlFile)); 87 byte[] xmlFileByteArray = com.sun.japex.Util.streamToByteArray(fis); 88 _inputStream = new ByteArrayInputStream (xmlFileByteArray); 89 fis.close(); 90 _inputSource = new InputSource (_inputStream); 91 92 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 93 dbf.setNamespaceAware(true); 94 _db = dbf.newDocumentBuilder(); 95 } 96 catch (Exception e) { 97 } 98 } 99 100 public void warmup(TestCase testCase) { 101 try { 102 _inputStream.reset(); 103 DOMBuilder db = new DOMBuilder(_db.newDocument()); 104 _reader.setContentHandler(db); 105 _reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, db); 106 _reader.parse(_inputSource); 107 } 108 catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 public void run(TestCase testCase) { 114 try { 115 _inputStream.reset(); 116 DOMBuilder db = new DOMBuilder(_db.newDocument()); 117 _reader.setContentHandler(db); 118 _reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, db); 119 _reader.parse(_inputSource); 120 } 121 catch (Exception e) { 122 e.printStackTrace(); 123 } 124 } 125 126 public void finish(TestCase testCase) { 127 } 128 129 public void terminateDriver() { 130 } 131 } 132 | Popular Tags |