달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
import java.io.Writer;
import java.util.regex.Pattern;

import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;

public class MyPrettyPrintWriter extends PrettyPrintWriter {
    private static final char[] AMP = "&".toCharArray();
    private static final char[] LT = "<".toCharArray();
    private static final char[] GT = ">".toCharArray();
    private static final char[] SLASH_R = " ".toCharArray();
    private static final char[] QUOT = """.toCharArray();
    private static final char[] APOS = "'".toCharArray();
   
    public LocalPrettyPrintWriter(Writer writer) {
        super(writer);
    }

    protected void writeText(QuickWriter writer, String text) {       
       
        String CDATAPrefix = "<![CDATA[";
        String CDATASuffix = "]]>";

        if (!text.startsWith(CDATAPrefix) && !Pattern.matches("[^[0-9]]+", text)) {
            text = CDATAPrefix+text+CDATASuffix;
        }
       
        int length = text.length();
        if (!text.startsWith(CDATAPrefix)) {
            for (int i = 0; i < length; i++) {
                char c = text.charAt(i);
                switch (c) {
                case '&':
                    writer.write(AMP);
                    break;
                case '<':
                    writer.write(LT);
                    break;
                case '>':
                    writer.write(GT);
                    break;
                case '"':
                    writer.write(QUOT);
                    break;
                case '\'':
                    writer.write(APOS);
                    break;
                case '\r':
                    writer.write(SLASH_R);
                    break;
                default:
                    writer.write(c);
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                char c = text.charAt(i);
                writer.write(c);
            }
        }
    }
}


import java.io.Writer;

import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class MyXppDriver extends XppDriver {

    public HierarchicalStreamWriter createWriter(Writer out) {
        return new MyPrettyPrintWriter(out);
    }
}





Posted by fromm0
|