use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class SubordinateControlHelper method registerSubordinateControl.
/**
* Register the Subordinate Control so that it can be applied by the {@link SubordinateControlInterceptor}.
*
* @param controlId the subordinate id
*/
public static void registerSubordinateControl(final String controlId) {
UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
if (uic == null) {
throw new SystemException("No User Context available to register Subordinate Control.");
}
Set<String> controls = (Set<String>) uic.getFwkAttribute(SUBORDINATE_CONTROL_SESSION_KEY);
if (controls == null) {
controls = new HashSet<>();
uic.setFwkAttribute(SUBORDINATE_CONTROL_SESSION_KEY, controls);
}
controls.add(controlId);
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class VelocityRendererImpl method renderTemplate.
/**
* {@inheritDoc}
*/
@Override
public void renderTemplate(final String templateName, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
LOG.debug("Rendering velocity template [" + templateName + "].");
// Velocity uses a ClassLoader so dont use an absolute path.
String name = templateName.startsWith("/") ? templateName.substring(1) : templateName;
try {
// Load template
Template template = getVelocityEngine().getTemplate(name);
// Map the tagged components to be used in the replace writer
Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
// Setup context
VelocityContext velocityContext = new VelocityContext();
for (Map.Entry<String, Object> entry : context.entrySet()) {
velocityContext.put(entry.getKey(), entry.getValue());
}
// Write template
UIContext uic = UIContextHolder.getCurrent();
try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
template.merge(velocityContext, velocityWriter);
}
} catch (ResourceNotFoundException e) {
throw new SystemException("Could not find velocity template [" + templateName + "]. " + e.getMessage(), e);
} catch (Exception e) {
throw new SystemException("Problems with velocity template [" + templateName + "]. " + e.getMessage(), e);
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class TreeUtil method findWComponents.
/**
* Retrieves WComponents by their path in the WComponent tree.
* <p>
* Paths are specified using class names, starting from the furthest ancestor. To reduce the path lengths, class
* names do not need to be fully-qualified. The path does not need to explicitly state intermediate components
* between components, and may include an index suffix to select a particular instance of a component in e.g. a
* repeater or a set of fields. Some example paths are shown below.
* </p>
* Example paths.
* <dl>
* <dt><code>{ "MyComponent" }</code></dt>
* <dd>Matches the first instance of MyComponent.</dd>
* <dt><code>{ "MyComponent[0]" }</code></dt>
* <dd>Also matches the first instance of MyComponent.</dd>
* <dt><code>{ "MyComponent[1]" }</code></dt>
* <dd>Matches the second instance of MyComponent.</dd>
* <dt><code>{ "MyPanel", "MyComponent" }</code></dt>
* <dd>Matches the first instance of MyComponent which is nested anywhere under a MyPanel.</dd>
* <dt><code>{ "MyApp", "MyPanel", "MyComponent" }</code></dt>
* <dd>Matches the first instance of MyComponent, nested within a MyPanel, which is in turn nested somewhere within
* a MyApp.</dd>
* </dl>
*
* @param component the component to search from.
* @param path the path to the WComponent.
* @param visibleOnly visible only
* @return the component matching the given path, or null if not found.
*/
public static ComponentWithContext[] findWComponents(final WComponent component, final String[] path, final boolean visibleOnly) {
UIContext uic = UIContextHolder.getCurrent();
if (uic == null) {
throw new IllegalStateException("No user context available.");
}
List<ComponentWithContext> matchAtLevel = new ArrayList<>();
matchAtLevel.add(new ComponentWithContext(component, uic));
for (int i = 0; i < path.length; i++) {
List<ComponentWithContext> matchAtLastLevel = matchAtLevel;
matchAtLevel = new ArrayList<>();
for (ComponentWithContext comp : matchAtLastLevel) {
String[] parts = path[i].trim().split("[\\[\\]]");
String className = parts[0].trim();
int index = parts.length == 2 ? Integer.parseInt(parts[1]) : -1;
FindComponentsByClassVisitor visitor = new FindComponentsByClassVisitor(comp.getComponent(), className, i == 0);
UIContextHolder.pushContext(comp.getContext());
try {
if (visibleOnly) {
TreeUtil.traverseVisible(comp.getComponent(), visitor);
} else {
TreeUtil.traverse(comp.getComponent(), visitor);
}
} finally {
UIContextHolder.popContext();
}
if (index >= 0) {
if (index < visitor.getResult().size()) {
matchAtLevel.add(visitor.getResult().get(index));
}
} else {
matchAtLevel.addAll(visitor.getResult());
}
}
}
return matchAtLevel.toArray(new ComponentWithContext[matchAtLevel.size()]);
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class WSuggestionsRenderer_Test method testDoPaintAjaxOptions.
@Test
public void testDoPaintAjaxOptions() throws IOException, SAXException, XpathException {
List<String> options = Arrays.asList("A", "B", "C");
WSuggestions field = new WSuggestions(options);
// Set action for AJAX refresh
field.setRefreshAction(new Action() {
@Override
public void execute(final ActionEvent event) {
// Do nothing
}
});
assertSchemaMatch(field);
assertXpathEvaluatesTo(field.getId(), "//ui:suggestions/@id", field);
assertXpathNotExists("//ui:suggestions/@min", field);
assertXpathNotExists("//ui:suggestions/@data", field);
// AJAX flag should be true
assertXpathEvaluatesTo("true", "//ui:suggestions/@ajax", field);
// Suggestions should only be rendered when refreshed via AJAX
assertXpathNotExists("//ui:suggestions/ui:suggestion", field);
// Setup suggestions as the current AJAX trigger
UIContext uic = createUIContext();
uic.setUI(new DefaultWComponent());
setActiveContext(uic);
try {
AjaxOperation operation = new AjaxOperation(field.getId(), field.getId());
AjaxHelper.setCurrentOperationDetails(operation, null);
assertSchemaMatch(field);
assertXpathExists("//ui:suggestions/ui:suggestion", field);
assertXpathEvaluatesTo(options.get(0), "//ui:suggestions/ui:suggestion[1]/@value", field);
assertXpathEvaluatesTo(options.get(1), "//ui:suggestions/ui:suggestion[2]/@value", field);
assertXpathEvaluatesTo(options.get(2), "//ui:suggestions/ui:suggestion[3]/@value", field);
} finally {
AjaxHelper.clearCurrentOperationDetails();
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class WTableRenderer_Test method testXssEscaping.
@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
WTable table = new WTable();
table.addColumn(new WTableColumn(getMaliciousContent(), WText.class));
table.addColumn(new WTableColumn(getMaliciousContent(), WText.class));
table.addColumn(new WTableColumn(getMaliciousContent(), WText.class));
table.setNoDataMessage(getMaliciousAttribute("ui:table"));
UIContext uic = createUIContext();
assertSafeContent(table);
WButton button = new WButton("dummy");
table.addAction(button);
table.addActionConstraint(button, new ActionConstraint(0, 1, false, getMaliciousAttribute("ui:action")));
assertSafeContent(table);
TableModel tableModel = createTableModel();
table.setTableModel(tableModel);
// clear out cached data from previous renders
uic.clearScratchMap();
assertSafeContent(table);
table.setCaption(getMaliciousAttribute("ui:table"));
assertSafeContent(table);
}
Aggregations