| 1 |
/* |
| 2 |
Copyright (C) 2013 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, version 2. |
| 9 |
|
| 10 |
IcedTea is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with IcedTea; see the file COPYING. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
02110-1301 USA. |
| 19 |
|
| 20 |
Linking this library statically or dynamically with other modules is |
| 21 |
making a combined work based on this library. Thus, the terms and |
| 22 |
conditions of the GNU General Public License cover the whole |
| 23 |
combination. |
| 24 |
|
| 25 |
As a special exception, the copyright holders of this library give you |
| 26 |
permission to link this library with independent modules to produce an |
| 27 |
executable, regardless of the license terms of these independent |
| 28 |
modules, and to copy and distribute the resulting executable under |
| 29 |
terms of your choice, provided that you also meet, for each linked |
| 30 |
independent module, the terms and conditions of the license of that |
| 31 |
module. An independent module is a module which is not derived from |
| 32 |
or based on this library. If you modify this library, you may extend |
| 33 |
this exception to your version of the library, but you are not |
| 34 |
obligated to do so. If you do not wish to do so, delete this |
| 35 |
exception statement from your version. |
| 36 |
*/ |
| 37 |
package net.sourceforge.jnlp.runtime.html; |
| 38 |
|
| 39 |
import java.net.MalformedURLException; |
| 40 |
import java.net.URL; |
| 41 |
import java.util.HashMap; |
| 42 |
import java.util.List; |
| 43 |
import java.util.Map; |
| 44 |
import net.sourceforge.jnlp.ParseException; |
| 45 |
import net.sourceforge.jnlp.Parser; |
| 46 |
import net.sourceforge.jnlp.PluginBridge; |
| 47 |
import net.sourceforge.jnlp.PluginParameters; |
| 48 |
import org.w3c.dom.Element; |
| 49 |
import org.w3c.dom.NamedNodeMap; |
| 50 |
|
| 51 |
public class AppletParser { |
| 52 |
|
| 53 |
private static final String[] PARAMS = new String[]{ |
| 54 |
"param", "PARAM", "Param" |
| 55 |
}; |
| 56 |
|
| 57 |
private final Element source; |
| 58 |
private final URL docBase; |
| 59 |
|
| 60 |
public AppletParser(Element applet, URL docbase) { |
| 61 |
this.source = applet; |
| 62 |
this.docBase = docbase; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public String toString() { |
| 67 |
StringBuilder sb = new StringBuilder(); |
| 68 |
sb.append("width: ").append(source.getAttribute("width")).append("\n"); |
| 69 |
sb.append("height: ").append(source.getAttribute("height")).append("\n"); |
| 70 |
sb.append("codebase: ").append(source.getAttribute("codebase")).append("\n"); |
| 71 |
sb.append("code: ").append(source.getAttribute("code")).append("\n"); |
| 72 |
sb.append("archive: ").append(source.getAttribute("archive")).append("\n"); |
| 73 |
sb.append("data: ").append(source.getAttribute("data")).append("\n"); |
| 74 |
List<Element> found = findParams(); |
| 75 |
sb.append("params: ").append(found.size()).append("\n"); |
| 76 |
for (Element element : found) { |
| 77 |
sb.append("param: ").append(element.getAttribute("name")).append("=").append(element.getAttribute("value")).append("\n"); |
| 78 |
} |
| 79 |
return sb.toString(); |
| 80 |
} |
| 81 |
|
| 82 |
public PluginBridge toPluginBridge() throws Exception { |
| 83 |
return new PluginBridge( |
| 84 |
createCodebase(), |
| 85 |
docBase, |
| 86 |
getArchives(), |
| 87 |
getMain(), |
| 88 |
//removing all chars from number - like whitespace, px and so on... |
| 89 |
new Integer(sanitizeSize(source.getAttribute("width"))), |
| 90 |
new Integer(sanitizeSize(source.getAttribute("height"))), |
| 91 |
createParams()); |
| 92 |
} |
| 93 |
|
| 94 |
private URL createCodebase() throws ParseException, MalformedURLException { |
| 95 |
String inHtmlCodebase = source.getAttribute("codebase"); |
| 96 |
if (inHtmlCodebase != null && inHtmlCodebase.trim().isEmpty()) { |
| 97 |
inHtmlCodebase = "."; |
| 98 |
} |
| 99 |
URL u = Parser.getURL(inHtmlCodebase, "In html " + source.getNodeName() + "'s codebase", docBase, false); |
| 100 |
if (!u.toExternalForm().endsWith("/")) { |
| 101 |
u = new URL(u.toExternalForm() + "/"); |
| 102 |
} |
| 103 |
return u; |
| 104 |
} |
| 105 |
|
| 106 |
private PluginParameters createParams() { |
| 107 |
Map<String, String> data = new HashMap<>(); |
| 108 |
List<Element> found = findParams(); |
| 109 |
for (Element element : found) { |
| 110 |
data.put(element.getAttribute("name"), element.getAttribute("value")); |
| 111 |
} |
| 112 |
//strange compatibility issue |
| 113 |
if (data.get("jnlp_href") == null) { |
| 114 |
if (data.get("code") == null && data.get("object") == null) { |
| 115 |
data.put("code", source.getAttribute("code")); |
| 116 |
} |
| 117 |
} |
| 118 |
//push all attributes to map |
| 119 |
NamedNodeMap atts = source.getAttributes(); |
| 120 |
for (int i = 0; i < atts.getLength(); i++) { |
| 121 |
String name = atts.item(i).getNodeName(); |
| 122 |
String value = atts.item(i).getTextContent(); |
| 123 |
if (name.trim().equalsIgnoreCase("width") |
| 124 |
|| name.trim().equalsIgnoreCase("height")) { |
| 125 |
value = sanitizeSize(value); |
| 126 |
} |
| 127 |
data.put(name, value); |
| 128 |
} |
| 129 |
return new PluginParameters(data); |
| 130 |
} |
| 131 |
|
| 132 |
private List<Element> findParams() { |
| 133 |
return AppletExtractor.findElements(PARAMS, source, new ElementValidator() { |
| 134 |
@Override |
| 135 |
public boolean isElementValid(Element e) { |
| 136 |
return true; |
| 137 |
} |
| 138 |
}); |
| 139 |
} |
| 140 |
|
| 141 |
private String getMain() { |
| 142 |
String s = source.getAttribute("code"); |
| 143 |
if (s == null || s.trim().length() == 0) { |
| 144 |
List<Element> found = findParams(); |
| 145 |
for (Element element : found) { |
| 146 |
if (element.getAttribute("name").equalsIgnoreCase("code")) { |
| 147 |
s = element.getAttribute("value"); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
if (s == null || s.trim().length() == 0) { |
| 153 |
s = source.getAttribute("classid"); |
| 154 |
if (s != null && s.contains(":")) { |
| 155 |
s = s.split(":")[1]; |
| 156 |
} |
| 157 |
} |
| 158 |
return s; |
| 159 |
} |
| 160 |
|
| 161 |
private String getArchives() { |
| 162 |
String s = source.getAttribute("archive"); |
| 163 |
if (s == null || s.trim().length() == 0) { |
| 164 |
List<Element> found = findParams(); |
| 165 |
for (Element element : found) { |
| 166 |
if (element.getAttribute("name").equalsIgnoreCase("archive")) { |
| 167 |
s = element.getAttribute("value"); |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
return s; |
| 173 |
} |
| 174 |
|
| 175 |
static String sanitizeSize(String attribute) { |
| 176 |
if (attribute == null) { |
| 177 |
return "1"; |
| 178 |
} |
| 179 |
//remove all nondigits |
| 180 |
return attribute.replaceAll("[^0-9]+", ""); |
| 181 |
} |
| 182 |
|
| 183 |
} |