diff --git a/ldif/ldif-core/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java b/ldif/ldif-core/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java index b43f2336cb..783111a1c8 100644 --- a/ldif/ldif-core/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java +++ b/ldif/ldif-core/src/main/java/org/springframework/ldap/ldif/support/DefaultAttributeValidationPolicy.java @@ -120,6 +120,11 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli private static final String BASE64_STRING = "(" + BASE64_CHAR + "*)"; + //UTF8 Definitions + private static final String UTF8_CHAR = "[\\p{L}|[0-9]|\\s]"; // \p{L} for any kind of letter from any language, including spaces and digits + + private static final String UTF8_STRING = "(" + UTF8_CHAR + "+)"; + //URL Components private static final String USER = "[" + UCHAR + "\\x3B\\x3F\\x26\\x3D]*"; //UCHAR|;|?|&|= @@ -242,13 +247,17 @@ public class DefaultAttributeValidationPolicy implements AttributeValidationPoli private static final String URL_ATTRIBUTE_EXPRESSION = "^" + ATTRIBUTE_DESCRIPTION + ATTRIBUTE_SEPARATOR + URL_INDICATOR + FILL + URL + "$"; //URL + private static final String UTF8_ATTRIBUTE_EXPRESSION = "^" + ATTRIBUTE_DESCRIPTION + ATTRIBUTE_SEPARATOR + FILL + UTF8_STRING; + //Pattern Declarations private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile(ATTRIBUTE_EXPRESSION); private static final Pattern BASE64_ATTRIBUTE_PATTERN = Pattern.compile(BASE64_ATTRIBUTE_EXPRESSION); private static final Pattern URL_ATTRIBUTE_PATTERN = Pattern.compile(URL_ATTRIBUTE_EXPRESSION); - + + private static final Pattern UTF8_ATTRIBUTE_PATTERN = Pattern.compile(UTF8_ATTRIBUTE_EXPRESSION); + private boolean ordered = false; /** @@ -279,11 +288,12 @@ public void setOrdered(boolean ordered) { /** * Validates attribute contained in the buffer and returns an LdapAttribute. *

- * Ensures attributes meets one of three prescribed patterns for valid attributes: + * Ensures attributes meets one of four prescribed patterns for valid attributes: *

    *
  1. A standard attribute pattern of the form: ATTR_ID[;options]: VALUE
  2. *
  3. A Base64 attribute pattern of the form: ATTR_ID[;options]:: BASE64_VALUE
  4. *
  5. A url attribute pattern of the form: ATTR_ID[;options]:< URL_VALUE
  6. + *
  7. A UTF8 attribute pattern of the form: ATTR_ID[;options]: UTF8_VALUE
  8. *
*

* Upon success an LdapAttribute object is returned. @@ -314,6 +324,12 @@ public Attribute parse(String buffer) { return parseUrlAttribute(matcher); } + matcher = UTF8_ATTRIBUTE_PATTERN.matcher(buffer); + if (matcher.matches()) { + //Is a UTF8 attribute... + return parseUtf8Attribute(matcher); + } + //default: no match. throw new InvalidAttributeFormatException("Not a valid attribute: [" + buffer + "]"); } @@ -363,5 +379,16 @@ private LdapAttribute parseUrlAttribute(Matcher matcher) { throw new InvalidAttributeFormatException(e); } } - + + private LdapAttribute parseUtf8Attribute(Matcher matcher) { + String id = matcher.group(1); + String value = matcher.group(3); + List options = Arrays.asList((!StringUtils.hasLength(matcher.group(2)) ? new String[] {} : matcher.group(2).replaceFirst(";","").split(OPTION_SEPARATOR))); + + if (options.isEmpty()) { + return new LdapAttribute(id, value, ordered); + } else { + return new LdapAttribute(id, value, options, ordered); + } + } } diff --git a/ldif/ldif-core/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java b/ldif/ldif-core/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java index 2bbee93e35..5a90729d27 100644 --- a/ldif/ldif-core/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java +++ b/ldif/ldif-core/src/test/java/org/springframework/ldap/ldif/DefaultAttributeValidationPolicyTest.java @@ -50,7 +50,7 @@ public class DefaultAttributeValidationPolicyTest { private static DefaultAttributeValidationPolicy policy = new DefaultAttributeValidationPolicy(); - private static enum AttributeType { STRING, BASE64, URL } + private static enum AttributeType { STRING, BASE64, URL, UTF8 } private String line; private String id; @@ -105,7 +105,10 @@ public static Collection data() { { "url:< prospero://host.dom:1525//pros/name;key=value", "url", "", "prospero://host.dom:1525//pros/name;key=value", AttributeType.URL}, { "url:< nntp://news.cs.hut.fi/alt.html/239157", "url", "", "nntp://news.cs.hut.fi/alt.html/239157", AttributeType.URL}, { "url:< wais://vega.lib.ncsu.edu/alawon.src?nren", "url", "", "wais://vega.lib.ncsu.edu/alawon.src?nren", AttributeType.URL}, - { "url:< http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", "url", "", "http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", AttributeType.URL} + { "url:< http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", "url", "", "http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28", AttributeType.URL}, + + //UTF8 + { "company: Østfold Akershus", "company", "", "Østfold Akershus", AttributeType.STRING } }); } @@ -159,6 +162,11 @@ public void parseAttribute() { assertThat(attribute.get() instanceof URI).as("Value is not a URL.").isTrue(); assertThat(attribute.get()).as("Values do not match: ").isEqualTo(url); break; + + case UTF8: + assertThat(attribute.get() instanceof String).as("Value is not a UTF8.").isTrue(); + assertThat(attribute.get()).as("Values do not match: ").isEqualTo(value); + break; } log.info("Success!");