RegexProperties.java Source code

Java tutorial

Introduction

Here is the source code for RegexProperties.java

Source

//Example File
/*       
#Email validator that adheres directly to the specification
#for email address naming. It allows for everything from
#ipaddress and country-code domains to very rare characters
#in the username.
email=^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-
9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-
9]{1,3})(\]?)$
    
#Matches UK postcodes according to the following rules 1. LN NLL
#eg N1 1AA 2. LLN NLL eg SW4 0QL 3. LNN NLL eg M23 4PJ 4. LLNN NLL
#eg WS14 0JT 5. LLNL NLL eg SW1N 4TB 6. LNL NLL eg W1C 8LQ Thanks
#to Simon Bell for informin ...
zip=^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$
    
#This regular expression matches dates of the form XX/XX/YYYY
#where XX can be 1 or 2 digits long and YYYY is always 4
#digits long.
dates=^\d{1,2}\/\d{1,2}\/\d{4}$
    
*/

import java.util.Properties;
import java.util.regex.*;
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.logging.Logger;

public class RegexProperties extends Properties {
    private static Logger log = Logger.getAnonymousLogger();

    public void load(String inStream) throws IOException, PatternSyntaxException {
        load(new FileInputStream(inStream));
    }

    public void load(FileInputStream inStream) throws IOException, PatternSyntaxException {
        FileChannel fc = inStream.getChannel();

        ByteBuffer bb = ByteBuffer.allocate((int) fc.size());
        fc.read(bb);
        bb.flip();
        String fileContent = new String(bb.array());

        Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(fileContent);

        while (matcher.find()) {
            String line = matcher.group(1);
            if (line != null && !"".equals(line.trim()) && !line.startsWith("#") && !line.startsWith("!")) {
                String keyValue[] = null;
                if (line.indexOf("=") > 0)
                    keyValue = line.split("=", 2);
                else
                    keyValue = line.split(":", 2);

                if (keyValue != null) {
                    super.put(keyValue[0].trim(), keyValue[1]);
                }
            }
        }
        fc = null;
        bb = null;
    }

    public void store(FileOutputStream out, String header) throws UnsupportedOperationException {
        throw new UnsupportedOperationException("unsupported for this class");
    }

    public void putAll(Map t) {
        throw new UnsupportedOperationException("unsupported for this class");
    }
}