Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
package nfe.util;
2
 
3
import com.sun.xml.txw2.output.XMLWriter;
4
import org.xml.sax.SAXException;
5
 
6
import java.io.IOException;
7
import java.io.Writer;
8
import java.util.regex.Pattern;
9
 
10
public class CDATAContentHandler extends XMLWriter {
11
    public CDATAContentHandler(Writer writer, String encoding) throws IOException {
12
        super(writer, encoding);
13
    }
14
 
15
    private static final Pattern XML_CHARS = Pattern.compile("[<>&]");
16
 
17
    public void characters(char[] ch, int start, int length) throws SAXException {
18
        boolean useCData = XML_CHARS.matcher(new String(ch, start, length)).find();
19
        if (useCData) {
20
            super.startCDATA();
21
        }
22
        super.characters(ch, start, length);
23
        if (useCData) {
24
            super.endCDATA();
25
        }
26
    }
27
}