Android Open Source - android-chess P G N Processor






From Project

Back to project page android-chess.

License

The source code is released under:

MIT License

If you think the Android project android-chess listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jwtc.android.chess.tools;
//from w w  w .j  av  a2s.  com
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.*;

import android.os.Handler;
import android.os.Message;
import android.util.Log;


public abstract class PGNProcessor {

  protected Handler m_threadUpdateHandler;
  protected Thread m_thread = null;
  
  public static final int MSG_PROCESSED_PGN = 1;
  public static final int MSG_FAILED_PGN = 2;
  //public static final int MSG_EXCEPTION = 3;
  public static final int MSG_FINISHED = 4;
  public static final int MSG_PROCESSED_FILE = 5;
  public static final int MSG_FATAL_ERROR = 6;
  
  public void processZipFile(final InputStream is){
    
    m_thread = new Thread(new Runnable(){
          public void run(){
        ZipInputStream zis = new ZipInputStream(is);
        ZipEntry entry;
        
        try {
          while((entry = zis.getNextEntry()) != null){
            if(entry.isDirectory() || (false == entry.getName().endsWith(".pgn"))) {
                  continue;
            } else {
              
              Log.i("hasEntry", entry.getName());
              
              StringBuffer sb = new StringBuffer();
              byte[] buffer = new byte[2048];
              int len;
            
              while((len = zis.read(buffer, 0, buffer.length)) != -1) {
                sb.append(new String(buffer, 0, len));
                
                processPGNPart(sb);
              }
              Message m = new Message();
              if(processPGN(sb.toString())){
                m.what = MSG_PROCESSED_PGN;
              } else {
                m.what = MSG_FAILED_PGN;
              }
              m_threadUpdateHandler.sendMessage(m);
              
              m = new Message();
              m.what = MSG_FINISHED;
              m_threadUpdateHandler.sendMessage(m);
            }
          }
        } catch (IOException e) {
          Message m = new Message();
          m.what = MSG_FATAL_ERROR;
          m_threadUpdateHandler.sendMessage(m);
          
          Log.e("PGNProcessor", e.toString());
        }
          }
    });
    m_thread.start();
  }
  
  public void stopProcessing(){
    if(m_thread != null){
      m_thread.stop();
      m_thread = null;
    }
  }
  
  public void processPGNFile(final InputStream is){
    
    new Thread(new Runnable(){
          public void run(){
          
        try {
          
          StringBuffer sb = new StringBuffer();
          int len;
          byte[] buffer = new byte[2048];
          
          //Log.i("processPGN", "available " + is.available());
          
          while((len = is.read(buffer, 0, buffer.length)) != -1){
            sb.append(new String(buffer, 0, len));
            
            processPGNPart(sb);
            
            //Log.i("processPGN", "loop stringbuffer " + len);
          }
        
          Message m = new Message();
          if(processPGN(sb.toString())){
            m.what = MSG_PROCESSED_PGN;
          } else {
            m.what = MSG_FAILED_PGN;
          }
          m_threadUpdateHandler.sendMessage(m);
          
          m = new Message();
          m.what = MSG_FINISHED;
          m_threadUpdateHandler.sendMessage(m);
          
        } catch(Exception e){
          Message m = new Message();
          m.what = MSG_FATAL_ERROR;
          m_threadUpdateHandler.sendMessage(m);
          
          Log.e("PGNProcessor", e.toString());
        }
          }
    }).start();
  }
  
  public void processPGNPart(StringBuffer sb){
    int pos1 = 0, pos2 = 0;
    String s;
    pos1 = sb.indexOf("[Event \"");
    while(pos1 >= 0){
      pos2 = sb.indexOf("[Event \"", pos1+10);
      if(pos2 == -1)
        break;
      s = sb.substring(pos1, pos2);
      
      Message m = new Message();
      
      if(processPGN(s)){
        m.what = MSG_PROCESSED_PGN;
      } else {
        m.what = MSG_FAILED_PGN;
      }
        
      m_threadUpdateHandler.sendMessage(m);
      
      sb.delete(0, pos2);
      
      pos1 = sb.indexOf("[Event \"");
    }
  }
  
  public abstract boolean processPGN(final String sPGN);
  public abstract String getString();
}




Java Source Code List

jwtc.android.chess.ChessFieldView.java
jwtc.android.chess.ChessImageView.java
jwtc.android.chess.ChessPreferences.java
jwtc.android.chess.ChessViewBase.java
jwtc.android.chess.ChessView.java
jwtc.android.chess.GamesListView.java
jwtc.android.chess.HtmlActivity.java
jwtc.android.chess.ImageCacheObject.java
jwtc.android.chess.MyBaseActivity.java
jwtc.android.chess.MyPGNProvider.java
jwtc.android.chess.PGNView.java
jwtc.android.chess.SaveGameDlg.java
jwtc.android.chess.UI.java
jwtc.android.chess.convergence.Connection.java
jwtc.android.chess.convergence.ConvergenceActivity.java
jwtc.android.chess.convergence.RestServer.java
jwtc.android.chess.iconifiedlist.IconifiedTextListAdapter.java
jwtc.android.chess.iconifiedlist.IconifiedTextView.java
jwtc.android.chess.iconifiedlist.IconifiedText.java
jwtc.android.chess.ics.CustomCommands.java
jwtc.android.chess.ics.ICSChatDlg.java
jwtc.android.chess.ics.ICSChessView.java
jwtc.android.chess.ics.ICSClient.java
jwtc.android.chess.ics.ICSConfirmDlg.java
jwtc.android.chess.ics.ICSMatchDlg.java
jwtc.android.chess.ics.TelnetSocket.java
jwtc.android.chess.ics.TimesealInputStream.java
jwtc.android.chess.ics.TimesealOutputStream.java
jwtc.android.chess.ics.TimesealPipe.java
jwtc.android.chess.ics.TimesealingSocket.java
jwtc.android.chess.puzzle.ChessViewPractice.java
jwtc.android.chess.puzzle.ChessViewPuzzle.java
jwtc.android.chess.puzzle.MyPuzzleProvider.java
jwtc.android.chess.puzzle.practice.java
jwtc.android.chess.puzzle.puzzle.java
jwtc.android.chess.tools.FileListView.java
jwtc.android.chess.tools.PGNProcessor.java
jwtc.android.chess.tools.importactivity.java
jwtc.android.chess.tools.pgntool.java
jwtc.android.chess.main.java
jwtc.android.chess.options.java
jwtc.android.chess.setup.java
jwtc.android.chess.start.java
jwtc.android.timeseal.TimesealingSocket.java
jwtc.android.timeseal.streams.a.java
jwtc.android.timeseal.streams.b.java
jwtc.android.timeseal.streams.c.java
jwtc.chess.ChessPuzzleProvider.java
jwtc.chess.GameControl.java
jwtc.chess.JNI.java
jwtc.chess.Move.java
jwtc.chess.PGNColumns.java
jwtc.chess.PGNEntry.java
jwtc.chess.PGNProvider.java
jwtc.chess.Pos.java
jwtc.chess.Valuation.java
jwtc.chess.algorithm.SearchAlgorithmRunner.java
jwtc.chess.algorithm.UCIWrapper.java
jwtc.chess.board.BoardConstants.java
jwtc.chess.board.BoardHashKeys.java
jwtc.chess.board.BoardMembers.java
jwtc.chess.board.BoardStatics.java
jwtc.chess.board.ChessBoard.java