Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы ParseConfigFile.java

Оглавление

Назад

// ==========================================
// ParseConfigFile.java
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ParseConfigFile
{
  public static void main(String args[])
  {
    FrameWindow frame;
    frame =  new FrameWindow(
      "Config file parser");
    frame.setVisible(true);
  }
}

// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
  implements ActionListener, WindowListener
{
  TextArea ta;
  
  MenuBar mb;
  Menu mFile;
  MenuItem miOpen;
  MenuItem miExit;
  
  String szCurrentFilename = "";
  byte buf[];
  
  // ============================================
  // FrameWindow
  // ============================================
  public FrameWindow(String szTitle)
  {
    super(szTitle);
    setSize(400, 300);
    
    mb = new MenuBar();
    mFile = new Menu("File");
    
    miOpen = new MenuItem("Open...");
    mFile.add(miOpen);
    
    mFile.add("-");
    
    miExit = new MenuItem("Exit");
    mFile.add(miExit);
    
    mb.add(mFile);
    
    miOpen.addActionListener(this);
    miExit.addActionListener(this);
    
    setMenuBar(mb);
    
    this.addWindowListener(this);
    
    ta = new TextArea(10, 30);
    setLayout(new BorderLayout());
    add("Center", ta);
    
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(miOpen))
    {
      FileOpen();
    }
    
    else if(e.getSource().equals(miExit))
    {
      setVisible(false);
      System.exit(0);
    }
  }
  
  // ============================================
  // windowClosing
  // ============================================
  public void windowClosing(WindowEvent e)
  {
    setVisible(false);
    System.exit(0);
  }
  
  public void windowOpened(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  
  // ============================================
  // FileOpen
  // ============================================
  void FileOpen()
  {
    FileDialog fdlg;
    FileInputStream is = null;
    
    fdlg = new FileDialog(this, "Open file", 
	FileDialog.LOAD);
    fdlg.show();

    szCurrentFilename = fdlg.getDirectory() +
      fdlg.getFile();
      
    setTitle("Parse: " + szCurrentFilename);
    
    ta.selectAll();
    ta.replaceRange(
      "", 0, ta.getSelectionEnd());
    
    try 
    {
      is = 
        new FileInputStream(szCurrentFilename);
    }
    catch (FileNotFoundException ex)
    {
      System.out.println(ex.toString());
    }
    catch (SecurityException ex)
    {
      System.out.println(ex.toString());
    }
	
    StreamTokenizer strtok = 
      new StreamTokenizer(is);
    
//  Reader r = new BufferedReader(
//    new InputStreamReader(is));
//  StreamTokenizer strtok = 
//    new StreamTokenizer(r);    
    
    strtok.commentChar('#');
    strtok.quoteChar('"');
    
    String szParmName = "";
    String szParmValue = "";
	
    try 
    {
      while(true)
      {
	if(strtok.nextToken() == 
          StreamTokenizer.TT_EOF)
	  break;
	  
	if(strtok.ttype == 
          StreamTokenizer.TT_WORD)
	{
          szParmName = strtok.sval;
	}
	else
	  break;
	
	if(strtok.nextToken() == 
          StreamTokenizer.TT_EOF)
	  break;
	  
	if(strtok.ttype != '=')
	  break;
	  
	if(strtok.nextToken() == 
          StreamTokenizer.TT_EOF)
	  break;
	  
	if(strtok.ttype == 
          StreamTokenizer.TT_WORD)
	{
          szParmValue = strtok.sval;
	}
	
	else if(strtok.ttype == '"')
	{
          szParmValue = strtok.sval;
	}
	
	else if(strtok.ttype == 
          StreamTokenizer.TT_NUMBER)
	{
          szParmValue = 
            Double.toString(strtok.nval);
	}
	else 
	  break;
	
	ta.append(szParmName +
          "=" + szParmValue + "\r\n");
      }  
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
    
    try
    {
      is.close();
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
  }  
}

[Назад]