View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.ui.browser;
5   
6   import java.awt.Component;
7   import java.io.File;
8   import java.io.FileInputStream;
9   import java.io.IOException;
10  import java.net.MalformedURLException;
11  import java.net.URL;
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Properties;
15  import java.util.logging.Logger;
16  
17  import org.telscenter.pas.steps.Pedraw;
18  
19  import com.webrenderer.BrowserFactory;
20  import com.webrenderer.IBrowserCanvas;
21  import com.webrenderer.dom.IDocument;
22  import com.webrenderer.event.BrowserAdapter;
23  import com.webrenderer.event.BrowserEvent;
24  import com.webrenderer.event.NetworkAdapter;
25  import com.webrenderer.event.NetworkEvent;
26  
27  /***
28   * BrowserFactory that makes browsers using JadeLiquid's WebRenderer library.
29   * 
30   * @author turadg
31   */
32  public class WebRendererFactory extends
33  		org.telscenter.pas.ui.browser.BrowserFactory {
34  	/***
35  	 * Logger for this class
36  	 */
37  	private static final Logger logger = Logger
38  			.getLogger(WebRendererFactory.class.getName());
39  
40  	private String browserType;
41  
42  	static {
43  		setUserLicenseKey();
44  	}
45  
46  	class WebRendererBrowser implements IBrowser {
47  		private IBrowserCanvas browserCanvas;
48  
49  		private String type;
50  
51  		private List<IBrowserListener> listeners = new ArrayList<IBrowserListener>();
52  
53  		NavigationPolicy policy;
54  
55  		public WebRendererBrowser(IBrowserCanvas browserCanvas, String type) {
56  			if (browserCanvas == null)
57  				throw new IllegalArgumentException(
58  						"null browserCanvas argument");
59  			if (type == null || type.length() == 0)
60  				throw new IllegalArgumentException("empty type argument");
61  			this.browserCanvas = browserCanvas;
62  			this.type = type;
63  
64  			browserCanvas.addBrowserListener(new BrowserAdapter() {
65  				public void onLoadIntercept(BrowserEvent be) {
66  					if (policy == null) {
67  						return;
68  					}
69  
70  					if (!policy.isCompliant(be.getURL())) {
71  						// TODO: DELETE println after completely satisfied...but
72  						// for now it's handy to have
73  						logger
74  								.info(be.getURL()
75  										+ " cannot be viewed according to NavigationPolicy");
76  						be.blockLoad();
77  					} else {
78  						// TODO: DELETE println after completely satisfied...but
79  						// for now it's handy to have
80  						logger
81  								.info(be.getURL()
82  										+ " can be viewed according to NavigationPolicy");
83  					}
84  				}
85  			});
86  
87  			browserCanvas.addNetworkListener(new NetworkAdapter() {
88  				@Override
89  				public void onDocumentComplete(NetworkEvent arg0) {
90  					org.telscenter.pas.ui.browser.BrowserEvent event = new org.telscenter.pas.ui.browser.BrowserEvent(
91  							WebRendererBrowser.this, -1, null);
92  
93  					// go backwards incase a listener removes itself
94  					for (int i = listeners.size() - 1; i >= 0; i--) {
95  						listeners.get(i).downloadCompleted(event);
96  					}
97  				}
98  			});
99  		}
100 
101 		public void back() {
102 			browserCanvas.goBack();
103 		}
104 
105 		public void forward() {
106 			browserCanvas.goForward();
107 		}
108 
109 		public String getBrowserType() {
110 			return type;
111 		}
112 
113 		public Component getComponent() {
114 			return browserCanvas.getCanvas();
115 		}
116 
117 		public URL getUrl() {
118 			try {
119 				return new URL(browserCanvas.getURL());
120 			} catch (MalformedURLException e) {
121 				return null;
122 			}
123 		}
124 
125 		public void refresh() {
126 			browserCanvas.reload(IBrowserCanvas.RELOAD_NORMAL);
127 		}
128 
129 		public void setUrl(URL url) {
130 			if (url == null)
131 				throw new IllegalArgumentException("null url argument");
132 			browserCanvas.loadURL(url);
133 		}
134 
135 		public void setUrl(URL url, String postData) {
136 			if (url == null)
137 				throw new IllegalArgumentException("null url argument");
138 			browserCanvas.httpPOST(url.toExternalForm(), postData);
139 		}
140 
141 		public void stop() {
142 			browserCanvas.stopLoad();
143 		}
144 
145 		public String getPageTitle() {
146 			return browserCanvas.getTitle();
147 		}
148 
149 		/***
150 		 * Specify navigation policy, eg for url filtering
151 		 */
152 		public void setNavigationPolicy(NavigationPolicy policy) {
153 			this.policy = policy;
154 
155 		}
156 
157 		public String getContent() {
158 			IDocument doc = browserCanvas.getDocument();
159 			if (doc == null) {
160 				return null;
161 			}
162 
163 			return doc.toString();
164 		}
165 
166 		public IBrowserCanvas getIBrowserCanvas() {
167 			return this.browserCanvas;
168 		}
169 
170 		public void addBrowserListener(IBrowserListener listener) {
171 			listeners.add(listener);
172 		}
173 
174 		public void removeBrowserListener(IBrowserListener listener) {
175 			listeners.remove(listener);
176 
177 		}
178 
179 		
180 		public void setContent(String content) {
181 			
182 			if( browserCanvas.getDocument() != null )
183 				browserCanvas.getDocument().write(content);
184 			
185 		}
186 	}
187 
188 	public IBrowser getBrowser() {
189 		IBrowserCanvas wrBrowser;
190 
191 		if (browserType == null) {
192 			// then figure out what type to be using
193 
194 			// try spawning Internet Explorer
195 			wrBrowser = BrowserFactory.spawnInternetExplorer();
196 			browserType = IBrowser.IE;
197 
198 			// if that fails, try Safari
199 			if (wrBrowser == null) {
200 				wrBrowser = BrowserFactory.spawnSafari();
201 				browserType = IBrowser.SAFARI;
202 			}
203 
204 			// if that fails, try Mozilla
205 			if (wrBrowser == null) {
206 				wrBrowser = BrowserFactory.spawnMozilla();
207 				browserType = IBrowser.MOZILLA;
208 			}
209 		} else {
210 			// otherwise generate on browserType
211 			if (browserType == IBrowser.IE)
212 				wrBrowser = BrowserFactory.spawnInternetExplorer();
213 			else if (browserType == IBrowser.SAFARI)
214 				wrBrowser = BrowserFactory.spawnSafari();
215 			else if (browserType == IBrowser.MOZILLA)
216 				wrBrowser = BrowserFactory.spawnMozilla();
217 			else
218 				throw new RuntimeException(
219 						"WebRenderer failed to generate any browser");
220 		}
221 
222 		IBrowser ibrowser = new WebRendererBrowser(wrBrowser, browserType);
223 
224 		return ibrowser;
225 	}
226 
227 	/***
228 	 * If the user has a key installed at
229 	 * ${user.dir}/.webrenderer/license.properties, use that
230 	 * 
231 	 */
232 	private static void setUserLicenseKey() {
233 		File userHome = new File(System.getProperty("user.home"));
234 		File licenseFile = new File(userHome, ".webrenderer/license.properties");
235 		if (!licenseFile.exists()) {
236 			logger.info("No WebRenderer license found at " + licenseFile);
237 			return;
238 		}
239 		// otherwise
240 		logger.info("Using WebRenderer license at " + licenseFile);
241 		Properties licenseProperties = new Properties();
242 		try {
243 			licenseProperties.load(new FileInputStream(licenseFile));
244 			String user = licenseProperties.getProperty("user");
245 			String key = licenseProperties.getProperty("key");
246 			BrowserFactory.setLicenseData(user, key);
247 		} catch (IOException e) {
248 			// TODO Auto-generated catch block
249 			e.printStackTrace();
250 		}
251 	}
252 
253 }