-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eschleb
committed
Feb 21, 2024
1 parent
b36730a
commit fa3b75b
Showing
13 changed files
with
470 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...nolia/definition/builder/validator/AbstractConfiguredFieldValidatorDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.ConfiguredFieldValidatorDefinition; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.inject.Provider; | ||
import java.util.Optional; | ||
|
||
public abstract class AbstractConfiguredFieldValidatorDefinitionBuilder<D extends ConfiguredFieldValidatorDefinition, B extends AbstractConfiguredFieldValidatorDefinitionBuilder<D, B>> { | ||
private final Provider<D> factory; | ||
|
||
@Nullable | ||
private String errorMessage; | ||
|
||
protected AbstractConfiguredFieldValidatorDefinitionBuilder(final Provider<D> factory) { | ||
this.factory = factory; | ||
} | ||
|
||
public B errorMessage(final String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
return self(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected B self() { | ||
return (B) this; | ||
} | ||
|
||
protected D build(final String name) { | ||
final D definition = factory.get(); | ||
definition.setName(name); | ||
Optional.ofNullable(errorMessage).ifPresent(definition::setErrorMessage); | ||
return definition; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...com/merkle/oss/magnolia/definition/builder/validator/EmailValidatorDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.EmailValidatorDefinition; | ||
|
||
public class EmailValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<EmailValidatorDefinition, EmailValidatorDefinitionBuilder>{ | ||
|
||
public EmailValidatorDefinitionBuilder() { | ||
super(EmailValidatorDefinition::new); | ||
} | ||
|
||
public EmailValidatorDefinition build() { | ||
return build("emailValidator"); | ||
} | ||
public EmailValidatorDefinition build(final String name) { | ||
return super.build(name); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../merkle/oss/magnolia/definition/builder/validator/NodeNameValidatorDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition; | ||
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition.Mode; | ||
import info.magnolia.ui.field.RegexpValidatorDefinition; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Optional; | ||
|
||
public class NodeNameValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<NodeNameValidatorDefinition, NodeNameValidatorDefinitionBuilder>{ | ||
|
||
@Nullable | ||
private String pattern; | ||
@Nullable | ||
private Mode mode; | ||
|
||
public NodeNameValidatorDefinitionBuilder() { | ||
super(NodeNameValidatorDefinition::new); | ||
} | ||
|
||
public NodeNameValidatorDefinitionBuilder pattern(final String pattern) { | ||
this.pattern = pattern; | ||
return self(); | ||
} | ||
|
||
public NodeNameValidatorDefinitionBuilder mode(final Mode mode) { | ||
this.mode = mode; | ||
return self(); | ||
} | ||
|
||
public NodeNameValidatorDefinition build() { | ||
return build("nodeNameValidator"); | ||
} | ||
public NodeNameValidatorDefinition build(final String name) { | ||
final NodeNameValidatorDefinition definition = super.build(name); | ||
Optional.ofNullable(mode).ifPresent(definition::setMode); | ||
Optional.ofNullable(pattern).ifPresent(definition::setPattern); | ||
return definition; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...om/merkle/oss/magnolia/definition/builder/validator/RegexpValidatorDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.RegexpValidatorDefinition; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Optional; | ||
|
||
public class RegexpValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<RegexpValidatorDefinition, RegexpValidatorDefinitionBuilder>{ | ||
|
||
@Nullable | ||
private String pattern; | ||
|
||
public RegexpValidatorDefinitionBuilder() { | ||
super(RegexpValidatorDefinition::new); | ||
} | ||
|
||
public RegexpValidatorDefinitionBuilder pattern(final String pattern) { | ||
this.pattern = pattern; | ||
return self(); | ||
} | ||
|
||
public RegexpValidatorDefinition build() { | ||
return build("regexpValidator"); | ||
} | ||
public RegexpValidatorDefinition build(final String name) { | ||
final RegexpValidatorDefinition definition = super.build(name); | ||
Optional.ofNullable(pattern).ifPresent(definition::setPattern); | ||
return definition; | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
.../merkle/oss/magnolia/definition/builder/validator/SafeHtmlValidatorDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.SafeHtmlValidatorDefinition; | ||
import info.magnolia.ui.field.SafeHtmlValidatorDefinition.Attribute; | ||
import info.magnolia.ui.field.SafeHtmlValidatorDefinition.Protocol; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class SafeHtmlValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<SafeHtmlValidatorDefinition, SafeHtmlValidatorDefinitionBuilder>{ | ||
|
||
@Nullable | ||
private List<String> allowedTags; | ||
@Nullable | ||
private List<String> globallyAllowedAttributes; | ||
@Nullable | ||
private List<Attribute> allowedAttributes; | ||
@Nullable | ||
private List<Protocol> allowedProtocols; | ||
|
||
public SafeHtmlValidatorDefinitionBuilder() { | ||
super(SafeHtmlValidatorDefinition::new); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedTag(final String allowedTag) { | ||
return item(this::allowedTags, allowedTags, allowedTag); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedTags(final List<String> allowedTags) { | ||
this.allowedTags = allowedTags; | ||
return self(); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder globallyAllowedAttribute(final String globallyAllowedAttribute) { | ||
return item(this::globallyAllowedAttributes, globallyAllowedAttributes, globallyAllowedAttribute); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder globallyAllowedAttributes(final List<String> globallyAllowedAttributes) { | ||
this.globallyAllowedAttributes = globallyAllowedAttributes; | ||
return self(); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedAttribute(final String tag, final String... attributes) { | ||
return allowedAttribute(new AttributeBuilder().attributes(List.of(attributes)).build(tag)); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedAttribute(final Attribute allowedAttribute) { | ||
return item(this::allowedAttributes, allowedAttributes, allowedAttribute); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedAttributes(final List<Attribute> allowedAttributes) { | ||
this.allowedAttributes = allowedAttributes; | ||
return self(); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedProtocol(final String tag, final String attribute, final String... protocols) { | ||
return allowedProtocol(new ProtocolBuilder().protocols(List.of(protocols)).build(tag, attribute)); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedProtocol(final Protocol allowedProtocol) { | ||
return item(this::allowedProtocols, allowedProtocols, allowedProtocol); | ||
} | ||
|
||
public SafeHtmlValidatorDefinitionBuilder allowedProtocols(final List<Protocol> allowedProtocols) { | ||
this.allowedProtocols = allowedProtocols; | ||
return self(); | ||
} | ||
|
||
private <T> SafeHtmlValidatorDefinitionBuilder item(final Consumer<List<T>> consumer, final List<T> items, final T item) { | ||
consumer.accept(Stream.concat( | ||
Stream.ofNullable(items).flatMap(Collection::stream), | ||
Stream.of(item) | ||
).collect(Collectors.toList())); | ||
return self(); | ||
} | ||
|
||
public SafeHtmlValidatorDefinition build() { | ||
return build("safeHtmlValidator"); | ||
} | ||
public SafeHtmlValidatorDefinition build(final String name) { | ||
final SafeHtmlValidatorDefinition definition = super.build(name); | ||
Stream.ofNullable(allowedTags).flatMap(Collection::stream).forEach(definition.getAllowedTags()::add); | ||
Stream.ofNullable(globallyAllowedAttributes).flatMap(Collection::stream).forEach(definition.getGloballyAllowedAttributes()::add); | ||
Stream.ofNullable(allowedAttributes).flatMap(Collection::stream).forEach(definition.getAllowedAttributes()::add); | ||
Stream.ofNullable(allowedProtocols).flatMap(Collection::stream).forEach(definition.getAllowedProtocols()::add); | ||
return definition; | ||
} | ||
|
||
public static class AttributeBuilder { | ||
private List<String> attributes = new ArrayList<>(); | ||
|
||
public AttributeBuilder attribute(final String attribute) { | ||
this.attributes.add(attribute); | ||
return this; | ||
} | ||
|
||
public AttributeBuilder attributes(final List<String> attributes) { | ||
this.attributes = attributes; | ||
return this; | ||
} | ||
|
||
public Attribute build(final String tag) { | ||
final Attribute attribute = new Attribute(); | ||
attribute.setTag(tag); | ||
attribute.setAttributes(attributes.toArray(String[]::new)); | ||
return attribute; | ||
} | ||
} | ||
|
||
public static class ProtocolBuilder { | ||
private List<String> protocols = new ArrayList<>(); | ||
|
||
public ProtocolBuilder protocol(final String protocol) { | ||
this.protocols.add(protocol); | ||
return this; | ||
} | ||
|
||
public ProtocolBuilder protocols(final List<String> protocols) { | ||
this.protocols = protocols; | ||
return this; | ||
} | ||
|
||
public Protocol build(final String tag, final String attribute) { | ||
final Protocol protocol = new Protocol(); | ||
protocol.setTag(tag); | ||
protocol.setAttribute(attribute); | ||
protocol.setProtocols(protocols.toArray(String[]::new)); | ||
return protocol; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...merkle/oss/magnolia/definition/builder/validator/SvgUploadValidatorDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.SvgUploadValidatorDefinition; | ||
|
||
public class SvgUploadValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<SvgUploadValidatorDefinition, SvgUploadValidatorDefinitionBuilder>{ | ||
|
||
public SvgUploadValidatorDefinitionBuilder() { | ||
super(SvgUploadValidatorDefinition::new); | ||
} | ||
|
||
public SvgUploadValidatorDefinition build() { | ||
return build("svgUploadValidator"); | ||
} | ||
public SvgUploadValidatorDefinition build(final String name) { | ||
return super.build(name); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...finition/builder/validator/AbstractConfiguredFieldValidatorDefinitionBuilderTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.ConfiguredFieldValidatorDefinition; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public abstract class AbstractConfiguredFieldValidatorDefinitionBuilderTestCase { | ||
|
||
public <D extends ConfiguredFieldValidatorDefinition, B extends AbstractConfiguredFieldValidatorDefinitionBuilder<D, B>> B assertValidator(final B builder, final BiFunction<String, B, D> buildFunction) { | ||
final D definition = buildFunction.apply( | ||
"someName", | ||
builder | ||
.errorMessage("someErrorMessage") | ||
); | ||
assertEquals("someErrorMessage", definition.getErrorMessage()); | ||
return builder; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...merkle/oss/magnolia/definition/builder/validator/EmailValidatorDefinitionBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.EmailValidatorDefinition; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class EmailValidatorDefinitionBuilderTest extends AbstractConfiguredFieldValidatorDefinitionBuilderTestCase { | ||
@Test | ||
void testBuilder() { | ||
final EmailValidatorDefinition definition = super.assertValidator(new EmailValidatorDefinitionBuilder(), (name, builder) -> builder.build(name)) | ||
.build("somEmailValidator"); | ||
assertEquals("somEmailValidator", definition.getName()); | ||
|
||
final EmailValidatorDefinition emptyDefinition = new EmailValidatorDefinitionBuilder().build(); | ||
assertEquals("emailValidator", emptyDefinition.getName()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...kle/oss/magnolia/definition/builder/validator/NodeNameValidatorDefinitionBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class NodeNameValidatorDefinitionBuilderTest extends AbstractConfiguredFieldValidatorDefinitionBuilderTestCase { | ||
@Test | ||
void testBuilder() { | ||
final NodeNameValidatorDefinition definition = super.assertValidator(new NodeNameValidatorDefinitionBuilder(), (name, builder) -> builder.build(name)) | ||
.mode(NodeNameValidatorDefinition.Mode.ADD) | ||
.pattern("somePattern") | ||
.build("someNodeNameValidator"); | ||
assertEquals("someNodeNameValidator", definition.getName()); | ||
assertEquals("somePattern", definition.getPattern()); | ||
assertEquals(NodeNameValidatorDefinition.Mode.ADD, definition.getMode()); | ||
|
||
final NodeNameValidatorDefinition emptyDefinition = new NodeNameValidatorDefinitionBuilder().build(); | ||
assertEquals("nodeNameValidator", emptyDefinition.getName()); | ||
assertEquals(".+", emptyDefinition.getPattern()); | ||
assertEquals(NodeNameValidatorDefinition.Mode.EDIT, emptyDefinition.getMode()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...erkle/oss/magnolia/definition/builder/validator/RegexpValidatorDefinitionBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.merkle.oss.magnolia.definition.builder.validator; | ||
|
||
import info.magnolia.ui.field.RegexpValidatorDefinition; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
class RegexpValidatorDefinitionBuilderTest extends AbstractConfiguredFieldValidatorDefinitionBuilderTestCase { | ||
@Test | ||
void testBuilder() { | ||
final RegexpValidatorDefinition definition = super.assertValidator(new RegexpValidatorDefinitionBuilder(), (name, builder) -> builder.build(name)) | ||
.pattern("somePattern") | ||
.build("someRegexpValidator"); | ||
assertEquals("someRegexpValidator", definition.getName()); | ||
assertEquals("somePattern", definition.getPattern()); | ||
|
||
final RegexpValidatorDefinition emptyDefinition = new RegexpValidatorDefinitionBuilder().build(); | ||
assertEquals("regexpValidator", emptyDefinition.getName()); | ||
assertNull(emptyDefinition.getPattern()); | ||
} | ||
} |
Oops, something went wrong.