From a632fbfad0ddbe8b343c6abe8e28dc41e3df9b7e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 10 Jul 2015 10:16:25 +0000
Subject: [PATCH] Code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/schema/DITContentRuleSyntax.java |  158 +++++++++++-----------------------------------------
 1 files changed, 35 insertions(+), 123 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/schema/DITContentRuleSyntax.java b/opendj-server-legacy/src/main/java/org/opends/server/schema/DITContentRuleSyntax.java
index f342d9c..ae99a04 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/schema/DITContentRuleSyntax.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/schema/DITContentRuleSyntax.java
@@ -36,6 +36,7 @@
 import java.util.List;
 
 import org.forgerock.i18n.LocalizableMessage;
+import org.forgerock.i18n.LocalizableMessageDescriptor.Arg2;
 import org.forgerock.opendj.ldap.ByteSequence;
 import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldap.schema.ObjectClassType;
@@ -504,28 +505,10 @@
           {
             StringBuilder woidBuffer = new StringBuilder();
             pos = readWOID(lowerStr, woidBuffer, (pos));
+            attrs.add(getAttribute(schema, allowUnknownElements, valueStr, woidBuffer,
+                ERR_ATTR_SYNTAX_DCR_UNKNOWN_REQUIRED_ATTR));
 
-            AttributeType attr = schema.getAttributeType(woidBuffer.toString());
-            if (attr == null)
-            {
-              // This isn't good because it means that the DIT content rule
-              // requires an attribute type that we don't know anything about.
-              if (allowUnknownElements)
-              {
-                attr = DirectoryServer.getDefaultAttributeType(woidBuffer.toString());
-              }
-              else
-              {
-                throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
-                    ERR_ATTR_SYNTAX_DCR_UNKNOWN_REQUIRED_ATTR.get(valueStr, woidBuffer));
-              }
-            }
-
-            attrs.add(attr);
-
-
-            // The next character must be either a dollar sign or a closing
-            // parenthesis.
+            // The next character must be either a dollar sign or a closing parenthesis.
             c = valueStr.charAt(pos++);
             if (c == ')')
             {
@@ -544,25 +527,8 @@
         {
           StringBuilder woidBuffer = new StringBuilder();
           pos = readWOID(lowerStr, woidBuffer, (pos-1));
-
-          AttributeType attr = schema.getAttributeType(woidBuffer.toString());
-          if (attr == null)
-          {
-            // This isn't good because it means that the DIT content rule
-            // requires an attribute type that we don't know anything about.
-            if (allowUnknownElements)
-            {
-              attr = DirectoryServer.getDefaultAttributeType(
-                                          woidBuffer.toString());
-            }
-            else
-            {
-              throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
-                  ERR_ATTR_SYNTAX_DCR_UNKNOWN_REQUIRED_ATTR.get(valueStr, woidBuffer));
-            }
-          }
-
-          attrs.add(attr);
+          attrs.add(getAttribute(schema, allowUnknownElements, valueStr, woidBuffer,
+              ERR_ATTR_SYNTAX_DCR_UNKNOWN_REQUIRED_ATTR));
         }
 
         requiredAttributes.addAll(attrs);
@@ -582,29 +548,10 @@
           {
             StringBuilder woidBuffer = new StringBuilder();
             pos = readWOID(lowerStr, woidBuffer, (pos));
+            attrs.add(getAttribute(schema, allowUnknownElements, valueStr, woidBuffer,
+                ERR_ATTR_SYNTAX_DCR_UNKNOWN_OPTIONAL_ATTR));
 
-            AttributeType attr = schema.getAttributeType(woidBuffer.toString());
-            if (attr == null)
-            {
-              // This isn't good because it means that the DIT content rule
-              // allows an attribute type that we don't know anything about.
-              if (allowUnknownElements)
-              {
-                attr = DirectoryServer.getDefaultAttributeType(
-                                            woidBuffer.toString());
-              }
-              else
-              {
-                throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
-                    ERR_ATTR_SYNTAX_DCR_UNKNOWN_OPTIONAL_ATTR.get(valueStr, woidBuffer));
-              }
-            }
-
-            attrs.add(attr);
-
-
-            // The next character must be either a dollar sign or a closing
-            // parenthesis.
+            // The next character must be either a dollar sign or a closing parenthesis.
             c = valueStr.charAt(pos++);
             if (c == ')')
             {
@@ -623,25 +570,8 @@
         {
           StringBuilder woidBuffer = new StringBuilder();
           pos = readWOID(lowerStr, woidBuffer, (pos-1));
-
-          AttributeType attr = schema.getAttributeType(woidBuffer.toString());
-          if (attr == null)
-          {
-            // This isn't good because it means that the DIT content rule allows
-            // an attribute type that we don't know anything about.
-            if (allowUnknownElements)
-            {
-              attr = DirectoryServer.getDefaultAttributeType(
-                                          woidBuffer.toString());
-            }
-            else
-            {
-              throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
-                  ERR_ATTR_SYNTAX_DCR_UNKNOWN_OPTIONAL_ATTR.get(valueStr, woidBuffer));
-            }
-          }
-
-          attrs.add(attr);
+          attrs.add(getAttribute(schema, allowUnknownElements, valueStr, woidBuffer,
+              ERR_ATTR_SYNTAX_DCR_UNKNOWN_OPTIONAL_ATTR));
         }
 
         optionalAttributes.addAll(attrs);
@@ -661,30 +591,10 @@
           {
             StringBuilder woidBuffer = new StringBuilder();
             pos = readWOID(lowerStr, woidBuffer, (pos));
+            attrs.add(getAttribute(schema, allowUnknownElements, valueStr, woidBuffer,
+                ERR_ATTR_SYNTAX_DCR_UNKNOWN_PROHIBITED_ATTR));
 
-            AttributeType attr = schema.getAttributeType(woidBuffer.toString());
-            if (attr == null)
-            {
-              // This isn't good because it means that the DIT content rule
-              // prohibits an attribute type that we don't know anything about.
-              if (allowUnknownElements)
-              {
-                attr = DirectoryServer.getDefaultAttributeType(
-                                            woidBuffer.toString());
-              }
-              else
-              {
-                throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
-                    ERR_ATTR_SYNTAX_DCR_UNKNOWN_PROHIBITED_ATTR.get(
-                        valueStr, woidBuffer));
-              }
-            }
-
-            attrs.add(attr);
-
-
-            // The next character must be either a dollar sign or a closing
-            // parenthesis.
+            // The next character must be either a dollar sign or a closing parenthesis.
             c = valueStr.charAt(pos++);
             if (c == ')')
             {
@@ -703,25 +613,8 @@
         {
           StringBuilder woidBuffer = new StringBuilder();
           pos = readWOID(lowerStr, woidBuffer, (pos-1));
-
-          AttributeType attr = schema.getAttributeType(woidBuffer.toString());
-          if (attr == null)
-          {
-            // This isn't good because it means that the DIT content rule
-            // prohibits an attribute type that we don't know anything about.
-            if (allowUnknownElements)
-            {
-              attr = DirectoryServer.getDefaultAttributeType(
-                                          woidBuffer.toString());
-            }
-            else
-            {
-              throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
-                  ERR_ATTR_SYNTAX_DCR_UNKNOWN_PROHIBITED_ATTR.get(valueStr, woidBuffer));
-            }
-          }
-
-          attrs.add(attr);
+          attrs.add(getAttribute(schema, allowUnknownElements, valueStr, woidBuffer,
+              ERR_ATTR_SYNTAX_DCR_UNKNOWN_PROHIBITED_ATTR));
         }
 
         prohibitedAttributes.addAll(attrs);
@@ -770,6 +663,25 @@
                               isObsolete, extraProperties);
   }
 
+  private static AttributeType getAttribute(Schema schema, boolean allowUnknownElements, String valueStr,
+      StringBuilder woidBuffer, Arg2<Object, Object> msg) throws DirectoryException
+  {
+    String woidString = woidBuffer.toString();
+    AttributeType attr = schema.getAttributeType(woidString);
+    if (attr == null)
+    {
+      // This isn't good because it means that the DIT content rule
+      // refers to an attribute type that we don't know anything about.
+      if (!allowUnknownElements)
+      {
+        throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
+            msg.get(valueStr, woidString));
+      }
+      attr = DirectoryServer.getDefaultAttributeType(woidString);
+    }
+    return attr;
+  }
+
   /**
    * Reads the next token name from the DIT content rule definition, skipping
    * over any leading or trailing spaces, and appends it to the provided buffer.

--
Gitblit v1.10.0