Search in sources :

Example 6 with DocumentSection

use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.

the class XWikiDocumentTest method testGetSections.

public void testGetSections() throws XWikiException {
    this.document.setContent("content not in section\n" + "= header 1=\nheader 1 content\n" + "== header 2==\nheader 2 content");
    this.document.setSyntax(Syntax.XWIKI_2_0);
    List<DocumentSection> headers = this.document.getSections();
    assertEquals(2, headers.size());
    DocumentSection header1 = headers.get(0);
    DocumentSection header2 = headers.get(1);
    assertEquals("header 1", header1.getSectionTitle());
    assertEquals(-1, header1.getSectionIndex());
    assertEquals(1, header1.getSectionNumber());
    assertEquals("1", header1.getSectionLevel());
    assertEquals("header 2", header2.getSectionTitle());
    assertEquals(-1, header2.getSectionIndex());
    assertEquals(2, header2.getSectionNumber());
    assertEquals("1.1", header2.getSectionLevel());
}
Also used : DocumentSection(com.xpn.xwiki.api.DocumentSection)

Example 7 with DocumentSection

use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.

the class XWikiDocumentTest method testGetSections10.

public void testGetSections10() throws XWikiException {
    this.document.setContent("content not in section\n" + "1 header 1\nheader 1 content\n" + "1.1 header 2\nheader 2 content");
    List<DocumentSection> headers = this.document.getSections();
    assertEquals(2, headers.size());
    DocumentSection header1 = headers.get(0);
    DocumentSection header2 = headers.get(1);
    assertEquals("header 1", header1.getSectionTitle());
    assertEquals(23, header1.getSectionIndex());
    assertEquals(1, header1.getSectionNumber());
    assertEquals("1", header1.getSectionLevel());
    assertEquals("header 2", header2.getSectionTitle());
    assertEquals(51, header2.getSectionIndex());
    assertEquals(2, header2.getSectionNumber());
    assertEquals("1.1", header2.getSectionLevel());
}
Also used : DocumentSection(com.xpn.xwiki.api.DocumentSection)

Example 8 with DocumentSection

use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.

the class XWikiDocumentTest method testGetDocumentSection10.

public void testGetDocumentSection10() throws XWikiException {
    this.document.setContent("content not in section\n" + "1 header 1\nheader 1 content\n" + "1.1 header 2\nheader 2 content");
    DocumentSection header1 = this.document.getDocumentSection(1);
    DocumentSection header2 = this.document.getDocumentSection(2);
    assertEquals("header 1", header1.getSectionTitle());
    assertEquals(23, header1.getSectionIndex());
    assertEquals(1, header1.getSectionNumber());
    assertEquals("1", header1.getSectionLevel());
    assertEquals("header 2", header2.getSectionTitle());
    assertEquals(51, header2.getSectionIndex());
    assertEquals(2, header2.getSectionNumber());
    assertEquals("1.1", header2.getSectionLevel());
}
Also used : DocumentSection(com.xpn.xwiki.api.DocumentSection)

Example 9 with DocumentSection

use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.

the class XWikiDocument method updateDocumentSection10.

/**
 * Update a section content in document.
 *
 * @param sectionNumber the index (+1) of the section in the list of all sections in the document.
 * @param newSectionContent the new section content.
 * @return the new document content.
 * @throws XWikiException error when updating document content with section content
 */
private String updateDocumentSection10(int sectionNumber, String newSectionContent) throws XWikiException {
    StringBuilder newContent = new StringBuilder();
    // get document section that will be edited
    DocumentSection docSection = getDocumentSection(sectionNumber);
    int numberOfSections = getSections().size();
    int indexSection = docSection.getSectionIndex();
    if (numberOfSections == 1) {
        // there is only a sections in document
        String contentBegin = getContent().substring(0, indexSection);
        newContent = newContent.append(contentBegin).append(newSectionContent);
        return newContent.toString();
    } else if (sectionNumber == numberOfSections) {
        // edit lastest section that doesn't contain subtitle
        String contentBegin = getContent().substring(0, indexSection);
        newContent = newContent.append(contentBegin).append(newSectionContent);
        return newContent.toString();
    } else {
        String sectionLevel = docSection.getSectionLevel();
        int nextSectionIndex = 0;
        // get index of next section
        for (int i = sectionNumber; i < numberOfSections; i++) {
            // get next section
            DocumentSection nextSection = getDocumentSection(i + 1);
            String nextSectionLevel = nextSection.getSectionLevel();
            if (sectionLevel.equals(nextSectionLevel)) {
                nextSectionIndex = nextSection.getSectionIndex();
                break;
            } else if (sectionLevel.length() > nextSectionLevel.length()) {
                nextSectionIndex = nextSection.getSectionIndex();
                break;
            }
        }
        if (nextSectionIndex == 0) {
            // edit the last section
            newContent = newContent.append(getContent().substring(0, indexSection)).append(newSectionContent);
            return newContent.toString();
        } else {
            String contentAfter = getContent().substring(nextSectionIndex);
            String contentBegin = getContent().substring(0, indexSection);
            newContent = newContent.append(contentBegin).append(newSectionContent).append(contentAfter);
        }
        return newContent.toString();
    }
}
Also used : DocumentSection(com.xpn.xwiki.api.DocumentSection) ToString(org.suigeneris.jrcs.util.ToString)

Aggregations

DocumentSection (com.xpn.xwiki.api.DocumentSection)9 ToString (org.suigeneris.jrcs.util.ToString)4 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 XDOM (org.xwiki.rendering.block.XDOM)1 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)1 MacroBlockMatcher (org.xwiki.rendering.block.match.MacroBlockMatcher)1