Search in sources :

Example 6 with MarkupException

use of org.apache.wicket.markup.MarkupException in project wicket by apache.

the class Component method internalRenderComponent.

/**
 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT!
 * <p>
 * Renders the component at the current position in the given markup stream. The method
 * onComponentTag() is called to allow the component to mutate the start tag. The method
 * onComponentTagBody() is then called to permit the component to render its body.
 */
protected final void internalRenderComponent() {
    final IMarkupFragment markup = getMarkup();
    if (markup == null) {
        throw new MarkupException("Markup not found. Component: " + toString());
    }
    final MarkupStream markupStream = new MarkupStream(markup);
    // Get mutable copy of next tag
    final ComponentTag openTag = markupStream.getTag();
    final ComponentTag tag = openTag.mutable();
    // Call any tag handler
    onComponentTag(tag);
    // If we're an openclose tag
    if (!tag.isOpenClose() && !tag.isOpen()) {
        // We were something other than <tag> or <tag/>
        markupStream.throwMarkupException("Method renderComponent called on bad markup element: " + tag);
    }
    if (tag.isOpenClose() && openTag.isOpen()) {
        markupStream.throwMarkupException("You can not modify a open tag to open-close: " + tag);
    }
    try {
        // Render open tag
        boolean renderBodyOnly = getRenderBodyOnly();
        if (renderBodyOnly) {
            ExceptionSettings.NotRenderableErrorStrategy notRenderableErrorStrategy = ExceptionSettings.NotRenderableErrorStrategy.LOG_WARNING;
            if (Application.exists()) {
                notRenderableErrorStrategy = getApplication().getExceptionSettings().getNotRenderableErrorStrategy();
            }
            if (getFlag(FLAG_OUTPUT_MARKUP_ID)) {
                String message = String.format("Markup id set on a component that renders its body only. " + "Markup id: %s, component id: %s.", getMarkupId(), getId());
                if (notRenderableErrorStrategy == ExceptionSettings.NotRenderableErrorStrategy.THROW_EXCEPTION) {
                    throw new IllegalStateException(message);
                }
                log.warn(message);
            }
            if (getFlag(FLAG_PLACEHOLDER)) {
                String message = String.format("Placeholder tag set on a component that renders its body only. " + "Component id: %s.", getId());
                if (notRenderableErrorStrategy == ExceptionSettings.NotRenderableErrorStrategy.THROW_EXCEPTION) {
                    throw new IllegalStateException(message);
                }
                log.warn(message);
            }
        } else {
            renderComponentTag(tag);
        }
        markupStream.next();
        // Render the body only if open-body-close. Do not render if open-close.
        if (tag.isOpen()) {
            // Render the body. The default strategy will simply call the component's
            // onComponentTagBody() implementation.
            getMarkupSourcingStrategy().onComponentTagBody(this, markupStream, tag);
            // Render close tag
            if (openTag.isOpen()) {
                renderClosingComponentTag(markupStream, tag, renderBodyOnly);
            } else if (renderBodyOnly == false) {
                if (needToRenderTag(openTag)) {
                    // Close the manually opened tag. And since the user might have changed the
                    // tag name ...
                    getResponse().write(tag.syntheticCloseTagString());
                }
            }
        }
    } catch (WicketRuntimeException wre) {
        throw wre;
    } catch (RuntimeException re) {
        throw new WicketRuntimeException("Exception in rendering component: " + this, re);
    }
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupException(org.apache.wicket.markup.MarkupException) ExceptionSettings(org.apache.wicket.settings.ExceptionSettings)

Example 7 with MarkupException

use of org.apache.wicket.markup.MarkupException in project wicket by apache.

the class MarkupContainer method addedComponent.

/**
 * @param child
 *            Component being added
 */
private void addedComponent(final Component child) {
    // Check for degenerate case
    Args.notNull(child, "child");
    MarkupContainer parent = child.getParent();
    if (parent != null && parent != this) {
        parent.remove(child);
    }
    // Set child's parent
    child.setParent(this);
    final DebugSettings debugSettings = Application.get().getDebugSettings();
    if (debugSettings.isLinePreciseReportingOnAddComponentEnabled() && debugSettings.getComponentUseCheck()) {
        child.setMetaData(ADDED_AT_KEY, ComponentStrings.toString(child, new MarkupException("added")));
    }
    Page page = findPage();
    if (page != null) {
        // tell the page a component has been added first, to allow it to initialize
        page.componentAdded(child);
        // initialize the component
        if (page.isInitialized()) {
            child.internalInitialize();
        }
    }
    // beforeRender on this component's children. So we need to initialize the newly added one
    if (isPreparedForRender()) {
        child.beforeRender();
    }
}
Also used : DebugSettings(org.apache.wicket.settings.DebugSettings) MarkupException(org.apache.wicket.markup.MarkupException)

Example 8 with MarkupException

use of org.apache.wicket.markup.MarkupException in project wicket by apache.

the class WicketMessageResolver method resolve.

@Override
public Component resolve(final MarkupContainer container, final MarkupStream markupStream, final ComponentTag tag) {
    if (tag instanceof WicketTag) {
        WicketTag wtag = (WicketTag) tag;
        if (wtag.isMessageTag()) {
            IValueMap attributes = wtag.getAttributes();
            String messageKey = attributes.getString(KEY_ATTRIBUTE);
            if (Strings.isEmpty(messageKey)) {
                throw new MarkupException("Wrong format of <wicket:message key='xxx'>: attribute 'key' is missing");
            }
            boolean escape = attributes.getBoolean(ESCAPE_ATTRIBUTE);
            final String id = wtag.getId();
            MessageContainer label = new MessageContainer(id, messageKey, escape);
            label.setRenderBodyOnly(container.getApplication().getMarkupSettings().getStripWicketTags());
            return label;
        }
    }
    // We were not able to handle the tag
    return null;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) IValueMap(org.apache.wicket.util.value.IValueMap) MarkupException(org.apache.wicket.markup.MarkupException)

Example 9 with MarkupException

use of org.apache.wicket.markup.MarkupException in project wicket by apache.

the class FragmentMarkupSourcingStrategy method getMarkup.

/**
 * Search for the child's markup in the fragment markup.
 */
@Override
public IMarkupFragment getMarkup(final MarkupContainer container, final Component child) {
    // Get the markup to search for the fragment markup
    IMarkupFragment markup = chooseMarkup(container);
    if (markup == null) {
        throw new MarkupException("The fragments markup provider has no associated markup. " + "No markup to search for fragment markup with id: " + markupId);
    }
    // Search for the fragment markup
    IMarkupFragment childMarkup = TagUtils.findTagMarkup(markup, markupId, FRAGMENT, 1);
    if (childMarkup == null) {
        // There is one more option if the markup provider has associated markup
        MarkupContainer markupProvider = getMarkupProvider(container);
        Markup associatedMarkup = markupProvider.getAssociatedMarkup();
        if (associatedMarkup != null) {
            markup = associatedMarkup;
            if (markup != null) {
                childMarkup = markup.find(markupId);
            }
        }
    }
    if (childMarkup == null) {
        throw new MarkupNotFoundException("No Markup found for Fragment " + markupId + " in providing markup container " + getMarkupProvider(container));
    } else {
        MarkupElement fragmentTag = childMarkup.get(0);
        if ((fragmentTag instanceof WicketTag && ((WicketTag) fragmentTag).isFragmentTag()) == false) {
            throw new MarkupNotFoundException("Markup found for Fragment '" + markupId + "' in providing markup container " + getMarkupProvider(container) + " is not a <wicket:fragment> tag");
        }
    }
    if (child == null) {
        return childMarkup;
    }
    // search for the child inside the fragment markup
    return childMarkup.find(child.getId());
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) MarkupContainer(org.apache.wicket.MarkupContainer) Markup(org.apache.wicket.markup.Markup) MarkupNotFoundException(org.apache.wicket.markup.MarkupNotFoundException) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupElement(org.apache.wicket.markup.MarkupElement) MarkupException(org.apache.wicket.markup.MarkupException)

Example 10 with MarkupException

use of org.apache.wicket.markup.MarkupException in project wicket by apache.

the class ExceptionErrorPage method getErrorMessage.

/**
 * Converts a Throwable to a string.
 *
 * @param throwable
 *            The throwable
 * @return The string
 */
public String getErrorMessage(final Throwable throwable) {
    if (throwable != null) {
        StringBuilder sb = new StringBuilder(256);
        // first print the last cause
        List<Throwable> al = convertToList(throwable);
        int length = al.size() - 1;
        Throwable cause = al.get(length);
        sb.append("Last cause: ").append(cause.getMessage()).append('\n');
        if (throwable instanceof WicketRuntimeException) {
            String msg = throwable.getMessage();
            if ((msg != null) && (msg.equals(cause.getMessage()) == false)) {
                if (throwable instanceof MarkupException) {
                    MarkupStream stream = ((MarkupException) throwable).getMarkupStream();
                    if (stream != null) {
                        String text = "\n" + stream.toString();
                        if (msg.endsWith(text)) {
                            msg = msg.substring(0, msg.length() - text.length());
                        }
                    }
                }
                sb.append("WicketMessage: ");
                sb.append(msg);
                sb.append("\n\n");
            }
        }
        return sb.toString();
    } else {
        return "[Unknown]";
    }
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupException(org.apache.wicket.markup.MarkupException)

Aggregations

MarkupException (org.apache.wicket.markup.MarkupException)10 MarkupStream (org.apache.wicket.markup.MarkupStream)5 ComponentTag (org.apache.wicket.markup.ComponentTag)4 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)4 WicketTag (org.apache.wicket.markup.WicketTag)3 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 MarkupElement (org.apache.wicket.markup.MarkupElement)2 MarkupContainer (org.apache.wicket.MarkupContainer)1 Markup (org.apache.wicket.markup.Markup)1 MarkupFragment (org.apache.wicket.markup.MarkupFragment)1 MarkupNotFoundException (org.apache.wicket.markup.MarkupNotFoundException)1 BorderMarkupSourcingStrategy (org.apache.wicket.markup.html.panel.BorderMarkupSourcingStrategy)1 DebugSettings (org.apache.wicket.settings.DebugSettings)1 ExceptionSettings (org.apache.wicket.settings.ExceptionSettings)1 IValueMap (org.apache.wicket.util.value.IValueMap)1