Search in sources :

Example 1 with WikiMacroException

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

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

the class DefaultWikiMacroInitializer method registerMacro.

/**
 * Register a wiki macro in the component manager, if the macro author has the required rights.
 *
 * @param wikiMacroDocumentReference the document holding the macro definition
 * @param wikiMacroDocumentAuthor the author of the macro document
 * @param xcontext the current request context
 */
private void registerMacro(DocumentReference wikiMacroDocumentReference, String wikiMacroDocumentAuthor, XWikiContext xcontext) {
    this.logger.debug("Registering macro in document [{}]...", wikiMacroDocumentReference);
    DocumentReference originalAuthor = xcontext.getUserReference();
    try {
        WikiMacro macro = this.wikiMacroFactory.createWikiMacro(wikiMacroDocumentReference);
        this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, macro);
        this.logger.debug("Macro [{}] from document [{}] is now registered.", macro.getDescriptor().getId().getId(), wikiMacroDocumentReference);
    } catch (InsufficientPrivilegesException ex) {
        // Just log the exception and skip to the next.
        // We only log at the debug level here as this is not really an error
        this.logger.debug(ex.getMessage(), ex);
    } catch (WikiMacroException ex) {
        // Just log the exception and skip to the next.
        this.logger.error(ex.getMessage(), ex);
    } finally {
        xcontext.setUserReference(originalAuthor);
    }
}
Also used : InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with WikiMacroException

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

the class DefaultWikiMacroManager method registerWikiMacro.

@Override
public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException {
    WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor();
    // Verify that the user has the right to register this wiki macro the chosen visibility
    if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) {
        DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
        componentDescriptor.setRoleType(Macro.class);
        componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId());
        // Save current context informations
        String currentUser = this.bridge.getCurrentUser();
        EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference();
        try {
            // Put the proper context information to let components manager use the proper keys to find
            // components to unregister
            this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference()));
            this.modelContext.setCurrentEntityReference(documentReference);
            // Register the macro against the right Component Manager, depending on the defined macro visibility.
            findComponentManager(macroDescriptor.getVisibility()).registerComponent(componentDescriptor, wikiMacro);
            this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro));
        } catch (Exception e) {
            throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()), e);
        } finally {
            // Restore previous context informations
            this.bridge.setCurrentUser(currentUser);
            this.modelContext.setCurrentEntityReference(currentEntityReference);
        }
    } else {
        throw new InsufficientPrivilegesException(String.format("Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()));
    }
}
Also used : InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) Macro(org.xwiki.rendering.macro.Macro) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) EntityReference(org.xwiki.model.reference.EntityReference) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException)

Example 4 with WikiMacroException

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

the class WikiMacroEventListener method registerMacro.

/**
 * @param documentReference the reference of the document containing the macro to register
 */
private void registerMacro(DocumentReference documentReference) {
    // Unregister any existing macro registered under this document.
    if (unregisterMacro(documentReference)) {
        // Check whether the given document has a wiki macro defined in it.
        if (this.macroFactory.containsWikiMacro(documentReference)) {
            // Attempt to create a wiki macro.
            WikiMacro wikiMacro;
            try {
                wikiMacro = this.macroFactory.createWikiMacro(documentReference);
            } catch (WikiMacroException e) {
                this.logger.debug(String.format("Failed to create wiki macro [%s]", documentReference), e);
                return;
            }
            // Register the macro.
            registerMacro(documentReference, wikiMacro);
        }
    }
}
Also used : WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException)

Example 5 with WikiMacroException

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

the class DefaultWikiMacroManager method unregisterWikiMacro.

@Override
public void unregisterWikiMacro(DocumentReference documentReference) throws WikiMacroException {
    WikiMacroData macroData = this.wikiMacroMap.get(documentReference);
    if (macroData != null) {
        WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) macroData.getWikiMacro().getDescriptor();
        // Verify that the user has the right to unregister this wiki macro for the chosen visibility
        if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) {
            String currentUser = this.bridge.getCurrentUser();
            EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference();
            try {
                // Put the proper context information to let components manager use the proper keys to find
                // components to unregister
                this.bridge.setCurrentUser(this.serializer.serialize(macroData.getWikiMacro().getAuthorReference()));
                this.modelContext.setCurrentEntityReference(documentReference);
                findComponentManager(macroDescriptor.getVisibility()).unregisterComponent(Macro.class, macroData.getHint());
                this.wikiMacroMap.remove(documentReference);
            } catch (Exception e) {
                throw new WikiMacroException(String.format("Failed to unregister macro [%s] in [%s] for " + "visibility [%s]", macroData.getHint(), documentReference, macroDescriptor.getVisibility()), e);
            } finally {
                this.bridge.setCurrentUser(currentUser);
                this.modelContext.setCurrentEntityReference(currentEntityReference);
            }
        } else {
            throw new WikiMacroException(String.format("Unable to unregister macro [%s] in [%s] for visibility " + "[%s] due to insufficient privileges", macroData.getWikiMacro().getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()));
        }
    } else {
        throw new WikiMacroException(String.format("Macro in [%s] isn't registered", documentReference));
    }
}
Also used : WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) EntityReference(org.xwiki.model.reference.EntityReference) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException)

Aggregations

WikiMacroException (org.xwiki.rendering.macro.wikibridge.WikiMacroException)5 InsufficientPrivilegesException (org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException)3 WikiMacro (org.xwiki.rendering.macro.wikibridge.WikiMacro)3 WikiMacroDescriptor (org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 EntityReference (org.xwiki.model.reference.EntityReference)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ArrayList (java.util.ArrayList)1 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)1 XDOM (org.xwiki.rendering.block.XDOM)1 DefaultWikiMacro (org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro)1 Macro (org.xwiki.rendering.macro.Macro)1 MacroId (org.xwiki.rendering.macro.MacroId)1 ContentDescriptor (org.xwiki.rendering.macro.descriptor.ContentDescriptor)1 DefaultContentDescriptor (org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor)1 MacroDescriptor (org.xwiki.rendering.macro.descriptor.MacroDescriptor)1 WikiMacroParameterDescriptor (org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor)1 WikiMacroVisibility (org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility)1 MissingParserException (org.xwiki.rendering.parser.MissingParserException)1