summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoWrefAttr.java
blob: 95583e40c8c89ea4b9b4893c441ee38a0b332641 (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
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);
	String wrefSupplementStr = wrefMatcher.group(5);
	String targetFile = "";
	boolean isLocal = false;
	if ( ctx.wsc != null && ctx.wsc.sel != null
	     && ctx.wsc.sel.contains(new Integer(wrefNumberStr)) ) {
	    isLocal = true;
	    targetFile = "";
	} else
	    targetFile = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"))
		+ wrefYearStr + "-" + wrefMonthStr + ".html";
	String targetFragment = "d." + wrefYearStr + "-" + wrefMonthStr
	    + "-" + wrefDayStr + "." + wrefNumberStr + wrefSupplementStr;
	String target = targetFile + "#" + targetFragment;
	if ( ! this.owner.hasAttributeNS(null, "class") )
	    this.owner.setAttributeNS(null, "class", "weblog-internal-link");
	WeblogSummary wsum = WeblogSummary.getSummary();
	if ( wsum != null && wsum.entries != null ) {
	    WeblogSummary.EntrySummary esum
		= wsum.entries.get(new Integer(wrefNumberStr));
	    if ( esum != null ) {
		if ( ! ((wrefYearStr+"-"+wrefMonthStr+"-"+wrefDayStr)
			.equals(esum.date)) )
		    throw new IllegalArgumentException("date mismatch for reference to entry "+wrefNumberStr);
		if ( esum.doSinglePage != null
		     && ! ( wrefSupplementStr.equals("")
			    && isLocal )
		     // 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 = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"))
			+ esum.date + "-" + esum.doSinglePage + ".html";
		    target = targetFile + "#" + targetFragment;
		}
		if ( wrefSupplementStr.equals("")
		     && ! this.owner.hasAttributeNS(null, "title")
		     && esum.title != null )
		    this.owner.setAttributeNS(null, "title", esum.title);
	    }
	}
	this.owner.removeAttributeNode(this.attr);
	this.owner.setAttributeNS(null, "href", target);
    }

}