Skip to content

Instantly share code, notes, and snippets.

@brettwooldridge
Last active November 15, 2018 09:33
Show Gist options
  • Save brettwooldridge/ab031e07b606a1a6b34a58dfca2ba14a to your computer and use it in GitHub Desktop.
Save brettwooldridge/ab031e07b606a1a6b34a58dfca2ba14a to your computer and use it in GitHub Desktop.
Java to Kotlin comparison... just anecdotal:

Interesting Kotlin/Java comparison ... this Java:

         XMLOutputFactory factory = XMLOutputFactory.newInstance();
         XMLStreamWriter xmlWriter = factory.createXMLStreamWriter(writer);

         xmlWriter.writeStartDocument();
         xmlWriter.writeStartElement("html");
         xmlWriter.writeStartElement("head");

         xmlWriter.writeStartElement("title");
         xmlWriter.writeCharacters("Summary");
         xmlWriter.writeEndElement(); // title

         // jQuery library
         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("src", "https://www.dancernetworks.com/jquery.js");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("");
         xmlWriter.writeEndElement(); // script

         // Graph generator library
         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("src", "https://www.dancernetworks.com/jquery.flot.js");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("");
         xmlWriter.writeEndElement(); // script

         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("src", "https://www.dancernetworks.com/jquery.flot.crosshair.js");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("");
         xmlWriter.writeEndElement(); // script

         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("src", "https://www.dancernetworks.com/jquery.flot.stack.js");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("");
         xmlWriter.writeEndElement(); // script

         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("src", "https://www.dancernetworks.com/jquery.flot.time.js");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("");
         xmlWriter.writeEndElement(); // script

         // Inject the last performance timestamp as a global variable
         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("var TIMEBASE=" + (PerformanceSampler.getInstance().getLastSampleTime() - TimeUnit.MINUTES.toMillis(DAILY_MIN)) + ";");
         xmlWriter.writeCharacters("var ONE_MINUTE=" + TimeUnit.MINUTES.toMillis(1) + ";");
         xmlWriter.writeEndElement(); // script

         // Table generator script
         xmlWriter.writeStartElement("script");
         xmlWriter.writeAttribute("type", "text/javascript");
         xmlWriter.writeCharacters("//");
         String javaScript = IOUtils.toString(new AutoCloseInputStream(SupportElf.class.getResourceAsStream("/META-INF/summary.js")), "UTF-8");
         xmlWriter.writeCData(javaScript);
         xmlWriter.writeCharacters("\n");
         xmlWriter.writeEndElement(); // script

         // CSS file
         xmlWriter.writeStartElement("style");
         xmlWriter.writeCharacters(IOUtils.toString(new AutoCloseInputStream(SupportElf.class.getResourceAsStream("/META-INF/summary.css")), "UTF-8"));
         xmlWriter.writeCharacters("\n");
         xmlWriter.writeEndElement();

Transformed to this Kotlin (using kotlinx.html):

   fun getSummaryHtml() = StringBuilder().appendHTML(prettyPrint = true)
      .html {
         head {
            title("Summary")
            script(type = "text/javascript", src = "https://www.dancernetworks.com/jquery.js") {}
            script(type = "text/javascript", src = "https://www.dancernetworks.com/jquery.flot.js") {}
            script(type = "text/javascript", src = "https://www.dancernetworks.com/jquery.flot.crosshair.js") {}
            script(type = "text/javascript", src = "https://www.dancernetworks.com/jquery.flot.stack.js") {}
            script(type = "text/javascript", src = "https://www.dancernetworks.com/jquery.flot.time.js") {}
            script(type = "text/javascript") {
               comment("""
                     var TIMEBASE=${(PerformanceSampler.getInstance().lastSampleTime - TimeUnit.MINUTES.toMillis(DAILY_MIN.toLong()))};
                     var ONE_MINUTE=60000;
                     ${IOUtils.toString(AutoCloseInputStream(SupportElf::class.java.getResourceAsStream("/META-INF/summary.js")), "UTF-8")}
                  """.trimIndent()
               )
            }

            style(type = "text/css") {
               unsafe {
                  +IOUtils.toString(AutoCloseInputStream(SupportElf::class.java.getResourceAsStream("/META-INF/summary.css")), "UTF-8")
               }
            }
         }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment