KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > context > ContextFactory


1 /*
2  * $Id: ContextFactory.java,v 1.5 2002/06/06 15:02:07 valis Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.context;
52
53 import org.openlaszlo.iv.flash.api.*;
54 import org.openlaszlo.iv.flash.util.*;
55 import org.openlaszlo.iv.flash.url.*;
56 import org.openlaszlo.iv.flash.xml.*;
57
58 import org.w3c.dom.Node JavaDoc;
59 import java.io.*;
60 import java.util.*;
61
62 /**
63  * Factory class for creating various types of context from other objects.
64  *
65  * @author James Taylor
66  */

67
68 public abstract class ContextFactory
69 {
70     // Standard Contexts
71

72     /**
73      * Creates StandardContext from specified data and specified row.
74      *
75      * @param data specified strings array
76      * @param row take values from this specified row
77      * @return StandardContext without parent (null parent)
78      * @exception IVException
79      * @see #createContext(Context,String[][],int)
80      */

81     public static Context createContext( String JavaDoc[][] data, int row ) throws IVException
82     {
83         return createContext( null, data, row );
84     }
85
86     /**
87      * Creates StandardContext from specified data and specified row
88      * <P>
89      * Format of the data is the following:<BR>
90      * <PRE>
91      * name0 , name1 , name2 , ...
92      * value00, value01, value02, ...
93      * value10, value11, value12, ...
94      * ....
95      * </PRE>
96      * <BR>
97      * Created context will contain values from specified row
98      *
99      * @param parent parent of return context
100      * @param data specified strings array
101      * @param row take values from this specified row
102      * @return StandardContext with parent
103      * @exception IVException
104      */

105     public static Context createContext( Context parent, String JavaDoc[][] data, int row ) throws IVException
106     {
107         StandardContext context = new StandardContext( parent );
108
109         setStandardContextData( context, data, row );
110
111         return context;
112     }
113
114     /**
115      * Creates StandardContext from specified data
116      *
117      * @param data specified strings array
118      * @return StandardContext without parent
119      * @exception IVException
120      * @see #createContext(Context,String[][])
121      */

122     public static Context createContext( String JavaDoc[][] data ) throws IVException
123     {
124         return createContext( null, data );
125     }
126
127     /**
128      * Creates StandardContext from specified data
129      * <P>
130      * Format of the data is the following:<BR>
131      * <PRE>
132      * foo0 , ... , name , value , fooN, ...
133      * ... , ... , name0 , value0 , ... , ...
134      * ... , ... , name1 , value1 , ...
135      * .....
136      * </PRE>
137      * <BR>
138      * Created context will contain values with names from corresponding columns
139      *
140      * @param parent parent of return context
141      * @param data specified strings array
142      * @return StandardContext with parent
143      * @exception IVException
144      */

145     public static Context createContext( Context parent, String JavaDoc[][] data ) throws IVException
146     {
147         StandardContext context = new StandardContext( parent );
148
149         setStandardContextData( context, data );
150
151         return context;
152     }
153
154     // XML Contexts
155

156     public static Context createContext( Node JavaDoc n )
157     {
158         return createContext( null, n );
159     }
160
161     public static Context createContext( Context parent, Node JavaDoc n ) {
162         return XMLContext.newXMLContext(parent, n);
163     }
164
165     // Bean Contexts
166

167     public static Context createContext( Map o )
168     {
169         return createContext( null, o );
170     }
171
172     public static Context createContext( Context parent, Map o )
173     {
174         return new BeanContext( parent, o );
175     }
176
177     /**
178      * Reads datasource from specified url, detects the type of this context text or url (so far)
179      * and creates LineReader for text or IVUrl for xml.
180      * <P>
181      * I believe it has to be rewritten in future, it's too ugly!
182      *
183      * @param surl either url to datasource or datasource itself (perfixed with #)
184      * @param flashFile flash file which location is used to resolve relative urls
185      * @return LineReader for text datasources or IVUrl for xml datasources
186      * @exception IVException
187      * @exception java.io.IOException
188      */

189     public static Object JavaDoc readContext( String JavaDoc surl,
190                                       FlashFile flashFile ) throws IVException, java.io.IOException JavaDoc
191     {
192         return DataSourceHelper.readContextData(surl, flashFile);
193     }
194
195     /**
196      * Reads datasource from specified url, assuming that it's in text (tabular) format and
197      * returns two-dimensional array of Strings.
198      * <p>
199      * If the datasource is not in tabular format, then throw an exception
200      *
201      * @param surl either url to datasource or datasource itself (perfixed with #)
202      * @param flashFile flash file which location is used to resolve relative urls
203      * @return two-dimensional array of Strings
204      * @exception IVException
205      * @exception java.io.IOException
206      */

207     public static String JavaDoc[][] readStandardContext( String JavaDoc surl,
208                                                   FlashFile flashFile ) throws IVException, java.io.IOException JavaDoc
209     {
210         Object JavaDoc dsrc = readContext( surl, flashFile );
211         if( !(dsrc instanceof LineReader) ) {
212             throw new IVException( Resource.EXPECTSTDCONTEXT );
213         }
214
215         DataSource ds = new DataSource( (LineReader) dsrc );
216         return ds.getData();
217     }
218
219     /**
220      * Reads datasource from given url, detects it's type: xml or text (so far)
221      * and creates a Context of the corresponding type.<P>
222      * Datasources can be specified by url or inline. If url starts
223      * with '#' then this is inline datasource which is completely given
224      * in the url string.
225      *
226      * @param parent will be used as parent for the created context ( null ok )
227      * @param surl url or inline datasource
228      * @param flashFile current flash file from which this datasource is
229      * requested
230      * @param useRowStyle if the datasource is tabular, determines whether
231      * the row or column datasource style is used.
232      * @return a context
233      * @exception IVException
234      * @exception IOException
235      */

236
237     public static Context createContext( Context parent,
238                                          String JavaDoc surl,
239                                          FlashFile flashFile,
240                                          boolean useRowStyle ) throws IVException, java.io.IOException JavaDoc
241     {
242         // read context
243
Object JavaDoc dsrc = readContext( surl, flashFile );
244
245         if( dsrc instanceof LineReader )
246         {
247             // Standard ( tabular text ) datasource
248
DataSource ds = new DataSource( (LineReader) dsrc );
249
250             StandardContext context = new StandardContext( parent );
251
252             if ( useRowStyle )
253             {
254                 setStandardContextData( context, ds.getData(), 1 );
255             }
256             else
257             {
258                 setStandardContextData( context, ds.getData() );
259             }
260
261             return context;
262         }
263         else
264         {
265             try {
266                 return XMLContext.newXMLContext(parent, XMLHelper.getNode((IVUrl)dsrc));
267             } catch ( Exception JavaDoc e ) { // otherwise it requires xml libraries to be in classpath
268
throw new IVException( e );
269             }
270         }
271     }
272
273     /**
274      * Sets specified data to the specified context
275      * <P>
276      * Format of the data is the following:<BR>
277      * <PRE>
278      * foo0 , ... , name , value , fooN, ...
279      * ... , ... , name0 , value0 , ... , ...
280      * ... , ... , name1 , value1 , ...
281      * .....
282      * </PRE>
283      * <BR>
284      *
285      * @param context specified standard context
286      * @param data specified data
287      * @exception IVException
288      */

289
290     public static void setStandardContextData( StandardContext context,
291                                                String JavaDoc[][] data )
292         throws IVException
293     {
294         int j, k;
295
296         for ( j = 0; j < data[ 0 ].length && ! data[ 0 ][ j ].equalsIgnoreCase( "NAME" ); j++ );
297
298         if( j == data[0].length )
299         {
300             throw new IVException( Resource.COLNOTFOUNDCMD, new Object JavaDoc[] {"", "NAME", ""} );
301         }
302
303         for( k = 0; k < data[ 0 ].length && ! data[ 0 ][ k ].equalsIgnoreCase( "VALUE" ); k++ );
304
305         if( k == data[ 0 ].length )
306         {
307             throw new IVException( Resource.COLNOTFOUNDCMD, new Object JavaDoc[] {"", "VALUE", ""} );
308         }
309
310         for( int i = 1; i < data.length; i++ )
311         {
312             context.setValue( context.apply( data[i][j] ),
313                               context.apply( data[i][k] ) );
314         }
315     }
316
317     /**
318      * Sets specified data at specified row to the specified context
319      * <P>
320      * Format of the data is the following:<BR>
321      * <PRE>
322      * name0 , name1 , name2 , ...
323      * value00, value01, value02, ...
324      * value10, value11, value12, ...
325      * ....
326      * </PRE>
327      * <BR>
328      *
329      * @param context specified standard context
330      * @param data specified data
331      * @param row specified row
332      * @exception IVException
333      */

334
335     public static void setStandardContextData( StandardContext context,
336                                                String JavaDoc[][] data,
337                                                int row )
338         throws IVException
339     {
340         for ( int i = 0; i < data[ row ].length; i++ )
341         {
342             context.setValue( context.apply( data[ 0 ][ i ] ),
343                               context.apply( data[ row ][ i ] ) );
344         }
345     }
346 }
347
Popular Tags