| 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.io.IOException; |
| 40 |
import java.io.InputStream; |
| 41 |
import java.lang.reflect.Method; |
| 42 |
import java.net.URL; |
| 43 |
import java.util.LinkedList; |
| 44 |
import java.util.List; |
| 45 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 46 |
import javax.xml.parsers.ParserConfigurationException; |
| 47 |
import net.sourceforge.jnlp.JNLPFile; |
| 48 |
import net.sourceforge.jnlp.OptionsDefinitions; |
| 49 |
import net.sourceforge.jnlp.ParseException; |
| 50 |
import net.sourceforge.jnlp.Parser; |
| 51 |
import net.sourceforge.jnlp.ParserSettings; |
| 52 |
import net.sourceforge.jnlp.cache.UpdatePolicy; |
| 53 |
import net.sourceforge.jnlp.runtime.JNLPRuntime; |
| 54 |
import net.sourceforge.jnlp.runtime.Translator; |
| 55 |
import net.sourceforge.jnlp.util.logging.OutputController; |
| 56 |
import org.w3c.dom.Document; |
| 57 |
import org.w3c.dom.Element; |
| 58 |
import org.w3c.dom.Node; |
| 59 |
import org.w3c.dom.NodeList; |
| 60 |
import org.xml.sax.SAXException; |
| 61 |
|
| 62 |
/** |
| 63 |
* This class is taking HTML document url as input, try to sanitize with |
| 64 |
* tagsoup, if availale - and then finds and applet declarations here. Then it |
| 65 |
* returns first, selected or last one as PluginBridge instance. See parse |
| 66 |
* method. |
| 67 |
*/ |
| 68 |
public class AppletExtractor { |
| 69 |
|
| 70 |
private final URL html; |
| 71 |
private static final String[] APPLETS = new String[]{ |
| 72 |
"applet", "APPLET", "Applet", |
| 73 |
"object", "OBJECT", "Object", |
| 74 |
"embed", "EMBED", "Embed"}; |
| 75 |
private final ParserSettings ps; |
| 76 |
|
| 77 |
public AppletExtractor(URL html) { |
| 78 |
this(html, null); |
| 79 |
} |
| 80 |
public AppletExtractor(URL html, ParserSettings ps) { |
| 81 |
JNLPRuntime.saveHistory(html.toExternalForm()); |
| 82 |
this.html = html; |
| 83 |
this.ps = ps; |
| 84 |
} |
| 85 |
|
| 86 |
public URL getHtml() { |
| 87 |
return html; |
| 88 |
} |
| 89 |
|
| 90 |
private InputStream cleanStreamIfPossible(InputStream is) { |
| 91 |
try { |
| 92 |
if (ps != null && ps.isMalformedXmlAllowed()){ |
| 93 |
Object parser = Parser.getParserInstance(ps); |
| 94 |
Method m = parser.getClass().getMethod("xmlizeInputStream", InputStream.class); |
| 95 |
return (InputStream) m.invoke(null, is); |
| 96 |
} else { |
| 97 |
OutputController.getLogger().log(OutputController.Level.WARNING_ALL, Translator.R("TAGSOUPhtmlNotUsed", OptionsDefinitions.OPTIONS.XML.option)); |
| 98 |
} |
| 99 |
} catch (Exception ex) { |
| 100 |
OutputController.getLogger().log(OutputController.Level.WARNING_ALL, Translator.R("TAGSOUPhtmlBroken")); |
| 101 |
OutputController.getLogger().log(ex); |
| 102 |
} |
| 103 |
return is; |
| 104 |
} |
| 105 |
|
| 106 |
public List<Element> findAppletsOnPage() { |
| 107 |
try{ |
| 108 |
return findAppletsOnPageImpl(openDocument(cleanStreamIfPossible(JNLPFile.openURL(html, null, UpdatePolicy.ALWAYS)))); |
| 109 |
} catch (SAXException sex) { |
| 110 |
throw new RuntimeException(new ParseException(sex)); |
| 111 |
} catch (IOException | ParserConfigurationException ex) { |
| 112 |
throw new RuntimeException(ex); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private List<Element> findAppletsOnPageImpl(Document doc) throws ParserConfigurationException, SAXException, IOException { |
| 117 |
OutputController.getLogger().log("Root element :" + doc.getDocumentElement().getNodeName()); |
| 118 |
//search for applets |
| 119 |
//search for embed/object |
| 120 |
//<embed type="application/x-java-applet" |
| 121 |
//<object type="application/x-java-applet" |
| 122 |
//warning all searches are case sensitive |
| 123 |
return findElements(APPLETS, doc.getDocumentElement(), new ElementValidator() { |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isElementValid(Element e) { |
| 127 |
return isApplet(e); |
| 128 |
} |
| 129 |
}); |
| 130 |
} |
| 131 |
|
| 132 |
private Document openDocument(InputStream is) throws SAXException, ParserConfigurationException, IOException { |
| 133 |
OutputController.getLogger().log("Reading " + html.toExternalForm()); |
| 134 |
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); |
| 135 |
doc.getDocumentElement().normalize(); |
| 136 |
return doc; |
| 137 |
} |
| 138 |
|
| 139 |
//warning, even application/x-java-applet;jpi-version=1.5.0_07 and more blah blah can be in type, so of embed/object start with |
| 140 |
//search for |
| 141 |
//OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" |
| 142 |
//<object codetype="application/x-java-applet" height="120" width="81" |
| 143 |
private static boolean isApplet(Element eElement) { |
| 144 |
if (eElement.getNodeName().toLowerCase().equals("applet")) { |
| 145 |
return true; |
| 146 |
} else { |
| 147 |
String type = eElement.getAttribute("type"); |
| 148 |
String codeType = eElement.getAttribute("codetype"); |
| 149 |
String classid = eElement.getAttribute("classid"); |
| 150 |
if ((type != null && type.toLowerCase().contains("application/x-java-applet")) |
| 151 |
|| (codeType != null && codeType.toLowerCase().contains("application/x-java-applet")) |
| 152 |
|| (classid != null && classid.equalsIgnoreCase("clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"))) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
} |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
static List<Element> findElements(String[] elements, Element doc, ElementValidator elementValidator) { |
| 160 |
List<Element> found = new LinkedList(); |
| 161 |
for (String key : elements) { |
| 162 |
NodeList nList = doc.getElementsByTagName(key); |
| 163 |
for (int temp = 0; temp < nList.getLength(); temp++) { |
| 164 |
|
| 165 |
Node nNode = nList.item(temp); |
| 166 |
OutputController.getLogger().log("Found in html: " + nNode.getNodeName()); |
| 167 |
if (nNode.getNodeType() == Node.ELEMENT_NODE) { |
| 168 |
Element eElement = (Element) nNode; |
| 169 |
if (elementValidator.isElementValid(eElement)) { |
| 170 |
found.add(eElement); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
return found; |
| 176 |
} |
| 177 |
|
| 178 |
} |