Search in sources :

Example 1 with FixedHTMLWriter

use of org.freeplane.core.util.FixedHTMLWriter in project freeplane by freeplane.

the class MTextController method getContent.

private String[] getContent(final String text, final int pos) {
    if (pos <= 0) {
        return null;
    }
    final String[] strings = new String[2];
    if (text.startsWith("<html>")) {
        final HTMLEditorKit kit = new HTMLEditorKit();
        final HTMLDocument doc = new HTMLDocument();
        final StringReader buf = new StringReader(text);
        try {
            kit.read(buf, doc, 0);
            final char[] firstText = doc.getText(0, pos).toCharArray();
            int firstStart = 0;
            int firstLen = pos;
            while ((firstStart < firstLen) && (firstText[firstStart] <= ' ')) {
                firstStart++;
            }
            while ((firstStart < firstLen) && (firstText[firstLen - 1] <= ' ')) {
                firstLen--;
            }
            int secondStart = 0;
            int secondLen = doc.getLength() - pos;
            if (secondLen <= 0)
                return null;
            final char[] secondText = doc.getText(pos, secondLen).toCharArray();
            while ((secondStart < secondLen) && (secondText[secondStart] <= ' ')) {
                secondStart++;
            }
            while ((secondStart < secondLen) && (secondText[secondLen - 1] <= ' ')) {
                secondLen--;
            }
            if (firstStart == firstLen || secondStart == secondLen) {
                return null;
            }
            StringWriter out = new StringWriter();
            new FixedHTMLWriter(out, doc, firstStart, firstLen - firstStart).write();
            strings[0] = out.toString();
            out = new StringWriter();
            new FixedHTMLWriter(out, doc, pos + secondStart, secondLen - secondStart).write();
            strings[1] = out.toString();
            return strings;
        } catch (final IOException e) {
            LogUtils.severe(e);
        } catch (final BadLocationException e) {
            LogUtils.severe(e);
        }
    } else {
        if (pos >= text.length()) {
            return null;
        }
        strings[0] = text.substring(0, pos);
        strings[1] = text.substring(pos);
    }
    return strings;
}
Also used : StringWriter(java.io.StringWriter) HTMLDocument(javax.swing.text.html.HTMLDocument) StringReader(java.io.StringReader) FixedHTMLWriter(org.freeplane.core.util.FixedHTMLWriter) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with FixedHTMLWriter

use of org.freeplane.core.util.FixedHTMLWriter in project freeplane by freeplane.

the class SplitNode method getFragment.

private String getFragment(final HTMLDocument doc, int start, final int end) throws BadLocationException, IOException {
    final String paragraphText = doc.getText(start, end - start).trim();
    if (paragraphText.length() > 0) {
        final StringWriter out = new StringWriter();
        new FixedHTMLWriter(out, doc, start, end - start).write();
        final String fragment = out.toString();
        if (!fragment.equals("")) {
            return fragment;
        }
    }
    return null;
}
Also used : StringWriter(java.io.StringWriter) FixedHTMLWriter(org.freeplane.core.util.FixedHTMLWriter)

Aggregations

StringWriter (java.io.StringWriter)2 FixedHTMLWriter (org.freeplane.core.util.FixedHTMLWriter)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 BadLocationException (javax.swing.text.BadLocationException)1 HTMLDocument (javax.swing.text.html.HTMLDocument)1 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)1