summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/WeblogPopulate.java
blob: 24c61dc74258beb692a4747b92ba577072c30cf7 (plain)
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
package org.madore.damlengine;

import java.util.regex.Pattern;
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 main(String[] args)
	throws Exception {

	final DocumentBuilder db = DamlEngine.GetDocumentBuilder.db;

	if ( args.length == 0 ) {
	    System.err.println("expecting filename as argument");
	}

	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 insertSt
	    = conn.prepareStatement("INSERT INTO entries(id,edate,lang,title,title_xml,content,sha1) VALUES (?,?,?,?,?::xml,?::xml,?)");
	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 (?,?)");

	for (String fname : args) {
	    Document doc = db.parse(fname);
	    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<entries.getLength() ; i++ ) {
		Element ent = (Element)(entries.item(i));
		String idStr = ent.getAttributeNS(null, "number");
		if ( ! Pattern.matches("^\\d{4}$", idStr) )
		    throw new IllegalArgumentException("entry number attribute must be of the form NNNN");
		int id = Integer.parseInt(idStr);
		String date = ent.getAttributeNS(null, "date");
		if ( ! Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", date) )
		    throw new IllegalArgumentException("entry date attribute must be of the form YYYY-MM-DD");
		String catStr = ent.getAttributeNS(null, "cat");
		String[] catList = catStr.split("\\s+");
		String lang = LangHelper.getLangRec(ent);
		String content = ser.writeToString(ent);
		sha1.reset();
		String digest = toHex(sha1.digest(content.getBytes("UTF-8")));
		
		checkSt.setInt(1, id);
		ResultSet checkRes = checkSt.executeQuery();
		boolean exists = checkRes.next();
		if ( exists && checkRes.getString(1).equals(digest) )
		    continue;
		Node titleNode = (Node)(texpr.evaluate(ent, XPathConstants.NODE));
		String titleTxt = (titleNode != null) ? titleNode.getTextContent() : null;
		String titleXml = (titleNode != null) ? ser.writeToString(titleNode) : null;
		conn.setAutoCommit(false);
		if ( exists ) {
		    System.err.println("Updating entry "+id);
		    updateSt.setString(1, date);
		    updateSt.setString(2, lang);
		    updateSt.setString(3, titleTxt);
		    updateSt.setString(4, titleXml);
		    updateSt.setString(5, content);
		    updateSt.setString(6, digest);
		    updateSt.executeUpdate();
		} else {
		    System.err.println("Registering entry "+id);
		    insertSt.setInt(1, id);
		    insertSt.setString(2, date);
		    insertSt.setString(3, lang);
		    insertSt.setString(4, titleTxt);
		    insertSt.setString(5, titleXml);
		    insertSt.setString(6, content);
		    insertSt.setString(7, digest);
		    insertSt.executeUpdate();
		}
		clearCatSt.setInt(1, id);
		clearCatSt.executeUpdate();
		for ( String cat : catList ) {
		    if ( ! cat.equals("") ) {
			setCatSt.setInt(1, id);
			setCatSt.setString(2, cat);
			setCatSt.executeUpdate();
		    }
		}
		conn.commit();
	    }
	}

	checkSt.close();
	insertSt.close();
	updateSt.close();
	clearCatSt.close();
	setCatSt.close();
	conn.close();

    }

}