Package asia.redact.bracket.properties

Source Code of asia.redact.bracket.properties.PropertiesParser2

/*
*  This file is part of Bracket Properties
*  Copyright 2011 David R. Smith
*
*/
package asia.redact.bracket.properties;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import asia.redact.bracket.properties.line.Line;
import asia.redact.bracket.properties.line.LineScanner;
/**
* <pre>
* Use the line scanner to load a Properties implementation.
*
* This class does not use PropertiesToken, it is a "streaming" parser
* which reads the tokens from the line and parses as it reads.
* It is intended for low memory situations or for very large properties files,
* where it ought to perform a bit better.
*
* </pre>
*
* @author Dave
*
*/
public class PropertiesParser2 {

  protected final LineScanner scanner;
  protected final Properties props;
  private final Lock lock = new ReentrantLock();
 
  public PropertiesParser2(LineScanner scanner) {
    this.scanner=scanner;
    this.props = new PropertiesImpl();
  }
 
  public PropertiesParser2(LineScanner scanner, Properties props) {
    this.scanner=scanner;
    this.props = props;
  }
 
  public void parse(){
   
    try {
    lock.lock();
      Line line = null;
      String key = null;
      BasicValueModel model = new BasicValueModel();
      boolean hasContinuation=false;
      while((line =scanner.line())!=null){
        if(hasContinuation){
          model.addValue(line.logicalLineContents());
          if(line.hasContinuation()){
            continue;
          }else{
            hasContinuation=false;
          }
        }
        if(line.isEmptyLine())continue;
        if(line.isPrivateComment())continue;
        if(line.isCommentLine()){
          if(key !=null){
            props.getPropertyMap().put(key, model);
            key=null;
            model=new BasicValueModel();
          }
          model.addComment(line.commentContents());
          continue;
        }else if(line.isNaturalLine()){
          if(key !=null){
            props.getPropertyMap().put(key, model);
            key=null;
            model=new BasicValueModel();
          }
          String [] parts = line.naturalLineContents();
          key = parts[0];
          model.setSeparator(parts[1].charAt(0));
          hasContinuation=line.hasContinuation();
          model.addValue(parts[2]);
         
        }
      }
      // last one
      if(key !=null){
        props.getPropertyMap().put(key, model);
      }
   
    }finally {
      lock.unlock();
    }
  }
 
  public Properties getProperties() {
    return props;
  }

}
TOP

Related Classes of asia.redact.bracket.properties.PropertiesParser2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.