package org.madore.damlengine; import java.util.regex.Pattern; import java.io.InputStream; import java.security.MessageDigest; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.xml.xpath.*; import org.w3c.dom.*; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSParser; import org.w3c.dom.ls.LSSerializer; import org.w3c.dom.ls.LSInput; public final class WeblogPopulate { private WeblogPopulate() { // Forbid instantiation throw new AssertionError("WeblogPopulate cannot be instantiated"); } public static final String toHex(byte[] digest) { StringBuilder sb = new StringBuilder(); for ( byte b : digest ) sb.append(String.format("%02x", b)); return sb.toString(); } public static void populate(InputStream in) throws Exception { final DOMImplementationLS domils = (DOMImplementationLS)(DamlEngine.IncantDOM.getDOMI()); LSSerializer ser = domils.createLSSerializer(); ser.getDomConfig().setParameter("xml-declaration", false); LSParser par = domils.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); par.getDomConfig().setParameter("resource-resolver", new Resolver()); par.getDomConfig().setParameter("http://xml.org/sax/features/validation", true); par.getDomConfig().setParameter("http://xml.org/sax/features/namespaces", true); par.getDomConfig().setParameter("http://apache.org/xml/properties/internal/error-handler", new DamlEngine.SelectiveErrorHandler()); MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); final Connection conn = WeblogDatabaseConnection.getConnection(); final PreparedStatement checkSt = conn.prepareStatement("SELECT sha1 FROM entries WHERE id=?"); final PreparedStatement insertNSt = conn.prepareStatement("INSERT INTO entries(id,edate,lang,do_single_page,title,title_xml,content,sha1) VALUES (?,?,?,?,?,?::xml,?::xml,?)"); final PreparedStatement insertCSt = conn.prepareStatement("INSERT INTO entries(id,edate,lang,do_single_page,title,title_xml,content,sha1,cdate) VALUES (?,?,?,?,?,?::xml,?::xml,?,?::timestamptz)"); final PreparedStatement updateNSt = conn.prepareStatement("UPDATE entries SET (edate,mdate,lang,do_single_page,title,title_xml,content,sha1)=(?,DEFAULT,?,?,?,?::xml,?::xml,?) WHERE id=?"); final PreparedStatement updateCSt = conn.prepareStatement("UPDATE entries SET (edate,mdate,lang,do_single_page,title,title_xml,content,sha1,cdate)=(?,DEFAULT,?,?,?,?::xml,?::xml,?,?::timestamptz) WHERE id=?"); final PreparedStatement checkDateSt = conn.prepareStatement("SELECT to_char(cdate,'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') FROM entries WHERE id=?"); final PreparedStatement clearCatSt = conn.prepareStatement("DELETE FROM incat WHERE id=?"); final PreparedStatement setCatSt = conn.prepareStatement("INSERT INTO incat(id,code) VALUES (?,?)"); LSInput input = domils.createLSInput(); input.setByteStream(in); Document doc = par.parse(input); XPathFactory xpf = XPathFactory.newInstance(); XPath xp = xpf.newXPath(); xp.setNamespaceContext(new DamlEngine.DamlNSMapping()); XPathExpression expr = xp.compile("//d:weblog/d:entry"); XPathExpression texpr = xp.compile("d:title"); NodeList entries = (NodeList)(expr.evaluate(doc, XPathConstants.NODESET)); for ( int i=0 ; i