Starting a web browser: Difference between revisions
Content added Content deleted
(Task created) |
(Scala added) |
||
Line 3: | Line 3: | ||
;Task: |
;Task: |
||
Write the code which automatically opens a web page in a browser showing the addresses.<br /><span style="color:#FFFFFF; background:#FF69B4">No static data must be shown, only processed data.</span> |
Write the code which automatically opens a web page in a browser showing the addresses.<br /><span style="color:#FFFFFF; background:#FF69B4">No static data must be shown, only processed data.</span> |
||
=={{header|Scala}}== |
|||
[[Category:Scala Implementations]] |
|||
{{libheader|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" |
|||
} |
|||
} |
|||
<table border="10"> |
|||
{(List(List("Given Address", "Street", "House Number")) ++ getSplittedAddresses(adressen)). |
|||
zipWithIndex.map { case (row, rownum) => (if (rownum == 0) Nil else rownum) +: row}.map(row => |
|||
<tr bgcolor={coloring(row.head)}> |
|||
{row.map(cell => |
|||
if (row.head == Nil) |
|||
<th> |
|||
{cell} |
|||
</th> |
|||
else |
|||
<td> |
|||
{cell} |
|||
</td>)} |
|||
</tr>)} |
|||
</table> |
|||
} // def generateTable |
|||
<html> |
|||
<head> |
|||
<title>Rosetta.org Task solution</title> |
|||
</head> |
|||
<body lang="en-US" bgcolor="#e6e6ff" dir="LTR"> |
|||
<p align="CENTER"> |
|||
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font> |
|||
</p> |
|||
<p align="CENTER"> |
|||
{generateTable} |
|||
</p> |
|||
</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>{{out}}<font face="Arial, sans-serif" size="5">Split the house number from the street name</font> |
|||
<p align="center"><table border="10"><tr bgcolor="#9bbb59"><th></th><th>Given Address</th><th>Street</th><th>House Number</th></tr><tr bgcolor="#d8e4bc"><td>1</td><td>Plataanstraat 5</td><td>Plataanstraat</td><td>5</td></tr><tr bgcolor="#ebf1de"><td>2</td><td>Straat 12</td><td>Straat</td><td>12</td></tr><tr bgcolor="#d8e4bc"><td>3</td><td>Straat 12 II</td><td>Straat</td><td>12 II</td></tr><tr bgcolor="#ebf1de"><td>4</td><td>Straat 1940 II</td><td>Straat 1940 II</td><td></td></tr><tr bgcolor="#d8e4bc"><td>5</td><td>Dr. J. Straat40</td><td>Dr. J. Straat </td><td>40</td></tr><tr bgcolor="#ebf1de"><td>6</td><td>Dr. J. Straat 12 a</td><td>Dr. J. Straat</td><td>12 a</td></tr><tr bgcolor="#d8e4bc"><td>7</td><td>Dr. J. Straat 12-14</td><td>Dr. J. Straat</td><td>12-14</td></tr><tr bgcolor="#ebf1de"><td>8</td><td>Laan 1940 – 1945 37</td><td>Laan 1940 – 1945</td><td>37</td></tr><tr bgcolor="#d8e4bc"><td>9</td><td>Plein 1940 2</td><td>Plein 1940</td><td>2</td></tr><tr bgcolor="#ebf1de"><td>10</td><td>1213-laan 11</td><td>1213-laan</td><td>11</td></tr><tr bgcolor="#d8e4bc"><td>11</td><td>16 april 1944 Pad 1</td><td>16 april 1944 Pad</td><td>1</td></tr><tr bgcolor="#ebf1de"><td>12</td><td>1e Kruisweg 36</td><td>1e Kruisweg</td><td>36</td></tr><tr bgcolor="#d8e4bc"><td>13</td><td>Laan 1940-’45 66</td><td>Laan 1940-’45</td><td>66</td></tr><tr bgcolor="#ebf1de"><td>14</td><td>Laan ’40-’45</td><td>Laan ’40-’45</td><td></td></tr><tr bgcolor="#d8e4bc"><td>15</td><td>Langeloërduinen 3 46</td><td>Langeloërduinen</td><td>3 46</td></tr><tr bgcolor="#ebf1de"><td>16</td><td>Marienwaerdt 2e Dreef 2</td><td>Marienwaerdt 2e Dreef</td><td>2</td></tr><tr bgcolor="#d8e4bc"><td>17</td><td>Provincialeweg N205 1</td><td>Provincialeweg N205</td><td>1</td></tr><tr bgcolor="#ebf1de"><td>18</td><td>Rivium 2e Straat 59.</td><td>Rivium 2e Straat</td><td>59.</td></tr><tr bgcolor="#d8e4bc"><td>19</td><td>Nieuwe gracht 20rd</td><td>Nieuwe gracht</td><td>20rd</td></tr><tr bgcolor="#ebf1de"><td>20</td><td>Nieuwe gracht 20rd 2</td><td>Nieuwe gracht</td><td>20rd 2</td></tr><tr bgcolor="#d8e4bc"><td>21</td><td>Nieuwe gracht 20zw /2</td><td>Nieuwe gracht</td><td>20zw /2</td></tr><tr bgcolor="#ebf1de"><td>22</td><td>Nieuwe gracht 20zw/3</td><td>Nieuwe gracht</td><td>20zw/3</td></tr><tr bgcolor="#d8e4bc"><td>23</td><td>Nieuwe gracht 20 zw/4</td><td>Nieuwe gracht</td><td>20 zw/4</td></tr><tr bgcolor="#ebf1de"><td>24</td><td>Bahnhofstr. 4</td><td>Bahnhofstr.</td><td>4</td></tr><tr bgcolor="#d8e4bc"><td>25</td><td>Wertstr. 10</td><td>Wertstr.</td><td>10</td></tr><tr bgcolor="#ebf1de"><td>26</td><td>Lindenhof 1</td><td>Lindenhof</td><td>1</td></tr><tr bgcolor="#d8e4bc"><td>27</td><td>Nordesch 20</td><td>Nordesch</td><td>20</td></tr><tr bgcolor="#ebf1de"><td>28</td><td>Weilstr. 6</td><td>Weilstr.</td><td>6</td></tr><tr bgcolor="#d8e4bc"><td>29</td><td>Harthauer Weg 2</td><td>Harthauer Weg</td><td>2</td></tr><tr bgcolor="#ebf1de"><td>30</td><td>Mainaustr. 49</td><td>Mainaustr.</td><td>49</td></tr><tr bgcolor="#d8e4bc"><td>31</td><td>August-Horch-Str. 3</td><td>August-Horch-Str.</td><td>3</td></tr><tr bgcolor="#ebf1de"><td>32</td><td>Marktplatz 31</td><td>Marktplatz</td><td>31</td></tr><tr bgcolor="#d8e4bc"><td>33</td><td>Schmidener Weg 3</td><td>Schmidener Weg</td><td>3</td></tr><tr bgcolor="#ebf1de"><td>34</td><td>Karl-Weysser-Str. 6</td><td>Karl-Weysser-Str.</td><td>6</td></tr></table></p> |
Revision as of 17:00, 10 August 2014
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.
Build and show a web page displaying the separated house or streetnubers in a formatted and colored table.
- 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
===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 Address | Street | House Number | |
---|---|---|---|
1 | Plataanstraat 5 | Plataanstraat | 5 |
2 | Straat 12 | Straat | 12 |
3 | Straat 12 II | Straat | 12 II |
4 | Straat 1940 II | Straat 1940 II | |
5 | Dr. J. Straat40 | Dr. J. Straat | 40 |
6 | Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
7 | Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
8 | Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
9 | Plein 1940 2 | Plein 1940 | 2 |
10 | 1213-laan 11 | 1213-laan | 11 |
11 | 16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
12 | 1e Kruisweg 36 | 1e Kruisweg | 36 |
13 | Laan 1940-’45 66 | Laan 1940-’45 | 66 |
14 | Laan ’40-’45 | Laan ’40-’45 | |
15 | Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
16 | Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
17 | Provincialeweg N205 1 | Provincialeweg N205 | 1 |
18 | Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
19 | Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
20 | Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
21 | Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
22 | Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
23 | Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
24 | Bahnhofstr. 4 | Bahnhofstr. | 4 |
25 | Wertstr. 10 | Wertstr. | 10 |
26 | Lindenhof 1 | Lindenhof | 1 |
27 | Nordesch 20 | Nordesch | 20 |
28 | Weilstr. 6 | Weilstr. | 6 |
29 | Harthauer Weg 2 | Harthauer Weg | 2 |
30 | Mainaustr. 49 | Mainaustr. | 49 |
31 | August-Horch-Str. 3 | August-Horch-Str. | 3 |
32 | Marktplatz 31 | Marktplatz | 31 |
33 | Schmidener Weg 3 | Schmidener Weg | 3 |
34 | Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |