Starting a web browser

Revision as of 17:00, 10 August 2014 by rosettacode>Siskus (Scala added)

Build and show a web page displaying the separated house or streetnubers in a formatted and colored table.

Starting a web browser is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Write the code which automatically opens a web page in a browser showing the addresses.
No static data must be shown, only processed data.

Scala

Library: Scala

===None of its uses other than Standard Library===<lang scala>import java.awt.Desktop import java.io.{IOException, PrintWriter} import java.net.{URI, ServerSocket} import scala.xml.Elem

class WebServer(port: Int, soleDocument: Elem) extends Thread {

 this.setName(s"Server at $port")
 override def run() {
   val listener = try {
     new ServerSocket(port)
   } catch {
     case e: java.net.BindException => throw new IllegalStateException(s"Port $port already taken!")
   }
   println(s"Listening on port ${listener.getLocalPort}")
   while (!Thread.interrupted()) {
     try {
       //print(".")
       val socket = listener.accept
       new PrintWriter(socket.getOutputStream, true).println(soleDocument)
       socket.close()
     } catch {
       case ioe: IOException => println(ioe)
     }
   }
 }

}

// class WebServer

object HtmlServer extends App {

 val PORT = 64507
 def HtmlBuilder: Elem = {
   def adressen: Iterator[String] =
     """Plataanstraat 5
       |Straat 12
       |Straat 12 II
       |Straat 1940 II
       |Dr. J. Straat   40
       |Dr. J. Straat 12 a
       |Dr. J. Straat 12-14
       |Laan 1940 – 1945 37
       |Plein 1940 2
       |1213-laan 11
       |16 april 1944 Pad 1
       |1e Kruisweg 36
       |Laan 1940-’45 66
       |Laan ’40-’45
       |Langeloërduinen 3 46
       |Marienwaerdt 2e Dreef 2
       |Provincialeweg N205 1
       |Rivium 2e Straat 59.
       |Nieuwe gracht 20rd
       |Nieuwe gracht 20rd 2
       |Nieuwe gracht 20zw /2
       |Nieuwe gracht 20zw/3
       |Nieuwe gracht 20 zw/4
       |Bahnhofstr. 4
       |Wertstr. 10
       |Lindenhof 1
       |Nordesch 20
       |Weilstr. 6
       |Harthauer Weg 2
       |Mainaustr. 49
       |August-Horch-Str. 3
       |Marktplatz 31
       |Schmidener Weg 3
       |Karl-Weysser-Str. 6""".stripMargin.lines
   def getSplittedAddresses(addresses: Iterator[String]) = {
     val extractor = new scala.util.matching.Regex( """(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$|\d+\['][40|45]$""")


     def splitsAdres(input: String): (String, String) =
       (extractor.split(input).mkString, extractor.findFirstIn(input).getOrElse(""))
     addresses.map(org => {
       val temp = splitsAdres(org)
       List(org, temp._1, temp._2)
     })
   }
   def generateTable: Elem = {
     def coloring(rownum: Any): String = {
       rownum match {
         case Nil => "#9bbb59"
         case n: Int => if (n % 2 == 0) "#ebf1de" else "#d8e4bc"
       }
     }
{(List(List("Given Address", "Street", "House Number")) ++ getSplittedAddresses(adressen)). zipWithIndex.map { case (row, rownum) => (if (rownum == 0) Nil else rownum) +: row}.map(row => {row.map(cell => if (row.head == Nil) else )} )}
             {cell}
             {cell}
   } // def generateTable
   <html>
     <head>
       <title>Rosetta.org Task solution</title>
     </head>
     <body lang="en-US" bgcolor="#e6e6ff" dir="LTR">

Split the house number from the street name

{generateTable}

     </body>
   </html>
 } // def content
 // Main //
 val thread = new WebServer(PORT, HtmlBuilder)
 thread.start()
 val uri = URI.create(s"http://localhost:$PORT/")
 if (Desktop.isDesktopSupported && Desktop.getDesktop.isSupported(Desktop.Action.BROWSE))
   Desktop.getDesktop.browse(uri)
 else println(s"Automatic start of Web browser not possible.\nWeb browser must be started manually, use $uri.")
 if (!thread.isAlive) sys.exit(-1)
 println("Web server started.")
 do print("Do you want to shutdown this server? <Y(es)/N>: ") while (!readBoolean)
 sys.exit()

}</lang>

Output:

Split the house number from the street name

Given AddressStreetHouse Number
1Plataanstraat 5Plataanstraat5
2Straat 12Straat12
3Straat 12 IIStraat12 II
4Straat 1940 IIStraat 1940 II
5Dr. J. Straat40Dr. J. Straat 40
6Dr. J. Straat 12 aDr. J. Straat12 a
7Dr. J. Straat 12-14Dr. J. Straat12-14
8Laan 1940 – 1945 37Laan 1940 – 194537
9Plein 1940 2Plein 19402
101213-laan 111213-laan11
1116 april 1944 Pad 116 april 1944 Pad1
121e Kruisweg 361e Kruisweg36
13Laan 1940-’45 66Laan 1940-’4566
14Laan ’40-’45Laan ’40-’45
15Langeloërduinen 3 46Langeloërduinen3 46
16Marienwaerdt 2e Dreef 2Marienwaerdt 2e Dreef2
17Provincialeweg N205 1Provincialeweg N2051
18Rivium 2e Straat 59.Rivium 2e Straat59.
19Nieuwe gracht 20rdNieuwe gracht20rd
20Nieuwe gracht 20rd 2Nieuwe gracht20rd 2
21Nieuwe gracht 20zw /2Nieuwe gracht20zw /2
22Nieuwe gracht 20zw/3Nieuwe gracht20zw/3
23Nieuwe gracht 20 zw/4Nieuwe gracht20 zw/4
24Bahnhofstr. 4Bahnhofstr.4
25Wertstr. 10Wertstr.10
26Lindenhof 1Lindenhof1
27Nordesch 20Nordesch20
28Weilstr. 6Weilstr.6
29Harthauer Weg 2Harthauer Weg2
30Mainaustr. 49Mainaustr.49
31August-Horch-Str. 3August-Horch-Str.3
32Marktplatz 31Marktplatz31
33Schmidener Weg 3Schmidener Weg3
34Karl-Weysser-Str. 6Karl-Weysser-Str.6