{"id":1594,"date":"2021-02-28T16:47:55","date_gmt":"2021-02-28T15:47:55","guid":{"rendered":"https:\/\/benjiweber.co.uk\/blog\/?p=1594"},"modified":"2021-02-28T16:59:31","modified_gmt":"2021-02-28T15:59:31","slug":"revisiting-html-in-java","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/","title":{"rendered":"Revisiting Html in Java"},"content":{"rendered":"\n<p class=\"lead\">Some time ago I wrote a post about creating an embedded dsl for <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/21\/html-in-java\/\">Html in Java<\/a>. Sadly, it was based on an abuse of lambda name reflection that was later removed from Java.<\/p>\n\n\n\n<p>I thought I should do a followup because a lot of people still visit the old article. While it&#8217;s no longer possible to use lambda parameter names in this way, we can still get fairly close.\u00a0<\/p>\n\n\n\n<p>The following approach is slightly less concise. That said, it does have some benefits over the original:<\/p>\n\n\n\n<p><strong>a)<\/strong> You no longer need to have parameter name reflection enabled at compile time.<\/p>\n\n\n\n<p><strong>b)<\/strong> The compiler can check your attribute names are valid, and you can autocomplete them.<\/p>\n\n\n\n<h2>What does it look like?&nbsp;<\/h2>\n\n\n\n<pre lang=\"java\">html(\n   head(\n       title(\"Hello Html World\"),\n       meta($ -> $.charset = \"utf-8\"),\n       link($->{ $.rel=stylesheet; $.type=css; $.href=\"\/my.css\"; }),\n       script($->{ $.type= javascript; $.src=\"\/some.js\"; })\n   ),\n   body(\n       div($-> $.cssClass = \"article\",\n           a($-> $.href=\"https:\/\/benjiweber.com\/\",\n               span($->$.cssClass=\"label\", \"Click Here\"),\n               img($->{$.src=\"\/htmldsl2.png\"; $.width=px(25); $.height=px(25); })\n           ),\n           p(span(\"some text\"), div(\"block\"))\n       )\n   )\n)<\/pre>\n\n\n\n<p>This generates the following html<\/p>\n\n\n\n<pre lang=\"java\"><html>\n <head>\n   <title>Hello Html World<\/title>\n   <meta charset=\"utf-8\" \/>\n   <link rel=\"stylesheet\" type=\"css\" href=\"\/my.css\" \/>\n   <script type=\"text\/javascript\" src=\"\/some.js\" ><\/script>\n <\/head>\n <body>\n   <div class=\"article\">\n     <a href=\"https:\/\/benjiweber.com\/\">\n       <span class=\"label\">Click Here<\/span>\n       <img loading=\"lazy\" src=\"\/htmldsl2.png\" width=\"25\" height=\"25\" \/>\n     <\/a>\n     <p>\n       <span>some text<\/span>\n       <div>block<\/div>\n     <\/p>\n   <\/div>\n <\/body>\n<\/html>\n<\/pre>\n\n\n\n<p>You get nice autocompletion, and feedback if you specify inappropriate values:<\/p>\n\n\n\n<img src=\"https:\/\/benjiweber.co.uk\/linkrel.gif\" style=\"width:100%;margin-bottom:20px;\"\/>\n\n\n\n<p>You&#8217;ll also get a helping hand from the types to not put tags in inappropriate places:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/benjiweber.com\/htmldsl2b.png\" alt=\"\" style=\"width:100%; margin-bottom:20px\"\/><\/figure>\n\n\n\n<h2>Generating Code<\/h2>\n\n\n\n<p>As it&#8217;s Java you can easily mix other code to generate markup dynamically:<\/p>\n\n\n\n<pre lang=\"java\">assertEquals(\n       \"\"\"\n       <html>\n         <head>\n           <meta charset=\"utf-8\" \/>\n         <\/head>\n         <body>\n           <p>Paragraph one<\/p>\n           <p>Paragraph two<\/p>\n           <p>Paragraph three<\/p>\n         <\/body>\n       <\/html>\n       \"\"\".trim(),\n       html(\n           head(\n               meta($ -> $.charset = \"utf-8\")\n           ),\n           body(\n               Stream.of(\"one\",\"two\",\"three\")\n                   .map(number -> \"Paragraph \" + number)\n                   .map(content -> p(content))\n           )\n       ).formatted()\n);<\/pre>\n\n\n\n<p>And the code can help you avoid injection attacks by escaping literal values:\u00a0<\/p>\n\n\n\n<pre lang=\"java\">assertEquals(\n       \"\"\"\n       <html>\n         <head>\n           <meta charset=\"utf-8\" \/>\n         <\/head>\n         <body>\n           <p>&lt;script src=\"attack.js\"&gt;&lt;\/script&gt;<\/p>\n         <\/body>\n       <\/html>\n       \"\"\".trim(),\n       html(\n           head(\n               meta($-> $.charset = \"utf-8\")\n           ),\n           body(\n               p(\"<script src=\\\"attack.js\\\"><\/script>\")\n           )\n       ).formatted()\n);\n<\/pre>\n\n\n\n<h2>How does it work?<\/h2>\n\n\n\n<p>There&#8217;s only one &#8220;trick&#8221; here that&#8217;s particularly useful for DSLs. Using the Parameter Objects pattern from my <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/04\/lambda-type-references\/#!:~:text=Parameter%20Objects\">lambda type references<\/a> post.&nbsp;<\/p>\n\n\n\n<p>The lambdas used for specifying the tag attributes are &#8220;aware&#8221; of their own types. And capable of instantiating the configuration they specify.<\/p>\n\n\n\n<p>When we call&nbsp;<\/p>\n\n\n\n<pre lang=\"java\">meta($ -> $.charset=\"utf-8\")<\/pre>\n\n\n\n<p>We make a call to&nbsp;<\/p>\n\n\n\n<pre lang=\"java\">default Meta meta(Parameters<Meta> params, Tag... children) { \u2026 }\n<\/pre>\n\n\n\n<p>The lambda specifying the attribute config is structurally equivalent to the Parameters&lt;Meta> type. This provides a get() function that instantiates an instance of Meta, and then passes the new instance to the lambda function to apply the config.<\/p>\n\n\n\n<pre lang=\"java\">public interface Parameters<T> extends NewableConsumer<T> {\n   default T get() {\n       T t = newInstance();\n       accept(t);\n       return t;\n   }\n}<\/pre>\n\n\n\n<p>Under the hood the newInstance() method <a href=\"https:\/\/github.com\/benjiman\/java-html-dsl2\/blob\/97a9624d35cc3d735107f3dae920259544458f0c\/src\/main\/java\/com\/benjiweber\/typeref\/MethodFinder.java#L32\">uses reflection to examine the SerializedLambda contents<\/a> and find the type parameter (in this case &#8220;Meta&#8221;) before instantiating it.<\/p>\n\n\n\n<p>You can <a href=\"https:\/\/github.com\/benjiman\/java-html-dsl2\/blob\/main\/src\/main\/java\/com\/benjiweber\/typeref\/NewableConsumer.java\">follow the code<\/a> or see <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/04\/lambda-type-references\/#!:~:text=Parameter%20Objects\">the previous post <\/a>which explains it in a bit more detail.<\/p>\n\n\n\n<h2>Add Mixins<\/h2>\n\n\n\n<p>It&#8217;s helpful to use interfaces as mixins to avoid having to have one enormous class with all the builder definitions.&nbsp;<\/p>\n\n\n\n<pre lang=\"java\">public interface HtmlDsl extends\n   Html.Dsl,\n   Head.Dsl,\n   Title.Dsl,\n   Meta.Dsl,\n   Link.Dsl,\n   Script.Dsl,\n   Body.Dsl,\n   Div.Dsl,\n   Span.Dsl,\n   A.Dsl,\n   P.Dsl,\n   Img.Dsl {}<\/pre>\n\n\n\n<p>Each tag definition then contains its own builder methods. We compose them together into a single HtmlDsl interface for convenience. This saves having to import hundreds of different methods. By implementing the Dsl interface a consumer gets access to all the builder methods.<\/p>\n\n\n\n<figure class=\"wp-block-image\" style=\"height: 194px; overflow:hidden;\"><img src=\"https:\/\/benjiweber.co.uk\/htmldsl2.png\" alt=\"\" \/><\/figure>\n\n\n\n<h2>Show me the code<\/h2>\n\n\n\n<p>It&#8217;s <a href=\"https:\/\/github.com\/benjiman\/java-html-dsl2\">all on github<\/a>. I&#8217;d start from the <a href=\"https:\/\/github.com\/benjiman\/java-html-dsl2\/blob\/main\/src\/test\/java\/com\/benjiweber\/html\/HtmlExampleTest.java\">test examples<\/a>. Bear in mind that it&#8217;s merely a port of the old proof of concept to a slightly different approach. I hope it helps illustrate the technique. It&#8217;s in no way attempting to be a complete implementation.<\/p>\n\n\n\n<p>This approach can also be useful as an alternative to the builder pattern for passing a specification or configuration to a method. There&#8217;s another example on the <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/04\/lambda-type-references\/#!:~:text=Parameter%20Objects\">type references<\/a> article.<\/p>\n\n\n\n<p>What else could you use this technique for?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago I wrote a post about creating an embedded dsl for Html in Java. Sadly, it was based on an abuse of lambda name reflection that was later removed from Java. I thought I should do a followup because a lot of people still visit the old article. While it&#8217;s no longer possible&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/\" class=\"more-link\" title=\"Read Revisiting Html in Java\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"description\" content=\"A followup to my article on embedded html dsl in Java. This one works with more modern Java. Exploiting a different lambda feature.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Revisiting Html in Java - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"A followup to my article on embedded html dsl in Java. This one works with more modern Java. Exploiting a different lambda feature.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-28T15:47:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-28T15:59:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/benjiweber.co.uk\/htmldsl2.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"\/htmldsl2.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/\",\"name\":\"Revisiting Html in Java - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/#primaryimage\"},\"datePublished\":\"2021-02-28T15:47:55+00:00\",\"dateModified\":\"2021-02-28T15:59:31+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"description\":\"A followup to my article on embedded html dsl in Java. This one works with more modern Java. Exploiting a different lambda feature.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2021\/02\/28\/revisiting-html-in-java\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":false,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1594"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=1594"}],"version-history":[{"count":40,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1594\/revisions"}],"predecessor-version":[{"id":1636,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1594\/revisions\/1636"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=1594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=1594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=1594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}