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

import org.w3c.dom.*;

public final class TodoWeblogSelectionElement extends TodoDefaultElement {

    public enum Type {
	RECENT_COUNT,
	MONTH_YEAR,
	MONTH_MONTH,
	CATEGORY_CODE;
    }

    public static class Factory extends TodoElement.Factory {
	final Type t;
	public Factory(Type t) {
	    super();
	    this.t = t;
	}
	@Override
	public TodoWeblogSelectionElement newItem(Element node,
						  Context ctx,
						  TodoItem caller) {
	    return new TodoWeblogSelectionElement(t, node, ctx, caller);
	}
    }

    final Type t;

    public TodoWeblogSelectionElement(Type t,
				      Element node,
				      Context ctx,
				      TodoItem caller) {
	super(node, ctx, caller);
	this.t = t;
    }

    @Override
    public void handleNodeOnly() {
	if ( ctx.wsc == null )
	    throw new IllegalStateException("weblog-selection element encountered with no weblog selection state");
	// String lang = LangHelper.getLangRec(node);
	String str;
	switch ( t ) {
	case RECENT_COUNT:
	    if ( ! ( ctx.wsc instanceof Context.WeblogRecentSelectionContext ) )
		throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog recent selection state");
	    str = String.format("%d", ((Context.WeblogRecentSelectionContext)(ctx.wsc)).count );
	    break;
	case MONTH_YEAR:
	    if ( ! ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) )
		throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog month selection state");
	    str = ((Context.WeblogMonthSelectionContext)(ctx.wsc)).year;
	    break;
	case MONTH_MONTH:
	    if ( ! ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) )
		throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog month selection state");
	    str = ((Context.WeblogMonthSelectionContext)(ctx.wsc)).month;
	    break;
	case CATEGORY_CODE:
	    if ( ! ( ctx.wsc instanceof Context.WeblogCategorySelectionContext ) )
		throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog category selection state");
	    str = ((Context.WeblogCategorySelectionContext)(ctx.wsc)).code;
	    break;
	default:
	    throw new AssertionError("unknown type");
	}
	Node txt = ctx.doc.createTextNode(str);
	node.getParentNode().replaceChild(txt, node);
    }

}