// 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:   TutorialFileProcessor.java

package com.ptc.windchill.enterprise.tutorials.server;

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class TutorialFileProcessor
{

    public TutorialFileProcessor()
    {
    }

    public Vector validateResourceFiles(Vector topics, String codebase)
    {
        Vector tempVector = new Vector();
        for(Enumeration e = topics.elements(); e.hasMoreElements();)
        {
            HashMap fileMap = (HashMap)e.nextElement();
            String filename = (String)fileMap.get("Resource");
            if(filename != null)
            {
                String file = codebase + filename;
                if(fileExists(file))
                    tempVector.add(fileMap);
                else
                    System.err.println("Removed " + file);
            } else
            {
                tempVector.add(fileMap);
            }
        }

        return tempVector;
    }

    public boolean fileExists(String fileName)
    {
        File file = new File(fileName);
        return file.isFile();
    }

    public static File createFile(String filename)
    {
        File file = new File(filename);
        return file;
    }

    public static boolean writeJavascriptFile(File file, String data)
    {
        try
        {
            FileWriter writer = new FileWriter(file);
            writer.write(data);
            writer.flush();
            writer.close();
        }
        catch(IOException ie)
        {
            System.err.println(ie);
            return false;
        }
        return true;
    }

    public static File getTutorialFile(File directory)
    {
        if(directory.isDirectory())
        {
            File tempList[] = directory.listFiles();
            for(int i = 0; i < tempList.length; i++)
            {
                String fileName = tempList[i].getName();
                if(fileName.indexOf("menuObjects") > -1)
                    return tempList[i];
            }

        }
        return null;
    }

    public static Vector getDirectories(String directory)
    {
        File helpDirectory = new File(directory);
        Vector tempVector = new Vector();
        if(helpDirectory.isDirectory())
        {
            File tempList[] = helpDirectory.listFiles();
            for(int i = 0; i < tempList.length; i++)
                if(tempList[i].isDirectory())
                    tempVector.add(tempList[i]);

        }
        return tempVector;
    }

    public static Vector getDirectories(File directory)
    {
        Vector tempVector = new Vector();
        if(directory.isDirectory())
        {
            File tempList[] = directory.listFiles();
            for(int i = 0; i < tempList.length; i++)
                if(tempList[i].isDirectory())
                    tempVector.add(tempList[i]);

        }
        return tempVector;
    }

    public static ZipFile buildZip(File zipThisDirectory)
    {
        File tutorialZip;
        IOException ie;
        tutorialZip = null;
        try
        {
            tutorialZip = new File(zipThisDirectory.getParentFile().getAbsolutePath() + File.separator + "tutorials.zip");
            FileOutputStream fos = new FileOutputStream(tutorialZip);
            ZipOutputStream zos = new ZipOutputStream(fos);
            buildDirectoryZip(zos, zipThisDirectory, "");
            zos.finish();
            zos.close();
            fos.close();
        }
        catch(FileNotFoundException fnfe)
        {
            System.err.println("TutorialFileProcessor: FileNotFoundException " + fnfe);
            return null;
        }
        // Misplaced declaration of an exception variable
        catch(IOException ie)
        {
            System.err.println("TutorialFileProcessor: IOException " + ie);
            return null;
        }
        return new ZipFile(tutorialZip);
        ZipException ze;
        ze;
        System.err.println("TutorialFileProcessor: ZipException " + ze);
        return null;
        ze;
        System.err.println("TutorialFileProcessor: IOException " + ze);
        return null;
    }

    private static void buildDirectoryZip(ZipOutputStream zos, File directory, String filePath)
    {
        String path = filePath + directory.getName() + File.separator;
        File tempList[] = directory.listFiles();
        for(int x = 0; x < tempList.length; x++)
        {
            File file = tempList[x];
            if(file.isDirectory())
                buildDirectoryZip(zos, file, path);
            else
                buildFileZip(zos, file, path);
        }

    }

    private static void buildFileZip(ZipOutputStream zos, File file, String filePath)
    {
        try
        {
            FileInputStream fis = new FileInputStream(file);
            long size = file.length();
            byte content[] = new byte[(int)size];
            int length = fis.read(content);
            fis.close();
            ZipEntry ze = null;
            ze = new ZipEntry(filePath + file.getName());
            ze.setMethod(8);
            zos.putNextEntry(ze);
            zos.write(content, 0, (int)size);
            zos.closeEntry();
        }
        catch(IOException ie)
        {
            System.err.println("TutorialFileProcessor: IOException " + ie);
        }
    }

    public static boolean unzipHelpEn(ZipFile zf, File targetDirectory)
    {
        try
        {
            ZipEntry target;
            for(Enumeration enum = zf.entries(); enum.hasMoreElements(); saveEntry(target, zf, targetDirectory))
                target = (ZipEntry)enum.nextElement();

        }
        catch(FileNotFoundException fnfe)
        {
            System.err.println("TutorialFileProcessor: FileNotFoundException " + fnfe);
            return false;
        }
        catch(ZipException ze)
        {
            System.err.println("TutorialFileProcessor: ZipException " + ze);
            return false;
        }
        catch(IOException ie)
        {
            System.err.println("TutorialFileProcessor: IOException " + ie);
            return false;
        }
        return true;
    }

    public static void saveEntry(ZipEntry target, ZipFile zf, File dir)
        throws ZipException, IOException
    {
        int EOF = -1;
        File file = new File(dir.getPath() + File.separator + "tutorials" + File.separator + target.getName());
        if(target.isDirectory())
        {
            file.mkdirs();
        } else
        {
            java.io.InputStream is = zf.getInputStream(target);
            BufferedInputStream bis = new BufferedInputStream(is);
            file.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            int c;
            while((c = bis.read()) != EOF) 
                bos.write((byte)c);
            bos.close();
            fos.close();
        }
    }
}
