From 4f26d86d308762f4e1a16806042e8bb4c096c2f2 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 9 Oct 2014 16:52:22 +0200 Subject: Support element for automatic calendar generation. --- org/madore/damlengine/TodoElement.java | 1 + .../damlengine/TodoWeblogMonthsCalendar.java | 114 +++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 org/madore/damlengine/TodoWeblogMonthsCalendar.java diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index 0b2baf5..83275ad 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -70,6 +70,7 @@ public abstract class TodoElement extends TodoItem { damlFactories.put("weblog-selection-cat-name", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.CATEGORY_NAME)); damlFactories.put("weblog-selection-single-number", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.SINGLE_NUMBER)); damlFactories.put("weblog-selection-single-title", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.SINGLE_TITLE)); + damlFactories.put("weblog-months-calendar", new TodoWeblogMonthsCalendar.Factory()); damlFactories.put("cut-here", new TodoCutHere.Factory()); } diff --git a/org/madore/damlengine/TodoWeblogMonthsCalendar.java b/org/madore/damlengine/TodoWeblogMonthsCalendar.java new file mode 100644 index 0000000..d9e0573 --- /dev/null +++ b/org/madore/damlengine/TodoWeblogMonthsCalendar.java @@ -0,0 +1,114 @@ +package org.madore.damlengine; + +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.w3c.dom.*; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSParser; +import org.w3c.dom.ls.LSInput; + +public final class TodoWeblogMonthsCalendar extends TodoDefaultElement { + + public static final String[] monthAbbr = { + null, + "Jan", "Feb", "Mar", "Apr", + "May", "Jun", "Jul", "Aug", + "Sep", "Oct", "Nov", "Dec", + }; + + public static class Factory extends TodoElement.Factory { + @Override + public TodoWeblogMonthsCalendar newItem(Element node, + Context ctx, + TodoItem caller) { + return new TodoWeblogMonthsCalendar(node, ctx, caller); + } + } + + public TodoWeblogMonthsCalendar(Element node, + Context ctx, + TodoItem caller) { + super(node, ctx, caller); + } + + @Override + public void handleNodeOnly() { + Element table = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "table"); + node.getParentNode().replaceChild(table, node); + table.setAttributeNS(null, "border", "1"); + table.appendChild(ctx.doc.createTextNode("\n\n")); + String targetStdDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"); + try { + final Connection conn = WeblogDatabaseConnection.getConnection(); + final PreparedStatement selSt + = conn.prepareStatement("SELECT min(edate) , max(edate) FROM entries"); + final ResultSet selRes = selSt.executeQuery(); + int minedateYear, minedateMonth, maxedateYear, maxedateMonth; + if ( selRes.next() ) { + String minedate = selRes.getString(1); + String maxedate = selRes.getString(2); + Matcher matcher; + if ( (matcher=Pattern.compile("(\\d{4})-(\\d{2})-\\d{2}").matcher(minedate)).matches() ) { + minedateYear = Integer.parseInt(matcher.group(1)); + minedateMonth = Integer.parseInt(matcher.group(2)); + } else + throw new IllegalStateException("entry date field must be of the form YYYY-MM-DD"); + if ( (matcher=Pattern.compile("(\\d{4})-(\\d{2})-\\d{2}").matcher(maxedate)).matches() ) { + maxedateYear = Integer.parseInt(matcher.group(1)); + maxedateMonth = Integer.parseInt(matcher.group(2)); + } else + throw new IllegalStateException("entry date field must be of the form YYYY-MM-DD"); + } else + throw new IllegalStateException("failed to extract bounding dates"); + for ( int year = maxedateYear ; year >= minedateYear ; year-- ) { + Element tr = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "tr"); + table.appendChild(tr); + table.appendChild(ctx.doc.createTextNode("\n\n")); + Element th = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "th"); + tr.appendChild(th); + th.appendChild(ctx.doc.createTextNode(Integer.toString(year))); + tr.appendChild(ctx.doc.createTextNode("\n")); + if ( year==minedateYear && minedateMonth > 1 ) { + Element td = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "td"); + td.setAttributeNS(null, "colspan", + Integer.toString(minedateMonth-1)); + tr.appendChild(td); + tr.appendChild(ctx.doc.createTextNode("\n")); + } + for ( int month = (year==minedateYear?minedateMonth:1) ; + month <= (year==maxedateYear?maxedateMonth:12) ; + month++ ) { + Element td = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "td"); + tr.appendChild(td); + if ( ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) + && year == Integer.parseInt(((Context.WeblogMonthSelectionContext)(ctx.wsc)).year) + && month == Integer.parseInt(((Context.WeblogMonthSelectionContext)(ctx.wsc)).month) ) + td.setAttributeNS(null, "class", "weblog-calendar-selected-month"); + Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); + td.appendChild(a); + a.setAttributeNS(null, "href", + targetStdDir + Integer.toString(year) + "-" + + String.format("%02d",month) + ".html"); + a.appendChild(ctx.doc.createTextNode(monthAbbr[month] + " " + Integer.toString(year))); + tr.appendChild(ctx.doc.createTextNode("\n")); + } + if ( year==maxedateYear && maxedateMonth < 12 ) { + Element td = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "td"); + td.setAttributeNS(null, "colspan", + Integer.toString(12-maxedateMonth)); + tr.appendChild(td); + tr.appendChild(ctx.doc.createTextNode("\n")); + } + } + } catch (SQLException e) { + } + } + +} -- cgit v1.2.3 From 240a3c4d99aba9aca244033425569ce56f7dc0c8 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 9 Oct 2014 16:57:58 +0200 Subject: Might as well add a class attribute on the table as well. --- org/madore/damlengine/TodoWeblogMonthsCalendar.java | 1 + 1 file changed, 1 insertion(+) diff --git a/org/madore/damlengine/TodoWeblogMonthsCalendar.java b/org/madore/damlengine/TodoWeblogMonthsCalendar.java index d9e0573..044f05f 100644 --- a/org/madore/damlengine/TodoWeblogMonthsCalendar.java +++ b/org/madore/damlengine/TodoWeblogMonthsCalendar.java @@ -43,6 +43,7 @@ public final class TodoWeblogMonthsCalendar extends TodoDefaultElement { Element table = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "table"); node.getParentNode().replaceChild(table, node); table.setAttributeNS(null, "border", "1"); + table.setAttributeNS(null, "class", "weblog-calendar"); table.appendChild(ctx.doc.createTextNode("\n\n")); String targetStdDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"); try { -- cgit v1.2.3 From 8ed1ac19770dca2b5c37c529fe4420068e107f77 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 9 Oct 2014 17:27:58 +0200 Subject: Allow substitution of literal or numeric month in weblog month selection. --- org/madore/damlengine/TodoElement.java | 4 ++-- .../damlengine/TodoWeblogMonthsCalendar.java | 22 +++++++++++++++++++++ .../damlengine/TodoWeblogSelectionElement.java | 23 ++++++++++++++-------- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index 83275ad..acccac0 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -64,8 +64,8 @@ public abstract class TodoElement extends TodoItem { damlFactories.put("weblog-select", new TodoWeblogSelectElement.Factory()); damlFactories.put("weblog-index-select", new TodoWeblogIndexSelectElement.Factory()); damlFactories.put("weblog-selection-recent-count", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.RECENT_COUNT)); - damlFactories.put("weblog-selection-month-year", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.MONTH_YEAR)); - damlFactories.put("weblog-selection-month-month", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.MONTH_MONTH)); + damlFactories.put("weblog-selection-month-numeric", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.MONTH_NUMERIC)); + damlFactories.put("weblog-selection-month-literal", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.MONTH_LITERAL)); damlFactories.put("weblog-selection-cat-code", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.CATEGORY_CODE)); damlFactories.put("weblog-selection-cat-name", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.CATEGORY_NAME)); damlFactories.put("weblog-selection-single-number", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.SINGLE_NUMBER)); diff --git a/org/madore/damlengine/TodoWeblogMonthsCalendar.java b/org/madore/damlengine/TodoWeblogMonthsCalendar.java index 044f05f..481f05a 100644 --- a/org/madore/damlengine/TodoWeblogMonthsCalendar.java +++ b/org/madore/damlengine/TodoWeblogMonthsCalendar.java @@ -23,6 +23,28 @@ public final class TodoWeblogMonthsCalendar extends TodoDefaultElement { "Sep", "Oct", "Nov", "Dec", }; + public static final String[] monthNamesEn = { + null, + "January", "February", "March", "April", + "May", "June", "July", "August", + "September", "October", "November", "December", + }; + + public static final String[] monthNamesFr = { + null, + "janvier", "f\u00e9vrier", "mars", "avril", + "mai", "juin", "juillet", "ao\u00fbt", + "septembre", "octobre", "novembre", "d\u00e9cembre", + }; + + public static final Map monthNames; + + static { + monthNames = new HashMap(); + monthNames.put("en", monthNamesEn); + monthNames.put("fr", monthNamesFr); + } + public static class Factory extends TodoElement.Factory { @Override public TodoWeblogMonthsCalendar newItem(Element node, diff --git a/org/madore/damlengine/TodoWeblogSelectionElement.java b/org/madore/damlengine/TodoWeblogSelectionElement.java index c9088eb..637bc85 100644 --- a/org/madore/damlengine/TodoWeblogSelectionElement.java +++ b/org/madore/damlengine/TodoWeblogSelectionElement.java @@ -12,8 +12,8 @@ public final class TodoWeblogSelectionElement extends TodoDefaultElement { public enum Type { RECENT_COUNT, - MONTH_YEAR, - MONTH_MONTH, + MONTH_NUMERIC, + MONTH_LITERAL, CATEGORY_NAME, CATEGORY_CODE, SINGLE_NUMBER, @@ -114,19 +114,26 @@ public final class TodoWeblogSelectionElement extends TodoDefaultElement { newNode = ctx.doc.createTextNode(str); node.getParentNode().replaceChild(newNode, node); break; - case MONTH_YEAR: + case MONTH_NUMERIC: if ( ! ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) ) - throw new IllegalStateException("weblog-selection-month-year element encountered while not in weblog month selection state"); - str = ((Context.WeblogMonthSelectionContext)(ctx.wsc)).year; + throw new IllegalStateException("weblog-selection-month-numeric element encountered while not in weblog month selection state"); + str = ((Context.WeblogMonthSelectionContext)(ctx.wsc)).year + "-" + + ((Context.WeblogMonthSelectionContext)(ctx.wsc)).month; newNode = ctx.doc.createTextNode(str); node.getParentNode().replaceChild(newNode, node); break; - case MONTH_MONTH: + case MONTH_LITERAL: if ( ! ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) ) - throw new IllegalStateException("weblog-selection-month-month element encountered while not in weblog month selection state"); - str = ((Context.WeblogMonthSelectionContext)(ctx.wsc)).month; + throw new IllegalStateException("weblog-selection-month-literal element encountered while not in weblog month selection state"); + int mnum = Integer.parseInt(((Context.WeblogMonthSelectionContext)(ctx.wsc)).month); + String lang = LangHelper.getLangRec(node); + String[] mnames = TodoWeblogMonthsCalendar.monthNames.get(lang); + if ( mnames == null ) + mnames = TodoWeblogMonthsCalendar.monthNamesEn; + str = mnames[mnum] + " " + ((Context.WeblogMonthSelectionContext)(ctx.wsc)).year; newNode = ctx.doc.createTextNode(str); node.getParentNode().replaceChild(newNode, node); + LangHelper.setLangRec(node, lang); break; case CATEGORY_CODE: if ( ! ( ctx.wsc instanceof Context.WeblogCategorySelectionContext ) ) -- cgit v1.2.3