Skip to content

Commit 5832265

Browse files
committed
snazzify form
1 parent eec27ce commit 5832265

File tree

6 files changed

+86
-17
lines changed

6 files changed

+86
-17
lines changed

cmdline/src/main/kotlin/com/fractalwrench/json2kotlin/App.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fun main(args: Array<String>) {
2121

2222
if (inputFile.exists()) {
2323
val outputFile = findOutputFile(inputFile)
24-
Kotlin2JsonConverter().convert(inputFile.inputStream(), outputFile.outputStream(), ConversionArgs())
24+
Kotlin2JsonConverter().convert(inputFile.inputStream(), outputFile.outputStream(), ConversionArgs()) // FIXME respect package name
2525
println("Generated source available at '$outputFile'")
2626
} else {
2727
println("Failed to find file '$inputFile'")

spring/src/main/kotlin/com/fractalwrench/json2kotlin/ConversionController.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
package com.fractalwrench.json2kotlin
22

3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.http.HttpRequest
35
import org.springframework.stereotype.Controller
46
import org.springframework.web.bind.annotation.GetMapping
57
import org.springframework.ui.Model
68
import org.springframework.web.bind.annotation.ModelAttribute
79
import org.springframework.web.bind.annotation.PostMapping
8-
import java.io.ByteArrayOutputStream
910

1011

1112
@Controller
1213
class ConversionController {
1314

15+
companion object {
16+
val formKey = "conversionForm"
17+
}
18+
19+
@Autowired
20+
lateinit var conversionService: KotlinConversionService
21+
22+
23+
24+
1425
@GetMapping("/")
1526
fun displayConversionForm(model: Model): String {
16-
model.addAttribute("conversionForm", ConversionForm())
27+
if (!model.containsAttribute(formKey)) {
28+
model.addAttribute(formKey, ConversionForm())
29+
}
1730
return "conversion"
1831
}
1932

2033
@PostMapping("/")
2134
fun convertToKotlin(model: Model, @ModelAttribute conversionForm: ConversionForm): String {
22-
val os = ByteArrayOutputStream()
23-
Kotlin2JsonConverter().convert(conversionForm.json, os, ConversionArgs())// TODO handle failure etc with redirect
24-
model.addAttribute("conversionForm", conversionForm)
35+
val os = conversionService.convert(conversionForm.json)
36+
model.addAttribute(formKey, conversionForm)
2537
model.addAttribute("kotlin", String(os.toByteArray()))
2638
return displayConversionForm(model)
2739
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.fractalwrench.json2kotlin
22

3-
class ConversionForm(var json: String = "")
3+
class ConversionForm(var json: String = "{\"foo\":\"Hello World!\"}")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fractalwrench.json2kotlin
2+
3+
import org.springframework.stereotype.Service
4+
import java.io.ByteArrayInputStream
5+
import java.io.ByteArrayOutputStream
6+
7+
@Service
8+
class KotlinConversionService {
9+
10+
companion object {
11+
val maxPayloadSize = 10000
12+
}
13+
14+
fun convert(json: String): ByteArrayOutputStream {
15+
val os = ByteArrayOutputStream()
16+
17+
if (json.length > maxPayloadSize) {
18+
throw IllegalArgumentException("JSON input cannot be larger than $maxPayloadSize")
19+
}
20+
21+
val inputStream = ByteArrayInputStream(json.toByteArray())
22+
23+
Kotlin2JsonConverter().convert(inputStream, os, ConversionArgs())
24+
return os
25+
}
26+
}

spring/src/main/resources/static/main.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
textarea {
33
width:60%;
44
resize: vertical;
5-
min-height:100px;
5+
min-height:300px;
66
}
77
body {
88
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
@@ -359,12 +359,16 @@ table tr td {
359359
pre {
360360
font-size: 16px;
361361
line-height: 1.5em;
362+
margin-left:20%;
363+
margin-right:20%;
362364
}
363365
pre code {
364366
white-space: pre;
365367
}
366368

367369
code {
370+
font-weight: bold;
371+
text-align: left;
368372
padding: 2px 4px;
369373
font-size: 90%;
370374
color: #c7254e;
@@ -382,6 +386,10 @@ code.hljs {
382386
border-color: #ccc;
383387
}
384388

389+
.j2kcontainer {
390+
margin-top:60px;
391+
}
392+
385393
/* MailChimp Form Embed Code - Horizontal Super Slim - 12/16/2015 v10.7
386394
Adapted from: http://blog.heyimcat.com/universal-signup-form/ */
387395

spring/src/main/resources/templates/conversion.html

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@
2525
</head>
2626
<body>
2727
<div class="container" style="text-align:center">
28-
<h1>Convert JSON to Kotlin Data Class</h1>
29-
<form id="jsonForm" action="#" th:action="@{/}" th:object="${conversionForm}"
30-
method="post" onsubmit="return validateForm()">
31-
<textarea placeholder="Paste JSON here..." cols="40" th:field="*{json}"></textarea>
32-
<p><input type="reset" value="Reset"/> <input type="submit" value="Convert"/></p>
33-
</form>
34-
<h2>Results</h2>
35-
<textarea th:text="${kotlin}"></textarea>
36-
</div>
28+
<div class="j2kcontainer">
29+
<h1>Convert JSON to Kotlin Data Class</h1>
30+
<form id="jsonForm" action="#" th:action="@{/}" th:object="${conversionForm}"
31+
method="post" onsubmit="return validateForm()">
32+
<textarea maxlength="10000" placeholder="Paste JSON here..." th:field="*{json}"></textarea>
33+
<p><input type="reset" value="Reset"/> <input type="submit" value="Convert"/></p>
34+
</form>
35+
</div>
36+
<div class="j2kcontainer" th:if="${kotlin != null}">
37+
<h2 id="results">Generated Kotlin</h2>
38+
<pre><code class="language-kotlin hljs" th:text="${kotlin}"></code></pre>
39+
40+
<form id="downloadForm" action="#" method="post" onsubmit="return validateForm()">
41+
<p><input type="button" value="Copy to Clipboard"/> <input type="button" value="Download"/></p>
42+
</form>
43+
</div>
44+
<div class="j2kcontainer">
45+
<!-- TODO -->
46+
</div>
3747
<footer>
3848
<div>
3949
<ul class="list-inline">
@@ -55,6 +65,7 @@ <h2>Results</h2>
5565
</footer>
5666
<link rel="stylesheet" type="text/css" href="/main.css"/>
5767
<link rel="stylesheet" type="text/css" href="/fonticons.css"/>
68+
5869
<script type="text/javascript">
5970
function validateForm() {
6071
var json = document.forms["jsonForm"]["json"].value;
@@ -65,7 +76,19 @@ <h2>Results</h2>
6576
return false;
6677
}
6778
}
79+
80+
6881
</script>
82+
83+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
84+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/>
85+
<script> hljs.initHighlightingOnLoad(); </script>
86+
87+
<script type="text/javascript" th:if="${kotlin != null}">
88+
window.location = '#results'; // autoscroll to results
89+
90+
</script>
91+
6992
</body>
7093
</html>
7194
<!-- TODO GA -->

0 commit comments

Comments
 (0)