Search in sources :

Example 21 with Syntax

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

the class EditableGadgetRenderer method getGadgetContentRenderer.

/**
 * @return the renderer corresponding to the "annotated" version of the current target syntax
 */
protected BlockRenderer getGadgetContentRenderer() {
    // Get the current syntax
    Syntax currentTargetSyntax = renderingContext.getTargetSyntax();
    if (currentTargetSyntax == null) {
        // (it should never happen actually)
        return defaultGadgetContentRenderer;
    }
    // Get the annotated syntax corresponding to the current target syntax
    String annotatedTargetSyntax = currentTargetSyntax.toIdString();
    if (!StringUtils.startsWith(annotatedTargetSyntax, ANNOTATED_SYNTAXES_PREFIX)) {
        annotatedTargetSyntax = ANNOTATED_SYNTAXES_PREFIX + annotatedTargetSyntax;
    }
    try {
        return componentManager.getInstance(BlockRenderer.class, annotatedTargetSyntax);
    } catch (ComponentLookupException e) {
        logger.warn("Failed to load the syntax [{}].", annotatedTargetSyntax);
        // Failback to the default renderer
        return defaultGadgetContentRenderer;
    }
}
Also used : ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 22 with Syntax

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

the class DefaultExtendedRenderingConfigurationTest method getConfiguredAndDisabledSyntaxesWhenNoConfigXObjectAndNoXWikiCfgProperty.

@Test
public void getConfiguredAndDisabledSyntaxesWhenNoConfigXObjectAndNoXWikiCfgProperty() throws Exception {
    ConfigurationSource renderingSource = this.mocker.getInstance(ConfigurationSource.class, "rendering");
    when(renderingSource.getProperty("disabledSyntaxes")).thenReturn(null);
    ConfigurationSource xwikiCfgSource = this.mocker.getInstance(ConfigurationSource.class, "xwikicfg");
    when(xwikiCfgSource.getProperty("xwiki.rendering.syntaxes", List.class)).thenReturn(null);
    CoreConfiguration coreConfiguration = this.mocker.getInstance(CoreConfiguration.class);
    Syntax defaultSyntax = new Syntax(new SyntaxType("xwiki", "XWiki"), "2.1");
    when(coreConfiguration.getDefaultDocumentSyntax()).thenReturn(defaultSyntax);
    // Register some Syntaxes for the test
    Parser defaultSyntaxParser = this.mocker.registerMockComponent(Parser.class, "xwiki/2.1");
    when(defaultSyntaxParser.getSyntax()).thenReturn(defaultSyntax);
    Parser syntax1Parser = this.mocker.registerMockComponent(Parser.class, "syntax1/1.0");
    Syntax syntax1 = new Syntax(new SyntaxType("syntax1", "Syntax 1"), "1.0");
    when(syntax1Parser.getSyntax()).thenReturn(syntax1);
    Parser syntax2Parser = this.mocker.registerMockComponent(Parser.class, "syntax2/1.0");
    Syntax syntax2 = new Syntax(new SyntaxType("syntax2", "Syntax 2"), "1.0");
    when(syntax2Parser.getSyntax()).thenReturn(syntax2);
    List<Syntax> disabledSyntaxes = this.mocker.getComponentUnderTest().getDisabledSyntaxes();
    assertEquals(2, disabledSyntaxes.size());
    assertTrue(disabledSyntaxes.contains(syntax1));
    assertTrue(disabledSyntaxes.contains(syntax2));
    List<Syntax> configuredSyntaxes = this.mocker.getComponentUnderTest().getConfiguredSyntaxes();
    assertEquals(1, configuredSyntaxes.size());
    assertTrue(configuredSyntaxes.contains(defaultSyntax));
}
Also used : ConfigurationSource(org.xwiki.configuration.ConfigurationSource) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) CoreConfiguration(com.xpn.xwiki.CoreConfiguration) Syntax(org.xwiki.rendering.syntax.Syntax) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Example 23 with Syntax

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

the class TextAreaClass method getObjectDocumentSyntax.

/**
 * @return the syntax for the document to which the passed objects belongs to or the XWiki Syntax 1.0 if the object
 *         document cannot be retrieved
 */
private Syntax getObjectDocumentSyntax(BaseCollection object, XWikiContext context) {
    Syntax syntax;
    try {
        XWikiDocument doc = object.getOwnerDocument();
        if (doc == null) {
            doc = context.getWiki().getDocument(object.getDocumentReference(), context);
        }
        syntax = doc.getSyntax();
    } catch (Exception e) {
        // Used to convert a Document Reference to string (compact form without the wiki part if it matches the
        // current wiki).
        EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
        LOGGER.warn("Error while getting the syntax corresponding to object [" + compactWikiEntityReferenceSerializer.serialize(object.getDocumentReference()) + "]. Defaulting to using XWiki 1.0 syntax. Internal error [" + e.getMessage() + "]");
        syntax = Syntax.XWIKI_1_0;
    }
    return syntax;
}
Also used : EntityReferenceSerializer(org.xwiki.model.reference.EntityReferenceSerializer) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Syntax(org.xwiki.rendering.syntax.Syntax) EditException(org.xwiki.edit.EditException)

Example 24 with Syntax

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

the class DocumentInstanceOutputFilterStreamTest method testImportDocumentsWithoutLocaleAndRevision.

@Test
public void testImportDocumentsWithoutLocaleAndRevision() throws FilterException, XWikiException, ParseException {
    DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
    outputProperties.setVersionPreserved(true);
    outputProperties.setVerbose(false);
    importFromXML("document1-2", outputProperties);
    XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
    Assert.assertFalse(document.isNew());
    Assert.assertEquals(Locale.ENGLISH, document.getDefaultLocale());
    Assert.assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
    Assert.assertEquals("customclass", document.getCustomClass());
    Assert.assertEquals("title", document.getTitle());
    Assert.assertEquals("defaultTemplate", document.getDefaultTemplate());
    Assert.assertEquals("validationScript", document.getValidationScript());
    Assert.assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
    Assert.assertEquals(true, document.isHidden());
    Assert.assertEquals("content", document.getContent());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
    Assert.assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
    Assert.assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
    Assert.assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
    Assert.assertEquals(true, document.isMinorEdit());
    Assert.assertEquals("comment", document.getComment());
    Assert.assertEquals("1.1", document.getVersion());
    // Attachment
    Assert.assertEquals(1, document.getAttachmentList().size());
    XWikiAttachment attachment = document.getAttachment("attachment.txt");
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(10, attachment.getLongSize());
    Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
    Assert.assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
    Assert.assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
    Assert.assertEquals("1.1", attachment.getVersion());
    Assert.assertEquals("attachment comment", attachment.getComment());
    // XClass
    BaseClass xclass = document.getXClass();
    Assert.assertEquals("customClass", xclass.getCustomClass());
    Assert.assertEquals("customMapping", xclass.getCustomMapping());
    Assert.assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
    Assert.assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
    Assert.assertEquals("defaultWeb", xclass.getDefaultWeb());
    Assert.assertEquals("nameField", xclass.getNameField());
    Assert.assertEquals("validationScript", xclass.getValidationScript());
    Assert.assertEquals(1, xclass.getFieldList().size());
    NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
    Assert.assertEquals("prop1", numberFiled.getName());
    Assert.assertEquals(false, numberFiled.isDisabled());
    Assert.assertEquals(1, numberFiled.getNumber());
    Assert.assertEquals("long", numberFiled.getNumberType());
    Assert.assertEquals("Prop1", numberFiled.getPrettyName());
    Assert.assertEquals(30, numberFiled.getSize());
    Assert.assertEquals(false, numberFiled.isUnmodifiable());
    // Objects
    Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
    Assert.assertEquals(2, objects.size());
    // Object 1
    List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
    Assert.assertEquals(1, documentObjects.size());
    BaseObject documentObject = documentObjects.get(0);
    Assert.assertEquals(0, documentObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
    Assert.assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
    Assert.assertEquals(1, documentObject.getFieldList().size());
    Assert.assertEquals(1, documentObject.getIntValue("prop1"));
    // Object 2
    List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
    Assert.assertEquals(1, otherObjects.size());
    BaseObject otherObject = otherObjects.get(0);
    Assert.assertEquals(0, otherObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
    Assert.assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
    Assert.assertEquals(1, otherObject.getFieldList().size());
    Assert.assertEquals(2, otherObject.getIntValue("prop2"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) NumberClass(com.xpn.xwiki.objects.classes.NumberClass) List(java.util.List) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test) AbstractInstanceFilterStreamTest(com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest)

Example 25 with Syntax

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

the class DocumentInstanceOutputFilterStreamTest method testImportDocumentsPreserveVersion.

// Tests
@Test
public void testImportDocumentsPreserveVersion() throws FilterException, XWikiException, ParseException {
    DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
    outputProperties.setVersionPreserved(true);
    outputProperties.setVerbose(false);
    importFromXML("document1", outputProperties);
    XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
    Assert.assertFalse(document.isNew());
    Assert.assertEquals(Locale.ENGLISH, document.getDefaultLocale());
    Assert.assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
    Assert.assertEquals("customclass", document.getCustomClass());
    Assert.assertEquals("title", document.getTitle());
    Assert.assertEquals("defaultTemplate", document.getDefaultTemplate());
    Assert.assertEquals("validationScript", document.getValidationScript());
    Assert.assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
    Assert.assertEquals(true, document.isHidden());
    Assert.assertEquals("content", document.getContent());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
    Assert.assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
    Assert.assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
    Assert.assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
    Assert.assertEquals(true, document.isMinorEdit());
    Assert.assertEquals("comment", document.getComment());
    Assert.assertEquals("1.42", document.getVersion());
    // Attachment
    Assert.assertEquals(1, document.getAttachmentList().size());
    XWikiAttachment attachment = document.getAttachment("attachment.txt");
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(10, attachment.getLongSize());
    Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
    Assert.assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
    Assert.assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
    Assert.assertEquals("15.1", attachment.getVersion());
    Assert.assertEquals("attachment comment", attachment.getComment());
    // XClass
    BaseClass xclass = document.getXClass();
    Assert.assertEquals(1, xclass.getFieldList().size());
    Assert.assertEquals("customClass", xclass.getCustomClass());
    Assert.assertEquals("customMapping", xclass.getCustomMapping());
    Assert.assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
    Assert.assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
    Assert.assertEquals("defaultWeb", xclass.getDefaultWeb());
    Assert.assertEquals("nameField", xclass.getNameField());
    Assert.assertEquals("validationScript", xclass.getValidationScript());
    NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
    Assert.assertEquals("prop1", numberFiled.getName());
    Assert.assertEquals(false, numberFiled.isDisabled());
    Assert.assertEquals(1, numberFiled.getNumber());
    Assert.assertEquals("long", numberFiled.getNumberType());
    Assert.assertEquals("Prop1", numberFiled.getPrettyName());
    Assert.assertEquals(30, numberFiled.getSize());
    Assert.assertEquals(false, numberFiled.isUnmodifiable());
    // Objects
    Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
    Assert.assertEquals(2, objects.size());
    // Object 1
    List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
    Assert.assertEquals(1, documentObjects.size());
    BaseObject documentObject = documentObjects.get(0);
    Assert.assertEquals(0, documentObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
    Assert.assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
    Assert.assertEquals(1, documentObject.getFieldList().size());
    Assert.assertEquals(1, documentObject.getIntValue("prop1"));
    // Object 2
    List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
    Assert.assertEquals(1, otherObjects.size());
    BaseObject otherObject = otherObjects.get(0);
    Assert.assertEquals(0, otherObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
    Assert.assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
    Assert.assertEquals(1, otherObject.getFieldList().size());
    Assert.assertEquals(2, otherObject.getIntValue("prop2"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) NumberClass(com.xpn.xwiki.objects.classes.NumberClass) List(java.util.List) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test) AbstractInstanceFilterStreamTest(com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest)

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