// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// 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;
        final Upgrade this$0;

        public mapPair()
        {
            this$0 = Upgrade.this;
            super();
            grpInName = new String();
            xmlFileName = new String();
        }

        public mapPair(String s, String s1)
        {
            this$0 = Upgrade.this;
            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((new StringBuilder()).append("IEException caught: ").append(ieexception.getmessage()).toString());
            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((new StringBuilder()).append("\nOUTPUT:\nThe upgraded template filename:\n ").append(templateFilename).toString());
            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 = (new StringBuilder()).append(webject.getParam("TASK").getData()).append(".xml").toString();
                                mappair.xmlFileName = s2;
                                Maper.addElement(mappair);
                            }
                            String s3 = (new StringBuilder()).append("<B>\n").append(dsp4x1.message).append("</B><BR>\n").toString();
                            fileoutputstream.write(s3.getBytes());
                            s3 = getDisplayFormat(webject);
                            fileoutputstream.write(s3.getBytes());
                        } else
                        {
                            System.out.println((new StringBuilder()).append(mappair.grpInName).append(" par = null  so else").toString());
                            String s4 = (new StringBuilder()).append("<B>\n").append(dsp4x1.message).append("</B><BR>\n").toString();
                            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((new StringBuilder()).append("Error :  IOException in createHtmlFile ").append(ioexception.getMessage()).toString());
        }
    }

    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((new StringBuilder()).append(s8).append(mappair.xmlFileName).toString());
            System.out.println((new StringBuilder()).append(mappair.xmlFileName).append("\n").toString());
            String s7 = (new StringBuilder()).append(s).append(s1).toString();
            s7 = (new StringBuilder()).append(s7).append(s2).append("\"").append(mappair.xmlFileName).append("\">\n\n").toString();
            s7 = (new StringBuilder()).append(s7).append(s4).append("\t   This is a Converted file\n").append(s5).toString();
            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 = (new StringBuilder()).append("\t<WEBJECT NAME=\"").append(webject.getName()).append("\"").toString();
        s1 = (new StringBuilder()).append(s1).append("\tTYPE=\"").append(webject.getType()).append("\">\n").toString();
        for(Enumeration enumeration1 = webject.getParams(null); enumeration1.hasMoreElements();)
        {
            Param param = (Param)enumeration1.nextElement();
            s1 = (new StringBuilder()).append(s1).append("\t   <PARAM NAME=\"").append(param.getName()).append("\"").toString();
            s1 = (new StringBuilder()).append(s1).append("\t   DATA=\"").append(param.getData()).append("\"/>\n").toString();
        }

        s1 = (new StringBuilder()).append(s1).append("\t</WEBJECT>\n\n").toString();
        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 = (new StringBuilder()).append(s).append("-").append(param.getData()).toString();
        Utilities utilities = new Utilities();
        s1 = (new StringBuilder()).append(utilities.toMixedCase(s1)).append(".xml").toString();
        File file;
        for(file = new File(s1); file.exists(); file = new File(s1))
        {
            s1 = (new StringBuilder()).append(s).append("-").append(param.getData()).append(i++).toString();
            s1 = (new StringBuilder()).append(utilities.toMixedCase(s1)).append(".xml").toString();
        }

        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 = (new StringBuilder()).append(s).append("/").toString();
        }
        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 = (new StringBuilder()).append(s).append("\t   <PARAM KEY=").append(param.getName()).append("\t   VALUE=\"").toString();
                s = (new StringBuilder()).append(s).append(param.getData()).append("\">\n").toString();
            }
        }

        s = (new StringBuilder()).append(s).append("\t</WEBJECT>\n\n").toString();
        if(flag)
            s = (new StringBuilder()).append("\t<WEBJECT NAME=\"").append(webject.getName()).append("\"\tTYPE=\"").append(webject.getType()).append("\"\tTASK=\"").append(s1).append("\">\n").append(s).toString();
        else
            s = (new StringBuilder()).append("\t<WEBJECT NAME=\"").append(webject.getName()).append("\"\tTYPE=\"").append(webject.getType()).append("\">\n").append(s).toString();
        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((new StringBuilder()).append("Application Map : appMap = ").append(s).append("\n").toString());
        taskdirname1 = properties.getProperty("TASKDIR");
        System.out.println((new StringBuilder()).append("Task directory : TASKDIR = ").append(properties.getProperty("TASKDIR")).append("\n").toString());
        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;
}
