diff options
author | David A. Madore <david+git@madore.org> | 2014-10-10 21:21:14 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2014-10-10 21:21:14 +0200 |
commit | d6b9db7926af4dc6d7df5af55d261731bd623c23 (patch) | |
tree | d8f289bdfb30202a92404d4be7608a0230c19242 | |
parent | 5b15f489d9793afdfe5dde2a2934c975b61567bd (diff) | |
download | damlengine-d6b9db7926af4dc6d7df5af55d261731bd623c23.tar.gz damlengine-d6b9db7926af4dc6d7df5af55d261731bd623c23.tar.bz2 damlengine-d6b9db7926af4dc6d7df5af55d261731bd623c23.zip |
Introduce a WeblogLink class to refactor a lot of code on how links are written.
-rw-r--r-- | org/madore/damlengine/TodoCategories.java | 13 | ||||
-rw-r--r-- | org/madore/damlengine/TodoComments.java | 8 | ||||
-rw-r--r-- | org/madore/damlengine/TodoCutHere.java | 16 | ||||
-rw-r--r-- | org/madore/damlengine/TodoEntryElement.java | 39 | ||||
-rw-r--r-- | org/madore/damlengine/TodoWeblogIndexSelectElement.java | 30 | ||||
-rw-r--r-- | org/madore/damlengine/TodoWrefAttr.java | 79 | ||||
-rw-r--r-- | org/madore/damlengine/WeblogLink.java | 75 | ||||
-rw-r--r-- | org/madore/damlengine/WeblogRSS.java | 41 |
8 files changed, 186 insertions, 115 deletions
diff --git a/org/madore/damlengine/TodoCategories.java b/org/madore/damlengine/TodoCategories.java index cc104e8..5d1a175 100644 --- a/org/madore/damlengine/TodoCategories.java +++ b/org/madore/damlengine/TodoCategories.java @@ -40,20 +40,21 @@ public final class TodoCategories extends TodoElement { if ( ctx.ent == null ) throw new IllegalStateException("entry context not defined in categories element"); for ( String cat : ctx.ent.catList ) { + WeblogLink lk = new WeblogLink(ctx.ent.year, ctx.ent.month, + ctx.ent.day, ctx.ent.number, "", + ctx.ent.doSinglePage); + lk.setTypeCat(cat); + String baseDir = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")); p.appendChild(ctx.doc.createTextNode(" ")); Element a1 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); - String target1URL = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")) - + cat + ".html"; - a1.setAttributeNS(null, "href", target1URL); + a1.setAttributeNS(null, "href", lk.getFile(baseDir)); a1.setAttributeNS(null, "title", TodoWeblogSelectionElement.categoryName(cat, lang)); p.appendChild(a1); a1.appendChild(ctx.doc.createTextNode(cat)); p.appendChild(ctx.doc.createTextNode("(")); Element a2 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); - String target2URL = target1URL - + "#d." + ctx.ent.date + "." + ctx.ent.number; - a2.setAttributeNS(null, "href", target2URL); + a2.setAttributeNS(null, "href", lk.getTarget(baseDir)); p.appendChild(a2); a2.appendChild(ctx.doc.createTextNode("\u2022")); p.appendChild(ctx.doc.createTextNode(")")); diff --git a/org/madore/damlengine/TodoComments.java b/org/madore/damlengine/TodoComments.java index e5cb04e..38b5c00 100644 --- a/org/madore/damlengine/TodoComments.java +++ b/org/madore/damlengine/TodoComments.java @@ -48,9 +48,11 @@ public final class TodoComments extends TodoElement { p.appendChild(a); a.appendChild(ctx.doc.createTextNode(linkNameTable.get(lang))); Element span = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span"); - span.setAttributeNS(null, "id", - "d." + ctx.ent.date + "." - + ctx.ent.number + ".numcomments"); + final WeblogLink lknc = new WeblogLink(ctx.ent.year, ctx.ent.month, + ctx.ent.day, ctx.ent.number, + ".numcomments", + ctx.ent.doSinglePage); + span.setAttributeNS(null, "id", lknc.getFragment()); p.appendChild(span); span.appendChild(ctx.doc.createComment(" EMPTY ")); if ( ctx.gc.commentsJSParam == null ) { diff --git a/org/madore/damlengine/TodoCutHere.java b/org/madore/damlengine/TodoCutHere.java index e935c4e..898c784 100644 --- a/org/madore/damlengine/TodoCutHere.java +++ b/org/madore/damlengine/TodoCutHere.java @@ -27,12 +27,15 @@ public final class TodoCutHere extends TodoDefaultElement { throw new IllegalStateException("cut-here element found in non single-page entry"); String lang = LangHelper.getLangRec(node); String explicitLang = LangHelper.getLangNorec(node); + final WeblogLink lk + = new WeblogLink(ctx.ent.year, ctx.ent.month, + ctx.ent.day, ctx.ent.number, + ".CUT", ctx.ent.doSinglePage); + lk.setTypeSingle(); if ( ctx.wsc == null || ctx.wsc instanceof Context.WeblogSingleSelectionContext ) { Element div = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "div"); - div.setAttributeNS(null, "id", - "d." + ctx.ent.date + "." + ctx.ent.number - + ".CUT"); + div.setAttributeNS(null, "id", lk.getFragment()); div.setAttributeNS(null, "class", "cut-anchor"); div.appendChild(ctx.doc.createComment(" EMPTY ")); node.getParentNode().replaceChild(div, node); @@ -40,11 +43,8 @@ public final class TodoCutHere extends TodoDefaultElement { Element p = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "p"); Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); p.appendChild(a); - a.setAttributeNS(null, "href", - ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")) - + ctx.ent.date + "-" + ctx.ent.doSinglePage + ".html" - + "#d." + ctx.ent.date + "." + ctx.ent.number - + ".CUT"); + final String baseDir = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")); + a.setAttributeNS(null, "href", lk.getTarget(baseDir)); if ( explicitLang != null ) LangHelper.setLangNorec(p, explicitLang); p.setAttributeNS(null, "class", "cut-link"); diff --git a/org/madore/damlengine/TodoEntryElement.java b/org/madore/damlengine/TodoEntryElement.java index 6ba7c5d..7a8c272 100644 --- a/org/madore/damlengine/TodoEntryElement.java +++ b/org/madore/damlengine/TodoEntryElement.java @@ -35,16 +35,16 @@ public final class TodoEntryElement extends TodoDefaultElement { LangHelper.setLangNorec(article, explicitLang); node.getParentNode().replaceChild(article, node); - String entryNumberStr = node.getAttributeNS(null, "number"); + final String entryNumberStr = node.getAttributeNS(null, "number"); if ( ! Pattern.matches("^\\d{4}$", entryNumberStr) ) throw new IllegalArgumentException("entry number attribute must be of the form NNNN"); - String entryDateStr = node.getAttributeNS(null, "date"); - Matcher entryDateMatcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(entryDateStr); + final String entryDateStr = node.getAttributeNS(null, "date"); + final Matcher entryDateMatcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(entryDateStr); if ( ! entryDateMatcher.matches() ) throw new IllegalArgumentException("entry date attribute must be of the form YYYY-MM-DD"); - String entryYearStr = entryDateMatcher.group(1); - String entryMonthStr = entryDateMatcher.group(2); - String entryDayStr = entryDateMatcher.group(3); + final String entryYearStr = entryDateMatcher.group(1); + final String entryMonthStr = entryDateMatcher.group(2); + final String entryDayStr = entryDateMatcher.group(3); String entryDowStr = node.getAttributeNS(null, "day-of-week"); { final int entryYear = Integer.parseInt(entryYearStr); @@ -61,10 +61,12 @@ public final class TodoEntryElement extends TodoDefaultElement { } } } - String entryCatStr = node.getAttributeNS(null, "cat"); - String entryDoSinglePageStr = node.getAttributeNS(null, "single-page"); - String entryCdateStr = node.getAttributeNS(null, "cdate"); - ArrayList<String> entryCatList; + final String entryCatStr = node.getAttributeNS(null, "cat"); + final String entryDoSinglePageStr + = node.hasAttributeNS(null, "single-page") + ? node.getAttributeNS(null, "single-page") : null; + final String entryCdateStr = node.getAttributeNS(null, "cdate"); + final ArrayList<String> entryCatList; if ( entryCatStr.equals("") ) { entryCatList = new ArrayList<String>(0); } else { @@ -76,7 +78,11 @@ public final class TodoEntryElement extends TodoDefaultElement { } } - String entryIdStr = "d."+entryDateStr+"."+entryNumberStr; + final WeblogLink lk = new WeblogLink(entryYearStr, entryMonthStr, + entryDayStr, entryNumberStr, "", + entryDoSinglePageStr); + lk.setTypeStandard(); + String entryIdStr = lk.getFragment(); article.setAttributeNS(null, "id", entryIdStr); { String classAtt = node.getAttributeNS(null, "class"); @@ -102,16 +108,7 @@ public final class TodoEntryElement extends TodoDefaultElement { header.appendChild(ctx.doc.createTextNode("\n")); Element permalink = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); - if ( entryDoSinglePageStr.equals("") ) - permalink.setAttributeNS(null, "href", - entryYearStr+"-"+entryMonthStr+".html" - +"#"+entryIdStr); - else - permalink.setAttributeNS(null, "href", - entryYearStr+"-"+entryMonthStr - +"-"+entryDayStr+"-"+entryDoSinglePageStr - +".html" - +"#"+entryIdStr); + permalink.setAttributeNS(null, "href", lk.getTarget("")); permalink.setAttributeNS(null, "rel", "bookmark"); Element time = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "time"); permalink.appendChild(time); diff --git a/org/madore/damlengine/TodoWeblogIndexSelectElement.java b/org/madore/damlengine/TodoWeblogIndexSelectElement.java index 004e330..0323078 100644 --- a/org/madore/damlengine/TodoWeblogIndexSelectElement.java +++ b/org/madore/damlengine/TodoWeblogIndexSelectElement.java @@ -45,28 +45,24 @@ public final class TodoWeblogIndexSelectElement extends TodoDefaultElement { = new ArrayList<TodoElement>(entlist.size()*2); Context ctx2 = ctx.clone(); ctx2.killA = true; for ( Iterator<Integer> iter=entlist.descendingIterator() ; iter.hasNext() ; ) { - WeblogSummary.EntrySummary ent = wsum.entries.get(iter.next()); - Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(ent.date); + final WeblogSummary.EntrySummary ent = wsum.entries.get(iter.next()); + final Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(ent.date); if ( ! matcher.matches() ) throw new IllegalArgumentException("entry "+ent.id+" has badly formed date"); - String yearStr = matcher.group(1); - String monthStr = matcher.group(2); - String dayStr = matcher.group(3); - String numberStr = String.format("%04d",ent.id); - String targetFile = ""; - if ( ent.doSinglePage == null ) - targetFile = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")) - + yearStr + "-" + monthStr + ".html"; - else - targetFile = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")) - + ent.date + "-" + ent.doSinglePage + ".html"; - String targetFragment = "d." + yearStr + "-" + monthStr - + "-" + dayStr + "." + numberStr; - String target = targetFile + "#" + targetFragment; + final String yearStr = matcher.group(1); + final String monthStr = matcher.group(2); + final String dayStr = matcher.group(3); + final String numberStr = String.format("%04d",ent.id); + final String baseDir = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")); + final WeblogLink lk = new WeblogLink(yearStr, monthStr, dayStr, + numberStr, "", + ent.doSinglePage); + lk.setTypeStandard(); + String target = lk.getTarget(baseDir); Element li = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "li"); ul.appendChild(li); ul.appendChild(ctx.doc.createTextNode("\n")); - li.setAttributeNS(null, "id", targetFragment); + li.setAttributeNS(null, "id", lk.getFragment()); li.setAttributeNS(null, "class", "weblog-index-entry"); LangHelper.setLangRec(li, ent.lang); Element link; diff --git a/org/madore/damlengine/TodoWrefAttr.java b/org/madore/damlengine/TodoWrefAttr.java index 428000a..642ecae 100644 --- a/org/madore/damlengine/TodoWrefAttr.java +++ b/org/madore/damlengine/TodoWrefAttr.java @@ -23,16 +23,16 @@ public class TodoWrefAttr extends TodoAttr { @Override public void handle() { - String wrefStr = attr.getValue(); - Matcher wrefMatcher = Pattern.compile("^\\#d\\.(\\d{4})-(\\d{2})-(\\d{2})\\.(\\d{4})(|\\..*)$").matcher(wrefStr); + final String wrefStr = attr.getValue(); + final 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); + final String wrefYearStr = wrefMatcher.group(1); + final String wrefMonthStr = wrefMatcher.group(2); + final String wrefDayStr = wrefMatcher.group(3); + final String wrefNumberStr = wrefMatcher.group(4); + final int wrefNumber = Integer.parseInt(wrefNumberStr); + final String wrefSupplementStr = wrefMatcher.group(5); String wrefCat; if ( this.owner.hasAttributeNS(DamlEngine.DAML_NS, "wrefcat") ) { wrefCat = this.owner.getAttributeNS(DamlEngine.DAML_NS, "wrefcat"); @@ -40,10 +40,9 @@ public class TodoWrefAttr extends TodoAttr { } else { wrefCat = null; } - String targetFile; - String targetStdDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"); - WeblogSummary wsum = WeblogSummary.getSummary(); - WeblogSummary.EntrySummary esum; + final String baseDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"); + final WeblogSummary wsum = WeblogSummary.getSummary(); + final WeblogSummary.EntrySummary esum; // Get entry for target link from weblog summary if ( wsum != null && wsum.entries != null ) esum = wsum.entries.get(wrefNumber); @@ -60,39 +59,45 @@ public class TodoWrefAttr extends TodoAttr { 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("") ) { + // Prepare link object + final WeblogLink lk; + if ( esum != null && esum.doSinglePage != null ) + lk = new WeblogLink(wrefYearStr, wrefMonthStr, wrefDayStr, + wrefNumberStr, wrefSupplementStr, + esum.doSinglePage); + else + lk = new WeblogLink(wrefYearStr, wrefMonthStr, wrefDayStr, + wrefNumberStr, wrefSupplementStr); + // Decide whether link target is on this page + final boolean onThisPage; + if ( lk.singlePage != null && ! lk.supplementStr.equals("") ) { // Linking to a _fragment_ of an entry that has a single page. - if ( wrefCat != null && ! wrefCat.equals("@single") ) - System.err.println("warning: ignoring d:wrefcat attribute!"); if ( ctx.wsc != null && ctx.wsc.sel != null && ctx.wsc instanceof Context.WeblogSingleSelectionContext && ((Context.WeblogSingleSelectionContext)ctx.wsc).number == wrefNumber ) - targetFile = ""; + onThisPage = true; else - targetFile = targetStdDir + esum.date + "-" + esum.doSinglePage + ".html"; - } else if ( ctx.wsc != null && ctx.wsc.sel != null - && ctx.wsc.sel.contains(wrefNumber) ) { - // Linking to an entry already present on this page. - targetFile = ""; - } else if ( wrefCat != null && ! wrefCat.equals("@month") - && ! wrefCat.equals("@single") ) { - // Linking to an entry in a designated category. - targetFile = targetStdDir + wrefCat + ".html"; + onThisPage = false; } else { - // Linking to an entry in its month, or single page. - 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". - && ( wrefCat == null || wrefCat.equals("@single") ) ) { - targetFile = targetStdDir + esum.date + "-" + esum.doSinglePage + ".html"; - } + if ( ctx.wsc != null && ctx.wsc.sel != null + && ctx.wsc.sel.contains(wrefNumber) ) + onThisPage = true; + else + onThisPage = false; } + // Choose file portion of link target + if ( onThisPage ) + lk.setTypeRelative(); + else if ( wrefCat == null ) + lk.setTypeStandard(); + else if ( wrefCat.equals("@month") ) + lk.setTypeMonth(); + else if ( wrefCat.equals("@single") ) + lk.setTypeSingle(); + else + lk.setTypeCat(wrefCat); // Construct full link target - String targetFragment = "d." + wrefYearStr + "-" + wrefMonthStr - + "-" + wrefDayStr + "." + wrefNumberStr + wrefSupplementStr; - String target = targetFile + "#" + targetFragment; + String target = lk.getTarget(baseDir); // Class and title attributes if ( ! this.owner.hasAttributeNS(null, "class") ) this.owner.setAttributeNS(null, "class", "weblog-internal-link"); diff --git a/org/madore/damlengine/WeblogLink.java b/org/madore/damlengine/WeblogLink.java new file mode 100644 index 0000000..f29c69e --- /dev/null +++ b/org/madore/damlengine/WeblogLink.java @@ -0,0 +1,75 @@ +package org.madore.damlengine; + +public final class WeblogLink { + + private enum Type { RELATIVE, MONTH, CAT, SINGLE } + + public String yearStr; + public String monthStr; + public String dayStr; + public String numberStr; + public String supplementStr; + public String singlePage; // May be null + + private Type t = Type.MONTH; + private String cat; // Only defined for type CAT + + public WeblogLink(String yearStr, String monthStr, String dayStr, + String numberStr, String supplementStr, + String singlePage) { + this.yearStr = yearStr; + this.monthStr = monthStr; + this.dayStr = dayStr; + this.numberStr = numberStr; + this.supplementStr = supplementStr; + this.singlePage = singlePage; + } + + public WeblogLink(String yearStr, String monthStr, String dayStr, + String numberStr, String supplementStr) { + this(yearStr, monthStr, dayStr, numberStr, supplementStr, null); + } + + public void setTypeRelative() { this.t = Type.RELATIVE; } + public void setTypeMonth() { this.t = Type.MONTH; } + public void setTypeCat(String cat) { + this.t = Type.CAT; + this.cat = cat; + } + public void setTypeSingle() { this.t = Type.SINGLE; } + + public void setTypeStandard() { + if ( singlePage == null ) + this.t = Type.MONTH; + else + this.t = Type.SINGLE; + } + + public String getFragment() { + return "d." + yearStr + "-" + monthStr + + "-" + dayStr + "." + numberStr + supplementStr; + } + + public String getFile(String baseDir) { + switch ( t ) { + case RELATIVE: + return ""; + case MONTH: + return baseDir + yearStr + "-" + monthStr + ".html"; + case CAT: + return baseDir + cat + ".html"; + case SINGLE: + if ( singlePage == null ) + throw new IllegalArgumentException("can't have single page link to non single-page entry"); + return baseDir + yearStr + "-" + monthStr + "-" + dayStr + + "-" + singlePage + ".html"; + default: + throw new AssertionError("unknown type"); + } + } + + public String getTarget(String baseDir) { + return this.getFile(baseDir) + "#" + this.getFragment(); + } + +} diff --git a/org/madore/damlengine/WeblogRSS.java b/org/madore/damlengine/WeblogRSS.java index 0f3b296..85d9fc0 100644 --- a/org/madore/damlengine/WeblogRSS.java +++ b/org/madore/damlengine/WeblogRSS.java @@ -93,31 +93,26 @@ public final class WeblogRSS { itemsSeq.appendChild(rssDoc.createTextNode("\n")); while ( selRes.next() ) { - int id = selRes.getInt(1); - String date = selRes.getString(2); - String cdate = selRes.getString(3); - String lang = selRes.getString(4); - String doSinglePage = selRes.getString(5); - String title = selRes.getString(6); - String contentXml = selRes.getString(7); - Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(date); + final int id = selRes.getInt(1); + final String date = selRes.getString(2); + final String cdate = selRes.getString(3); + final String lang = selRes.getString(4); + final String doSinglePage = selRes.getString(5); + final String title = selRes.getString(6); + final String contentXml = selRes.getString(7); + final Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(date); if ( ! matcher.matches() ) throw new IllegalArgumentException("entry "+id+" has badly formed date"); - String yearStr = matcher.group(1); - String monthStr = matcher.group(2); - // String dayStr = matcher.group(3); - String numberStr = String.format("%04d",id); - String aboutUri = weblogBaseURI - + "#d." + date + "." + numberStr; - String linkUri; - if ( doSinglePage == null ) - linkUri = weblogBaseURI - + yearStr + "-" + monthStr + ".html" - + "#d." + date + "." + numberStr; - else - linkUri = weblogBaseURI - + date + "-" + doSinglePage + ".html" - + "#d." + date + "." + numberStr; + final String yearStr = matcher.group(1); + final String monthStr = matcher.group(2); + final String dayStr = matcher.group(3); + final String numberStr = String.format("%04d",id); + final WeblogLink lk = new WeblogLink(yearStr, monthStr, dayStr, + numberStr, "", + doSinglePage); + lk.setTypeStandard(); + final String aboutUri = weblogBaseURI + "#" + lk.getFragment(); + final String linkUri = lk.getTarget(weblogBaseURI); Element li = rssDoc.createElementNS(DamlEngine.RDF_NS, "rdf:li"); itemsSeq.appendChild(li); itemsSeq.appendChild(rssDoc.createTextNode("\n")); |