1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.drazzib.netpisteur.ui.render;
28
29 import java.net.MalformedURLException;
30 import java.net.URL;
31
32 import javax.swing.text.Document;
33 import javax.swing.text.Element;
34 import javax.swing.text.SimpleAttributeSet;
35 import javax.swing.text.View;
36 import javax.swing.text.ViewFactory;
37 import javax.swing.text.html.HTML;
38 import javax.swing.text.html.HTMLDocument;
39 import javax.swing.text.html.HTMLEditorKit;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44
45 public class HTMLViewer extends HTMLEditorKit {
46
47 static Log log = LogFactory.getLog(HTMLViewer.class);
48
49 /**
50 *
51 */
52 private static final long serialVersionUID = -2311353613329497885L;
53
54 URL _url;
55
56 /**
57 * Create an new implementation of an HTMLEditorKit especially designed for
58 * NetPisteur project.
59 */
60 public HTMLViewer() {
61 super();
62
63
64
65
66
67 }
68
69 /**
70 * Define the current URL used to correct relative path access
71 */
72 public void setURL(URL u) {
73 AudioPlayer.reset();
74 this._url = u;
75 }
76
77 /**
78 * Define a default HTMLDocument for this EditorKit to extends default
79 * HTMLDoc with an synchronous loading of the page
80 */
81 public Document createDefaultDocument() {
82 log.debug("HV: Creation d'un docu par default");
83 Document doc = super.createDefaultDocument();
84
85
86 ((HTMLDocument) doc).setAsynchronousLoadPriority(-1);
87 return doc;
88 }
89
90 /**
91 * Redefine the ViewFactory used by this HTMLEditorKit
92 */
93 public ViewFactory getViewFactory() {
94 log.debug("HV: Get View Factory !");
95 return new HTMLViewerFactory();
96 }
97
98 public class HTMLViewerFactory extends HTMLEditorKit.HTMLFactory {
99
100 public HTMLViewerFactory() {
101 super();
102 log.debug("HV: Creation d'une HTMLFactory");
103 }
104
105 /**
106 * For each Element of an HTMLDocument this method is called to create
107 * implied View's for painting <BR>
108 * <BR>
109 * <B>It's here that the parsing of special Tag and their Attributes is
110 * done. For the moment only BGSOUND and EMBED is parsed. But in a near
111 * future, FORM and SCRIPT will be also parsed by this method </B>
112 */
113 public View create(Element elem) {
114
115 log.trace("HV: " + elem.toString());
116 log.trace("HV: elem "+elem.getName());
117
118 if (elem.getName().equalsIgnoreCase("bgsound")) {
119 log.trace("HV: tag "+elem.getName().toString());
120 SimpleAttributeSet attribs = new SimpleAttributeSet(elem
121 .getAttributes());
122 if (attribs.getAttribute(HTML.Attribute.SRC) != null) {
123 srcPlayer(
124 (String) attribs.getAttribute(HTML.Attribute.SRC),
125 (String) attribs.getAttribute("loop"));
126 }
127
128 } else if (elem.getName().equalsIgnoreCase("embed")) {
129 log.trace("HV: tag "+elem.getName().toString());
130 SimpleAttributeSet attribs = new SimpleAttributeSet(elem
131 .getAttributes());
132 if (attribs.getAttribute(HTML.Attribute.SRC) != null) {
133 srcPlayer(
134 (String) attribs.getAttribute(HTML.Attribute.SRC),
135 (String) attribs.getAttribute("loop"));
136 }
137
138 }
139
140 return super.create(elem);
141
142 }
143
144 /**
145 * Transform the relative url of a page to an integer URL Use the _URL
146 * of the doc (ie : http://foo.org/test/test.html) <BR>
147 * "src/test.wav" => "http://foo.org/test/src/test.wav" <BR>
148 * "/src/test.wav" => "http://foo.org/src/test.wav"
149 */
150 private String baseURL(String url) {
151
152 String ret = "";
153
154 if (url.startsWith("http://")) {
155 ret = url;
156 } else if (url.startsWith("/")) {
157 ret = HTMLViewer.this._url.getProtocol() + "://"
158 + HTMLViewer.this._url.getHost() + url;
159 } else {
160 ret = HTMLViewer.this._url.toString().substring(0,
161 HTMLViewer.this._url.toString().lastIndexOf("/"))
162 + "/" + url;
163 }
164
165 return ret;
166 }
167
168 /**
169 * Parse the URL to get if it's an audio file. If it's true -> get a new
170 * AudioPlayer object !
171 */
172 private AudioPlayer srcPlayer(String theFile, String loop) {
173 AudioPlayer myPlayer = null;
174
175 try {
176 URL myStreamUrl = new URL(baseURL(theFile));
177 if (theFile.endsWith(".wav") || theFile.endsWith(".mid")
178 || theFile.endsWith(".au") || theFile.endsWith(".aif")
179 || theFile.endsWith(".rmf")) {
180
181 if (loop != null) {
182 if (loop.equalsIgnoreCase("-1")) {
183 myPlayer = new AudioPlayer(myStreamUrl, true);
184 } else {
185 myPlayer = new AudioPlayer(myStreamUrl, false);
186 }
187 } else {
188 myPlayer = new AudioPlayer(myStreamUrl, true);
189 }
190 }
191 } catch (MalformedURLException e) {
192 log.error("URL du fichier son incorrecte : " + theFile, e);
193 }
194
195 return myPlayer;
196 }
197
198 }
199
200 }