From 787a62c89cd05bdd68c391adb8b5c62f04257a4a Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 30 Nov 2012 00:34:59 +0000
Subject: [PATCH] Fix OPENDJ-656: Add support for generating LDAP filters and DNs from printf style templates

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AVA.java |   58 ++++++++++++++++++++++++++++++----------------------------
 1 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AVA.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AVA.java
index e46452e..54d8a9e 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AVA.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AVA.java
@@ -148,6 +148,35 @@
         return new AVA(attribute, value);
     }
 
+    static void escapeAttributeValue(final String str, final StringBuilder builder) {
+        if (str.length() > 0) {
+            char c = str.charAt(0);
+            int startPos = 0;
+            if ((c == ' ') || (c == '#')) {
+                builder.append('\\');
+                builder.append(c);
+                startPos = 1;
+            }
+            final int length = str.length();
+            for (int si = startPos; si < length; si++) {
+                c = str.charAt(si);
+                if (c < ' ') {
+                    for (final byte b : getBytes(String.valueOf(c))) {
+                        builder.append('\\');
+                        builder.append(StaticUtils.byteToLowerHex(b));
+                    }
+                } else {
+                    if ((c == ' ' && si == length - 1)
+                            || (c == '"' || c == '+' || c == ',' || c == ';' || c == '<'
+                            || c == '=' || c == '>' || c == '\\' || c == '\u0000')) {
+                        builder.append('\\');
+                    }
+                    builder.append(c);
+                }
+            }
+        }
+    }
+
     private static void appendHexChars(final SubstringReader reader,
             final StringBuilder valueBuffer, final StringBuilder hexBuffer) throws DecodeException {
         final int length = hexBuffer.length();
@@ -720,34 +749,7 @@
                 builder.append("#");
                 StaticUtils.toHex(attributeValue, builder);
             } else {
-                final String str = attributeValue.toString();
-                if (str.length() == 0) {
-                    return builder;
-                }
-                char c = str.charAt(0);
-                int startPos = 0;
-                if ((c == ' ') || (c == '#')) {
-                    builder.append('\\');
-                    builder.append(c);
-                    startPos = 1;
-                }
-                final int length = str.length();
-                for (int si = startPos; si < length; si++) {
-                    c = str.charAt(si);
-                    if (c < ' ') {
-                        for (final byte b : getBytes(String.valueOf(c))) {
-                            builder.append('\\');
-                            builder.append(StaticUtils.byteToLowerHex(b));
-                        }
-                    } else {
-                        if ((c == ' ' && si == length - 1)
-                                || (c == '"' || c == '+' || c == ',' || c == ';' || c == '<'
-                                        || c == '=' || c == '>' || c == '\\' || c == '\u0000')) {
-                            builder.append('\\');
-                        }
-                        builder.append(c);
-                    }
-                }
+                escapeAttributeValue(attributeValue.toString(), builder);
             }
         }
         return builder;

--
Gitblit v1.10.0