summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoWeblogIndexSelectElement.java
blob: cf326d89cd68507b67e98eacb7f1427d8e432617 (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
package org.madore.damlengine;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
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 TodoWeblogIndexSelectElement extends TodoDefaultElement {

    public static class Factory extends TodoElement.Factory {
	@Override
	public TodoWeblogIndexSelectElement newItem(Element node,
						    Context ctx,
						    TodoItem caller) {
	    return new TodoWeblogIndexSelectElement(node, ctx, caller);
	}
    }

    public TodoWeblogIndexSelectElement(Element node,
					Context ctx,
					TodoItem caller) {
	super(node, ctx, caller);
    }

    @Override
    public void handleNodeOnly() {
	WeblogSummary wsum = WeblogSummary.getSummary(ctx.dc);
	if ( wsum == null || wsum.entries == null ) {
	    throw new IllegalStateException("weblog-index-select element encountered with no weblog summary available");
	}
	TreeSet<Integer> entlist = new TreeSet<Integer>(wsum.entries.keySet());
	Element ul0 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "ul");
	ul0.setAttributeNS(null, "id", "index");
	ul0.appendChild(ctx.doc.createTextNode("\n"));
	node.getParentNode().replaceChild(ul0, node);
	final DOMImplementationLS domi
	    = (DOMImplementationLS)ctx.doc.getImplementation();
	LSParser par = domi.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
	ArrayList<TodoElement> toProcess
	    = new ArrayList<TodoElement>(entlist.size()*2);
	Context ctx2 = ctx.clone();  ctx2.killA = true;
	String prevYandm = null;
	Element li0 = null;  Element ul = null;
	final String baseDir = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"));
	for ( Iterator<Integer> iter=entlist.descendingIterator() ; iter.hasNext() ; ) {
	    final WeblogSummary.EntrySummary ent = wsum.entries.get(iter.next());
	    final Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(ent.date);
	    if ( ! matcher.matches() )
		throw new IllegalArgumentException("entry "+ent.id+" has badly formed date");
	    final String yearStr = matcher.group(1);
	    final String monthStr = matcher.group(2);
	    final String dayStr = matcher.group(3);
	    final String numberStr = String.format("%04d",ent.id);
	    final String yandmStr = yearStr + "-" + monthStr;
	    final WeblogLink lk = new WeblogLink(yearStr, monthStr, dayStr,
						 numberStr, "",
						 ent.specialName);
	    if ( li0 == null || ul == null || ! yandmStr.equals(prevYandm) ) {
		li0 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "li");
		ul0.appendChild(li0);
		ul0.appendChild(ctx.doc.createTextNode("\n"));
		li0.setAttributeNS(null, "id", "d."+yandmStr);
		Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a");
		li0.appendChild(a);
		lk.setTypeMonth();
		a.setAttributeNS(null, "href", lk.getFile(baseDir));
		String mstr = TodoWeblogMonthsCalendar.monthAbbr[Integer.parseInt(monthStr)] + " " + yearStr;
		a.appendChild(ctx.doc.createTextNode(mstr));
		li0.appendChild(ctx.doc.createTextNode(":\n"));
		ul = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "ul");
		li0.appendChild(ul);
		li0.appendChild(ctx.doc.createTextNode("\n"));
		ul.appendChild(ctx.doc.createTextNode("\n"));
		prevYandm = yandmStr;
	    }
	    lk.setTypeStandard();
	    String target = lk.getTarget(baseDir);
	    Element li = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "li");
	    ul.appendChild(li);
	    ul.appendChild(ctx.doc.createTextNode("\n"));
	    li.setAttributeNS(null, "id", lk.getFragment());
	    li.setAttributeNS(null, "class", "weblog-index-entry");
	    LangHelper.setLangRec(li, ent.lang);
	    Element link;
	    if ( ent.titleXml == null ) {
		link = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a");
		li.appendChild(link);
		link.appendChild(ctx.doc.createTextNode(ent.date));
	    } else {
		li.appendChild(ctx.doc.createTextNode(ent.date+": "));
		link = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a");
		li.appendChild(link);
		LSInput input = domi.createLSInput();
		input.setStringData(ent.titleXml);
		Document temp = par.parse(input);
		Node newNode = ctx.doc.adoptNode(temp.getDocumentElement());
		String explicitLang = LangHelper.getLangNorec(newNode);
		if ( explicitLang != null )
		    LangHelper.setLangNorec(link, explicitLang);
		ArrayList<Node> childList = getChildList(newNode);
		for ( Node child : childList ) {
		    link.appendChild(child);
		    if ( child instanceof Element ) {
			TodoElement it
			    = TodoElement.getTodoElement((Element)child,
							 ctx2, this);
			toProcess.add(it);
		    }
		}
	    }
	    link.setAttributeNS(null, "href", target);
	}
	this.ownerDeque.registerAtStart(toProcess);
    }

}