use of org.apache.tapestry5.MarkupWriter in project tapestry-5 by apache.
the class BeanFieldValidator method render.
@Override
public void render(final MarkupWriter writer) {
final BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
if (beanValidationContext == null) {
return;
}
final Validator validator = validatorFactory.getValidator();
final String currentProperty = beanValidationContext.getCurrentProperty();
if (currentProperty == null)
return;
final ValidationInfo validationInfo = getValidationInfo(beanValidationContext, currentProperty, validator);
final PropertyDescriptor propertyDescriptor = validationInfo.getPropertyDescriptor();
if (propertyDescriptor == null)
return;
for (final ConstraintDescriptor<?> descriptor : propertyDescriptor.getConstraintDescriptors()) {
Class<? extends Annotation> annotationType = descriptor.getAnnotation().annotationType();
ClientConstraintDescriptor clientConstraintDescriptor = clientValidatorSource.getConstraintDescriptor(annotationType);
if (clientConstraintDescriptor == null) {
continue;
}
String message = format("%s %s", field.getLabel(), interpolateMessage(descriptor));
Map<String, Object> attributes = CollectionFactory.newMap();
for (String attribute : clientConstraintDescriptor.getAttributes()) {
Object object = descriptor.getAttributes().get(attribute);
if (object == null) {
throw new NullPointerException(String.format("Attribute '%s' of %s is null but is required to apply client-side validation.", attribute, descriptor));
}
attributes.put(attribute, object);
}
clientConstraintDescriptor.applyClientValidation(writer, message, attributes);
}
}
use of org.apache.tapestry5.MarkupWriter in project tapestry-5 by apache.
the class TapestryModule method contributeTypeCoercer.
/**
* Adds coercions:
* <ul>
* <li>String to {@link SelectModel}
* <li>Map to {@link SelectModel}
* <li>Collection to {@link GridDataSource}
* <li>null to {@link GridDataSource}
* <li>List to {@link SelectModel}
* <li>{@link ComponentResourcesAware} (typically, a component) to {@link ComponentResources}
* <li>{@link ComponentResources} to {@link PropertyOverrides}
* <li>String to {@link Renderable}
* <li>{@link Renderable} to {@link Block}
* <li>String to {@link DateFormat}
* <li>String to {@link Resource} (via {@link AssetSource#resourceForPath(String)})
* <li>{@link Renderable} to {@link RenderCommand}</li>
* <li>String to {@link Pattern}</li>
* <li>String to {@link DateFormat}</li>
* <li>{@link Resource} to {@link DynamicTemplate}</li>
* <li>{@link Asset} to {@link Resource}</li>
* <li>{@link ValueEncoder} to {@link ValueEncoderFactory}</li>
* </ul>
*/
public static void contributeTypeCoercer(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration, final ObjectLocator objectLocator, @Builtin final ThreadLocale threadLocale, @Core final AssetSource assetSource, @Core final DynamicTemplateParser dynamicTemplateParser) {
CoercionTuple<ComponentResources, PropertyOverrides> componentResourcesToPropertyOverrides = CoercionTuple.create(ComponentResources.class, PropertyOverrides.class, new Coercion<ComponentResources, PropertyOverrides>() {
public PropertyOverrides coerce(ComponentResources input) {
return new PropertyOverridesImpl(input);
}
});
configuration.add(componentResourcesToPropertyOverrides.getKey(), componentResourcesToPropertyOverrides);
// See TAP5-2184 for why this causes some trouble!
CoercionTuple<String, SelectModel> stringToSelectModel = CoercionTuple.create(String.class, SelectModel.class, new Coercion<String, SelectModel>() {
public SelectModel coerce(String input) {
return TapestryInternalUtils.toSelectModel(input);
}
});
configuration.add(stringToSelectModel.getKey(), stringToSelectModel);
CoercionTuple<Map, SelectModel> mapToSelectModel = CoercionTuple.create(Map.class, SelectModel.class, new Coercion<Map, SelectModel>() {
@SuppressWarnings("unchecked")
public SelectModel coerce(Map input) {
return TapestryInternalUtils.toSelectModel(input);
}
});
configuration.add(mapToSelectModel.getKey(), mapToSelectModel);
CoercionTuple<Collection, GridDataSource> collectionToGridDataSource = CoercionTuple.create(Collection.class, GridDataSource.class, new Coercion<Collection, GridDataSource>() {
public GridDataSource coerce(Collection input) {
return new CollectionGridDataSource(input);
}
});
configuration.add(collectionToGridDataSource.getKey(), collectionToGridDataSource);
CoercionTuple<Void, GridDataSource> voidToGridDataSource = CoercionTuple.create(void.class, GridDataSource.class, new Coercion<Void, GridDataSource>() {
private final GridDataSource source = new NullDataSource();
public GridDataSource coerce(Void input) {
return source;
}
});
configuration.add(voidToGridDataSource.getKey(), voidToGridDataSource);
CoercionTuple<List, SelectModel> listToSelectModel = CoercionTuple.create(List.class, SelectModel.class, new Coercion<List, SelectModel>() {
private SelectModelFactory selectModelFactory;
@SuppressWarnings("unchecked")
public SelectModel coerce(List input) {
// to another value, and a race condition is harmless.
if (selectModelFactory == null) {
selectModelFactory = objectLocator.getService(SelectModelFactory.class);
}
return selectModelFactory.create(input);
}
});
configuration.add(listToSelectModel.getKey(), listToSelectModel);
CoercionTuple<String, Pattern> stringToPattern = CoercionTuple.create(String.class, Pattern.class, new Coercion<String, Pattern>() {
public Pattern coerce(String input) {
return Pattern.compile(input);
}
});
configuration.add(stringToPattern.getKey(), stringToPattern);
CoercionTuple<ComponentResourcesAware, ComponentResources> componentResourcesAwareToComponentResources = CoercionTuple.create(ComponentResourcesAware.class, ComponentResources.class, new Coercion<ComponentResourcesAware, ComponentResources>() {
public ComponentResources coerce(ComponentResourcesAware input) {
return input.getComponentResources();
}
});
configuration.add(componentResourcesAwareToComponentResources.getKey(), componentResourcesAwareToComponentResources);
CoercionTuple<String, Renderable> stringToRenderable = CoercionTuple.create(String.class, Renderable.class, new Coercion<String, Renderable>() {
public Renderable coerce(String input) {
return new StringRenderable(input);
}
});
configuration.add(stringToRenderable.getKey(), stringToRenderable);
CoercionTuple<Renderable, Block> renderableToBlock = CoercionTuple.create(Renderable.class, Block.class, new Coercion<Renderable, Block>() {
public Block coerce(Renderable input) {
return new RenderableAsBlock(input);
}
});
configuration.add(renderableToBlock.getKey(), renderableToBlock);
CoercionTuple<String, DateFormat> stringToDateFormat = CoercionTuple.create(String.class, DateFormat.class, new Coercion<String, DateFormat>() {
public DateFormat coerce(String input) {
final SimpleDateFormat dateFormat = new SimpleDateFormat(input, threadLocale.getLocale());
final String lenient = objectLocator.getService(SymbolSource.class).valueForSymbol(SymbolConstants.LENIENT_DATE_FORMAT);
dateFormat.setLenient(Boolean.parseBoolean(lenient));
return dateFormat;
}
});
configuration.add(stringToDateFormat.getKey(), stringToDateFormat);
CoercionTuple<String, Resource> stringToResource = CoercionTuple.create(String.class, Resource.class, new Coercion<String, Resource>() {
public Resource coerce(String input) {
return assetSource.resourceForPath(input);
}
});
configuration.add(stringToResource.getKey(), stringToResource);
CoercionTuple<Renderable, RenderCommand> renderableToRenderCommand = CoercionTuple.create(Renderable.class, RenderCommand.class, new Coercion<Renderable, RenderCommand>() {
public RenderCommand coerce(final Renderable input) {
return new RenderCommand() {
public void render(MarkupWriter writer, RenderQueue queue) {
input.render(writer);
}
};
}
});
configuration.add(renderableToRenderCommand.getKey(), renderableToRenderCommand);
CoercionTuple<Date, Calendar> dateToCalendar = CoercionTuple.create(Date.class, Calendar.class, new Coercion<Date, Calendar>() {
public Calendar coerce(Date input) {
Calendar calendar = Calendar.getInstance(threadLocale.getLocale());
calendar.setTime(input);
return calendar;
}
});
configuration.add(dateToCalendar.getKey(), dateToCalendar);
CoercionTuple<Resource, DynamicTemplate> resourceToDynamicTemplate = CoercionTuple.create(Resource.class, DynamicTemplate.class, new Coercion<Resource, DynamicTemplate>() {
public DynamicTemplate coerce(Resource input) {
return dynamicTemplateParser.parseTemplate(input);
}
});
configuration.add(resourceToDynamicTemplate.getKey(), resourceToDynamicTemplate);
CoercionTuple<Asset, Resource> assetToResource = CoercionTuple.create(Asset.class, Resource.class, new Coercion<Asset, Resource>() {
public Resource coerce(Asset input) {
return input.getResource();
}
});
configuration.add(assetToResource.getKey(), assetToResource);
CoercionTuple<ValueEncoder, ValueEncoderFactory> valueEncoderToValueEncoderFactory = CoercionTuple.create(ValueEncoder.class, ValueEncoderFactory.class, new Coercion<ValueEncoder, ValueEncoderFactory>() {
public ValueEncoderFactory coerce(ValueEncoder input) {
return new GenericValueEncoderFactory(input);
}
});
configuration.add(valueEncoderToValueEncoderFactory.getKey(), valueEncoderToValueEncoderFactory);
}
use of org.apache.tapestry5.MarkupWriter in project tapestry-5 by apache.
the class TapestryModule method contributePartialMarkupRenderer.
/**
* Contributes {@link PartialMarkupRendererFilter}s used when rendering a
* partial Ajax response.
* <dl>
* <dt>DocumentLinker
* <dd>Provides {@link org.apache.tapestry5.internal.services.DocumentLinker}
* <dt>ClientBehaviorSupport</dt>
* <dd>Provides {@link ClientBehaviorSupport}</dd>
* <dt>Heartbeat</dt>
* <dd>Provides {@link org.apache.tapestry5.services.Heartbeat}</dd>
* <dt>DefaultValidationDecorator</dt>
* <dt>ValidationDecorator</dt>
* <dd>Provides {@link org.apache.tapestry5.ValidationDecorator} (via {@link ValidationDecoratorFactory#newInstance(org.apache.tapestry5.MarkupWriter)})</dd>
* </dl>
*/
public void contributePartialMarkupRenderer(OrderedConfiguration<PartialMarkupRendererFilter> configuration, final ValidationDecoratorFactory validationDecoratorFactory) {
PartialMarkupRendererFilter documentLinker = new PartialMarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
PartialMarkupDocumentLinker linker = new PartialMarkupDocumentLinker();
environment.push(DocumentLinker.class, linker);
renderer.renderMarkup(writer, reply);
environment.pop(DocumentLinker.class);
linker.commit(reply);
}
};
PartialMarkupRendererFilter clientBehaviorSupport = new PartialMarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
ClientBehaviorSupportImpl support = new ClientBehaviorSupportImpl();
environment.push(ClientBehaviorSupport.class, support);
renderer.renderMarkup(writer, reply);
environment.pop(ClientBehaviorSupport.class);
}
};
PartialMarkupRendererFilter heartbeat = new PartialMarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
Heartbeat heartbeat = new HeartbeatImpl();
heartbeat.begin();
environment.push(Heartbeat.class, heartbeat);
renderer.renderMarkup(writer, reply);
environment.pop(Heartbeat.class);
heartbeat.end();
}
};
PartialMarkupRendererFilter defaultValidationDecorator = new PartialMarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
ValidationDecorator decorator = validationDecoratorFactory.newInstance(writer);
environment.push(ValidationDecorator.class, decorator);
renderer.renderMarkup(writer, reply);
environment.pop(ValidationDecorator.class);
}
};
configuration.add("DocumentLinker", documentLinker);
configuration.add("ClientBehaviorSupport", clientBehaviorSupport, "after:JavaScriptSupport");
configuration.add("Heartbeat", heartbeat);
configuration.add("ValidationDecorator", defaultValidationDecorator);
}
use of org.apache.tapestry5.MarkupWriter in project tapestry-5 by apache.
the class OutputRawTest method value_is_null.
@Test
public void value_is_null() {
MarkupWriter writer = mockMarkupWriter();
replay();
OutputRaw component = new OutputRaw();
assertFalse(component.beginRender(writer));
verify();
}
use of org.apache.tapestry5.MarkupWriter in project tapestry-5 by apache.
the class OutputTest method null_output.
@Test
public void null_output() {
MarkupWriter writer = createMarkupWriter();
ComponentResources resources = mockComponentResources();
replay();
Output component = new Output();
component.setup(null, format, true, null, resources);
writer.element("root");
assertFalse(component.beginRender(writer));
writer.end();
verify();
assertEquals(writer.toString(), "<root></root>");
}
Aggregations