001: /*
002: * $Id: DataSourceHelper.java,v 1.5 2002/04/26 05:23:11 skavish 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.util;
052:
053: import java.io.*;
054: import java.util.*;
055:
056: import org.openlaszlo.iv.flash.api.*;
057: import org.openlaszlo.iv.flash.url.*;
058: import org.openlaszlo.iv.flash.xml.*;
059:
060: import org.w3c.dom.*;
061:
062: /**
063: * Helper class for reading any kind of datasources.
064: *
065: * @author Dmitry Skavish
066: * @see XMLDataSource
067: * @see DataSource
068: */
069: public class DataSourceHelper {
070:
071: /**
072: * Reads datasource from given url, detects it's type: xml or text
073: * and creates either LineReader (text) or else (xml)<P>
074: * Datasources can be specified by url or inline. If url starts
075: * with '#' then this is inline datasource which is completely given
076: * in the url string.
077: *
078: * @param surl url or inline datasource
079: * @param flashFile current flash file from which this datasource is requested
080: * @return either LineReader (plain datasource) or IVUrl
081: * @exception IOException
082: * @exception IVException
083: */
084: public static Object readContextData(String surl,
085: FlashFile flashFile) throws IVException, IOException {
086: if (surl == null || surl.length() == 0) {
087: throw new IOException("null datasource");
088: }
089:
090: Object dsrc = null;
091:
092: if (surl.charAt(0) == '#') {
093: if (surl.charAt(1) == '<') {
094: // xml datasource
095: byte[] bytes = flashFile.getEncoding() != null ? surl
096: .substring(1).getBytes(flashFile.getEncoding())
097: : PropertyManager.defaultEncoding != null ? surl
098: .substring(1)
099: .getBytes(
100: PropertyManager.defaultEncoding)
101: : surl.substring(1).getBytes();
102: dsrc = new BufferedUrl(new FlashBuffer(bytes));
103: } else {
104: // text datasource
105: dsrc = new NativeLineReader(surl);
106: }
107: } else {
108: int idx = surl.indexOf(';');
109: if (idx == -1) {
110: dsrc = new BufferedUrl(surl, flashFile);
111: } else {
112: StringTokenizer st = new StringTokenizer(surl, ";");
113: IVVector urls = new IVVector();
114: while (st.hasMoreTokens()) {
115: IVUrl url = IVUrl.newUrl(st.nextToken(), flashFile);
116: urls.addElement(url);
117: }
118: dsrc = new MultipleUrlsReader(urls, flashFile);
119: }
120: }
121:
122: if (dsrc instanceof BufferedUrl) {
123: BufferedUrl burl = (BufferedUrl) dsrc;
124: byte[] buf = burl.getFlashBuffer().getBuf();
125: if (!(buf.length > 3 && (buf[0] == '<' || (buf[0] == -1
126: && buf[1] == -2 && buf[2] == '<')))) {
127: dsrc = Util.getUrlReader(flashFile, burl);
128: }
129: }
130:
131: return dsrc;
132: }
133:
134: /**
135: * Return context data.<p>
136: * Reads datasource from given url, detects it's type: xml or text
137: * and creates either xml Node or array of strings.<P>
138: * Datasources can be specified by url or inline. If url starts
139: * with '#' then this is inline datasource which is completely given
140: * in the utl string.
141: *
142: * @param surl url or inline datasource
143: * @param flashFile current flash file from which this datasource is requested
144: * @return either Node or String[][]
145: * @exception IVException
146: * @exception IOException
147: */
148: public static Object getContextData(String surl, FlashFile flashFile)
149: throws IVException, IOException {
150: Object dsrc = readContextData(surl, flashFile);
151:
152: if (dsrc instanceof LineReader) {
153: DataSource ds = new DataSource((LineReader) dsrc);
154: String[][] data = ds.getData();
155: return data;
156: } else {
157: try {
158: return XMLHelper.getNode((IVUrl) dsrc);
159: } catch (Exception e) { // otherwise it requires xml libs to be in classpath
160: if (e instanceof IOException)
161: throw (IOException) e;
162: throw new IVException(e);
163: }
164: }
165:
166: }
167:
168: }
|