Browse code

add css, html, java, javascript and txt conversion

devnewton authored on 31/01/2016 17:53:24
Showing 14 changed files
... ...
@@ -1,66 +1,76 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
-	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
-	<modelVersion>4.0.0</modelVersion>
5
-	<parent>
6
-		<groupId>org.springframework.boot</groupId>
7
-		<artifactId>spring-boot-starter-parent</artifactId>
8
-		<version>1.3.1.RELEASE</version>
9
-	</parent>
10
-	<artifactId>a2html</artifactId>
11
-	<groupId>im.bci</groupId>
12
-	<name>a2html</name>
13
-	<description>convert text to ascii html</description>
14
-	<version>1.0-SNAPSHOT</version>
15
-	<url>http://devnewton.bci.im</url>
16
-	<properties>
17
-		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
-		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19
-	</properties>
20
-	<dependencies>
21
-		<dependency>
22
-			<groupId>org.springframework.boot</groupId>
23
-			<artifactId>spring-boot-starter</artifactId>
24
-		</dependency>
25
-		<dependency>
26
-			<groupId>pl.allegro.tech.boot</groupId>
27
-			<artifactId>handlebars-spring-boot-starter</artifactId>
28
-			<version>0.2.8</version>
29
-		</dependency>
30
-		<dependency>
31
-			<groupId>org.jsoup</groupId>
32
-			<artifactId>jsoup</artifactId>
33
-			<version>1.8.3</version>
34
-		</dependency>
35
-		<dependency>
36
-			<groupId>org.webjars</groupId>
37
-			<artifactId>jquery</artifactId>
38
-			<version>3.0.0-alpha1</version>
39
-		</dependency>
40
-	</dependencies>
41
-	<build>
42
-		<resources>
43
-			<resource>
44
-				<directory>src/main/resources</directory>
45
-				<filtering>false</filtering>
46
-				<includes>
47
-					<include>**/*.html</include>
48
-					<include>**/*.properties</include>
49
-					<include>**/*.xml</include>
50
-					<include>**/*.js</include>
51
-					<include>**/*.css</include>
52
-					<include>**/*.png</include>
53
-				</includes>
54
-			</resource>
55
-		</resources>
56
-		<plugins>
57
-			<plugin>
58
-				<groupId>org.springframework.boot</groupId>
59
-				<artifactId>spring-boot-maven-plugin</artifactId>
60
-				<configuration>
61
-					<mainClass>im.bci.a2html.A2htmlApplication</mainClass>
62
-				</configuration>
63
-			</plugin>
64
-		</plugins>
65
-	</build>
3
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+    <modelVersion>4.0.0</modelVersion>
5
+    <parent>
6
+        <groupId>org.springframework.boot</groupId>
7
+        <artifactId>spring-boot-starter-parent</artifactId>
8
+        <version>1.3.2.RELEASE</version>
9
+    </parent>
10
+    <artifactId>a2html</artifactId>
11
+    <groupId>im.bci</groupId>
12
+    <name>a2html</name>
13
+    <description>convert text to ascii html</description>
14
+    <version>1.0-SNAPSHOT</version>
15
+    <url>http://devnewton.bci.im</url>
16
+    <properties>
17
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19
+    </properties>
20
+    <dependencies>
21
+        <dependency>
22
+            <groupId>org.springframework.boot</groupId>
23
+            <artifactId>spring-boot-starter</artifactId>
24
+        </dependency>
25
+        <dependency>
26
+            <groupId>pl.allegro.tech.boot</groupId>
27
+            <artifactId>handlebars-spring-boot-starter</artifactId>
28
+            <version>0.2.8</version>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>org.jsoup</groupId>
32
+            <artifactId>jsoup</artifactId>
33
+            <version>1.8.3</version>
34
+        </dependency>
35
+        <dependency>
36
+            <groupId>org.unbescape</groupId>
37
+            <artifactId>unbescape</artifactId>
38
+            <version>1.1.2.RELEASE</version>
39
+        </dependency>
40
+        <dependency>
41
+            <groupId>com.moparisthebest</groupId>
42
+            <artifactId>junidecode</artifactId>
43
+            <version>0.1.1</version>
44
+        </dependency>
45
+        <dependency>
46
+            <groupId>org.webjars</groupId>
47
+            <artifactId>jquery</artifactId>
48
+            <version>3.0.0-alpha1</version>
49
+        </dependency>
50
+    </dependencies>
51
+    <build>
52
+        <resources>
53
+            <resource>
54
+                <directory>src/main/resources</directory>
55
+                <filtering>false</filtering>
56
+                <includes>
57
+                    <include>**/*.html</include>
58
+                    <include>**/*.properties</include>
59
+                    <include>**/*.xml</include>
60
+                    <include>**/*.js</include>
61
+                    <include>**/*.css</include>
62
+                    <include>**/*.png</include>
63
+                </includes>
64
+            </resource>
65
+        </resources>
66
+        <plugins>
67
+            <plugin>
68
+                <groupId>org.springframework.boot</groupId>
69
+                <artifactId>spring-boot-maven-plugin</artifactId>
70
+                <configuration>
71
+                    <mainClass>im.bci.a2html.A2htmlApplication</mainClass>
72
+                </configuration>
73
+            </plugin>
74
+        </plugins>
75
+    </build>
66 76
 </project>
... ...
@@ -1,5 +1,6 @@
1 1
 package im.bci.a2html.controllers;
2 2
 
3
+import im.bci.a2html.converters.Converter;
3 4
 import org.jsoup.Jsoup;
4 5
 import org.jsoup.nodes.Document.OutputSettings;
5 6
 import org.jsoup.nodes.Entities.EscapeMode;
... ...
@@ -11,16 +12,26 @@ import org.springframework.web.bind.annotation.RestController;
11 12
 
12 13
 import im.bci.a2html.frontend.ConvertMV;
13 14
 import im.bci.a2html.frontend.ConvertRQ;
15
+import org.springframework.beans.factory.annotation.Autowired;
16
+import org.unbescape.java.JavaEscape;
17
+import org.unbescape.java.JavaEscapeLevel;
14 18
 
15 19
 @RestController
16 20
 @RequestMapping("/ajax")
17 21
 public class A2htmlAjaxController {
18 22
 
23
+    @Autowired
24
+    private Converter[] converters;
25
+
19 26
     @RequestMapping(value = "/convert", method = RequestMethod.POST)
20 27
     public ConvertMV convert(ConvertRQ rq, Model model) {
21 28
         ConvertMV mv = new ConvertMV();
22
-        String cleaned = Jsoup.clean(rq.getText(), "", Whitelist.basic(), new OutputSettings().prettyPrint(false).escapeMode(EscapeMode.extended).charset("ASCII"));
23
-        mv.setConvertedText(cleaned);
29
+        for (Converter converter : converters) {
30
+            if (rq.getConversion() == converter.getType()) {
31
+                mv.setConvertedText(converter.convert(rq.getText()));
32
+                break;
33
+            }
34
+        }
24 35
         return mv;
25 36
     }
26 37
 
27 38
new file mode 100644
... ...
@@ -0,0 +1,13 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+
5
+/**
6
+ *
7
+ * @author devnewton
8
+ */
9
+public interface Converter {
10
+    ConversionType getType();
11
+    
12
+    String convert(String text);
13
+}
0 14
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import org.springframework.stereotype.Component;
5
+import org.unbescape.css.CssEscape;
6
+import org.unbescape.css.CssStringEscapeLevel;
7
+import org.unbescape.css.CssStringEscapeType;
8
+
9
+/**
10
+ *
11
+ * @author devnewton
12
+ */
13
+@Component
14
+public class CssConverter implements Converter {
15
+
16
+    @Override
17
+    public ConversionType getType() {
18
+        return ConversionType.TO_CSS;
19
+    }
20
+
21
+    @Override
22
+    public String convert(String text) {
23
+        return CssEscape.escapeCssString(text, CssStringEscapeType.COMPACT_HEXA, CssStringEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
24
+    }
25
+
26
+}
0 27
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import org.jsoup.Jsoup;
5
+import org.jsoup.nodes.Document;
6
+import org.jsoup.nodes.Entities;
7
+import org.jsoup.safety.Whitelist;
8
+import org.springframework.stereotype.Component;
9
+
10
+/**
11
+ *
12
+ * @author devnewton
13
+ */
14
+@Component
15
+public class HtmlConverter implements Converter {
16
+
17
+    @Override
18
+    public ConversionType getType() {
19
+        return ConversionType.TO_HTML;
20
+    }
21
+
22
+    @Override
23
+    public String convert(String text) {
24
+        String cleaned = Jsoup.clean(text, "", Whitelist.basicWithImages(), new Document.OutputSettings().prettyPrint(false).escapeMode(Entities.EscapeMode.extended).charset("ASCII"));
25
+        return cleaned;
26
+    }
27
+
28
+}
0 29
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import org.springframework.stereotype.Component;
5
+import org.unbescape.java.JavaEscape;
6
+import org.unbescape.java.JavaEscapeLevel;
7
+
8
+/**
9
+ *
10
+ * @author devnewton
11
+ */
12
+@Component
13
+public class JavaConverter implements Converter {
14
+
15
+    @Override
16
+    public ConversionType getType() {
17
+        return ConversionType.TO_JAVA;
18
+    }
19
+
20
+    @Override
21
+    public String convert(String text) {
22
+        return JavaEscape.escapeJava(text, JavaEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
23
+    }
24
+
25
+}
0 26
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import org.springframework.stereotype.Component;
5
+import org.unbescape.javascript.JavaScriptEscape;
6
+import org.unbescape.javascript.JavaScriptEscapeLevel;
7
+import org.unbescape.javascript.JavaScriptEscapeType;
8
+
9
+/**
10
+ *
11
+ * @author devnewton
12
+ */
13
+@Component
14
+public class JavascriptConverter implements Converter {
15
+
16
+    @Override
17
+    public ConversionType getType() {
18
+        return ConversionType.TO_JAVASCRIPT;
19
+    }
20
+
21
+    @Override
22
+    public String convert(String text) {
23
+        return JavaScriptEscape.escapeJavaScript(text, JavaScriptEscapeType.UHEXA, JavaScriptEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
24
+    }
25
+
26
+}
0 27
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import org.springframework.stereotype.Component;
5
+import org.unbescape.json.JsonEscape;
6
+import org.unbescape.json.JsonEscapeLevel;
7
+import org.unbescape.json.JsonEscapeType;
8
+
9
+/**
10
+ *
11
+ * @author devnewton
12
+ */
13
+@Component
14
+public class JsonConverter implements Converter {
15
+
16
+    @Override
17
+    public ConversionType getType() {
18
+        return ConversionType.TO_JSON;
19
+    }
20
+
21
+    @Override
22
+    public String convert(String text) {
23
+        return JsonEscape.escapeJson(text, JsonEscapeType.UHEXA, JsonEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
24
+    }
25
+
26
+}
0 27
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import org.springframework.stereotype.Component;
5
+import org.unbescape.properties.PropertiesEscape;
6
+import org.unbescape.properties.PropertiesValueEscapeLevel;
7
+
8
+/**
9
+ *
10
+ * @author devnewton
11
+ */
12
+@Component
13
+public class PropertiesConverter implements Converter {
14
+
15
+    @Override
16
+    public ConversionType getType() {
17
+        return ConversionType.TO_PROPERTIES;
18
+    }
19
+
20
+    @Override
21
+    public String convert(String text) {
22
+        return PropertiesEscape.escapePropertiesValue(text, PropertiesValueEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
23
+    }
24
+
25
+}
0 26
new file mode 100644
... ...
@@ -0,0 +1,24 @@
1
+package im.bci.a2html.converters;
2
+
3
+import im.bci.a2html.frontend.ConversionType;
4
+import net.sf.junidecode.Junidecode;
5
+import org.springframework.stereotype.Component;
6
+
7
+/**
8
+ *
9
+ * @author devnewton
10
+ */
11
+@Component
12
+public class TxtConverter implements Converter {
13
+
14
+    @Override
15
+    public ConversionType getType() {
16
+        return ConversionType.TO_TXT;
17
+    }
18
+
19
+    @Override
20
+    public String convert(String text) {
21
+        return Junidecode.unidecode(text);
22
+    }
23
+
24
+}
0 25
new file mode 100644
... ...
@@ -0,0 +1,15 @@
1
+package im.bci.a2html.frontend;
2
+
3
+/**
4
+ *
5
+ * @author devnewton
6
+ */
7
+public enum ConversionType {
8
+    TO_CSS,
9
+    TO_HTML,
10
+    TO_JAVA,
11
+    TO_JAVASCRIPT,
12
+    TO_JSON,
13
+    TO_PROPERTIES,
14
+    TO_TXT
15
+}
... ...
@@ -6,6 +6,7 @@ public class ConvertRQ implements Serializable {
6 6
 
7 7
     private static final long serialVersionUID = 1L;
8 8
     private String text;
9
+    private ConversionType conversion = ConversionType.TO_HTML;
9 10
 
10 11
     public String getText() {
11 12
         return text;
... ...
@@ -15,4 +16,12 @@ public class ConvertRQ implements Serializable {
15 16
         this.text = text;
16 17
     }
17 18
 
19
+    public ConversionType getConversion() {
20
+        return conversion;
21
+    }
22
+
23
+    public void setConversion(ConversionType conversion) {
24
+        this.conversion = conversion;
25
+    }
26
+
18 27
 }
... ...
@@ -1,9 +1,9 @@
1
-$("#convert").click(function() {
2
-	$.ajax({
3
-		url : "/ajax/convert",
4
-		method: "POST",
5
-		data: { text : $("#input").val() }
6
-	}).done(function(data) {
7
-		$("#output").val(data.convertedText);
8
-	})
1
+$("#convert").click(function () {
2
+    $.ajax({
3
+        url: "/ajax/convert",
4
+        method: "POST",
5
+        data: {text: $("#input").val(), conversion: $('#conversion').val()}
6
+    }).done(function (data) {
7
+        $("#output").val(data.convertedText);
8
+    })
9 9
 })
10 10
\ No newline at end of file
... ...
@@ -1,19 +1,35 @@
1 1
 <!DOCTYPE html>
2 2
 <html>
3
-<head>
4
-<title>a2html</title>
5
-<meta charset="UTF-8">
6
-<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
-<meta name="robots" content="noindex,nofollow">
8
-<link rel="icon" type="image/png" href="/favicon.png">
9
-</head>
10
-<body>
11
-	<label for="input">Input:</label>
12
-	<textarea id="input" rows="20" cols="80"></textarea>
13
-	<input id="convert" type="button" value="Convert">
14
-	<label for="output">Output</label>
15
-	<textarea id="output" rows="20" cols="80"></textarea>
16
-	<script src="/webjars/jquery/3.0.0-alpha1/jquery.js" defer></script>
17
-	<script src="/a2html.js" defer></script>
18
-</body>
3
+    <head>
4
+        <title>a2html</title>
5
+        <meta charset="UTF-8">
6
+        <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+        <meta name="robots" content="noindex,nofollow">
8
+        <link rel="icon" type="image/png" href="/favicon.png">
9
+    </head>
10
+    <body>
11
+        <label for="conversion">Conversion:</label>
12
+        <select id="conversion" name="conversion">
13
+            <optgroup label="Web">
14
+                <option value="TO_HTML">to html</option>
15
+                <option value="TO_CSS">to css</option>
16
+            </optgroup>
17
+            <optgroup label="Programming">
18
+                <option value="TO_JAVA">to java</option>
19
+                <option value="TO_JAVASCRIPT">to javascript</option>
20
+            </optgroup>
21
+            <optgroup label="Data">
22
+                <option value="TO_JSON">to json</option>
23
+                <option value="TO_PROPERTIES">to properties</option>
24
+                <option value="TO_TXT">to text</option>
25
+            </optgroup>
26
+        </select>
27
+        <input id="convert" type="button" value="Convert">
28
+        <label for="input">Input:</label>
29
+        <textarea id="input" rows="20" cols="80"></textarea>
30
+        <label for="output">Output</label>
31
+        <textarea id="output" rows="20" cols="80"></textarea>
32
+        <script src="/webjars/jquery/3.0.0-alpha1/jquery.js" defer></script>
33
+        <script src="/a2html.js" defer></script>
34
+    </body>
19 35
 </html>
20 36
\ No newline at end of file