Search in sources :

Example 11 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class OfficeImporterScriptServiceTest method saveWithAppend.

@Test
public void saveWithAppend() throws Exception {
    XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    String syntaxId = "test/1.0";
    when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
    when(documentAccessBridge.exists(documentReference)).thenReturn(true);
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    when(documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
    when(document.getSyntax()).thenReturn(new Syntax(new SyntaxType("test", "Test"), "1.0"));
    when(documentAccessBridge.getDocumentContent(documentReference, null)).thenReturn("before");
    when(doc.getContentAsString(syntaxId)).thenReturn("after");
    assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, null, null, true));
    verify(documentAccessBridge).setDocumentContent(documentReference, "before\nafter", "Updated by office importer.", false);
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) Syntax(org.xwiki.rendering.syntax.Syntax) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 12 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class DisplayScriptService method title.

/**
 * Displays the document title. If a title has not been provided through the title field, it looks for a section
 * title in the document's content and if not found return the page name. The returned title is also interpreted
 * which means it's allowed to use Velocity, Groovy, etc. syntax within a title.
 *
 * @param document the document whose title is displayed
 * @param parameters the display parameters
 * @return the result of displaying the title of the given document
 */
public String title(Document document, Map<String, Object> parameters) {
    Map<String, Object> actualParameters = new HashMap<String, Object>(parameters);
    DocumentDisplayerParameters displayerParameters = (DocumentDisplayerParameters) parameters.get(DISPLAYER_PARAMETERS_KEY);
    if (displayerParameters == null) {
        displayerParameters = new DocumentDisplayerParameters();
        // Default title display parameters.
        displayerParameters.setExecutionContextIsolated(true);
    } else if (!displayerParameters.isTitleDisplayed()) {
        // Clone because we have to enforce title display.
        displayerParameters = displayerParameters.clone();
    }
    // Ensure the title is displayed.
    displayerParameters.setTitleDisplayed(true);
    Syntax outputSyntax = getOutputSyntax(parameters);
    displayerParameters.setTargetSyntax(outputSyntax);
    actualParameters.put(DISPLAYER_PARAMETERS_KEY, displayerParameters);
    return document(document, actualParameters, outputSyntax);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) HashMap(java.util.HashMap) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 13 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class DisplayScriptService method content.

/**
 * Displays the content of the given document.
 *
 * @param document the document whose content is displayed
 * @param parameters the display parameters
 * @return the result of rendering the content of the given document using the provided parameters
 */
public String content(Document document, Map<String, Object> parameters) {
    XWikiContext context = getXWikiContext();
    String content = null;
    try {
        content = document.getTranslatedContent();
    } catch (XWikiException e) {
        this.logger.warn("Failed to get the translated content of document [{}].", document.getPrefixedFullName(), e);
        return null;
    }
    String renderedContent = this.renderingCache.getRenderedContent(document.getDocumentReference(), content, context);
    if (renderedContent == null) {
        Map<String, Object> actualParameters = new HashMap<String, Object>(parameters);
        DocumentDisplayerParameters displayerParameters = (DocumentDisplayerParameters) parameters.get(DISPLAYER_PARAMETERS_KEY);
        if (displayerParameters == null) {
            displayerParameters = new DocumentDisplayerParameters();
            // Default content display parameters.
            displayerParameters.setExecutionContextIsolated(true);
            displayerParameters.setContentTranslated(true);
        } else if (displayerParameters.isTitleDisplayed()) {
            // Clone because we have to enforce content display.
            displayerParameters = displayerParameters.clone();
        }
        // Ensure the content is displayed.
        displayerParameters.setTitleDisplayed(false);
        Syntax outputSyntax = getOutputSyntax(parameters);
        displayerParameters.setTargetSyntax(outputSyntax);
        actualParameters.put(DISPLAYER_PARAMETERS_KEY, displayerParameters);
        renderedContent = document(document, actualParameters, outputSyntax);
        if (renderedContent != null) {
            this.renderingCache.setRenderedContent(document.getDocumentReference(), content, renderedContent, context);
        }
    }
    return renderedContent;
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) HashMap(java.util.HashMap) XWikiContext(com.xpn.xwiki.XWikiContext) Syntax(org.xwiki.rendering.syntax.Syntax) XWikiException(com.xpn.xwiki.XWikiException)

Example 14 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class PanelWikiUIExtensionComponentBuilder method buildComponents.

@Override
public List<WikiComponent> buildComponents(BaseObject baseObject) throws WikiComponentException {
    String content = baseObject.getStringValue("content");
    Syntax syntax = baseObject.getOwnerDocument().getSyntax();
    DocumentReference documentReference = baseObject.getOwnerDocument().getDocumentReference();
    DocumentReference authorReference = baseObject.getOwnerDocument().getAuthorReference();
    XDOM xdom = this.parser.parse(content, syntax, documentReference);
    try {
        return Collections.<WikiComponent>singletonList(new PanelWikiUIExtension(baseObject.getReference(), authorReference, xdom, syntax, this.componentManager));
    } catch (ComponentLookupException e) {
        throw new WikiComponentException(String.format("Failed to initialize Panel UI extension [%s]", baseObject), e);
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) XDOM(org.xwiki.rendering.block.XDOM) WikiComponent(org.xwiki.component.wiki.WikiComponent) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 15 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class XWikiDocument method setSyntaxId.

/**
 * Note that this method cannot be removed for now since it's used by Hibernate for saving a XWikiDocument.
 *
 * @param syntaxId the new syntax id to set (e.g. {@code xwiki/2.0}, {@code xwiki/2.1}, etc)
 * @see #getSyntaxId()
 * @deprecated since 2.3M1, use {link #setSyntax(Syntax)} instead
 */
@Deprecated
public void setSyntaxId(String syntaxId) {
    Syntax syntax;
    // syntax/1.0 syntax.
    if (StringUtils.isBlank(syntaxId)) {
        syntax = Syntax.XWIKI_1_0;
    } else {
        try {
            syntax = Syntax.valueOf(syntaxId);
        } catch (ParseException e) {
            syntax = getDefaultDocumentSyntax();
            LOGGER.warn("Failed to set syntax [" + syntaxId + "] for [" + getDefaultEntityReferenceSerializer().serialize(getDocumentReference()) + "], setting syntax [" + syntax.toIdString() + "] instead.", e);
        }
    }
    setSyntax(syntax);
}
Also used : ParseException(org.xwiki.rendering.parser.ParseException) Syntax(org.xwiki.rendering.syntax.Syntax)

Aggregations

Syntax (org.xwiki.rendering.syntax.Syntax)35 Test (org.junit.Test)16 DocumentReference (org.xwiki.model.reference.DocumentReference)15 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)11 Expectations (org.jmock.Expectations)10 SyntaxType (org.xwiki.rendering.syntax.SyntaxType)7 ParseException (org.xwiki.rendering.parser.ParseException)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 IOException (java.io.IOException)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 Parser (org.xwiki.rendering.parser.Parser)4 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)3 HashMap (java.util.HashMap)3 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)3 XDOM (org.xwiki.rendering.block.XDOM)3 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 AbstractInstanceFilterStreamTest (com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest)2 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2