package org.madore.damlengine; import java.util.regex.Pattern; import java.util.regex.Matcher; import org.w3c.dom.*; public class TodoWrefAttr extends TodoAttr { public static class Factory extends TodoAttr.Factory { @Override public TodoWrefAttr newItem(Attr attr, Element owner, Context ctx, TodoItem caller) { return new TodoWrefAttr(attr, owner, ctx, caller); } } public TodoWrefAttr(Attr attr, Element owner, Context ctx, TodoItem caller) { super(attr, owner, ctx, caller); } @Override public void handle() { String wrefStr = attr.getValue(); Matcher wrefMatcher = Pattern.compile("^\\#d\\.(\\d{4})-(\\d{2})-(\\d{2})\\.(\\d{4})(|\\..*)$").matcher(wrefStr); if ( ! wrefMatcher.matches() ) throw new IllegalArgumentException("wref attribute must be of the form #d.YYYY-MM-DD.NNNN[.xxx]"); String wrefYearStr = wrefMatcher.group(1); String wrefMonthStr = wrefMatcher.group(2); String wrefDayStr = wrefMatcher.group(3); String wrefNumberStr = wrefMatcher.group(4); int wrefNumber = Integer.parseInt(wrefNumberStr); String wrefSupplementStr = wrefMatcher.group(5); String wrefCat; if ( this.owner.hasAttributeNS(DamlEngine.DAML_NS, "wrefcat") ) { wrefCat = this.owner.getAttributeNS(DamlEngine.DAML_NS, "wrefcat"); this.owner.removeAttributeNS(DamlEngine.DAML_NS, "wrefcat"); } else { wrefCat = null; } String targetFile; String targetStdDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"); WeblogSummary wsum = WeblogSummary.getSummary(); WeblogSummary.EntrySummary esum; // Get entry for target link from weblog summary if ( wsum != null && wsum.entries != null ) esum = wsum.entries.get(wrefNumber); else esum = null; // Check consistency if summary was available if ( esum != null ) { if ( ! ((wrefYearStr+"-"+wrefMonthStr+"-"+wrefDayStr) .equals(esum.date)) ) throw new IllegalArgumentException("date mismatch for reference to entry "+wrefNumberStr); if ( wrefCat != null ) { if ( esum.catSet == null || ! esum.catSet.contains(wrefCat) ) throw new IllegalArgumentException("reference to entry "+wrefNumberStr+" in wrong category "+wrefCat); } } // Choose file portion of link target if ( esum != null && esum.doSinglePage != null && ! wrefSupplementStr.equals("") ) { if ( ctx.wsc != null && ctx.wsc.sel != null && ctx.wsc instanceof Context.WeblogSingleSelectionContext && ((Context.WeblogSingleSelectionContext)ctx.wsc).number == wrefNumber ) targetFile = ""; else targetFile = targetStdDir + esum.date + "-" + esum.doSinglePage + ".html"; } else if ( ctx.wsc != null && ctx.wsc.sel != null && ctx.wsc.sel.contains(wrefNumber) ) { targetFile = ""; } else if ( wrefCat != null ) { targetFile = targetStdDir + wrefCat + ".html"; } else { targetFile = targetStdDir + wrefYearStr + "-" + wrefMonthStr + ".html"; if ( esum != null && esum.doSinglePage != null // Note to self: remember we could be called from d:wxref // because of link on "older entries": we don't want // to link to single entry page in this case. :-\ && ! ( caller instanceof TodoWXrefAttr ) ) { targetFile = targetStdDir + esum.date + "-" + esum.doSinglePage + ".html"; } } // Construct full link target String targetFragment = "d." + wrefYearStr + "-" + wrefMonthStr + "-" + wrefDayStr + "." + wrefNumberStr + wrefSupplementStr; String target = targetFile + "#" + targetFragment; // Class and title attributes if ( ! this.owner.hasAttributeNS(null, "class") ) this.owner.setAttributeNS(null, "class", "weblog-internal-link"); if ( wrefSupplementStr.equals("") && ! this.owner.hasAttributeNS(null, "title") && esum != null && esum.title != null ) this.owner.setAttributeNS(null, "title", esum.title); // Replace this attribute by computed target this.owner.removeAttributeNode(this.attr); this.owner.setAttributeNS(null, "href", target); } }