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

import org.w3c.dom.*;

public class TodoWXrefAttr extends TodoAttr {

    public static class Factory extends TodoAttr.Factory {
	@Override
	public TodoWXrefAttr newItem(Attr attr, Element owner,
				     Context ctx,
				     TodoItem caller) {
	    return new TodoWXrefAttr(attr, owner, ctx, caller);
	}
    }

    public TodoWXrefAttr(Attr attr, Element owner,
			 Context ctx,
			 TodoItem caller) {
	super(attr, owner, ctx, caller);
    }

    @Override
    public void handle() {
	String wxrefStr = attr.getValue();
	if ( wxrefStr.equals("##weblog-selection-older")
	     || wxrefStr.equals("##weblog-selection-newer") ) {
	    if ( ctx.wsc == null )
		throw new IllegalStateException("wxref attribute encountered with no weblog selection state");
	    final int trgt = ctx.wsc.sel.first()
		+ (wxrefStr.equals("##weblog-selection-older") ? -1 : 1);
	    WeblogSummary sum = WeblogSummary.getSummary(ctx.dc);
	    if ( sum != null && sum.entries != null
		 && sum.entries.containsKey(new Integer(trgt)) ) {
		WeblogSummary.EntrySummary ent
		    = sum.entries.get(new Integer(trgt));
		String target = "#d." + ent.date + "."
		    + String.format("%04d", ent.id);
		this.owner.removeAttributeNode(this.attr);
		Attr newAttr
		    = ctx.doc.createAttributeNS(DamlEngine.DAML_NS, "d:wref");
		newAttr.setValue(target);
		this.owner.setAttributeNodeNS(newAttr);
		if ( ! this.owner.hasAttributeNS(DamlEngine.DAML_NS, "d:wrefcat") ) {
		    if ( ctx.wsc instanceof Context.WeblogMonthSelectionContext
			 || ctx.wsc instanceof Context.WeblogRecentSelectionContext )
			this.owner.setAttributeNS(DamlEngine.DAML_NS, "d:wrefcat", "@month");
		}
		this.ownerDeque.registerAtStart(new TodoWrefAttr(newAttr, this.owner,
								 this.ctx, this));
	    } else {
		String target = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"))
		    + "weblog-index.html";
		this.owner.removeAttributeNode(this.attr);
		Attr newAttr = ctx.doc.createAttributeNS(null, "href");
		newAttr.setValue(target);
		this.owner.setAttributeNodeNS(newAttr);
	    }
	} else
	    throw new IllegalArgumentException("couldn't understand argument to wxref attribute");
    }

}