case 'u':  if (formatting && keyfollow) { p.setText(buf.toString()); buf.delete(0, buf.length()); doc.addStyledParagraph(p); p = createParagraphFromParent(p); p.setUnderline(false); formatting = false; keyfollow = false; } else { if (formatting) { value.append(c); } else { buf.append(c); } }  break;
    public static TextDocument parseDXFMText(DXFMText text) {        // initialize        TextDocument doc = new TextDocument();        StringBuffer buf = new StringBuffer();        StringBuffer value = new StringBuffer();        StyledTextParagraph p = new StyledTextParagraph();        p.setFontHeight(text.getHeight());        p.setInsertPoint(text.getInsertPoint());        switch (text.getAlignment()) {        case DXFMText.ATTACHMENT_TOP:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_TOP);            break;        case DXFMText.ATTACHMENT_TOP_CENTER:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_TOP);            break;        case DXFMText.ATTACHMENT_TOP_RIGHT:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_TOP);            break;        case DXFMText.ATTACHMENT_MIDDLE_LEFT:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_CENTER);            break;        case DXFMText.ATTACHMENT_MIDDLE_CENTER:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_CENTER);            break;        case DXFMText.ATTACHMENT_MIDDLE_RIGHT:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_CENTER);            break;        case DXFMText.ATTACHMENT_BOTTOM_LEFT:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_BASELINE);            break;        case DXFMText.ATTACHMENT_BOTTOM_CENTER:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_BASELINE);            break;        case DXFMText.ATTACHMENT_BOTTOM_RIGHT:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_BASELINE);            break;        default:            p.setValign(StyledTextParagraph.VERTICAL_ALIGNMENT_BASELINE);            break;        }        boolean formatting = false;        boolean keyfollow = false;        boolean complete = true;        Stack paras = new Stack();        int linecount = 0;        String str = text.getText();        char key = ' ';        //parse the symbols        str = parseSymbols(str);        //parse the style        for (int i = 0; i < str.length(); i++) {            char c = str.charAt(i);            switch (c) {            case '\\':                if (formatting) {                    if (!complete) {                        parseStyledTextParagraphSettings(key, value.toString(),                            p);                        value.delete(0, value.length());                        formatting = true;                        keyfollow = true;                    } else {                        buf.append(c);                        formatting = false;                    }                } else {                    formatting = true;                    keyfollow = true;                    complete = false;                }                break;            case '~':                if (formatting) {                    buf.append(c);                    formatting = false;                    keyfollow = false;                }                break;            case ';':                if (formatting) {                    parseStyledTextParagraphSettings(key, value.toString(), p);                    value.delete(0, value.length());                    formatting = false;                    complete = true;                    keyfollow = false;                } else {                    buf.append(c);                }                break;            case '}':                if (formatting && keyfollow) {                    buf.append(c);                    formatting = false;                    keyfollow = false;                } else {                    if (formatting) {                        value.append(c);                    } else {                        // format change end                        doc.addStyledParagraph(p);                        p = createParagraphFromParent(p);                        if (paras.size() > 0) {                            p = (StyledTextParagraph) paras.pop();                        }                    }                }                break;            case '{':                if (formatting && keyfollow) {                    buf.append(c);                    formatting = false;                    keyfollow = false;                } else {                    if (formatting) {                        value.append(c);                    } else {                        // start format change                        if (i != 0) {                            paras.add(p);                            p = new StyledTextParagraph();                        }                    }                }                break;            case 'O':                if (formatting && keyfollow) {                    p.setText(buf.toString());                    buf.delete(0, buf.length());                    doc.addStyledParagraph(p);                    p = createParagraphFromParent(p);                    p.setOverline(true);                    formatting = false;                    keyfollow = false;                } else {                    if (formatting) {                        value.append(c);                    } else {                        buf.append(c);                    }                }                break;            case 'o':                if (formatting && keyfollow) {                    p.setText(buf.toString());                    buf.delete(0, buf.length());                    doc.addStyledParagraph(p);                    p = createParagraphFromParent(p);                    p.setOverline(false);                    formatting = false;                    keyfollow = false;                } else {                    if (formatting) {                        value.append(c);                    } else {                        buf.append(c);                    }                }                break;            case 'L':                if (formatting && keyfollow) {                    p.setText(buf.toString());                    buf.delete(0, buf.length());                    doc.addStyledParagraph(p);                    p = createParagraphFromParent(p);                    p.setUnderline(true);                    formatting = false;                    keyfollow = false;                } else {                    if (formatting) {                        value.append(c);                    } else {                        buf.append(c);                    }                }                break;            case 'l':                if (formatting && keyfollow) {                    p.setText(buf.toString());                    buf.delete(0, buf.length());                    doc.addStyledParagraph(p);                    p = createParagraphFromParent(p);                    p.setUnderline(false);                    formatting = false;                    keyfollow = false;                } else {                    if (formatting) {                        value.append(c);                    } else {                        buf.append(c);                    }                }                break;            case 'P':                if (formatting && keyfollow) {                    linecount++;                    p.setText(buf.toString());                    buf.delete(0, buf.length());                    doc.addStyledParagraph(p);                    p = createParagraphFromParent(p);                    formatting = false;                    keyfollow = false;                    p.setLineIndex(linecount);                    p.setNewline(true);                } else {                    if (formatting) {                        value.append(c);                    } else {                        buf.append(c);                    }                }                break;            default:                if (formatting) {                    if (keyfollow) {                        key = c;                        keyfollow = false;                    } else {                        value.append(c);                    }                } else {                    buf.append(c);                }                break;            }        }        if (formatting) {            parseStyledTextParagraphSettings(key, value.toString(), p);        }        if (buf.length() > 0) {            p.setText(buf.toString());            doc.addStyledParagraph(p);        }        if (doc.getParagraphCount() == 0) {            doc.addStyledParagraph(p);        }        return doc;    }