package org.madore.damlengine; import java.util.Map; import java.util.HashMap; import org.w3c.dom.*; public final class TodoWeblogSelectionElement extends TodoDefaultElement { public enum Type { RECENT_COUNT, MONTH_YEAR, MONTH_MONTH, CATEGORY_NAME, 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; } protected final static Map> categoryNames; static { categoryNames = new HashMap>(); categoryNames.put("en", new HashMap()); categoryNames.put("fr", new HashMap()); categoryNames.put("de", new HashMap()); categoryNames.put("ia", new HashMap()); 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("lang", "Languages & Linguistics"); categoryNames.get("fr").put("lang", "Langues & Linguistique"); categoryNames.get("ia").put("lang", "Liguas & Linguistica"); categoryNames.get("en").put("unicode", "Unicode"); categoryNames.get("fr").put("unicode", "Unicode"); } 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; 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; case CATEGORY_NAME: if ( ! ( ctx.wsc instanceof Context.WeblogCategorySelectionContext ) ) throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog category selection state"); str = categoryName(((Context.WeblogCategorySelectionContext)(ctx.wsc)).code, LangHelper.getLangRec(node)); break; default: throw new AssertionError("unknown type"); } Node txt = ctx.doc.createTextNode(str); node.getParentNode().replaceChild(txt, node); } }