summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoWeblogSelectionElement.java
blob: 3029f9f3eb65cff4f59fc6d439b046dce64e9081 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package org.madore.damlengine;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
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 TodoWeblogSelectionElement extends TodoDefaultElement {

    public enum Type {
	RECENT_COUNT,
	MONTH_NUMERIC,
	MONTH_LITERAL,
	CATEGORY_NAME,
	CATEGORY_CODE,
	SINGLE_NUMBER,
	SINGLE_TITLE;
    }

    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;
    }

    protected final static Map<String,Map<String,String>> categoryNames;

    static {
	categoryNames = new HashMap<String,Map<String,String>>();
	categoryNames.put("en", new HashMap<String,String>());
	categoryNames.put("fr", new HashMap<String,String>());
	categoryNames.put("de", new HashMap<String,String>());
	categoryNames.put("ia", new HashMap<String,String>());
	categoryNames.get("en").put("glf", "Gratuitous Literary Fragments");
	categoryNames.get("fr").put("glf", "Fragments litt\u00e9raires gratuits");
	categoryNames.get("ia").put("glf", "Fragmentos litterari gratuite");
	categoryNames.get("en").put("math", "Mathematics");
	categoryNames.get("fr").put("math", "Math\u00e9matiques");
	categoryNames.get("en").put("phys", "Physics");
	categoryNames.get("fr").put("phys", "Physique");
	categoryNames.get("en").put("ego", "my life");
	categoryNames.get("fr").put("ego", "ma vie");
	categoryNames.get("en").put("html", "Web technologies");
	categoryNames.get("fr").put("html", "technologies Web");
	categoryNames.get("en").put("gay", "gay themes");
	categoryNames.get("fr").put("gay", "th\u00e8mes gay");
	categoryNames.get("en").put("relig", "Religion");
	categoryNames.get("fr").put("relig", "Religion");
	categoryNames.get("en").put("philo", "Philosophy");
	categoryNames.get("fr").put("philo", "Philosophie");
	categoryNames.get("en").put("polit", "Politics");
	categoryNames.get("fr").put("polit", "Politique");
	categoryNames.get("ia").put("polit", "Politica");
	categoryNames.get("en").put("lex", "Law");
	categoryNames.get("fr").put("lex", "Droit");
	categoryNames.get("en").put("libri", "Books");
	categoryNames.get("fr").put("libri", "Livres");
	categoryNames.get("en").put("lang", "Languages & Linguistics");
	categoryNames.get("fr").put("lang", "Langues & Linguistique");
	categoryNames.get("ia").put("lang", "Liguas & Linguistica");
	categoryNames.get("en").put("meteo", "Meteorology");
	categoryNames.get("fr").put("meteo", "M\u00e9t\u00e9orologie");
	categoryNames.get("en").put("unicode", "Unicode");
	categoryNames.get("fr").put("unicode", "Unicode");
	categoryNames.get("en").put("cinema", "Films & Movies");
	categoryNames.get("fr").put("cinema", "Films & Cin\u00e9ma");
	categoryNames.get("de").put("cinema", "Film & Kino");
	categoryNames.get("en").put("linux", "Linux");
	categoryNames.get("fr").put("linux", "Linux");
	categoryNames.get("en").put("comp-hw", "Computer hardware");
	categoryNames.get("fr").put("comp-hw", "Mat\u00e9riel informatique");
	categoryNames.get("en").put("econom", "Economics");
	categoryNames.get("fr").put("econom", "\u00c9conomie");
	categoryNames.get("en").put("auto-moto", "Auto-Moto");
	categoryNames.get("fr").put("auto-moto", "Auto-Moto");
	categoryNames.get("en").put("gardinia", "Parks & Gardens");
	categoryNames.get("fr").put("gardinia", "Parcs & Jardins");
	categoryNames.get("en").put("covid-19", "Covid-19");
	categoryNames.get("fr").put("covid-19", "Covid-19");
	categoryNames.get("en").put("meta", "Meta");
	categoryNames.get("fr").put("meta", "M\u00e9ta");
    }

    public static String categoryName(String code, String lang) {
	if ( categoryNames.get(lang) == null )
	    return code;
	String name = categoryNames.get(lang).get(code);
	if ( name == null )
	    return code;
	return name;
    }

    @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;
	Node newNode;
	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);
	    newNode = ctx.doc.createTextNode(str);
	    node.getParentNode().replaceChild(newNode, node);
	    break;
	case MONTH_NUMERIC:
	    if ( ! ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) )
		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_LITERAL:
	    if ( ! ( ctx.wsc instanceof Context.WeblogMonthSelectionContext ) )
		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 ) )
		throw new IllegalStateException("weblog-selection-cat-code element encountered while not in weblog category selection state");
	    str = ((Context.WeblogCategorySelectionContext)(ctx.wsc)).code;
	    newNode = ctx.doc.createTextNode(str);
	    node.getParentNode().replaceChild(newNode, node);
	    break;
	case CATEGORY_NAME:
	    if ( ! ( ctx.wsc instanceof Context.WeblogCategorySelectionContext ) )
		throw new IllegalStateException("weblog-selection-cat-name element encountered while not in weblog category selection state");
	    str = categoryName(((Context.WeblogCategorySelectionContext)(ctx.wsc)).code, LangHelper.getLangRec(node));
	    newNode = ctx.doc.createTextNode(str);
	    node.getParentNode().replaceChild(newNode, node);
	    break;
	case SINGLE_NUMBER:
	    if ( ! ( ctx.wsc instanceof Context.WeblogSingleSelectionContext ) )
		throw new IllegalStateException("weblog-selection-single-number element encountered while not in weblog single entry selection state");
	    str = String.format("%04d", ((Context.WeblogSingleSelectionContext)(ctx.wsc)).number);
	    newNode = ctx.doc.createTextNode(str);
	    node.getParentNode().replaceChild(newNode, node);
	    break;
	case SINGLE_TITLE:
	    if ( ! ( ctx.wsc instanceof Context.WeblogSingleSelectionContext ) )
		throw new IllegalStateException("weblog-selection-single-title element encountered while not in weblog single entry selection state");
	    WeblogSummary wsum = WeblogSummary.getSummary(ctx.dc);
	    if ( wsum == null || wsum.entries == null )
		throw new IllegalStateException("failed to obtain weblog summary for weblog-selection-single-title");
	    WeblogSummary.EntrySummary esum
		= wsum.entries.get(Integer.valueOf(((Context.WeblogSingleSelectionContext)(ctx.wsc)).number));
	    if ( esum == null )
		throw new IllegalStateException("tried weblog-selection-single-title on inexistent entry");
	    {
		Element span = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span");
		node.getParentNode().replaceChild(span, node);
		LangHelper.setLangRec(span, esum.lang);
		DOMImplementationLS domi
		    = (DOMImplementationLS)ctx.doc.getImplementation();
		LSParser par = domi.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
		LSInput input = domi.createLSInput();
		if ( esum.titleXml == null ) {
		    // If there is no title, use number instead.
		    str = String.format("#%04d", ((Context.WeblogSingleSelectionContext)(ctx.wsc)).number);
		    Node strNode = ctx.doc.createTextNode(str);
		    span.appendChild(strNode);
		    break;  // Leave now!
		}
		input.setStringData(esum.titleXml);
		Document temp = par.parse(input);
		Node titleNode = ctx.doc.adoptNode(temp.getDocumentElement());
		String explicitLang = LangHelper.getLangNorec(titleNode);
		if ( explicitLang != null )
		    LangHelper.setLangNorec(span, explicitLang);
		ArrayList<Node> childList = getChildList(titleNode);
		for ( Node child : childList )
		    span.appendChild(child);
	    }
	    break;
	default:
	    throw new AssertionError("unknown type");
	}
    }

}