Escape Java/XML

mkdir escapefromreality
mkdir lib
echo 'package escapefromreality;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.text.StringEscapeUtils.Builder;

public class EscapeJavaCode {

  public static void main(String[] args) throws Exception {
    try {
      String strJava = FileUtils.readFileToString(new File("escapefromreality/EscapeJavaCode.java"));
      System.out.println(
        StringEscapeUtils.builder(StringEscapeUtils.ESCAPE_JAVA)
          .escape(strJava)
          .toString()
      );

      String strXML = FileUtils.readFileToString(new File("sample.xml"));
      System.out.println(
        StringEscapeUtils.builder(StringEscapeUtils.ESCAPE_XML11)
          .escape(strXML)
          .toString()
      );
    } catch(Exception ex) {
      // remember to handle the exception
    }
  }
}' > escapefromreality/EscapeJavaCode.java

echo '<xml>
  <sample attr="Hello"/>
  <![CDATA[Some CDATA
  section
  ]]>
</xml>' > sample.xml


curl -O http://ftp.man.poznan.pl/apache//commons/text/binaries/commons-text-1.4-bin.tar.gz
tar zxf commons-text-1.4-bin.tar.gz \
  --strip-components=1 \
  -C lib/ \
  commons-text-1.4/commons-text-1.4.jar

curl -O http://ftp.man.poznan.pl/apache//commons/io/binaries/commons-io-2.6-bin.tar.gz
tar zxf commons-io-2.6-bin.tar.gz \
  --strip-components=1 \
  -C lib/ \
  commons-io-2.6/commons-io-2.6.jar

curl -O http://ftp.ps.pl/pub/apache//commons//lang//binaries/commons-lang3-3.7-bin.tar.gz
tar -zxf commons-lang3-3.7-bin.tar.gz \
  --strip-components=1 \
  -C lib/ \
  commons-lang3-3.7/commons-lang3-3.7.jar

javac -cp lib/commons-text-1.4.jar:lib/commons-io-2.6.jar \
  escapefromreality/EscapeJavaCode.java

for filename in lib/*.jar; do
  CP=${CP}:${filename}
done

java -cp .:${CP} escapefromreality/EscapeJavaCode

Leave a comment

Your comment