1 19 package org.netbeans.modules.xslt.model.impl; 20 21 22 import java.io.IOException ; 23 import java.io.StringReader ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Document ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import org.netbeans.modules.xslt.model.Stylesheet; 34 import org.netbeans.modules.xslt.model.XslComponent; 35 import org.netbeans.modules.xslt.model.XslModel; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 39 import junit.framework.TestCase; 40 41 42 46 public class AbstractXslTestCase extends TestCase { 47 48 public static final String TEST = "test.xsl"; 49 50 public AbstractXslTestCase(String testName) { 51 super(testName); 52 } 53 54 protected void tearDown() throws Exception { 55 TestCatalogModel.getDefault().clearDocumentPool(); 56 } 57 58 protected void log( String str ) { 59 System.out.println( str ); 60 } 61 62 protected Stylesheet getStyleSheet( String resourse ) { 63 return getStyleSheet(resourse , false); 64 } 65 66 protected Stylesheet getStyleSheet( String resourse , boolean reload) { 67 if ( !reload && myStylesheet != null ) { 68 return myStylesheet; 69 } 70 try { 71 XslModel model = Utils.loadXslModel( resourse , reload ); 72 myStylesheet = model.getStylesheet(); 73 return myStylesheet; 74 } 75 catch (Exception e) { 76 RuntimeException exc = new RuntimeException ( e ); 77 throw exc; 78 } 79 } 80 81 protected String getContent( XslModel model ) { 82 Document doc = (Document )model.getModelSource().getLookup().lookup( 83 Document .class ); 84 assert doc != null; 85 String str = null ; 86 try { 87 str = doc.getText( 0, doc.getLength() ); 88 } 89 catch (BadLocationException e) { 90 assert false; 91 } 92 return str; 93 } 94 95 protected org.w3c.dom.Document getDOMDocument( XslModel model ) 96 { 97 99 Exception exception = null; 100 try { 101 String str = getContent(model); 102 DocumentBuilder builder = 103 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 104 return builder.parse( new InputSource ( new StringReader ( str )) ); 105 } 106 catch (ParserConfigurationException e) { 107 exception = e; 108 } 109 catch (SAXException e) { 110 exception = e; 111 } 112 catch (IOException e) { 113 exception = e; 114 } 115 if ( exception != null ) { 116 AssertionError error = new AssertionError ( ); 117 error.initCause( exception ); 118 throw error; 119 } 120 return null; 121 } 122 123 protected XslModel getModel() { 124 return myStylesheet.getModel(); 125 } 126 127 protected List <String > getChildrenTags( List <? extends XslComponent> list ){ 128 List <String > ret = new ArrayList <String >( list.size() ); 129 for (XslComponent component : list) { 130 String name = component.getPeer().getNodeName(); 131 133 ret.add( name ); 134 } 135 return ret; 136 } 137 138 private Stylesheet myStylesheet; 139 140 } 141 | Popular Tags |