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(Integer.valueOf(trgt)) ) { WeblogSummary.EntrySummary ent = sum.entries.get(Integer.valueOf(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"); } }