Search in sources :

Example 1 with ContainerInfo

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

the class AbstractMarkupFilter method getRequestUniqueId.

/**
 * Returns an id using the request-relative counter associated with the
 * underlying {@link org.apache.wicket.markup.MarkupResourceStream}'s owner container
 * (see {@link org.apache.wicket.markup.MarkupResourceStream#getContainerInfo()}).
 * This can be useful for autocomponent tags that need to get a tag id.
 *
 * @return
 * 		the request-relative id
 */
protected int getRequestUniqueId() {
    RequestCycle requestCycle = RequestCycle.get();
    Map<String, AtomicInteger> markupUniqueCounters = requestCycle.getMetaData(REQUEST_COUNTER_KEY);
    ContainerInfo containerInfo = getMarkupResourceStream().getContainerInfo();
    String cacheKey = containerInfo != null ? containerInfo.getContainerClass().getCanonicalName() : null;
    if (markupUniqueCounters == null) {
        markupUniqueCounters = new HashMap<>();
        requestCycle.setMetaData(REQUEST_COUNTER_KEY, markupUniqueCounters);
    }
    AtomicInteger counter = markupUniqueCounters.get(cacheKey);
    if (counter == null) {
        counter = new AtomicInteger();
        markupUniqueCounters.put(cacheKey, counter);
    }
    int cacheHash = cacheKey == null ? 0 : cacheKey.hashCode();
    // using the same algorithm of String#hashCode()
    return cacheHash * 31 + counter.getAndIncrement();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ContainerInfo(org.apache.wicket.markup.ContainerInfo)

Example 2 with ContainerInfo

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

the class BorderBehavior method findMarkupStream.

/**
 * @param owner
 * @return markup stream
 */
private MarkupStream findMarkupStream(final Component owner) {
    final MarkupType markupType = getMarkupType(owner);
    if (markupType == null) {
        return null;
    }
    // TODO we need to expose this functionality for any class not just for
    // markupcontainers in markupcache so we don't have to replicate this
    // logic here
    // Get locator to search for the resource
    final IResourceStreamLocator locator = Application.get().getResourceSettings().getResourceStreamLocator();
    final String style = owner.getStyle();
    final String variation = owner.getVariation();
    final Locale locale = owner.getLocale();
    MarkupResourceStream markupResourceStream = null;
    Class<?> containerClass = getClass();
    while (!(containerClass.equals(BorderBehavior.class))) {
        String path = containerClass.getName().replace('.', '/');
        IResourceStream resourceStream = locator.locate(containerClass, path, style, variation, locale, markupType.getExtension(), false);
        // Did we find it already?
        if (resourceStream != null) {
            ContainerInfo ci = new ContainerInfo(containerClass, locale, style, variation, markupType);
            markupResourceStream = new MarkupResourceStream(resourceStream, ci, containerClass);
            break;
        }
        // Walk up the class hierarchy one level, if markup has not
        // yet been found
        containerClass = containerClass.getSuperclass();
    }
    if (markupResourceStream == null) {
        throw new WicketRuntimeException("Could not find markup for component border `" + getClass().getName() + "`");
    }
    try {
        IMarkupFragment markup = MarkupFactory.get().newMarkupParser(markupResourceStream).parse();
        return new MarkupStream(markup);
    } catch (Exception e) {
        throw new WicketRuntimeException("Could not parse markup from markup resource stream: " + markupResourceStream.toString(), e);
    } finally {
        try {
            markupResourceStream.close();
        } catch (IOException e) {
            throw new WicketRuntimeException("Cannot close markup resource stream: " + markupResourceStream, e);
        }
    }
}
Also used : Locale(java.util.Locale) IResourceStream(org.apache.wicket.util.resource.IResourceStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) MarkupStream(org.apache.wicket.markup.MarkupStream) IOException(java.io.IOException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ContainerInfo(org.apache.wicket.markup.ContainerInfo) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupType(org.apache.wicket.markup.MarkupType)

Example 3 with ContainerInfo

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

the class BaseWicketTester method startComponentInPage.

/**
 * Process a component. A web page will be automatically created with the {@code pageMarkup}
 * provided. In case {@code pageMarkup} is null, the markup will be automatically created with
 * {@link #createPageMarkup(String)}.
 * <p>
 * <strong>Note</strong>: the component id is set by the user. To reach any of its children use
 * this id + their relative path to the component itself. For example if the started component
 * has id <em>compId</em> and a Link child component component with id "link" then after
 * starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
 * </p>
 *
 * @param <C>
 *            the type of the component
 * @param component
 *            the component to be tested
 * @param pageMarkup
 *            the markup for the Page that will be automatically created. May be {@code null}.
 * @return The component processed
 */
public final <C extends Component> C startComponentInPage(final C component, IMarkupFragment pageMarkup) {
    Args.notNull(component, "component");
    // Create a page object and assign the markup
    Page page = createPage();
    if (page == null) {
        fail("The automatically created page should not be null.");
    }
    // Automatically create the page markup if not provided
    if (pageMarkup == null) {
        String markup = createPageMarkup(component.getId());
        if (markup == null) {
            fail("The markup for the automatically created page should not be null.");
        }
        try {
            // set a ContainerInfo to be able to use HtmlHeaderContainer so header contribution
            // still work. WICKET-3700
            ContainerInfo containerInfo = new ContainerInfo(page);
            MarkupResourceStream markupResourceStream = new MarkupResourceStream(new StringResourceStream(markup), containerInfo, page.getClass());
            MarkupParser markupParser = getApplication().getMarkupSettings().getMarkupFactory().newMarkupParser(markupResourceStream);
            pageMarkup = markupParser.parse();
        } catch (Exception e) {
            String errorMessage = "Error while parsing the markup for the autogenerated page: " + e.getMessage();
            log.error(errorMessage, e);
            fail(errorMessage);
        }
    }
    if (page instanceof StartComponentInPage) {
        ((StartComponentInPage) page).setPageMarkup(pageMarkup);
    } else {
        page.setMarkup(pageMarkup);
    }
    // Add the child component
    page.add(component);
    // Preserve 'componentInPage' because #startPage() needs to null-fy it
    ComponentInPage oldComponentInPage = componentInPage;
    // Process the page
    startPage(page);
    // Remember the "root" component processes and return it
    if (oldComponentInPage != null) {
        componentInPage = oldComponentInPage;
    } else {
        componentInPage = new ComponentInPage();
        componentInPage.component = component;
    }
    return component;
}
Also used : ContainerInfo(org.apache.wicket.markup.ContainerInfo) WebPage(org.apache.wicket.markup.html.WebPage) Page(org.apache.wicket.Page) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ParseException(java.text.ParseException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) MarkupParser(org.apache.wicket.markup.MarkupParser)

Aggregations

ContainerInfo (org.apache.wicket.markup.ContainerInfo)3 IOException (java.io.IOException)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 MarkupResourceStream (org.apache.wicket.markup.MarkupResourceStream)2 ParseException (java.text.ParseException)1 Locale (java.util.Locale)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Page (org.apache.wicket.Page)1 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)1 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)1 MarkupParser (org.apache.wicket.markup.MarkupParser)1 MarkupStream (org.apache.wicket.markup.MarkupStream)1 MarkupType (org.apache.wicket.markup.MarkupType)1 WebPage (org.apache.wicket.markup.html.WebPage)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1 IResourceStream (org.apache.wicket.util.resource.IResourceStream)1 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)1 StringResourceStream (org.apache.wicket.util.resource.StringResourceStream)1