GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — enhancement: new body only template file mechanism (now support templ… · astamuse/asta4d@6c3ff67

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • asta4d-core/src
    • main
      • java/com/astamuse/asta4d
        • Page.java
        • extnode
          • ExtNodeConstants.java
      • jsoup/org/jsoup/parser
        • Asta4DTagSupportHtmlTreeBuilderState.java
    • test
      • java/com/astamuse/asta4d/test/render
        • TemplateExtractionTest.java
      • resources/com/astamuse/asta4d/test/render
        • confirms
          • TemplateWithBodyOnlyAttr.html
          • TemplateWithBodyOnlyMeta.html
          • TemplateWithoutBodyTag.html
        • templates
          • TemplateWithBodyOnlyAttr.html
          • TemplateWithBodyOnlyMeta.html
          • TemplateWithoutBodyTag.html
  • asta4d-web/src/main/java/com/astamuse/asta4d/web/dispatch/response/provider
    • Asta4DPageProvider.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/Page.java‎

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 numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
package com.astamuse.asta4d;
1919

20-
import java.io.ByteArrayOutputStream;
2120
import java.io.OutputStream;
21+
import java.nio.charset.StandardCharsets;
2222
import java.util.ArrayList;
2323
import java.util.List;
2424

2525
import org.jsoup.nodes.Document;
26+
import org.jsoup.select.Elements;
2627

28+
import com.astamuse.asta4d.extnode.ExtNodeConstants;
2729
import com.astamuse.asta4d.interceptor.PageInterceptor;
2830
import com.astamuse.asta4d.interceptor.base.ExceptionHandler;
2931
import com.astamuse.asta4d.interceptor.base.Executor;
@@ -34,6 +36,7 @@
3436
import com.astamuse.asta4d.render.Renderer;
3537
import com.astamuse.asta4d.template.Template;
3638
import com.astamuse.asta4d.template.TemplateResolver;
39+
import com.astamuse.asta4d.util.SelectorUtil;
3740

3841
public class Page {
3942

@@ -75,8 +78,10 @@ public final static List<PageInterceptorWrapper> buildList(List<PageInterceptor>
7578

7679
}
7780

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";
8085

8186
private Document renderedDocument;
8287

@@ -108,24 +113,49 @@ public void execute(Document doc) throws Exception {
108113
return doc;
109114
}
110115

111-
public Document getRenderedDocument() {
116+
protected Document getRenderedDocument() {
112117
return renderedDocument;
113118
}
114119

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+
115129
public void output(OutputStream out) throws Exception {
116-
out.write(renderedDocument.outerHtml().getBytes("utf-8"));
130+
out.write(output().getBytes(StandardCharsets.UTF_8));
117131
}
118132

119133
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();
127134

135+
// body only attr on body
136+
if (renderedDocument.body().hasAttr(ExtNodeConstants.ATTR_BODY_ONLY_WITH_NS)) {
137+
return renderedDocument.body().html();
128138
}
129139

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+
130160
}
131161
}
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/extnode/ExtNodeConstants.java‎

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 numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ private final static String addNS(String name) {
107107
public final static String ATTR_CLEAR = "clear";
108108
public final static String ATTR_CLEAR_WITH_NS = addNS(ATTR_CLEAR);
109109

110+
/**
111+
* sample of attr with body tag: <br>
112+
*
113+
* <pre>
114+
* &lt;body afd:bodyonly&gt;
115+
* &lt;div&gt;aaa&lt;/div&gt;
116+
* &lt;/body&gt;
117+
* </pre>
118+
*
119+
* sample of attr in meta: <br>
120+
*
121+
*
122+
* <pre>
123+
* &lt;html&gt;
124+
* &lt;head&gt;
125+
* &lt;meta afd:bodyonly&gt;
126+
* &lt;/head&gt;
127+
* &lt;body&gt;
128+
* &lt;div&gt;aaa&lt;/div&gt;
129+
* &lt;/body&gt;
130+
* &lt;/html&gt;
131+
* </pre>
132+
*
133+
* The output of both of above are as same as following:
134+
*
135+
* <pre>
136+
* &lt;div&gt;aaa&lt;/div&gt;
137+
* </pre>
138+
*/
139+
public final static String ATTR_BODY_ONLY_WITH_NS = addNS("bodyonly");
140+
110141
public final static String ATTR_DATAREF_PREFIX = "dataref-";
111142
public final static String ATTR_DATAREF_PREFIX_WITH_NS = addNS(ATTR_DATAREF_PREFIX);
112143

Collapse file

‎asta4d-core/src/main/jsoup/org/jsoup/parser/Asta4DTagSupportHtmlTreeBuilderState.java‎

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 numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.jsoup.nodes.DocumentType;
1313
import org.jsoup.nodes.Element;
1414
import org.jsoup.nodes.Node;
15+
import org.jsoup.parser.Token.StartTag;
1516

1617
import com.astamuse.asta4d.extnode.ExtNodeConstants;
1718

@@ -275,7 +276,12 @@ boolean process(Token t, Asta4DTagSupportHtmlTreeBuilder tb) {
275276
}
276277

277278
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);
279285
tb.framesetOk(true);
280286
return tb.process(t);
281287
}
@@ -989,8 +995,8 @@ boolean process(Token t, Asta4DTagSupportHtmlTreeBuilder tb) {
989995
tb.transition(InTable);
990996
}
991997
} 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"))) {
9941000
tb.error(this);
9951001
boolean processed = tb.process(new Token.EndTag("caption"));
9961002
if (processed)
@@ -1332,7 +1338,8 @@ boolean process(Token t, Asta4DTagSupportHtmlTreeBuilder tb) {
13321338
tb.error(this);
13331339
tb.process(new Token.EndTag("select"));
13341340
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")) {
13361343
tb.error(this);
13371344
if (tb.inTableScope(t.asEndTag().name())) {
13381345
tb.process(new Token.EndTag("select"));
@@ -1519,9 +1526,9 @@ private static void handleRawtext(Token.StartTag startTag, Asta4DTagSupportHtmlT
15191526
private static final class Constants {
15201527
private static final String[] InBodyStartToHead = new String[] { "base", "basefont", "bgsound", "command", "link", "meta",
15211528
"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" };
15251532
private static final String[] Headings = new String[] { "h1", "h2", "h3", "h4", "h5", "h6" };
15261533
private static final String[] InBodyStartPreListing = new String[] { "pre", "listing" };
15271534
private static final String[] InBodyStartLiBreakers = new String[] { "address", "div", "p" };
Collapse file

‎asta4d-core/src/test/java/com/astamuse/asta4d/test/render/TemplateExtractionTest.java‎

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 numberDiff line numberDiff line change
@@ -82,4 +82,19 @@ public void loadTempateWithExtensionAndEmbedMerge() throws Throwable {
8282
public void loadTemplateWithThreeLevelExtension() throws Throwable {
8383
new SimpleCase("ThreeLevelExtension_Child.html");
8484
}
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+
}
85100
}
Collapse file

‎asta4d-core/src/test/resources/com/astamuse/asta4d/test/render/confirms/TemplateWithBodyOnlyAttr.html‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
aaa
3+
</div>
Collapse file

‎asta4d-core/src/test/resources/com/astamuse/asta4d/test/render/confirms/TemplateWithBodyOnlyMeta.html‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
aaa
3+
</div>
Collapse file

‎asta4d-core/src/test/resources/com/astamuse/asta4d/test/render/confirms/TemplateWithoutBodyTag.html‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
aaa
3+
</div>
Collapse file

‎asta4d-core/src/test/resources/com/astamuse/asta4d/test/render/templates/TemplateWithBodyOnlyAttr.html‎

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<head></head>
3+
<body afd:bodyonly>
4+
<div>aaa</div>
5+
</body>
6+
</html>
Collapse file

‎asta4d-core/src/test/resources/com/astamuse/asta4d/test/render/templates/TemplateWithBodyOnlyMeta.html‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<meta afd:bodyonly>
4+
</head>
5+
<body>
6+
<div>aaa</div>
7+
</body>
8+
</html>
Collapse file

‎asta4d-core/src/test/resources/com/astamuse/asta4d/test/render/templates/TemplateWithoutBodyTag.html‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>aaa</div>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.