001: /*
002: * $Id: ContextFactory.java,v 1.5 2002/06/06 15:02:07 valis Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.context;
052:
053: import org.openlaszlo.iv.flash.api.*;
054: import org.openlaszlo.iv.flash.util.*;
055: import org.openlaszlo.iv.flash.url.*;
056: import org.openlaszlo.iv.flash.xml.*;
057:
058: import org.w3c.dom.Node;
059: import java.io.*;
060: import java.util.*;
061:
062: /**
063: * Factory class for creating various types of context from other objects.
064: *
065: * @author James Taylor
066: */
067:
068: public abstract class ContextFactory {
069: // Standard Contexts
070:
071: /**
072: * Creates StandardContext from specified data and specified row.
073: *
074: * @param data specified strings array
075: * @param row take values from this specified row
076: * @return StandardContext without parent (null parent)
077: * @exception IVException
078: * @see #createContext(Context,String[][],int)
079: */
080: public static Context createContext(String[][] data, int row)
081: throws IVException {
082: return createContext(null, data, row);
083: }
084:
085: /**
086: * Creates StandardContext from specified data and specified row
087: * <P>
088: * Format of the data is the following:<BR>
089: * <PRE>
090: * name0 , name1 , name2 , ...
091: * value00, value01, value02, ...
092: * value10, value11, value12, ...
093: * ....
094: * </PRE>
095: * <BR>
096: * Created context will contain values from specified row
097: *
098: * @param parent parent of return context
099: * @param data specified strings array
100: * @param row take values from this specified row
101: * @return StandardContext with parent
102: * @exception IVException
103: */
104: public static Context createContext(Context parent,
105: String[][] data, int row) throws IVException {
106: StandardContext context = new StandardContext(parent);
107:
108: setStandardContextData(context, data, row);
109:
110: return context;
111: }
112:
113: /**
114: * Creates StandardContext from specified data
115: *
116: * @param data specified strings array
117: * @return StandardContext without parent
118: * @exception IVException
119: * @see #createContext(Context,String[][])
120: */
121: public static Context createContext(String[][] data)
122: throws IVException {
123: return createContext(null, data);
124: }
125:
126: /**
127: * Creates StandardContext from specified data
128: * <P>
129: * Format of the data is the following:<BR>
130: * <PRE>
131: * foo0 , ... , name , value , fooN, ...
132: * ... , ... , name0 , value0 , ... , ...
133: * ... , ... , name1 , value1 , ...
134: * .....
135: * </PRE>
136: * <BR>
137: * Created context will contain values with names from corresponding columns
138: *
139: * @param parent parent of return context
140: * @param data specified strings array
141: * @return StandardContext with parent
142: * @exception IVException
143: */
144: public static Context createContext(Context parent, String[][] data)
145: throws IVException {
146: StandardContext context = new StandardContext(parent);
147:
148: setStandardContextData(context, data);
149:
150: return context;
151: }
152:
153: // XML Contexts
154:
155: public static Context createContext(Node n) {
156: return createContext(null, n);
157: }
158:
159: public static Context createContext(Context parent, Node n) {
160: return XMLContext.newXMLContext(parent, n);
161: }
162:
163: // Bean Contexts
164:
165: public static Context createContext(Map o) {
166: return createContext(null, o);
167: }
168:
169: public static Context createContext(Context parent, Map o) {
170: return new BeanContext(parent, o);
171: }
172:
173: /**
174: * Reads datasource from specified url, detects the type of this context text or url (so far)
175: * and creates LineReader for text or IVUrl for xml.
176: * <P>
177: * I believe it has to be rewritten in future, it's too ugly!
178: *
179: * @param surl either url to datasource or datasource itself (perfixed with #)
180: * @param flashFile flash file which location is used to resolve relative urls
181: * @return LineReader for text datasources or IVUrl for xml datasources
182: * @exception IVException
183: * @exception java.io.IOException
184: */
185: public static Object readContext(String surl, FlashFile flashFile)
186: throws IVException, java.io.IOException {
187: return DataSourceHelper.readContextData(surl, flashFile);
188: }
189:
190: /**
191: * Reads datasource from specified url, assuming that it's in text (tabular) format and
192: * returns two-dimensional array of Strings.
193: * <p>
194: * If the datasource is not in tabular format, then throw an exception
195: *
196: * @param surl either url to datasource or datasource itself (perfixed with #)
197: * @param flashFile flash file which location is used to resolve relative urls
198: * @return two-dimensional array of Strings
199: * @exception IVException
200: * @exception java.io.IOException
201: */
202: public static String[][] readStandardContext(String surl,
203: FlashFile flashFile) throws IVException,
204: java.io.IOException {
205: Object dsrc = readContext(surl, flashFile);
206: if (!(dsrc instanceof LineReader)) {
207: throw new IVException(Resource.EXPECTSTDCONTEXT);
208: }
209:
210: DataSource ds = new DataSource((LineReader) dsrc);
211: return ds.getData();
212: }
213:
214: /**
215: * Reads datasource from given url, detects it's type: xml or text (so far)
216: * and creates a Context of the corresponding type.<P>
217: * Datasources can be specified by url or inline. If url starts
218: * with '#' then this is inline datasource which is completely given
219: * in the url string.
220: *
221: * @param parent will be used as parent for the created context ( null ok )
222: * @param surl url or inline datasource
223: * @param flashFile current flash file from which this datasource is
224: * requested
225: * @param useRowStyle if the datasource is tabular, determines whether
226: * the row or column datasource style is used.
227: * @return a context
228: * @exception IVException
229: * @exception IOException
230: */
231:
232: public static Context createContext(Context parent, String surl,
233: FlashFile flashFile, boolean useRowStyle)
234: throws IVException, java.io.IOException {
235: // read context
236: Object dsrc = readContext(surl, flashFile);
237:
238: if (dsrc instanceof LineReader) {
239: // Standard ( tabular text ) datasource
240: DataSource ds = new DataSource((LineReader) dsrc);
241:
242: StandardContext context = new StandardContext(parent);
243:
244: if (useRowStyle) {
245: setStandardContextData(context, ds.getData(), 1);
246: } else {
247: setStandardContextData(context, ds.getData());
248: }
249:
250: return context;
251: } else {
252: try {
253: return XMLContext.newXMLContext(parent, XMLHelper
254: .getNode((IVUrl) dsrc));
255: } catch (Exception e) { // otherwise it requires xml libraries to be in classpath
256: throw new IVException(e);
257: }
258: }
259: }
260:
261: /**
262: * Sets specified data to the specified context
263: * <P>
264: * Format of the data is the following:<BR>
265: * <PRE>
266: * foo0 , ... , name , value , fooN, ...
267: * ... , ... , name0 , value0 , ... , ...
268: * ... , ... , name1 , value1 , ...
269: * .....
270: * </PRE>
271: * <BR>
272: *
273: * @param context specified standard context
274: * @param data specified data
275: * @exception IVException
276: */
277:
278: public static void setStandardContextData(StandardContext context,
279: String[][] data) throws IVException {
280: int j, k;
281:
282: for (j = 0; j < data[0].length
283: && !data[0][j].equalsIgnoreCase("NAME"); j++)
284: ;
285:
286: if (j == data[0].length) {
287: throw new IVException(Resource.COLNOTFOUNDCMD,
288: new Object[] { "", "NAME", "" });
289: }
290:
291: for (k = 0; k < data[0].length
292: && !data[0][k].equalsIgnoreCase("VALUE"); k++)
293: ;
294:
295: if (k == data[0].length) {
296: throw new IVException(Resource.COLNOTFOUNDCMD,
297: new Object[] { "", "VALUE", "" });
298: }
299:
300: for (int i = 1; i < data.length; i++) {
301: context.setValue(context.apply(data[i][j]), context
302: .apply(data[i][k]));
303: }
304: }
305:
306: /**
307: * Sets specified data at specified row to the specified context
308: * <P>
309: * Format of the data is the following:<BR>
310: * <PRE>
311: * name0 , name1 , name2 , ...
312: * value00, value01, value02, ...
313: * value10, value11, value12, ...
314: * ....
315: * </PRE>
316: * <BR>
317: *
318: * @param context specified standard context
319: * @param data specified data
320: * @param row specified row
321: * @exception IVException
322: */
323:
324: public static void setStandardContextData(StandardContext context,
325: String[][] data, int row) throws IVException {
326: for (int i = 0; i < data[row].length; i++) {
327: context.setValue(context.apply(data[0][i]), context
328: .apply(data[row][i]));
329: }
330: }
331: }
|