Simple htmlspecialchars in Java

Create htmlspecialchars without considering character code in Java

I wrote it because it became necessary to perform escaping in an environment where external libraries cannot be used. Environment is Windows and Java 10

Since default arguments cannot be used for java, it seems better to overload and create ʻENT_QUOTES` as a character string argument if you think about character codes in the future.

function



public class Escape{

	
	/**
		& to &
		" to " if flag.equals("ENT_COMPAT") flag.equals("ENT_QUOTES")
		' to '	if flag.equals("ENT_QUOTES")
		< to &lt;
		> to &gt;
	*/
	public static String htmlspecialchars(String s, String flag){
		String ret = s.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;");
		if(flag.equals("ENT_COMPAT")){
			return ret.replace("\"", "&quot;");
		}else if(flag.equals("ENT_QUOTES")){
			return ret.replace("'", "&apos;").replace("\"","&quot;");
		}else if(flag.equals("ENT_NOQUOTES")){
			return ret;
		}
		return ret;
	}
}

It's not very pretty, I felt that return ret; would be fine if ʻENT_COMPAT and ʻENT_QUOTES were not specified.

main


public static void main(String[] args){
		System.out.println(htmlspecialchars("<script>alert(\"1\");</script>","ENT_COMPAT"));
		System.out.println(htmlspecialchars("<script>alert(\"1\");</script>","ENT_NOQUOTES"));
		System.out.println(htmlspecialchars("<script>alert('1');</script>","ENT_QUOTES"));
	}

result


>java Escape
&lt;script&gt;alert(&quot;1&quot;);&lt;/script&gt;
&lt;script&gt;alert("1");&lt;/script&gt;
&lt;script&gt;alert(&apos;1&apos;);&lt;/script&gt;

Recommended Posts

Simple htmlspecialchars in Java
2 Implement simple parsing in Java
Very simple input reception in Java
3 Implement a simple interpreter in Java
A simple sample callback in Java
1 Implement simple lexical analysis in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
I made a simple calculation problem game in Java
[Personal memo] Make a simple deep copy in Java
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java