Example usage for javax.swing.text DefaultStyledDocument getDefaultRootElement

List of usage examples for javax.swing.text DefaultStyledDocument getDefaultRootElement

Introduction

In this page you can find the example usage for javax.swing.text DefaultStyledDocument getDefaultRootElement.

Prototype

public Element getDefaultRootElement() 

Source Link

Document

Gets the default root element.

Usage

From source file:com.mirth.connect.server.userutil.FileUtil.java

/**
 * Converts an RTF into plain text using the Swing RTFEditorKit.
 * /*from w  w  w. j  a va  2  s .  co m*/
 * @param message
 *            The RTF message to convert.
 * @param replaceLinebreaksWith
 *            If not null, any line breaks in the converted message will be replaced with this
 *            string.
 * @return The converted plain text message.
 * @throws IOException
 * @throws BadLocationException
 */
public static String rtfToPlainText(String message, String replaceLinebreaksWith)
        throws IOException, BadLocationException {

    String convertedPlainText;

    // Reading the RTF content string
    Reader in = new StringReader(message);

    // creating a default blank styled document
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();

    // Creating a RTF Editor kit
    RTFEditorKit rtfKit = new RTFEditorKit();

    // Populating the contents in the blank styled document
    rtfKit.read(in, styledDoc, 0);

    // Getting the root document
    Document doc = styledDoc.getDefaultRootElement().getDocument();

    convertedPlainText = doc.getText(0, doc.getLength());
    if (replaceLinebreaksWith != null) {
        convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith);
    }

    return convertedPlainText;
}

From source file:com.mirth.connect.server.util.FileUtil.java

public static String rtfToPlainText(String message, String replaceLinebreaksWith)
        throws IOException, BadLocationException {

    String convertedPlainText;// w  ww . j  a v  a2 s . co m

    // Reading the RTF content string
    Reader in = new StringReader(message);

    // creating a default blank styled document
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();

    // Creating a RTF Editor kit
    RTFEditorKit rtfKit = new RTFEditorKit();

    // Populating the contents in the blank styled document
    rtfKit.read(in, styledDoc, 0);

    // Getting the root document
    Document doc = styledDoc.getDefaultRootElement().getDocument();

    convertedPlainText = doc.getText(0, doc.getLength());
    if (replaceLinebreaksWith != null) {
        convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith);
    }

    return convertedPlainText;
}

From source file:org.alder.fotobuchconvert.scribus.RtfToScribusConverter.java

void output(XmlBuilder xml, DefaultStyledDocument doc, ScribusWriter scribus) {
    log.debug("Starting conversion of RTF data");
    if (log.isTraceEnabled())
        doc.dump(System.err);//from w w w . j ava 2  s  .co m

    try {
        Element section = doc.getDefaultRootElement();
        log.trace(section);
        assert section.getName().equals("section");

        final int nj = section.getElementCount();
        for (int j = 0; j < nj; j++) {
            Element paragraph = section.getElement(j);
            log.trace(paragraph);
            assert section.getName().equals("paragraph");

            // boolean firstInPara = true;
            AttributeSet attr = paragraph.getAttributes();
            Integer alignment = (Integer) attr.getAttribute(StyleConstants.Alignment);

            boolean elementsInThisLine = false;
            final int ni = paragraph.getElementCount();
            for (int i = 0; i < ni; i++) {
                Element content = paragraph.getElement(i);
                assert section.getName().equals("content");

                int start = content.getStartOffset();
                int end = content.getEndOffset();

                attr = content.getAttributes();
                Boolean italic = (Boolean) attr.getAttribute(StyleConstants.Italic);
                Boolean bold = (Boolean) attr.getAttribute(StyleConstants.Bold);
                Boolean underline = (Boolean) attr.getAttribute(StyleConstants.Underline);
                String family = (String) attr.getAttribute(StyleConstants.Family);
                Integer fontSize = (Integer) attr.getAttribute(StyleConstants.Size);
                Color color = (Color) attr.getAttribute(StyleConstants.ColorConstants.Foreground);

                String text = doc.getText(start, end - start);

                // if (firstInPara && text.trim().isEmpty() && family ==
                // null
                // && fontSize == null)
                // continue;
                // else
                // firstInPara = false;
                if (i == ni - 1 && text.trim().isEmpty() && text.length() < 3)
                    continue;
                elementsInThisLine = true;

                while (text.endsWith("\n") || text.endsWith("\r"))
                    text = text.substring(0, text.length() - 1);

                log.debug(italic + " " + bold + " " + underline + " " + family + " " + fontSize + " " + color
                        + "\t\"" + text + "\"");

                XmlBuilder el = xml.add(C.EL_ITEXT).set(C.CH, text);

                if (bold == Boolean.TRUE && italic == Boolean.TRUE)
                    el.set(C.FONT, family + " Bold Italic");
                else if (bold == Boolean.TRUE)
                    el.set(C.FONT, family + " Bold");
                else if (italic == Boolean.TRUE)
                    el.set(C.FONT, family + " Italic");
                else
                    el.set(C.FONT, family + " Regular");

                if (fontSize != null)
                    el.set(C.FONTSIZE, fontSize);

                if (color != null && color.equals(Color.BLACK) && scribus != null) {
                    String colname = scribus.colorManager.getColorName(color);
                    el.set(C.FCOLOR, colname);
                }
            }

            if (!elementsInThisLine && j == nj - 1)
                break; // don't convert last line if empty

            XmlBuilder el = xml.add(C.EL_PARA);
            if (alignment != null)
                switch (alignment) {
                case StyleConstants.ALIGN_LEFT:
                    el.set(C.ALIGN, 0);
                    break;
                case StyleConstants.ALIGN_CENTER:
                    el.set(C.ALIGN, 1);
                    break;
                case StyleConstants.ALIGN_RIGHT:
                    el.set(C.ALIGN, 2);
                    break;
                case StyleConstants.ALIGN_JUSTIFIED:
                    el.set(C.ALIGN, 3);
                    break;
                }
        }
    } catch (BadLocationException e) {
        throw new RuntimeException("This error should not occour", e);
    }

}