Search in sources :

Example 26 with Syntax

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

the class DefaultHTMLConverter method parseAndRender.

@Override
public String parseAndRender(String dirtyHTML, String syntaxId) {
    try {
        // Clean
        String html = this.htmlCleaner.clean(dirtyHTML);
        // Parse
        XDOM xdom = this.xhtmlParser.parse(new StringReader(html));
        // The XHTML parser sets the "syntax" meta data property of the created XDOM to "xhtml/1.0". The syntax meta
        // data is used as the default syntax for macro content. We have to change this to the specified syntax
        // because HTML is used only to be able to edit the source syntax in the WYSIWYG editor.
        Syntax syntax = Syntax.valueOf(syntaxId);
        xdom.getMetaData().addMetaData(MetaData.SYNTAX, syntax);
        // Execute the macro transformation
        executeMacroTransformation(xdom, Syntax.valueOf(syntaxId));
        // Render
        WikiPrinter printer = new DefaultWikiPrinter();
        this.xhtmlRenderer.render(xdom, printer);
        return printer.toString();
    } catch (Exception e) {
        this.logger.error(e.getLocalizedMessage(), e);
        throw new RuntimeException("Exception while refreshing HTML", e);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Syntax(org.xwiki.rendering.syntax.Syntax) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException)

Example 27 with Syntax

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

the class DefaultIOTargetServiceTest method testGetterWhenTargetIsTypedIndexedObjectProperty.

@Test
public void testGetterWhenTargetIsTypedIndexedObjectProperty() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            allowing(classResolver).resolve("XWiki.Class", new DocumentReference("wiki", "Space", "Page"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "Class")));
            oneOf(dabMock).getProperty(new DocumentReference("wiki", "Space", "Page"), new DocumentReference("wiki", "XWiki", "Class"), 1, "property");
            will(returnValue("defcontent"));
            oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
            will(returnValue(dmb));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    String reference = "OBJECT_PROPERTY://wiki:Space.Page^XWiki.Class[1].property";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 28 with Syntax

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

the class DefaultIOTargetServiceTest method testGettersWhenTargetIsEmptyString.

@Test
public void testGettersWhenTargetIsEmptyString() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            // default resolver should be used. Note that this will fail if default values change, not very well
            // isolated
            allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Main", "WebHome"));
            will(returnValue(dmb));
            oneOf(dmb).getContent();
            will(returnValue("defcontent"));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    // expect source ref to be used as is, as it doesn't parse to something acceptable
    String reference = "";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 29 with Syntax

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

the class DefaultIOTargetServiceTest method testGetterWhenTargetIsTypedDefaultObjectProperty.

@Test
public void testGetterWhenTargetIsTypedDefaultObjectProperty() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            allowing(classResolver).resolve("XWiki.Class", new DocumentReference("wiki", "Space", "Page"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "Class")));
            oneOf(dabMock).getProperty(new DocumentReference("wiki", "Space", "Page"), new DocumentReference("wiki", "XWiki", "Class"), "property");
            will(returnValue("defcontent"));
            oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
            will(returnValue(dmb));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    String reference = "OBJECT_PROPERTY://wiki:Space.Page^XWiki.Class.property";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 30 with Syntax

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

the class XAROutputFilterStream method beginWikiDocumentRevision.

@Override
public void beginWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException {
    checkXMLWriter();
    this.currentDocumentVersion = revision;
    try {
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_PARENT)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_PARENT, toString((EntityReference) parameters.get(XWikiWikiDocumentFilter.PARAMETER_PARENT)));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_AUTHOR, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_CUSTOMCLASS, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_AUTHOR, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_DATE, toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_DATE, toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)));
        }
        this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION, this.currentDocumentVersion);
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_TITLE)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_TITLE, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_TITLE));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTTEMPLATE, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_VALIDATIONSCRIPT, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_COMMENT, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_MINOR, toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_SYNTAX, toString((Syntax) parameters.get(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_HIDDEN, toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT));
        }
        if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML)) {
            this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_HTML, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML));
        }
    } catch (Exception e) {
        throw new FilterException(String.format("Failed to write begin document [%s] with version [%s]", this.currentDocumentReference, this.currentDocumentVersion), e);
    }
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) FilterException(org.xwiki.filter.FilterException) Syntax(org.xwiki.rendering.syntax.Syntax) Date(java.util.Date) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException)

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