package org.pac4j.core.authorization.generator;

import java.util.Arrays;
import java.util.StringTokenizer;

import org.pac4j.core.profile.CommonProfile;

/**
 * <p>Generate the authorization information by inspecting attributes.</p>
 * <p>The attributes containing the roles separated by the {@link #splitChar} property (can be set through {@link #setSplitChar(String)}) are
 * defined in the constructor. It's the same for the attributes containing the permissions.</p>
 * 
 * @author Jerome Leleu
 * @since 1.5.0
 */
public class FromAttributesAuthorizationGenerator<U extends CommonProfile> implements AuthorizationGenerator<U> {
    
    private final String[] roleAttributes;
    
    private final String[] permissionAttributes;
    
    private String splitChar = ",";
    
    public FromAttributesAuthorizationGenerator(final String[] roleAttributes, final String[] permissionAttributes) {
        if (roleAttributes != null) {
            this.roleAttributes = Arrays.copyOf(roleAttributes, roleAttributes.length);
        } else {
            this.roleAttributes = null;
        }
        if (permissionAttributes != null) {
            this.permissionAttributes = Arrays.copyOf(permissionAttributes, permissionAttributes.length);
        } else {
            this.permissionAttributes = null;
        }
    }
    
    public void generate(final U profile) {
        generateAuth(profile, this.roleAttributes, true);
        generateAuth(profile, this.permissionAttributes, false);
    }
    
    private void generateAuth(final U profile, final String[] attributes, final boolean isRole) {
        if (attributes != null) {
            for (final String attribute : attributes) {
                final Object value = profile.getAttribute(attribute);
                if (value != null && value instanceof String) {
                    final StringTokenizer st = new StringTokenizer((String) value, this.splitChar);
                    while (st.hasMoreTokens()) {
                        if (isRole) {
                            profile.addRole(st.nextToken());
                        } else {
                            profile.addPermission(st.nextToken());
                        }
                    }
                }
            }
        }
    }
    
    public String getSplitChar() {
        return this.splitChar;
    }
    
    public void setSplitChar(final String splitChar) {
        this.splitChar = splitChar;
    }
}

@@ -901,20 +901,21 @@
         
-if (
+this.
 roleAttr
@@ -917,34 +917,52 @@
 eAttributes 
-!
 = 
-null) %7B
+copyArray(roleAttributes);
 %0A           
@@ -954,37 +954,39 @@
 s);%0A        
-    
 this.
-role
+permission
 Attributes =
@@ -990,42 +990,28 @@
 s = 
+copy
 Array
-s.copyOf(roleAttributes, role
+(permission
 Attr
@@ -1020,225 +1020,159 @@
 utes
-.length
 );%0A    
- 
-   %7D else %7B%0A            this.roleAttributes = null;%0A        %7D%0A        if (permissionAttributes != null) %7B%0A            this.permissionAttributes = Arrays.copyOf(permissionAttributes, permissionAttributes
+%7D%0A%0A    private String%5B%5D copyArray(final String%5B%5D original) %7B%0A        if (original != null) %7B%0A            return Arrays.copyOf(original, original
 .len
@@ -1210,35 +1210,14 @@
     
-this.permissionAttributes =
+return
  nul
@@ -1227,36 +1227,32 @@
         %7D%0A    %7D%0A
-    
 %0A    public void

refactor array copy