/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program 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 General Public License for more details.

   $Project: polymake $$Id: PolymakeJvxParser.java,v 1.2 2004/06/22 16:22:39 gawrilow Exp $
*/

package de.tuberlin.polymake.common;

import java.awt.Color;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import jv.geom.*;
import jv.loader.*;
import jv.object.*;
import jv.rsrc.PsXmlNode;
import jv.rsrc.PsXmlSrc;
import jv.vecmath.PdVector;
import jv.vecmath.PiVector;
import jv.project.*;

import java.util.StringTokenizer;

/**
 * This class parses an extended Javaview jvx format. Any additional 
 * information must be stored in child nodes of the geometry node with 
 * a name specified in the constructor. After parsing, the additional 
 * information is stored in the parameters, which may be retrieved using
 * the getParam(String) method. 
 *
 * @author Thilo Schr&ouml;der
 */
public class PolymakeJvxParser {
	
    /**
     * Parse the data from a BufferedReader. The parameters are children 
     * of the geometry element in the Javaview jvx file format. The values
     * of the parameters will be set to the content of these elements. If
     * the elements don't exist the values will be <code>null</code>.
     * 
     * @param in	BufferedReader for textual input which will be parsed
     * @throws IOException	if problems reading from in occur
     */
    public static EmbeddedGeometries parse(java.io.BufferedReader in) throws IOException {

	PgJvxLoader jvxLoader = new PgJvxLoader();
	PgJvxSrc[] i_geoms = jvxLoader.read(in);

	EmbeddedGeometry[] geoms = new EmbeddedGeometry[i_geoms.length];
	for(int i = 0; i < i_geoms.length; ++i) {
	    String name = i_geoms[i].getName();
	    boolean dynamic = false;
	    StringTokenizer st = new StringTokenizer(name,":");
	    if(st.nextToken().equals("dynamic")) {
		dynamic = true;
		name = st.nextToken();
		i_geoms[i].setName(name);
	    }
	    int[] vertexList = null;
	    
	    if(dynamic) {
		vertexList = new int[i_geoms[i].getNumVertices()];
		for(int j = 0; j < vertexList.length; ++j) {
		    vertexList[j] = (int)(i_geoms[i].getVertex(j).getEntry(0));
		}
	    }
	    
	    switch (i_geoms[i].getType()) {
	    case PvGeometryIf.GEOM_POINT_SET:
		PgPointSet pointSet = new PgPointSet();
		pointSet.setJvx(i_geoms[i]);
		geoms[i] = new EmbeddedGeometry(vertexList,pointSet,dynamic);
		break;
	    case PvGeometryIf.GEOM_POLYGON_SET:
		PgPolygonSet polygonSet = new PgPolygonSet();
		polygonSet.setJvx(i_geoms[i]);
		polygonSet.showPolygonColors(true);
		geoms[i] = new EmbeddedGeometry(vertexList,polygonSet,dynamic);
		break;
	    case PvGeometryIf.GEOM_ELEMENT_SET:
		PgElementSet elementSet = new PgElementSet();
		elementSet.setJvx(i_geoms[i]);
		geoms[i] = new EmbeddedGeometry(vertexList,elementSet,dynamic);
		break;
	    case PvGeometryIf.GEOM_POLYGON:
		PgPolygon polygon = new PgPolygon();
		polygon.setJvx(i_geoms[i]);
		geoms[i] = new EmbeddedGeometry(vertexList,polygon,dynamic);
		break;
	    default:
		PsDebug.warning("geometry["+i+"] has unknown type = "+i_geoms[i].getType());
	    }
	}
	String title = i_geoms[0].getTitle();
	StringTokenizer st = new StringTokenizer(title,":");
	if(st.nextToken().equals("dynamic")) {
	    title = st.nextToken();
	}
	return new EmbeddedGeometries(title,geoms);
    }
}
