Search in sources :

Example 1 with WikiMacroParameterDescriptor

use of org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor 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 2 with WikiMacroParameterDescriptor

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

the class DefaultWikiMacroTest method registerWikiMacro.

private void registerWikiMacro(String macroId, String macroContent, Syntax syntax) throws Exception {
    List<WikiMacroParameterDescriptor> parameterDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", true), new WikiMacroParameterDescriptor("param2", "This is param2", true));
    registerWikiMacro(macroId, macroContent, syntax, parameterDescriptors);
}
Also used : WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor)

Example 3 with WikiMacroParameterDescriptor

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

the class DefaultWikiMacroTest method testDefaultParameterValues.

/**
 * Test default parameter value injection.
 */
@Test
public void testDefaultParameterValues() throws Exception {
    // Velocity Manager mock.
    final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class);
    DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>();
    descriptorVM.setRoleType(VelocityManager.class);
    getComponentManager().registerComponent(descriptorVM, mockVelocityManager);
    // Initialize velocity engine.
    final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class);
    Properties properties = new Properties();
    properties.setProperty("resource.loader", "file");
    vEngine.initialize(properties);
    // Hack into velocity context.
    Execution execution = getComponentManager().getInstance(Execution.class);
    Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext");
    final VelocityContext vContext = new VelocityContext();
    vContext.put("xcontext", xwikiContext);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockVelocityManager).getCurrentVelocityContext();
            will(returnValue(vContext));
            oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
            will(new Action() {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
                }

                @Override
                public void describeTo(Description description) {
                }
            });
        }
    });
    List<WikiMacroParameterDescriptor> parameterDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", false, "default_value"));
    registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.param1 $xcontext.macro.params.paraM1{{/velocity}}", Syntax.XWIKI_2_0, parameterDescriptors);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacro1/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
    Assert.assertEquals("default_value default_value", printer.toString());
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) StringReader(java.io.StringReader) Properties(java.util.Properties) WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) Execution(org.xwiki.context.Execution) VelocityManager(org.xwiki.velocity.VelocityManager) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) Test(org.junit.Test)

Example 4 with WikiMacroParameterDescriptor

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

the class DefaultWikiMacroTest method registerWikiMacro.

private void registerWikiMacro(String macroId, String macroContent) throws Exception {
    List<WikiMacroParameterDescriptor> parameterDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", true), new WikiMacroParameterDescriptor("param2", "This is param2", true));
    registerWikiMacro(macroId, macroContent, parameterDescriptors);
}
Also used : WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor)

Aggregations

WikiMacroParameterDescriptor (org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor)4 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 VelocityContext (org.apache.velocity.VelocityContext)1 Description (org.hamcrest.Description)1 Expectations (org.jmock.Expectations)1 Action (org.jmock.api.Action)1 Invocation (org.jmock.api.Invocation)1 Test (org.junit.Test)1 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)1 Execution (org.xwiki.context.Execution)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 XDOM (org.xwiki.rendering.block.XDOM)1 Converter (org.xwiki.rendering.converter.Converter)1