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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.lobobrowser.html.domimpl.ElementImpl;
import org.lobobrowser.html.domimpl.HTMLElementImpl;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.HtmlParser;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.style.AbstractCSS2Properties;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.html2.HTMLDocument;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TestParseIHM.java
*
* Created on 17 mai 2010, 13:25:49
*/
/**
*
* @author leef6251
*/
public class TestParseIHM extends javax.swing.JFrame {
/** Creates new form TestParseIHM */
public TestParseIHM() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jEditorPane2 = new javax.swing.JEditorPane();
jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
panel = new HtmlPanel();
panel.setPreferredWidth(800);
this.getContentPane().add(panel);
uacontext = new SimpleUserAgentContext();
uacontext.setExternalCSSEnabled(true);
rcontext = new SimpleHtmlRendererContext(panel, uacontext);
panel.setHtml("", TEST_URI, rcontext);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
startTime = System.currentTimeMillis();
//GEN-FIRST:event_jButton1ActionPerformed
//GEN-FIRST:event_jButton1ActionPerformed
uacontext.setExternalCSSEnabled(true);
uacontext.setScriptingEnabled(false);
builder = new DocumentBuilderImpl(uacontext);
Reader reader = getReader(TEST_URI);
InputSource is = new InputSourceImpl(reader, "http://www.holdemresources.net");
Document document = builder.parse(is);
DOMSerializer serializerin = new DOMSerializer();
StringWriter in = new StringWriter();
serializerin.serialize(document, in);
String inp = in.toString();
testXPath2(document);
}
catch (IOException ex) {
Logger.getLogger(TestParseIHM.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(TestParseIHM.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void testXPath2(Document document) {
System.out.println("XPATH2");
try {
/* On initialise un nouvel élément avec l'élément racine du
document. */
Text text = null;
String req = null;
String req2 = null;
int u = 0;
dores = builder.newDocument();
Element racine = dores.createElement("html");
Element head = dores.createElement("head");
Element body = dores.createElement("body");
Element link = dores.createElement("link");
link.setAttribute("href", "style.css");
link.setAttribute("media", "all");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
head.appendChild(link);
/* Quand je définis le style ainsi directement dans le document cible - dores, Cobra affiche correctement le style.
Element style = dores.createElement("style");
style.setAttribute("type", "text/css");
String desc = "table{border-collapse:collapse;}table, th, td{border: 1px solid black;}td{padding:15px;}td{text-align:left;}";
Text styleDefined = dores.createTextNode(desc);
style.appendChild(styleDefined);
head.appendChild(style);
*/
racine.appendChild(head);
XPath xpa = XPathFactory.newInstance().newXPath();
/* Recherche de la liste des tables */
NodeList nodeList = (NodeList) xpa.evaluate("//table[@class='simple']", document, XPathConstants.NODESET);
int length = nodeList.getLength();
/* On récupère tous les noeuds répondant au chemin //table */
Element noeudCourant = null;
for (int i = 0; i < length; i++) {
System.out.println("i: " + i);
noeudCourant = (Element) nodeList.item(i);
System.out.println("element " + i + ": " + noeudCourant.getNodeName() + " " + noeudCourant.getFirstChild().getNodeValue());
Element table = dores.createElement(noeudCourant.getNodeName());
req = "./tr";
NodeList nodeListRow = (NodeList) xpa.evaluate(req, noeudCourant, XPathConstants.NODESET);
int lengthRow = nodeListRow.getLength();
for (int j = 1; j < lengthRow; j++) {
System.out.println("j: " + j);
noeudCourant = (Element) nodeListRow.item(j);
System.out.println("element " + j + ": " + noeudCourant.getNodeName() + " " + noeudCourant.getFirstChild().getNodeValue());
Element ligne = dores.createElement(noeudCourant.getNodeName());
req2 = "./td";
NodeList nodeListCell = (NodeList) xpa.evaluate(req2, noeudCourant, XPathConstants.NODESET);
int lengthCell = nodeListCell.getLength();
for (int k = 0; k < lengthCell; k++) {
System.out.println("k: " + k);
noeudCourant = (Element) nodeListCell.item(k);
HTMLElementImpl htmle = (HTMLElementImpl) noeudCourant;
AbstractCSS2Properties abs = htmle.getCurrentStyle();
String col = abs.getBackgroundColor();
String col2 = abs.getDisplay();
Element cellule = dores.createElement(noeudCourant.getNodeName());
if (!(noeudCourant.hasChildNodes())) {
text = dores.createTextNode("");
} else {
text = dores.createTextNode(noeudCourant.getFirstChild().getTextContent());
}
System.out.println("text: " + text.getNodeValue());
cellule.appendChild(text);
ligne.appendChild(cellule);
}
table.appendChild(ligne);
}
body.appendChild(table);
}
racine.appendChild(body);
dores.appendChild(racine);
DOMSerializer serializer = new DOMSerializer();
StringWriter out = new StringWriter();
serializer.serialize(dores, out);
String res = out.toString();
panel.setHtml(res, "http://www.holdemresources.net", rcontext);
pack();
endTime = System.currentTimeMillis();
System.out.println("durée: " + (endTime - startTime));
} catch (IOException ex) {
Logger.getLogger(TestParseIHM.class.getName()).log(Level.SEVERE, null, ex);
} catch (XPathExpressionException ex) {
Logger.getLogger(ParseImagesTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestParseIHM().setVisible(true);
}
});
}
public Reader getReader(String url) {
Reader reader = null;
if (url.startsWith("http://")) {
URL u = null;
InputStream in = null;
try {
u = new URL(url);
in = u.openConnection().getInputStream();
reader = new InputStreamReader(in, "ISO-8859-1");
} catch (MalformedURLException ex) {
Logger.getLogger(ParseImagesTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestParseIHM.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
try {
reader = new FileReader(url);
} catch(FileNotFoundException fnfe) {
Logger.getLogger(ParseImagesTest.class.getName()).log(Level.SEVERE, null, fnfe);
}
}
return reader;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JEditorPane jEditorPane2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private HtmlPanel panel;
// End of variables declaration//GEN-END:variables
private static final String TEST_URI = "http://www.holdemresources.net/hr/sngs/icmcalculator.html?action=calculate&bb=200&sb=100&ante=0&structure=0.5%2C0.3%2C0.2&s1=1500&s2=1400&s3=1600&s4=2000&s5=1000&s6=3800&s7=2200&s8=&s9=";
DocumentBuilderImpl builder = null;
org.w3c.dom.Document dores = null;
private SimpleUserAgentContext uacontext;
private SimpleHtmlRendererContext rcontext;
long startTime = 0;
long endTime = 0;
} |
Partager