// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   Upgrade.java

package com.infoengine.convert;

import com.infoengine.object.IeNode;
import com.infoengine.object.factory.Param;
import com.infoengine.object.factory.Webject;
import com.infoengine.object.util.Utilities;
import com.infoengine.procunit.template.htmlStreamTokenizer;
import com.infoengine.procunit.webject.parseWebject2x;
import com.infoengine.util.IEException;
import java.io.*;
import java.util.*;

// Referenced classes of package com.infoengine.convert:
//            backupTemplate2x, UpdateNonDspWebject, dsp4x, ConvertAppMap, 
//            ResolveIncludes

public class Upgrade
{
    private class mapPair
    {

        String grpInName;
        String xmlFileName;

        public mapPair()
        {
            super();
            grpInName = new String();
            xmlFileName = new String();
        }

        public mapPair(String s, String s1)
        {
            super();
            grpInName = new String();
            xmlFileName = new String();
            grpInName = s;
            xmlFileName = s1;
        }
    }


    private Upgrade()
    {
        templateFilename = new String();
        taskdirname1 = "";
    }

    public Upgrade(String s)
    {
        this();
        templateFilename = s;
        nodePool = new Vector();
        Maper = new Vector();
        nonDspPool = new Vector();
        message = "";
        backupTemplate2x backuptemplate2x = new backupTemplate2x(templateFilename);
    }

    private boolean createNodePool()
    {
        java.io.BufferedReader bufferedreader = ResolveIncludes.getBufferedReader(templateFilename);
        if(bufferedreader == null)
            return false;
        htmlStreamTokenizer htmlstreamtokenizer = new htmlStreamTokenizer(bufferedreader);
        IeNode ienode = null;
        parseWebject2x parsewebject2x = new parseWebject2x();
        try
        {
            while((ienode = htmlstreamtokenizer.nextNode()) != null) 
                if(ienode.getType().equalsIgnoreCase("HTML"))
                {
                    nodePool.addElement(ienode);
                } else
                {
                    Webject webject = parsewebject2x.parseWebject(ienode);
                    if(webject == null)
                        System.out.println("Internal Error : Webject can not be parsed");
                    else
                        nodePool.addElement(webject);
                }
        }
        catch(IEException ieexception)
        {
            System.out.println("IEException caught: " + ieexception.getmessage());
            return false;
        }
        return true;
    }

    public void createHtmlFile()
    {
        Enumeration enumeration = nodePool.elements();
        boolean flag = false;
        String s = "";
        try
        {
            FileOutputStream fileoutputstream = new FileOutputStream(templateFilename, false);
            System.out.println("\nOUTPUT:\nThe upgraded template filename:\n " + templateFilename);
            while(enumeration.hasMoreElements()) 
            {
                Object obj = enumeration.nextElement();
                try
                {
                    Webject webject = (Webject)obj;
                    UpdateNonDspWebject updatenondspwebject = new UpdateNonDspWebject(webject);
                    webject = updatenondspwebject.updateTypeValue();
                    String s1 = updatenondspwebject.message;
                    webject = updatenondspwebject.removeBindParam(webject);
                    fileoutputstream.write(updatenondspwebject.message.getBytes());
                    webject = updatenondspwebject.convertFormatSyntax(webject);
                    Webject _tmp = webject;
                    if(webject.getType() == "DSP")
                    {
                        mapPair mappair = new mapPair();
                        Param param;
                        if((param = webject.getParam("GROUP_IN")) != null)
                            mappair.grpInName = param.getData();
                        dsp4x dsp4x1 = new dsp4x(webject);
                        webject = dsp4x1.get4xWebject();
                        if(webject != null)
                        {
                            if(param != null)
                            {
                                webject = uniqueXmlTaskName(webject);
                                String s2 = webject.getParam("TASK").getData() + ".xml";
                                mappair.xmlFileName = s2;
                                Maper.addElement(mappair);
                            }
                            String s3 = "<B>\n" + dsp4x1.message + "</B><BR>\n";
                            fileoutputstream.write(s3.getBytes());
                            s3 = getDisplayFormat(webject);
                            fileoutputstream.write(s3.getBytes());
                        } else
                        {
                            System.out.println(mappair.grpInName + " par = null  so else");
                            String s4 = "<B>\n" + dsp4x1.message + "</B><BR>\n";
                            fileoutputstream.write(s4.getBytes());
                        }
                    } else
                    {
                        webject = nonDspUpdate(webject);
                        fileoutputstream.write(s1.getBytes());
                        fileoutputstream.write(message.getBytes());
                        nonDspPool.addElement(webject);
                    }
                }
                catch(ClassCastException classcastexception)
                {
                    IeNode ienode = (IeNode)obj;
                    String s5 = "";
                    s5 = (String)ienode.getMetaValue("SOURCE");
                    fileoutputstream.write(s5.getBytes());
                }
            }
        }
        catch(IOException ioexception)
        {
            System.out.println("Error :  IOException in createHtmlFile " + ioexception.getMessage());
        }
    }

    public void createXmlFile(mapPair mappair)
    {
        String s = "<?xml version=\"1.0\" standalone=\"yes\" ?>\n";
        String s1 = "<!DOCTYPE PROCESS>\n\n";
        String s2 = "<PROCESS NAME=";
        String s3 = "</PROCESS>\n";
        String s4 = "\t<DESCRIPTION>\n";
        String s5 = "\t</DESCRIPTION>\n\n";
        String s6 = "";
        try
        {
            String s8 = getTaskDir();
            System.out.println(s8);
            FileOutputStream fileoutputstream = new FileOutputStream(s8 + mappair.xmlFileName);
            System.out.println(mappair.xmlFileName + "\n");
            String s7 = s + s1;
            s7 = s7 + s2 + "\"" + mappair.xmlFileName + "\">\n\n";
            s7 = s7 + s4 + "\t   This is a Converted file\n" + s5;
            fileoutputstream.write(s7.getBytes());
            Webject webject = getNonDspWebject(mappair.grpInName);
            if(webject != null)
                writeWebjectToXml(webject, fileoutputstream);
            fileoutputstream.write(s3.getBytes());
            fileoutputstream.close();
        }
        catch(FileNotFoundException filenotfoundexception)
        {
            System.out.println("Error Creating XML file");
        }
        catch(IOException ioexception)
        {
            System.out.println("IOExecption - Error Closing XML file");
        }
    }

    void writeWebjectToXml(Webject webject, FileOutputStream fileoutputstream)
        throws IOException
    {
        if(webject == null)
        {
            System.out.println("Internal error : Corresponding Task Webject not found");
            return;
        }
        Webject webject1;
        for(Enumeration enumeration = webject.getGroupInNames(); enumeration != null && enumeration.hasMoreElements(); writeWebjectToXml(webject1, fileoutputstream))
        {
            String s = (String)enumeration.nextElement();
            webject1 = getNonDspWebject(s);
        }

        String s1 = "\t<WEBJECT NAME=\"" + webject.getName() + "\"";
        s1 = s1 + "\tTYPE=\"" + webject.getType() + "\">\n";
        for(Enumeration enumeration1 = webject.getParams(null); enumeration1.hasMoreElements();)
        {
            Param param = (Param)enumeration1.nextElement();
            s1 = s1 + "\t   <PARAM NAME=\"" + param.getName() + "\"";
            s1 = s1 + "\t   DATA=\"" + param.getData() + "\"/>\n";
        }

        s1 = s1 + "\t</WEBJECT>\n\n";
        fileoutputstream.write(s1.getBytes());
    }

    public Webject uniqueXmlTaskName(Webject webject)
    {
        int i = 0;
        Param param = webject.getParam("TASK");
        int j = templateFilename.lastIndexOf(".");
        if(j < 0)
            j = templateFilename.length();
        String s = templateFilename.substring(0, j);
        String s1 = "";
        s1 = s + "-" + param.getData();
        Utilities utilities = new Utilities();
        s1 = utilities.toMixedCase(s1) + ".xml";
        File file;
        for(file = new File(s1); file.exists(); file = new File(s1))
        {
            s1 = s + "-" + param.getData() + i++;
            s1 = utilities.toMixedCase(s1) + ".xml";
        }

        String s2 = file.getName();
        s2 = s2.substring(0, s2.length() - 4);
        webject.setValue("TASK", s2);
        return webject;
    }

    public String getTaskDir()
    {
        String s = "";
        s = taskdirname1;
        if(s == null || s.equals(""))
        {
            s = "./tasks/";
        } else
        {
            s = s.trim();
            if(s.charAt(s.length() - 1) != '/')
                s = s + "/";
        }
        File file = new File(s);
        if(!file.exists())
            file.mkdir();
        return s;
    }

    private Webject getNonDspWebject(String s)
    {
        Object obj = null;
        for(Enumeration enumeration = nonDspPool.elements(); enumeration.hasMoreElements();)
        {
            Webject webject = (Webject)enumeration.nextElement();
            Param param = webject.getParam("GROUP_OUT");
            if(param == null)
                return null;
            if(param.getData().equalsIgnoreCase(s))
                return webject;
        }

        return null;
    }

    private String getDisplayFormat(Webject webject)
    {
        String s = "";
        String s1 = "";
        boolean flag = false;
        for(Enumeration enumeration = webject.getParams(null); enumeration.hasMoreElements();)
        {
            Param param = (Param)enumeration.nextElement();
            if(param.getName().equalsIgnoreCase("TASK"))
            {
                s1 = param.getData();
                flag = true;
            } else
            {
                s = s + "\t   <PARAM KEY=" + param.getName() + "\t   VALUE=\"";
                s = s + param.getData() + "\">\n";
            }
        }

        s = s + "\t</WEBJECT>\n\n";
        if(flag)
            s = "\t<WEBJECT NAME=\"" + webject.getName() + "\"\tTYPE=\"" + webject.getType() + "\"\tTASK=\"" + s1 + "\">\n" + s;
        else
            s = "\t<WEBJECT NAME=\"" + webject.getName() + "\"\tTYPE=\"" + webject.getType() + "\">\n" + s;
        return s;
    }

    public Webject nonDspUpdate(Webject webject)
    {
        UpdateNonDspWebject updatenondspwebject = new UpdateNonDspWebject(webject);
        webject = updatenondspwebject.updateWebject();
        message = updatenondspwebject.message;
        return webject;
    }

    public void appMapToProperties()
    {
        Properties properties = System.getProperties();
        String s = properties.getProperty("appMap", "conversionTool.properties");
        System.out.println("Application Map : appMap = " + s + "\n");
        taskdirname1 = properties.getProperty("TASKDIR");
        System.out.println("Task directory : TASKDIR = " + properties.getProperty("TASKDIR") + "\n");
        if(!s.equals("conversionTool.properties"))
        {
            System.out.println("Converting AppMap to System Properties...\n");
            ConvertAppMap convertappmap = new ConvertAppMap(s);
            convertappmap.toPropFile();
        }
        Properties properties1 = new Properties();
        try
        {
            FileInputStream fileinputstream = new FileInputStream("conversionTool.properties");
            properties1.load(fileinputstream);
            System.setProperties(properties1);
        }
        catch(FileNotFoundException filenotfoundexception)
        {
            System.err.println("The Manage object cannot start as the properties file conversionTool.propertiesdoes not exist.");
            System.exit(-1);
        }
        catch(Exception exception)
        {
            System.err.println("Exception reading properties file conversionTool.properties");
            exception.printStackTrace(System.err);
        }
        System.out.println("Upgrading template to I*E 4.0 ..........\n");
    }

    public static void main(String args[])
    {
        if(args.length == 0)
        {
            System.out.println("Usage : java -DTASKDIR=taskdir -DappMap=app.map Upgrade 'template2.x filename' ");
            System.exit(1);
        }
        for(int i = 0; i < args.length; i++)
        {
            Upgrade upgrade = new Upgrade(args[i]);
            upgrade.appMapToProperties();
            if(!upgrade.createNodePool())
            {
                System.out.println("Error Reading template file");
                System.exit(1);
            }
            upgrade.createHtmlFile();
            System.out.println("\nThe 4.0  .xml files :");
            mapPair mappair;
            for(Enumeration enumeration = upgrade.Maper.elements(); enumeration.hasMoreElements(); upgrade.createXmlFile(mappair))
                mappair = (mapPair)enumeration.nextElement();

        }

    }

    String templateFilename;
    Vector nodePool;
    Vector Maper;
    Vector nonDspPool;
    String message;
    String taskdirname1;
}
