Search in sources :

Example 1 with MacroId

use of org.xwiki.rendering.macro.MacroId in project xwiki-platform by xwiki.

the class RenderingScriptServiceTest method getMacroDescriptors.

@Test
public void getMacroDescriptors() throws Exception {
    MacroManager macroManager = this.mocker.registerMockComponent(MacroManager.class);
    MacroId macroId = new MacroId("macroid");
    when(macroManager.getMacroIds(Syntax.XWIKI_2_1)).thenReturn(Collections.singleton(macroId));
    Macro macro = mock(Macro.class);
    when(macroManager.getMacro(macroId)).thenReturn(macro);
    MacroDescriptor descriptor = mock(MacroDescriptor.class);
    when(macro.getDescriptor()).thenReturn(descriptor);
    List<MacroDescriptor> descriptors = this.mocker.getComponentUnderTest().getMacroDescriptors(Syntax.XWIKI_2_1);
    assertEquals(1, descriptors.size());
    assertSame(descriptor, descriptors.get(0));
}
Also used : MacroDescriptor(org.xwiki.rendering.macro.descriptor.MacroDescriptor) Macro(org.xwiki.rendering.macro.Macro) MacroId(org.xwiki.rendering.macro.MacroId) MacroManager(org.xwiki.rendering.macro.MacroManager) Test(org.junit.Test)

Example 2 with MacroId

use of org.xwiki.rendering.macro.MacroId in project xwiki-platform by xwiki.

the class DefaultWikiMacroFactory method buildMacro.

/**
 * Creates a {@link WikiMacro} from an {@link XWikiDocument} which contains a macro definition.
 *
 * @param doc the {@link XWikiDocument} to look for a macro definition
 * @return the {@link WikiMacro} found inside the document
 * @throws WikiMacroException when an invalid macro definition or no macro definition was found
 */
private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException {
    DocumentReference documentReference = doc.getDocumentReference();
    // Check whether this document contains a macro definition.
    BaseObject macroDefinition = doc.getObject(WIKI_MACRO_CLASS);
    if (null == macroDefinition) {
        throw new WikiMacroException(String.format("No macro definition found in document : [%s]", documentReference));
    }
    // Extract macro definition.
    String macroId = macroDefinition.getStringValue(MACRO_ID_PROPERTY);
    String macroName = macroDefinition.getStringValue(MACRO_NAME_PROPERTY);
    // The macro description as plain text
    String macroDescription = macroDefinition.getStringValue(MACRO_DESCRIPTION_PROPERTY);
    String macroDefaultCategory = macroDefinition.getStringValue(MACRO_DEFAULT_CATEGORY_PROPERTY);
    WikiMacroVisibility macroVisibility = WikiMacroVisibility.fromString(macroDefinition.getStringValue(MACRO_VISIBILITY_PROPERTY));
    boolean macroSupportsInlineMode = (macroDefinition.getIntValue(MACRO_INLINE_PROPERTY) == 0) ? false : true;
    String macroContentType = macroDefinition.getStringValue(MACRO_CONTENT_TYPE_PROPERTY);
    // The macro content description as plain text
    String macroContentDescription = macroDefinition.getStringValue(MACRO_CONTENT_DESCRIPTION_PROPERTY);
    String macroCode = macroDefinition.getStringValue(MACRO_CODE_PROPERTY);
    // Verify macro id.
    if (StringUtils.isEmpty(macroId)) {
        throw new WikiMacroException(String.format("Incomplete macro definition in [%s], macro id is empty", documentReference));
    }
    // Verify macro name.
    if (StringUtils.isEmpty(macroName)) {
        macroName = macroId;
        this.logger.debug(String.format("Incomplete macro definition in [%s], macro name is empty", documentReference));
    }
    // Verify macro description.
    if (StringUtils.isEmpty(macroDescription)) {
        this.logger.debug(String.format("Incomplete macro definition in [%s], macro description is empty", documentReference));
    }
    // Verify default macro category.
    if (StringUtils.isEmpty(macroDefaultCategory)) {
        macroDefaultCategory = null;
        this.logger.debug(String.format("Incomplete macro definition in [%s], default macro category is empty", documentReference));
    }
    // Verify macro content type.
    if (StringUtils.isEmpty(macroContentType)) {
        macroContentType = MACRO_CONTENT_OPTIONAL;
    }
    // Verify macro content description.
    if (!macroContentType.equals(MACRO_CONTENT_EMPTY) && StringUtils.isEmpty(macroContentDescription)) {
        String errorMsg = "Incomplete macro definition in [%s], macro content description is empty";
        this.logger.debug(String.format(errorMsg, documentReference));
        macroContentDescription = "Macro content";
    }
    // Verify macro code.
    if (StringUtils.isEmpty(macroCode)) {
        throw new WikiMacroException(String.format("Incomplete macro definition in [%s], macro code is empty", documentReference));
    }
    // Extract macro parameters.
    List<WikiMacroParameterDescriptor> parameterDescriptors = new ArrayList<WikiMacroParameterDescriptor>();
    Vector<BaseObject> macroParameters = doc.getObjects(WIKI_MACRO_PARAMETER_CLASS);
    if (null != macroParameters) {
        for (BaseObject macroParameter : macroParameters) {
            // Vectors can contain null values
            if (null == macroParameter) {
                continue;
            }
            // Extract parameter definition.
            String parameterName = macroParameter.getStringValue(PARAMETER_NAME_PROPERTY);
            String parameterDescription = macroParameter.getStringValue(PARAMETER_DESCRIPTION_PROPERTY);
            boolean parameterMandatory = (macroParameter.getIntValue(PARAMETER_MANDATORY_PROPERTY) == 0) ? false : true;
            String parameterDefaultValue = macroParameter.getStringValue(PARAMETER_DEFAULT_VALUE_PROPERTY);
            // Verify parameter name.
            if (StringUtils.isEmpty(parameterName)) {
                throw new WikiMacroException(String.format("Incomplete macro definition in [%s], macro parameter name is empty", documentReference));
            }
            // Verify parameter description.
            if (StringUtils.isEmpty(parameterDescription)) {
                String errorMessage = "Incomplete macro definition in [%s], macro parameter description is empty";
                this.logger.debug(String.format(errorMessage, documentReference));
            }
            // If field empty, assume no default value was provided.
            if (StringUtils.isEmpty(parameterDefaultValue)) {
                parameterDefaultValue = null;
            }
            // Create the parameter descriptor.
            parameterDescriptors.add(new WikiMacroParameterDescriptor(parameterName, parameterDescription, parameterMandatory, parameterDefaultValue));
        }
    }
    // Create macro content descriptor.
    ContentDescriptor contentDescriptor = null;
    if (!macroContentType.equals(MACRO_CONTENT_EMPTY)) {
        contentDescriptor = new DefaultContentDescriptor(macroContentDescription, macroContentType.equals(MACRO_CONTENT_MANDATORY));
    }
    // Create macro descriptor.
    // Note that we register wiki macros for all syntaxes FTM and there's currently no way to restrict a wiki
    // macro for a given syntax only.
    MacroId id = new MacroId(macroId);
    MacroDescriptor macroDescriptor = new WikiMacroDescriptor(id, macroName, macroDescription, macroDefaultCategory, macroVisibility, contentDescriptor, parameterDescriptors);
    XDOM xdom;
    try {
        xdom = parser.parse(macroCode, doc.getSyntax(), documentReference);
    } catch (MissingParserException ex) {
        throw new WikiMacroException("Could not find a parser for macro content", ex);
    } catch (ParseException ex) {
        throw new WikiMacroException("Error while parsing macro content", ex);
    }
    // Create & return the macro.
    return new DefaultWikiMacro(documentReference, doc.getAuthorReference(), macroSupportsInlineMode, macroDescriptor, xdom, doc.getSyntax(), this.componentManager);
}
Also used : DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) MissingParserException(org.xwiki.rendering.parser.MissingParserException) XDOM(org.xwiki.rendering.block.XDOM) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) ArrayList(java.util.ArrayList) MacroId(org.xwiki.rendering.macro.MacroId) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor) DefaultWikiMacro(org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro) BaseObject(com.xpn.xwiki.objects.BaseObject) DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) ContentDescriptor(org.xwiki.rendering.macro.descriptor.ContentDescriptor) MacroDescriptor(org.xwiki.rendering.macro.descriptor.MacroDescriptor) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ParseException(org.xwiki.rendering.parser.ParseException) WikiMacroVisibility(org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with MacroId

use of org.xwiki.rendering.macro.MacroId in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method registerWikiMacro.

private void registerWikiMacro(String macroId, String macroContent, Syntax syntax, List<WikiMacroParameterDescriptor> parameterDescriptors) throws Exception {
    WikiMacroDescriptor descriptor = new WikiMacroDescriptor(new MacroId(macroId), "Wiki Macro", "Description", "Test", WikiMacroVisibility.GLOBAL, new DefaultContentDescriptor(false), parameterDescriptors);
    Parser parser = getComponentManager().getInstance(Parser.class, syntax.toIdString());
    DefaultWikiMacro wikiMacro = new DefaultWikiMacro(wikiMacroDocumentReference, null, true, descriptor, parser.parse(new StringReader(macroContent)), syntax, getComponentManager());
    this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, wikiMacro);
}
Also used : DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) StringReader(java.io.StringReader) MacroId(org.xwiki.rendering.macro.MacroId) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) Parser(org.xwiki.rendering.parser.Parser)

Example 4 with MacroId

use of org.xwiki.rendering.macro.MacroId in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorListener method check.

@Override
protected void check(CancelableEvent event, MacroTransformationContext context, ScriptMacroParameters parameters) {
    // Traverse the XDOM tree up to the root
    if (context.getCurrentMacroBlock() != null) {
        MacroMarkerBlock parent = context.getCurrentMacroBlock().getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
        while (parent != null) {
            String parentId = parent.getId();
            try {
                Macro<?> macro = this.macroManager.getMacro(new MacroId(parentId));
                if (macro instanceof ScriptMacro) {
                    // Find the
                    event.cancel(String.format("Nested scripts are not allowed. Current Script Macro [%s] " + "(source [%s]) is executed inside Script Macro [%s] (source [%s])", context.getCurrentMacroBlock().getId(), extractSourceContentReference(context.getCurrentMacroBlock()), parentId, extractSourceContentReference(parent)));
                } else if (macro instanceof NestedScriptMacroEnabled) {
                    // This macro has the right to produce script macro whatever the parent.
                    return;
                } else if ("include".equals(parentId)) {
                    // use NestedScriptMacroEnabled, we should maybe find something more generic
                    return;
                }
            } catch (MacroLookupException exception) {
            // Shouldn't happen, the parent macro was already successfully executed earlier
            }
            parent = parent.getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
        }
    }
}
Also used : ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroId(org.xwiki.rendering.macro.MacroId) ScriptMacro(org.xwiki.rendering.macro.script.ScriptMacro) MacroLookupException(org.xwiki.rendering.macro.MacroLookupException)

Example 5 with MacroId

use of org.xwiki.rendering.macro.MacroId in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorTest method configure.

@Before
public void configure() throws Exception {
    // Mock macro manager returns a script macro for "script" and null otherwise.
    final MacroManager macroManager = getComponentManager().getInstance(MacroManager.class);
    final ScriptMacro scriptMacro = new DefaultScriptMacro();
    final TestNestedScriptMacroEnabled nestedScriptMacroEnabled = new TestNestedScriptMacroEnabled();
    getMockery().checking(new Expectations() {

        {
            allowing(macroManager).getMacro(with(new MacroId("script")));
            will(returnValue(scriptMacro));
            allowing(macroManager).getMacro(with(new MacroId("nestedscriptmacroenabled")));
            will(returnValue(nestedScriptMacroEnabled));
            allowing(macroManager).getMacro(with(any(MacroId.class)));
            will(returnValue(null));
        }
    });
    this.validator = getComponentManager().getInstance(EventListener.class, "nestedscriptmacrovalidator");
}
Also used : Expectations(org.jmock.Expectations) DefaultScriptMacro(org.xwiki.rendering.internal.macro.script.DefaultScriptMacro) DefaultScriptMacro(org.xwiki.rendering.internal.macro.script.DefaultScriptMacro) MacroId(org.xwiki.rendering.macro.MacroId) MacroManager(org.xwiki.rendering.macro.MacroManager) EventListener(org.xwiki.observation.EventListener) Before(org.junit.Before)

Aggregations

MacroId (org.xwiki.rendering.macro.MacroId)7 DefaultContentDescriptor (org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor)4 WikiMacroDescriptor (org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor)4 StringReader (java.io.StringReader)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XDOM (org.xwiki.rendering.block.XDOM)2 DefaultWikiMacro (org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro)2 MacroManager (org.xwiki.rendering.macro.MacroManager)2 MacroDescriptor (org.xwiki.rendering.macro.descriptor.MacroDescriptor)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ArrayList (java.util.ArrayList)1 Expectations (org.jmock.Expectations)1 Before (org.junit.Before)1 Test (org.junit.Test)1 EventListener (org.xwiki.observation.EventListener)1 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)1 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)1 WordBlock (org.xwiki.rendering.block.WordBlock)1 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)1 DefaultScriptMacro (org.xwiki.rendering.internal.macro.script.DefaultScriptMacro)1