KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > AbstractXslTestCase


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xslt.model.impl;
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39 import junit.framework.TestCase;
40
41
42 /**
43  * @author ads
44  *
45  */

46 public class AbstractXslTestCase extends TestCase {
47     
48     public static final String JavaDoc TEST = "test.xsl";
49
50     public AbstractXslTestCase(String JavaDoc testName) {
51         super(testName);
52     }
53
54     protected void tearDown() throws Exception JavaDoc {
55         TestCatalogModel.getDefault().clearDocumentPool();
56     }
57     
58     protected void log( String JavaDoc str ) {
59         System.out.println( str );
60     }
61     
62     protected Stylesheet getStyleSheet( String JavaDoc resourse ) {
63         return getStyleSheet(resourse , false);
64     }
65     
66     protected Stylesheet getStyleSheet( String JavaDoc 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 JavaDoc e) {
76             RuntimeException JavaDoc exc = new RuntimeException JavaDoc( e );
77             throw exc;
78         }
79     }
80     
81     protected String JavaDoc getContent( XslModel model ) {
82         Document JavaDoc doc = (Document JavaDoc)model.getModelSource().getLookup().lookup(
83                 Document JavaDoc.class );
84         assert doc != null;
85         String JavaDoc str = null ;
86         try {
87             str = doc.getText( 0, doc.getLength() );
88         }
89         catch (BadLocationException JavaDoc e) {
90             assert false;
91         }
92         return str;
93     }
94     
95     protected org.w3c.dom.Document JavaDoc getDOMDocument( XslModel model )
96     {
97         //model.getAccess().flush();
98

99         Exception JavaDoc exception = null;
100         try {
101             String JavaDoc str = getContent(model);
102             DocumentBuilder JavaDoc builder =
103                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
104             return builder.parse( new InputSource JavaDoc( new StringReader JavaDoc( str )) );
105         }
106         catch (ParserConfigurationException JavaDoc e) {
107             exception = e;
108         }
109         catch (SAXException JavaDoc e) {
110             exception = e;
111         }
112         catch (IOException JavaDoc e) {
113             exception = e;
114         }
115         if ( exception != null ) {
116             AssertionError JavaDoc error = new AssertionError JavaDoc( );
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 JavaDoc<String JavaDoc> getChildrenTags( List JavaDoc<? extends XslComponent> list ){
128         List JavaDoc<String JavaDoc> ret = new ArrayList JavaDoc<String JavaDoc>( list.size() );
129         for (XslComponent component : list) {
130             String JavaDoc name = component.getPeer().getNodeName();
131             /*String name = component.getComponentType().toString().toLowerCase();
132             assert name.indexOf( "-" )!=-1;*/

133             ret.add( name );
134         }
135         return ret;
136     }
137     
138     private Stylesheet myStylesheet;
139     
140 }
141
Popular Tags