GeezerVison is showing github.com — enhancement: new body only template file mechanism (now support templ… · astamuse/asta4d@6c3ff67
File treeExpand file treeCollapse file tree Open diff view settings java/com/astamuse/asta4d/test/render resources/com/astamuse/asta4d/test/render
asta4d-web/src/main/java/com/astamuse/asta4d/web/dispatch/response/provider
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: asta4d-core/src/main/java/com/astamuse/asta4d/Page.java +42-12Lines changed: 42 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 |
| 18 | 18 | package com.astamuse.asta4d; | 19 | 19 |
| 20 | | -import java.io.ByteArrayOutputStream; | 21 | 20 | import java.io.OutputStream; | | 21 | +import java.nio.charset.StandardCharsets; | 22 | 22 | import java.util.ArrayList; | 23 | 23 | import java.util.List; | 24 | 24 |
| 25 | 25 | import org.jsoup.nodes.Document; | | 26 | +import org.jsoup.select.Elements; | 26 | 27 |
| | 28 | +import com.astamuse.asta4d.extnode.ExtNodeConstants; | 27 | 29 | import com.astamuse.asta4d.interceptor.PageInterceptor; | 28 | 30 | import com.astamuse.asta4d.interceptor.base.ExceptionHandler; | 29 | 31 | import com.astamuse.asta4d.interceptor.base.Executor; | | 34 | 36 | import com.astamuse.asta4d.render.Renderer; | 35 | 37 | import com.astamuse.asta4d.template.Template; | 36 | 38 | import com.astamuse.asta4d.template.TemplateResolver; | | 39 | +import com.astamuse.asta4d.util.SelectorUtil; | 37 | 40 |
| 38 | 41 | public class Page { | 39 | 42 |
| @@ -75,8 +78,10 @@ public final static List<PageInterceptorWrapper> buildList(List<PageInterceptor> | 75 | 78 |
| 76 | 79 | } | 77 | 80 |
| 78 | | - private final static List<PageInterceptorWrapper> WrapperPageInterceptorList = PageInterceptorWrapper.buildList(Configuration | 79 | | - .getConfiguration().getPageInterceptorList()); | | 81 | + private final static List<PageInterceptorWrapper> WrapperPageInterceptorList = PageInterceptorWrapper | | 82 | + .buildList(Configuration.getConfiguration().getPageInterceptorList()); | | 83 | + | | 84 | + protected final static String DEFAULT_CONTENT_TYPE = "text/html; charset=UTF-8"; | 80 | 85 |
| 81 | 86 | private Document renderedDocument; | 82 | 87 |
| @@ -108,24 +113,49 @@ public void execute(Document doc) throws Exception { | 108 | 113 | return doc; | 109 | 114 | } | 110 | 115 |
| 111 | | - public Document getRenderedDocument() { | | 116 | + protected Document getRenderedDocument() { | 112 | 117 | return renderedDocument; | 113 | 118 | } | 114 | 119 |
| | 120 | + public String getContentType() { | | 121 | + Elements elems = renderedDocument.select("meta[http-equiv=Content-Type]"); | | 122 | + if (elems.size() == 0) { | | 123 | + return DEFAULT_CONTENT_TYPE; | | 124 | + } else { | | 125 | + return elems.get(0).attr("content"); | | 126 | + } | | 127 | + } | | 128 | + | 115 | 129 | public void output(OutputStream out) throws Exception { | 116 | | - out.write(renderedDocument.outerHtml().getBytes("utf-8")); | | 130 | + out.write(output().getBytes(StandardCharsets.UTF_8)); | 117 | 131 | } | 118 | 132 |
| 119 | 133 | public String output() { | 120 | | - ByteArrayOutputStream bos = new ByteArrayOutputStream(); | 121 | | - try { | 122 | | - output(bos); | 123 | | - return bos.toString("utf-8"); | 124 | | - } catch (Exception e) { | 125 | | - e.printStackTrace(); | 126 | | - return e.getMessage(); | 127 | 134 |
| | 135 | + // body only attr on body | | 136 | + if (renderedDocument.body().hasAttr(ExtNodeConstants.ATTR_BODY_ONLY_WITH_NS)) { | | 137 | + return renderedDocument.body().html(); | 128 | 138 | } | 129 | 139 |
| | 140 | + // body only meta | | 141 | + Elements bodyonlyMeta = renderedDocument.head().select(SelectorUtil.attr("meta", ExtNodeConstants.ATTR_BODY_ONLY_WITH_NS, null)); | | 142 | + if (bodyonlyMeta.size() > 0) { | | 143 | + return renderedDocument.body().html(); | | 144 | + } | | 145 | + | | 146 | + // full page | | 147 | + return renderedDocument.outerHtml(); | | 148 | + } | | 149 | + | | 150 | + /** | | 151 | + * This method is for back forward compatibility in framework internal implementation, client developers should never use it. | | 152 | + * | | 153 | + * @param out | | 154 | + * @throws Exception | | 155 | + */ | | 156 | + @Deprecated | | 157 | + public void outputBodyOnly(OutputStream out) throws Exception { | | 158 | + out.write(renderedDocument.body().html().getBytes(StandardCharsets.UTF_8)); | | 159 | + | 130 | 160 | } | 131 | 161 | } |
Collapse file Copy file name to clipboardExpand all lines: asta4d-core/src/main/java/com/astamuse/asta4d/extnode/ExtNodeConstants.java +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,6 +107,37 @@ private final static String addNS(String name) { | 107 | 107 | public final static String ATTR_CLEAR = "clear"; | 108 | 108 | public final static String ATTR_CLEAR_WITH_NS = addNS(ATTR_CLEAR); | 109 | 109 |
| | 110 | + /** | | 111 | + * sample of attr with body tag: <br> | | 112 | + * | | 113 | + * <pre> | | 114 | + * <body afd:bodyonly> | | 115 | + * <div>aaa</div> | | 116 | + * </body> | | 117 | + * </pre> | | 118 | + * | | 119 | + * sample of attr in meta: <br> | | 120 | + * | | 121 | + * | | 122 | + * <pre> | | 123 | + * <html> | | 124 | + * <head> | | 125 | + * <meta afd:bodyonly> | | 126 | + * </head> | | 127 | + * <body> | | 128 | + * <div>aaa</div> | | 129 | + * </body> | | 130 | + * </html> | | 131 | + * </pre> | | 132 | + * | | 133 | + * The output of both of above are as same as following: | | 134 | + * | | 135 | + * <pre> | | 136 | + * <div>aaa</div> | | 137 | + * </pre> | | 138 | + */ | | 139 | + public final static String ATTR_BODY_ONLY_WITH_NS = addNS("bodyonly"); | | 140 | + | 110 | 141 | public final static String ATTR_DATAREF_PREFIX = "dataref-"; | 111 | 142 | public final static String ATTR_DATAREF_PREFIX_WITH_NS = addNS(ATTR_DATAREF_PREFIX); | 112 | 143 |
| |
Collapse file Copy file name to clipboardExpand all lines: asta4d-core/src/main/jsoup/org/jsoup/parser/Asta4DTagSupportHtmlTreeBuilderState.java +14-7Lines changed: 14 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | import org.jsoup.nodes.DocumentType; | 13 | 13 | import org.jsoup.nodes.Element; | 14 | 14 | import org.jsoup.nodes.Node; | | 15 | +import org.jsoup.parser.Token.StartTag; | 15 | 16 |
| 16 | 17 | import com.astamuse.asta4d.extnode.ExtNodeConstants; | 17 | 18 |
| @@ -275,7 +276,12 @@ boolean process(Token t, Asta4DTagSupportHtmlTreeBuilder tb) { | 275 | 276 | } | 276 | 277 |
| 277 | 278 | private boolean anythingElse(Token t, Asta4DTagSupportHtmlTreeBuilder tb) { | 278 | | - tb.process(new Token.StartTag("body")); | | 279 | + StartTag tag = new Token.StartTag("body"); | | 280 | + // asta4d customization start | | 281 | + tag.appendAttributeName(ExtNodeConstants.ATTR_BODY_ONLY_WITH_NS); | | 282 | + tag.finaliseTag(); | | 283 | + // asta4d customization end | | 284 | + tb.process(tag); | 279 | 285 | tb.framesetOk(true); | 280 | 286 | return tb.process(t); | 281 | 287 | } | @@ -989,8 +995,8 @@ boolean process(Token t, Asta4DTagSupportHtmlTreeBuilder tb) { | 989 | 995 | tb.transition(InTable); | 990 | 996 | } | 991 | 997 | } else if ((t.isStartTag() && | 992 | | - StringUtil.in(t.asStartTag().name(), "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr") || t | 993 | | - .isEndTag() && t.asEndTag().name().equals("table"))) { | | 998 | + StringUtil.in(t.asStartTag().name(), "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr") || | | 999 | + t.isEndTag() && t.asEndTag().name().equals("table"))) { | 994 | 1000 | tb.error(this); | 995 | 1001 | boolean processed = tb.process(new Token.EndTag("caption")); | 996 | 1002 | if (processed) | @@ -1332,7 +1338,8 @@ boolean process(Token t, Asta4DTagSupportHtmlTreeBuilder tb) { | 1332 | 1338 | tb.error(this); | 1333 | 1339 | tb.process(new Token.EndTag("select")); | 1334 | 1340 | return tb.process(t); | 1335 | | - } else if (t.isEndTag() && StringUtil.in(t.asEndTag().name(), "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th")) { | | 1341 | + } else | | 1342 | + if (t.isEndTag() && StringUtil.in(t.asEndTag().name(), "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th")) { | 1336 | 1343 | tb.error(this); | 1337 | 1344 | if (tb.inTableScope(t.asEndTag().name())) { | 1338 | 1345 | tb.process(new Token.EndTag("select")); | @@ -1519,9 +1526,9 @@ private static void handleRawtext(Token.StartTag startTag, Asta4DTagSupportHtmlT | 1519 | 1526 | private static final class Constants { | 1520 | 1527 | private static final String[] InBodyStartToHead = new String[] { "base", "basefont", "bgsound", "command", "link", "meta", | 1521 | 1528 | "noframes", "script", "style", "title" }; | 1522 | | - private static final String[] InBodyStartPClosers = new String[] { "address", "article", "aside", "blockquote", "center", | 1523 | | - "details", "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "menu", "nav", "ol", "p", | 1524 | | - "section", "summary", "ul" }; | | 1529 | + private static final String[] InBodyStartPClosers = new String[] { "address", "article", "aside", "blockquote", "center", "details", | | 1530 | + "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "menu", "nav", "ol", "p", "section", | | 1531 | + "summary", "ul" }; | 1525 | 1532 | private static final String[] Headings = new String[] { "h1", "h2", "h3", "h4", "h5", "h6" }; | 1526 | 1533 | private static final String[] InBodyStartPreListing = new String[] { "pre", "listing" }; | 1527 | 1534 | private static final String[] InBodyStartLiBreakers = new String[] { "address", "div", "p" }; | |
Collapse file Copy file name to clipboardExpand all lines: asta4d-core/src/test/java/com/astamuse/asta4d/test/render/TemplateExtractionTest.java +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,4 +82,19 @@ public void loadTempateWithExtensionAndEmbedMerge() throws Throwable { | 82 | 82 | public void loadTemplateWithThreeLevelExtension() throws Throwable { | 83 | 83 | new SimpleCase("ThreeLevelExtension_Child.html"); | 84 | 84 | } | | 85 | + | | 86 | + @Test | | 87 | + public void loadTemplateWithoutBodyTag() throws Throwable { | | 88 | + new SimpleCase("TemplateWithoutBodyTag.html"); | | 89 | + } | | 90 | + | | 91 | + @Test | | 92 | + public void loadTemplateWithBodyOnlyAttr() throws Throwable { | | 93 | + new SimpleCase("TemplateWithBodyOnlyAttr.html"); | | 94 | + } | | 95 | + | | 96 | + @Test | | 97 | + public void loadTemplateWithBodyOnlyMeta() throws Throwable { | | 98 | + new SimpleCase("TemplateWithBodyOnlyMeta.html"); | | 99 | + } | 85 | 100 | } |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<div> | | 2 | + aaa | | 3 | +</div> |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<div> | | 2 | + aaa | | 3 | +</div> |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<div> | | 2 | + aaa | | 3 | +</div> |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<html> | | 2 | +<head></head> | | 3 | +<body afd:bodyonly> | | 4 | + <div>aaa</div> | | 5 | +</body> | | 6 | +</html> |
Collapse file Copy file name to clipboard +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<html> | | 2 | +<head> | | 3 | +<meta afd:bodyonly> | | 4 | +</head> | | 5 | +<body> | | 6 | + <div>aaa</div> | | 7 | +</body> | | 8 | +</html> |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<div>aaa</div> |
|