Saturday, May 02, 2009

Using Regular Expression to Parse the authz File

The Original authz file

### This file is an example authorization file for svnserve.
### Its format is identical to that of mod_authz_svn authorization
### files.
### As shown below each section defines authorizations for the path and
### (optional) repository specified by the section name.
### The authorizations follow. An authorization line can refer to:
###  - a single user,
###  - a group of users defined in a special [groups] section,
###  - an alias defined in a special [aliases] section,
###  - all authenticated users, using the '$authenticated' token,
###  - only anonymous users, using the '$anonymous' token,
###  - anyone, using the '*' wildcard.
###
### A match can be inverted by prefixing the rule with '~'. Rules can
### grant read ('r') access, read-write ('rw') access, or no access
### ('').

[aliases]
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average

[groups]
# harry_and_sally = harry,sally
# harry_sally_and_joe = harry,sally,&joe

# [/foo/bar]
# harry = rw
# &joe = r
# * =

# [repository:/baz/fuz]
# @harry_and_sally = rw
# * = r

[groups]
Sales = wlin,jshi,zbai
Quality = wqiu,she,wzhang,juguo,lchen,chunmchen

[/]
* = rw
# This account will be used for the svn web client
svnwebclient = r

[/Sales]
@Sales = rw
* = r

[/Quality]
@Quality = rw
* = r

# This is the Sales' private folder
[/Sales/Private]
@Sales = rw
* =

# This is the Quality's private folder
[/Quality/Private]
@Quality = rw
* =


Using Regular Expression to Parse the authz File


    public Authz() {
        try {
            InputStream stream = getClass().getResourceAsStream("authz");
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

            StringBuilder authz = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                authz.append(line);
                authz.append(Constant.ENTER_SIGN);
            }

            // begins with [ and ends with ]
            String regex = "^\\[([^\\[]*)\\]$";
            String input = authz.toString();

            Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
            Matcher matcher = pattern.matcher(input);

            int location = 0;
            boolean found = false;
            // add global comments of the authz file
            if (matcher.find()) {
                System.out.println(authz.substring(location, matcher.start()));
                location = matcher.start();
                found = true;
            }
            // add each segment
            String segment = null;
            while (matcher.find()) {
                segment = authz.substring(location, matcher.start());
                location = matcher.start();
                System.out.print(segment);
                System.out.println("segment:" + matcher.group(1));               
            }
            // then last segment
            if (found) {
                segment = authz.substring(location);
                System.out.print(segment);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



Below is the output:

### This file is an example authorization file for svnserve.
### Its format is identical to that of mod_authz_svn authorization
### files.
### As shown below each section defines authorizations for the path and
### (optional) repository specified by the section name.
### The authorizations follow. An authorization line can refer to:
###  - a single user,
###  - a group of users defined in a special [groups] section,
###  - an alias defined in a special [aliases] section,
###  - all authenticated users, using the '$authenticated' token,
###  - only anonymous users, using the '$anonymous' token,
###  - anyone, using the '*' wildcard.
###
### A match can be inverted by prefixing the rule with '~'. Rules can
### grant read ('r') access, read-write ('rw') access, or no access
### ('').


[aliases]
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average

segment:groups
[groups]
# harry_and_sally = harry,sally
# harry_sally_and_joe = harry,sally,&joe

# [/foo/bar]
# harry = rw
# &joe = r
# * =

# [repository:/baz/fuz]
# @harry_and_sally = rw
# * = r

segment:groups
[groups]
Sales = wlin,jshi,zbai
Quality = wqiu,she,wzhang,juguo,lchen,chunmchen

segment:/
[/]
* = rw
# This account will be used for the svn web client
svnwebclient = r

segment:/Sales
[/Sales]
@Sales = rw
* = r

segment:/Quality
[/Quality]
@Quality = rw
* = r

# This is the Sales' private folder
segment:/Sales/Private
[/Sales/Private]
@Sales = rw
* =

# This is the Quality's private folder
segment:/Quality/Private
[/Quality/Private]
@Quality = rw
* =

No comments: