Package clips.directory.editors.mkb10

Source Code of clips.directory.editors.mkb10.TxtParser

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package clips.directory.editors.mkb10;

import beans.directory.mkb10.MkbItem;
import cli_fmw.main.ClipsException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
*
* @author finder
*/
public class TxtParser {
  static final char      MKB_CODE_SEPARATOR = 9;
 
 
  BufferedReader        input;
  public TxtParser(File sourse) throws FileNotFoundException, ClipsException{
        try {
            FileInputStream inputStream = new FileInputStream(sourse);
            input = new BufferedReader(new InputStreamReader(inputStream, "windows-1251"));
        } catch (UnsupportedEncodingException ex) {
            throw new ClipsException("Требуется кодировка windows-1251", ex);
        }
  }
 
  public ArrayList<MkbItem> read() throws IOException, ClipsException{
    ArrayList<MkbItem>        target = new ArrayList<MkbItem>(8192);
    String              line = input.readLine();
    if (line == null) {
      throw new ClipsException("Некорректный фомат файла!");
    }
   
    line = input.readLine();
    while (line != null){
            if (!line.trim().isEmpty()) {
                target.add(parseLine(line.trim()));
            }
      line = input.readLine();
    }
    return target;
  }
 
  private void throwFormatError(String line) throws ClipsException{
    throw new ClipsException("Некорректный фомат файла! \"" + line + "\"");
  }
 
  private MkbItem parseLine(String line) throws ClipsException{
        int mkbPos = line.indexOf(MKB_CODE_SEPARATOR);
        if (mkbPos < 4) {
            throwFormatError("mkbpos = " + mkbPos + " ---> " + line);
        }
        if (mkbPos > 8) {
            throwFormatError("mkbpos = " + mkbPos + " ---> " + line);
        }
        if (line.charAt(mkbPos-1) != '.') {
            throwFormatError("mkbCode!  " + " ---> " + line);
        }
        String mkbCode = line.substring(0, mkbPos-1).trim();
        if (!mkbCode.matches("[A-Z_0-9_\\._\\-\\*]+")) {
            throwFormatError("mkbCode!  " + " ---> " + line);
        }
        String mkbStr = line.substring(mkbPos + 1, line.length()).trim();
        //System.out.println(mkbCode + (char)9 + mkbStr);
        return new MkbItem(mkbStr, mkbCode, null, null);
  }
}
TOP

Related Classes of clips.directory.editors.mkb10.TxtParser

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.