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.parsers.DocumentBuilder; import javax.xml.xpath.*; import org.w3c.dom.*; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; // import org.xml.sax.EntityResolver; 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 DocumentBuilder db = DamlEngine.GetDocumentBuilder.db; final DOMImplementationLS domi = (DOMImplementationLS)(db.getDOMImplementation()); LSSerializer ser = domi.createLSSerializer(); ser.getDomConfig().setParameter("xml-declaration", false); 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,title,title_xml,content,sha1) VALUES (?,?,?,?,?::xml,?::xml,?)"); final PreparedStatement insertCSt = conn.prepareStatement("INSERT INTO entries(id,edate,lang,title,title_xml,content,sha1,cdate) VALUES (?,?,?,?,?::xml,?::xml,?,?::timestamptz)"); final PreparedStatement updateSt = conn.prepareStatement("UPDATE entries SET (edate,mdate,lang,title,title_xml,content,sha1)=(?,DEFAULT,?,?,?::xml,?::xml,?) WHERE id=?"); final PreparedStatement clearCatSt = conn.prepareStatement("DELETE FROM incat WHERE id=?"); final PreparedStatement setCatSt = conn.prepareStatement("INSERT INTO incat(id,code) VALUES (?,?)"); Document doc = db.parse(in); 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