SurfaceFileTyper.java :  » Science » jmol-12.0 » org » jmol » util » Java Open Source

Java Open Source » Science » jmol 12.0 
jmol 12.0 » org » jmol » util » SurfaceFileTyper.java
/* $RCSfile$
 * $Author: hansonr $
 * $Date: 2007-04-05 09:07:28 -0500 (Thu, 05 Apr 2007) $
 * $Revision: 7326 $
 *
 * Copyright (C) 2003-2005  The Jmol Development Team
 *
 * Contact: jmol-developers@lists.sf.net
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.jmol.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class SurfaceFileTyper {

  /* moved to util because drag-drop can then determine an isosurface file 
   * type and automatically load the file 
   */
  
  public final static String PMESH_BINARY_MAGIC_NUMBER = "PM" + '\1' + '\0';
  
  public static String determineSurfaceFileType(InputStream is) {
    BufferedReader br = new BufferedReader(new InputStreamReader(
        new BufferedInputStream((InputStream) is, 8192)));
    return SurfaceFileTyper.determineSurfaceFileType(br);
  }
  
  public static String determineSurfaceFileType(BufferedReader bufferedReader) {
    // JVXL should be on the FIRST line of the file, but it may be 
    // after comments or missing.
    
    // Apbs, Jvxl, or Cube, also efvet
    
    String line;
    LimitedLineReader br = new LimitedLineReader(bufferedReader, 16000);
    //sure bets, but not REQUIRED:
    if ((line = br.info()).length() == 0)
      return null;
    if (line.indexOf("Here is your gzipped map") >= 0)
      return "UPPSALA" + line;
    if (line.indexOf("<jvxl") >= 0 && line.indexOf("<?xml") >= 0)
      return "JvxlXML";
    if (line.indexOf("#JVXL+") >= 0)
      return "Jvxl+";
    if (line.indexOf("#JVXL") >= 0)
      return "Jvxl";
    if (line.indexOf("&plot") == 0)
      return "Jaguar";
    if (line.indexOf("MAP ") == 208)
      return "MRC" + (line.charAt(67) == '\0' ? "L" : "B");
    if (line.indexOf("<efvet ") >= 0)
      return "Efvet";
    if (line.indexOf(PMESH_BINARY_MAGIC_NUMBER) == 0)
      return "Pmesh";
    if ("\n\r".indexOf(line.charAt(0)) >= 0 && line.indexOf("ZYX") >= 0)
      return "Xplor";
    if (line.length() > 37 && line.charAt(36) == 0 && line.charAt(37) == 100)
      return "DSN6B";
    if (line.length() > 37 && line.charAt(37) == 100 && line.charAt(36) == 0)
      return "DSN6L";
    
    // Apbs, Jvxl, or Cube, maybe formatted Plt

    line = br.readNonCommentLine();
    if (line.indexOf("object 1 class gridpositions counts") == 0)
      return "Apbs";

    String[] tokens = Parser.getTokens(line); 
    line = br.readNonCommentLine();// second line
    if (tokens.length == 2 
        && Parser.parseInt(tokens[0]) == 3 
        && Parser.parseInt(tokens[1])!= Integer.MIN_VALUE) {
      tokens = Parser.getTokens(line);
      if (tokens.length == 3 
          && Parser.parseInt(tokens[0])!= Integer.MIN_VALUE 
          && Parser.parseInt(tokens[1])!= Integer.MIN_VALUE
          && Parser.parseInt(tokens[2])!= Integer.MIN_VALUE)
        return "PltFormatted";
    }
    line = br.readNonCommentLine(); // third line
    //next line should be the atom line
    int nAtoms = Parser.parseInt(line);
    if (nAtoms == Integer.MIN_VALUE)
      return (line.indexOf("+") == 0 ? "Jvxl+" : null);
    if (nAtoms >= 0)
      return "Cube"; //Can't be a Jvxl file
    nAtoms = -nAtoms;
    for (int i = 4 + nAtoms; --i >=0;)
      if ((line = br.readNonCommentLine()) == null)
        return null;
    int nSurfaces = Parser.parseInt(line);
    if (nSurfaces == Integer.MIN_VALUE)
      return null;
    return (nSurfaces < 0 ?  "Jvxl" : "Cube"); //Final test looks at surface definition line
  }
  
}

class LimitedLineReader {
  //from Resolver
  private char[] buf;
  private int cchBuf;
  private int ichCurrent;

  LimitedLineReader(BufferedReader bufferedReader, int readLimit) {
    buf = new char[readLimit];
    try {
      bufferedReader.mark(readLimit);
      cchBuf = bufferedReader.read(buf);
      ichCurrent = 0;
      bufferedReader.reset();
    } catch (Exception e) {      
    }
  }

  protected String info() {
    return new String(buf);  
  }
  
  protected String readNonCommentLine() {
    while (ichCurrent < cchBuf) {
      int ichBeginningOfLine = ichCurrent;
      char ch = 0;
      while (ichCurrent < cchBuf &&
             (ch = buf[ichCurrent++]) != '\r' && ch != '\n') {
      }
      int cchLine = ichCurrent - ichBeginningOfLine;
      if (ch == '\r' && ichCurrent < cchBuf && buf[ichCurrent] == '\n')
        ++ichCurrent;
      if (buf[ichBeginningOfLine] == '#') // flush comment lines;
        continue;
      StringBuffer sb = new StringBuffer(cchLine);
      sb.append(buf, ichBeginningOfLine, cchLine);
      return sb.toString();
    }
    return "";
  }
}

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.