Use FileChannels and ByteBuffers to Store Patterns : Serialization « Regular Expressions « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Regular Expressions » Serialization 




Use FileChannels and ByteBuffers to Store Patterns

//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 inStreamthrows IOException,
      PatternSyntaxException {
    load(new FileInputStream(inStream));
  }

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

    ByteBuffer bb = ByteBuffer.allocate((intfc.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");
  }
}
           
       














Related examples in the same category
1.Using a Regular Expression to Filter Lines from a Reader
2.Reading Lines from a String Using a Regular Expression
3.Apply Regular Expressions on the contents of a file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.