2011-08-09 11 views
2

文字ストリームインターフェイスを持つJava用のHTML/JavaScriptエスケープライブラリはありますか?文字ストリームを使用したJava用ライブラリのエスケープ

+2

あなたは何をエスケープしますか?結婚? –

+0

@Michael - そのための図書館はありますか? :o – pepsi

+0

どのようなexacltyをHTML/JSでエスケープしたいですか? ['StringEscapeUtils'](http://commons.apache.org/lang/api-3.0/org/apache/commons/lang3/StringEscapeUtils.html)を試してみてください。 –

答えて

1

はい。 Caja's Escaping classは、エスケープされたテキストをHTML、CSS、JavaScript、JavaScriptの正規表現コンテンツ、JSON、およびXMLのAppendableに追加するエスケープを提供します。 Java WriterはおよびStringBufferと同様にAppendableを実装します。

また、JavaScriptを山形括弧で囲むことなく、HTML <script>要素またはXML CDATAセクションに埋め込むことができるモードでエスケープすることもできます。

/** 
* Given a plain text string writes an unquoted javascript string literal. 
* 
* @param s the plain text string to escape. 
* @param asciiOnly Makes sure that only ASCII characters are written to out. 
*  This is a good idea if you don't have control over the charset that 
*  the javascript will be served with. 
* @param embeddable True to make sure that nothing is written to out that 
*  could interfere with embedding inside a script tag or CDATA section, or 
*  other tag that typically contains markup. 
*  This does not make it safe to embed in an HTML attribute without 
*  further escaping. 
* @param out written to. 
*/ 
public static void escapeJsString(
    CharSequence s, boolean asciiOnly, boolean embeddable, Appendable out) 
    throws IOException 

は、同じ名前の便利な方法がありますが、それはStringBuilderを取り、IOExceptionを処理する必要はありません。

+0

出力用のストリームインターフェイスがありますが、入力用のインターフェイスはないようです。私は両方のライブラリがないとの結論に達したので、あなたの答えは最も近いものです。 – pepsi

関連する問題