• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 
22 /*
23  *
24  * TraxDatalet.java
25  *
26  */
27 package org.apache.qetest.xsl;
28 
29 import java.io.StringReader;
30 import java.util.Hashtable;
31 import java.util.Properties;
32 
33 import javax.xml.transform.Source;
34 import javax.xml.transform.stream.StreamSource;
35 
36 import org.apache.qetest.Datalet;
37 import org.apache.qetest.QetestUtils;
38 
39 /**
40  * Datalet for holding TrAX-like Sources and Results.
41  *
42  * Allows tester to set either a filename, URL, or Node for
43  * both the xml and xsl sources, and the application simply
44  * requests a Source object each time.
45  * We apply a simplistic algorithim to determine which kind of
46  * Source object we return.
47  * <b>Note:</b> Currently only supports StreamSources of various types.
48  *
49  * Note: should probably be moved to the org.apache.qetest.trax
50  * package, but I'm leaving it in the xsl package for now.
51  *
52  * @author Shane_Curcuru@lotus.com
53  * @version $Id$
54  */
55 public class TraxDatalet implements Datalet
56 {
57     /** URL of XSL source to use on disk.  */
58     protected String xslURL = null;
59 
60     /** String of characters to use as XSL source in a StringReader.  */
61     protected String xslString = null;
62 
63     /** @param s the local path/filename of the XSL to use.  */
setXSLName(String s)64     public void setXSLName(String s)
65     {
66         xslURL = QetestUtils.filenameToURL(s);
67     }
68 
69     /** @param s the URL/URI of the XSL to use.  */
setXSLURL(String s)70     public void setXSLURL(String s)
71     {
72         xslURL = s;
73     }
74 
75     /** @param s String of XSL to use in a StringReader, etc..  */
setXSLString(String s)76     public void setXSLString(String s)
77     {
78         xslString = s;
79     }
80 
81     /**
82      * Return a Source object representing our XSL.
83      * This may be any kind of source and is determined by which
84      * kinds of setXSL*() methods you've called.
85      * <ul>
86      * <li>if xslString is set, return new StreamSource(new StringReader(xslString))</li>
87      * <li>if xslURL/Name is set, return new StreamSource(xslURL)</li>
88      * <li>More types TBD</li>
89      * </ul>
90      * @return Source object representing our XSL, or an
91      * IllegalStateException if we don't have any XSL
92      */
getXSLSource()93     public Source getXSLSource()
94     {
95         if (null != xslString)
96             return new StreamSource(new StringReader(xslString));
97         else if (null != xslURL)
98             return new StreamSource(xslURL);
99         else
100             throw new IllegalStateException("TraxDatalet.getXSLSource() with no XSL!");
101     }
102 
103 
104     /** URL of XML source to use on disk.  */
105     protected String xmlURL = null;
106 
107     /** String of characters to use as XML source in a StringReader.  */
108     protected String xmlString = null;
109 
110     /** @param s the local path/filename of the XML to use.  */
setXMLName(String s)111     public void setXMLName(String s)
112     {
113         xmlURL = QetestUtils.filenameToURL(s);
114     }
115 
116     /** @param s the URL/URI of the XML to use.  */
setXMLURL(String s)117     public void setXMLURL(String s)
118     {
119         xmlURL = s;
120     }
121 
122     /** @param s String of XML to use in a StringReader, etc..  */
setXMLString(String s)123     public void setXMLString(String s)
124     {
125         xmlString = s;
126     }
127 
128     /**
129      * Return a Source object representing our XML.
130      * This may be any kind of source and is determined by which
131      * kinds of setXML*() methods you've called.
132      * <ul>
133      * <li>if xmlString is set, return new StreamSource(new StringReader(xmlString))</li>
134      * <li>if xmlURL/Name is set, return new StreamSource(xmlURL)</li>
135      * <li>More types TBD</li>
136      * </ul>
137      * @return Source object representing our XML, or an
138      * IllegalStateException if we don't have any XML
139      */
getXMLSource()140     public Source getXMLSource()
141     {
142         if (null != xmlString)
143             return new StreamSource(new StringReader(xmlString));
144         else if (null != xmlURL)
145             return new StreamSource(xmlURL);
146         else
147             throw new IllegalStateException("TraxDatalet.getXMLSource() with no XML!");
148     }
149 
150 
151     /**
152      * Convenience method: set both XML and XSL names at once.
153      *
154      * @param baseDir path/filename to your inputDir where
155      * your matched xml, xsl files are
156      * @param baseName base portion of filename (not including
157      * extension, which is automatically .xml and .xsl)
158      */
setNames(String baseDir, String baseName)159     public void setNames(String baseDir, String baseName)
160     {
161         // Note forward slash, since
162         String baseURL = QetestUtils.filenameToURL(baseDir) + "/";
163         xslURL = baseURL + baseName + ".xsl";
164         xmlURL = baseURL + baseName + ".xml";
165     }
166 
167 
168     /** Old-way: name to put output into; default:TraxDatalet.out.  */
169     public String outputName = "TraxDatalet.out";
170 
171     /** Old-way: name of the a gold file or data; default:no-gold-file.out.  */
172     public String goldName = "no-gold-file.out";
173 
174 
175     /**
176      * Generic placeholder for any additional options.
177      * @see StylesheetDatalet#options
178      */
179     public Properties options = new Properties();
180 
181 
182     /**
183      * A block of objects to validate.
184      * Users may put in various objects that they will use as
185      * expected data later on.  You can access this as a Properties
186      * block or as a Hashtable; it's up to each user to define this.
187      */
188     public Properties expected = new Properties();
189 
190 
191     /** No argument constructor is a no-op.  */
TraxDatalet()192     public TraxDatalet() { /* no-op */ }
193 
194 
195     /** Description of what this Datalet tests.  */
196     protected String description = "TraxDatalet: javax.xml.transform Source holder";
197 
198 
199     /**
200      * Accesor method for a brief description of this Datalet.
201      *
202      * @return String describing the specific set of data
203      * this Datalet contains (can often be used as the description
204      * of any check() calls made from the Testlet).
205      */
getDescription()206     public String getDescription()
207     {
208         return description;
209     }
210 
211 
212     /**
213      * Accesor method for a brief description of this Datalet.
214      *
215      * @param s description to use for this Datalet.
216      */
setDescription(String s)217     public void setDescription(String s)
218     {
219         description = s;
220     }
221 
222 
223     /**
224      * Load fields of this Datalet from a Hashtable.
225      * Caller must provide data for all of our fields.
226      *
227      * @param Hashtable to load
228      */
load(Hashtable h)229     public void load(Hashtable h)
230     {
231         if (null == h)
232             return;
233         xslURL = (String)h.get("xslURL");
234         xslString = (String)h.get("xslString");
235         xmlURL = (String)h.get("xmlURL");
236         xmlString = (String)h.get("xmlString");
237         outputName = (String)h.get("outputName");
238         goldName = (String)h.get("goldName");
239         description = (String)h.get("description");
240         try
241         {
242             options = (Properties)h.get("options");
243         }
244         catch (Exception e) { /* no-op, ignore */ }
245         try
246         {
247             expected = (Properties)h.get("expected");
248         }
249         catch (Exception e) { /* no-op, ignore */ }
250     }
251 
252 
253     /**
254      * Load fields of this Datalet from a String[].
255      * Order: xslURL, xmlURL, outputName, goldName, description
256      * If too few args, then fields at end of list are left at default value.
257      *
258      * @param s String array to load
259      */
load(String[] args)260     public void load(String[] args)
261     {
262         if (null == args)
263             return; //@todo should this have a return val or exception?
264 
265         try
266         {
267             xslURL = args[0];
268             xmlURL = args[1];
269             outputName = args[2];
270             goldName = args[3];
271             description = args[4];
272         }
273         catch (ArrayIndexOutOfBoundsException  aioobe)
274         {
275             // No-op, leave remaining items as default
276         }
277     }
278 }  // end of class TraxDatalet
279 
280