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();
}
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);
}
}
}
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;
}
Aggregations