import java.io.*;
import java.util.StringTokenizer;
import java.util.NoSuchElementException ;


public class vrml2txt {
File keyfile,textfile,vrmlfile;
String[] vert=new String[256];
String line,tmp;
int[] character=new int[257];
Integer[] vertex=new Integer[3];
int vertexnum,i,corrupt;

             public static void main(String[] args)
			{

				if (args.length<3){
				System.out.println("Usage: txt2vrml <keyfilename> <wrlfilename> <textfilename>");
				System.exit(1);}
			new vrml2txt(args[0],args[1],args[2]);
			}

vrml2txt(String keyfilename,String vrmlfilename,String textfilename)
	{
		read_keyfile(keyfilename);

			try {
                FileInputStream vrmlfile = new FileInputStream(vrmlfilename);
				DataInputStream vrmldis = new DataInputStream(vrmlfile);
                FileOutputStream textfile = new FileOutputStream(textfilename);
				DataOutputStream textdos = new DataOutputStream(textfile);
/******************************************seek to vertices**************************************************************/
      while ((line = vrmldis.readLine()).indexOf("point [") != -1){
		}
/*****************************************vertices->used characters in the text*****************************************/
  System.out.println("\nfinding characters of the text");
      while ((line = vrmldis.readLine()).indexOf("]")==-1)
	    {
        System.out.print(".");
      		    for ( i=0;i<256 ;i++ )
		    {
		        if ( line.indexOf(vert[i])!=-1 )
		        {
			     character[vertexnum]=i;
			     vertexnum++;
			     break;
			}
		    }
	    }
/****************************************skip to facets******************************************************************/
      while ((line = vrmldis.readLine()).indexOf("coordIndex [")==-1)
	    { }
/****************************************order of faces->order of chars->text*******************************************/

/****************************************firt 3 letters******************************************************************/
  System.out.println("");
 System.out.println("assembling the file");
line = vrmldis.readLine();
StringTokenizer st=new StringTokenizer(line,", \t\n\r");
try{
vertex[0]=Integer.valueOf(st.nextToken());
vertex[1]=Integer.valueOf(st.nextToken());
vertex[2]=Integer.valueOf(st.nextToken());
}
catch (NoSuchElementException e){
System.err.println("An error occured while parsing your wrl-file: it is either corrupt or was not created by txt2vrml using your key! \n");
System.err.println("Stringtokenizer: Sorry\n"+e);}

textdos.write(character[vertex[0].intValue()]);
textdos.write(character[vertex[1].intValue()]);
/*****************************************the rest: always vertex[2]*******************************************************/
      while ((line = vrmldis.readLine()).indexOf("]")==-1)
	    {
        System.out.print(".");
	textdos.write(character[vertex[2].intValue()]);
        st=new StringTokenizer(line,", \t\n\r");
        try{
            vertex[0]=Integer.valueOf(st.nextToken());
            vertex[1]=Integer.valueOf(st.nextToken());
            vertex[2]=Integer.valueOf(st.nextToken());
            }
            catch (NoSuchElementException e){
System.err.println("An error occured while parsing your wrl-file: it is either corrupt or was not created by txt2vrml using your key! \n");
System.err.println("Stringtokenizer: "+e);}
	    }
		textdos.writeBytes("\n");
				vrmldis.close();
				textdos.close();

                 }catch (Exception e) {
System.err.println("An error occured in the decodeing process, either your keyfile is corrupt or the wrl-file was not created by txt2vrml using your key! \n");                     
System.err.println("vrml2txt: Sorry\n" + e);
		    }
        System.out.println("\n Done.");

 }

/**************************************set up points from keyfile************************************************************/
		void read_keyfile(String keyfilename){
        System.out.println("Reading keyfile");
			try {
                     FileInputStream keyfile = new FileInputStream(keyfilename);              
					DataInputStream keydis = new DataInputStream(keyfile);

				for(i=0;i<256;i++)
					{
                                        System.out.print(".");
                                        vert[i]=keydis.readLine();
					}
					keydis.close();
                                } catch (Exception e) {
  System.err.println("An error occured during reading the keyfile: please check your input data\n");
                     System.err.println("txt2vrml.read_keyfile: Sorry \n" + e);
                                }
			      }

}
