Search in sources :

Example 6 with WikiComponent

use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.

the class WikiUIExtensionComponentBuilderTest method buildComponents.

@Test
public void buildComponents() throws Exception {
    BaseObject extensionObject = createExtensionObject("name", "extensionPointId", "content", "key=value=foo\nkey2=value2\ninvalid=\n\n=invalid", "user");
    ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
    VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
    List<WikiComponent> components = this.mocker.getComponentUnderTest().buildComponents(extensionObject);
    assertEquals(1, components.size());
    verify(contentParser).parse("content", Syntax.XWIKI_2_1, DOC_REF);
    UIExtension uiExtension = (UIExtension) components.get(0);
    Map<String, String> parameters = uiExtension.getParameters();
    assertEquals(2, parameters.size());
    verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("name:key"), eq("value=foo"));
    verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("name:key2"), eq("value2"));
}
Also used : StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) WikiComponent(org.xwiki.component.wiki.WikiComponent) VelocityContext(org.apache.velocity.VelocityContext) ContentParser(org.xwiki.component.wiki.internal.bridge.ContentParser) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 7 with WikiComponent

use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.

the class DefaultWikiComponentManagerEventListener method registerDocumentComponents.

/**
 * Registers the components linked to the given document. For that, we get a list of providers that can build a
 * component using this document, then the first available provider is selected in order to register a component.
 *
 * @param documentReference the document used to create the component
 */
private void registerDocumentComponents(DocumentReference documentReference) {
    // Unregister all wiki components registered under the given entity. We do this as otherwise we would need to
    // handle the specific cases of elements added, elements updated and elements deleted, etc.
    // Instead we unregister all wiki components and re-register them all.
    this.wikiComponentManagerEventListenerHelper.unregisterComponents(documentReference);
    // Re-register all wiki components in the passed document
    for (WikiComponentBuilder provider : this.wikiComponentProviders) {
        // Check whether the given document has a wiki component defined in it.
        if (provider.getDocumentReferences().contains(documentReference)) {
            try {
                List<WikiComponent> components = provider.buildComponents(documentReference);
                this.wikiComponentManagerEventListenerHelper.registerComponentList(components);
            } catch (WikiComponentException e) {
                this.logger.warn("Failed to create wiki component(s) for document [{}]: {}", documentReference, ExceptionUtils.getRootCauseMessage(e));
            }
            break;
        }
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) WikiComponentBuilder(org.xwiki.component.wiki.WikiComponentBuilder) WikiComponent(org.xwiki.component.wiki.WikiComponent)

Example 8 with WikiComponent

use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.

the class DefaultWikiComponentManager method unregisterWikiComponent.

private void unregisterWikiComponent(Iterator<WikiComponent> iterator) throws WikiComponentException {
    WikiComponent wikiComponent = iterator.next();
    // Save current context information
    DocumentReference currentUserReference = this.wikiComponentManagerContext.getCurrentUserReference();
    EntityReference currentEntityReference = this.wikiComponentManagerContext.getCurrentEntityReference();
    try {
        // Set the proper information so the component manager use the proper keys to find components to
        // unregister
        this.wikiComponentManagerContext.setCurrentUserReference(wikiComponent.getAuthorReference());
        this.wikiComponentManagerContext.setCurrentEntityReference(wikiComponent.getEntityReference());
        // Remove from the Component Manager
        getComponentManager(wikiComponent.getScope()).unregisterComponent(wikiComponent.getRoleType(), wikiComponent.getRoleHint());
        // Remove from the wiki component cache
        iterator.remove();
    } catch (ComponentLookupException e) {
        throw new WikiComponentException(String.format("Failed to find a component manager for scope [%s]", wikiComponent.getScope()), e);
    } finally {
        this.wikiComponentManagerContext.setCurrentUserReference(currentUserReference);
        this.wikiComponentManagerContext.setCurrentEntityReference(currentEntityReference);
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) WikiComponent(org.xwiki.component.wiki.WikiComponent) EntityReference(org.xwiki.model.reference.EntityReference) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

WikiComponent (org.xwiki.component.wiki.WikiComponent)8 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)5 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 Test (org.junit.Test)2 WikiComponentBuilder (org.xwiki.component.wiki.WikiComponentBuilder)2 XDOM (org.xwiki.rendering.block.XDOM)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 StringWriter (java.io.StringWriter)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 VelocityContext (org.apache.velocity.VelocityContext)1 WikiComponentRuntimeException (org.xwiki.component.wiki.WikiComponentRuntimeException)1 ContentParser (org.xwiki.component.wiki.internal.bridge.ContentParser)1 EntityReference (org.xwiki.model.reference.EntityReference)1 Syntax (org.xwiki.rendering.syntax.Syntax)1 VelocityManager (org.xwiki.velocity.VelocityManager)1