001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.installer;
028:
029: import java.util.Vector;
030:
031: import java.io.InputStreamReader;
032: import java.io.IOException;
033:
034: /**
035: * This class represents the Information need to download a MIDlet suite and
036: * display it to a user, in a list.
037: */
038: class SuiteDownloadInfo {
039: /** URL for the JAD of this suite */
040: String url;
041: /** label to display to the User for this suite */
042: String label;
043:
044: /**
045: * Read a HTML page and pickout the links for MIDlet suites.
046: * A MIDlet suite links end with .jad
047: *
048: * @param page HTML page to be read
049: *
050: * @return vector of URL/Label pairs
051: *
052: * @exception IOException is thrown if any error prevents the
053: * download of the HTML page.
054: */
055: static Vector getDownloadInfoFromPage(InputStreamReader page)
056: throws IOException {
057: Vector suites = new Vector();
058: SuiteDownloadInfo info;
059:
060: info = getNextJadLink(page);
061: while (info != null) {
062: if (info.url.endsWith(".jad") || info.url.endsWith(".jar")) {
063: suites.addElement(info);
064: }
065:
066: info = getNextJadLink(page);
067: }
068:
069: return suites;
070: }
071:
072: /**
073: * Read a HTML page and pickout next link.
074: *
075: * @param page HTML page to be read
076: *
077: * @return URL/Label pair
078: *
079: * @exception IOException is thrown if any error prevents the
080: * download of the HTML page.
081: */
082: private static SuiteDownloadInfo getNextJadLink(
083: InputStreamReader page) throws IOException {
084: String url;
085: String label;
086:
087: url = getNextUrl(page);
088: if (url == null) {
089: return null;
090: }
091:
092: label = getNextLabel(page);
093: if (label == null) {
094: label = url;
095: }
096:
097: return new SuiteDownloadInfo(url, label);
098: }
099:
100: /**
101: * Read a HTML page and pickout next href.
102: *
103: * @param page HTML page to be read
104: *
105: * @return URL
106: *
107: * @exception IOException is thrown if any error prevents the
108: * download of the HTML page.
109: */
110: private static String getNextUrl(InputStreamReader page)
111: throws IOException {
112: int currentChar;
113: StringBuffer url;
114:
115: if (!findString(page, "href=\"")) {
116: return null;
117: }
118:
119: url = new StringBuffer();
120:
121: currentChar = page.read();
122: while (currentChar != '"') {
123: if (currentChar == -1) {
124: return null;
125: }
126:
127: url.append((char) currentChar);
128: currentChar = page.read();
129: }
130:
131: if (url.length() == 0) {
132: return null;
133: }
134:
135: return url.toString();
136: }
137:
138: /**
139: * Read a HTML page and pickout the text after the beginning anchor.
140: *
141: * @param page HTML page to be read
142: *
143: * @return label
144: *
145: * @exception IOException is thrown if any error prevents the
146: * download of the HTML page.
147: */
148: private static String getNextLabel(InputStreamReader page)
149: throws IOException {
150: int currentChar;
151: StringBuffer label;
152:
153: if (!findChar(page, '>')) {
154: return null;
155: }
156:
157: label = new StringBuffer();
158:
159: currentChar = page.read();
160: while (currentChar != '<') {
161: if (currentChar == -1) {
162: return null;
163: }
164:
165: label.append((char) currentChar);
166: currentChar = page.read();
167: }
168:
169: if (label.length() == 0) {
170: return null;
171: }
172:
173: return label.toString();
174: }
175:
176: /**
177: * Find the next given string in an HTML page move past it.
178: *
179: * @param page HTML page to be read
180: * @param targetString string to move past
181: *
182: * @return true if string found, else false
183: *
184: * @exception IOException is thrown if any error prevents the
185: * download of the HTML page.
186: */
187: private static boolean findString(InputStreamReader page,
188: String targetString) throws IOException {
189: for (int i = 0; i < targetString.length(); i++) {
190: if (!findChar(page, targetString.charAt(i))) {
191: return false;
192: }
193: }
194:
195: return true;
196: }
197:
198: /**
199: * Find the next given char in an HTML page move past it.
200: *
201: * @param page HTML page to be read
202: * @param targetChar char to move past
203: *
204: * @return true if string found, else false
205: *
206: * @exception IOException is thrown if any error prevents the
207: * download of the HTML page.
208: */
209: private static boolean findChar(InputStreamReader page,
210: char targetChar) throws IOException {
211: int currentChar;
212:
213: currentChar = page.read();
214: while (Character.toLowerCase((char) currentChar) != targetChar) {
215: if (currentChar == -1) {
216: return false;
217: }
218:
219: currentChar = page.read();
220: }
221:
222: return true;
223: }
224:
225: /**
226: * Constructs a SuiteDownloadInfo.
227: *
228: * @param theUrl URL for this suite
229: * @param theLabel label for this suite
230: */
231: SuiteDownloadInfo(String theUrl, String theLabel) {
232: url = theUrl;
233: label = theLabel;
234: }
235: }
|