1 16 package org.apache.cocoon.xml; 17 18 import org.custommonkey.xmlunit.XMLTestCase; 19 import org.xml.sax.ContentHandler ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.helpers.AttributesImpl ; 22 import org.apache.cocoon.xml.dom.DOMBuilder; 23 24 import javax.xml.transform.TransformerFactory ; 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.Source ; 27 import javax.xml.transform.Result ; 28 import javax.xml.transform.stream.StreamResult ; 29 import javax.xml.transform.dom.DOMSource ; 30 import java.io.ByteArrayOutputStream ; 31 32 33 39 40 public abstract class AbstractXMLTestCase extends XMLTestCase { 41 42 public AbstractXMLTestCase(String s) { 43 super(s); 44 } 45 46 protected void generateLargeSAX( ContentHandler consumer ) throws SAXException { 47 AttributesImpl atts = new AttributesImpl(); 48 49 final int size = 65000; 50 char[] large = new char[size]; 51 for(int i=0;i<size;i++) { 52 large[i] = 'x'; 53 } 54 55 consumer.startDocument(); 56 consumer.startElement("", "root", "root", atts); 57 consumer.characters(large,0,size); 58 consumer.endElement("", "root", "root"); 59 consumer.endDocument(); 60 } 61 62 protected void generateSmallSAX( ContentHandler consumer ) throws SAXException { 63 AttributesImpl atts = new AttributesImpl(); 64 65 consumer.startDocument(); 66 consumer.startElement("", "root", "root", atts); 67 consumer.characters("test".toCharArray(),0,4); 68 consumer.endElement("", "root", "root"); 69 consumer.endDocument(); 70 } 71 72 protected byte[] generateByteArray() throws Exception { 73 DOMBuilder in = new DOMBuilder(); 74 generateSmallSAX(in); 75 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 76 TransformerFactory tFactory = TransformerFactory.newInstance(); 77 Transformer t = tFactory.newTransformer(); 78 Source input = new DOMSource (in.getDocument()); 79 Result output = new StreamResult (bos); 80 t.transform(input, output); 81 bos.close(); 82 83 return bos.toByteArray(); 84 } 85 } 86 87 | Popular Tags |