package com.philisense.www.xml;

import java.lang.reflect.*;
import java.util.*;
import java.util.Map.Entry;
import java.beans.*;
import java.io.*;

public class PersistenceService implements XMLConstants
{
    private static final HashMap<Class, PersistenceDelegate> allPDs;
    public static int BUFSIZE;
    
    private PersistenceService() {
    }
    
    private static final ExceptionListener createExceptionListener() {
        return new ExceptionListener() {
            @Override
            public void exceptionThrown(Exception var1) {
                throw var1 instanceof UndeclaredThrowableException ? (UndeclaredThrowableException)var1 : new UndeclaredThrowableException(var1);
            }
        };
    }
    
    public static final synchronized void setPersistenceDelegate(final Class clazz, final PersistenceDelegate persistenceDelegate) {
        PersistenceService.allPDs.put(clazz, persistenceDelegate);
    }
    
    protected static synchronized void installPersistenceDelegates(Encoder var0) {
        Iterator var1 = allPDs.entrySet().iterator();

        while(var1.hasNext()) {
            Entry var2 = (Entry)var1.next();
            var0.setPersistenceDelegate((Class)var2.getKey(), (PersistenceDelegate)var2.getValue());
        }

    }
    
    public static void store(final Object o, final OutputStream outputStream) throws PersistenceServiceException {
        if (null == outputStream) {
            throw new NullPointerException();
        }
        try {
            OutputStream outputStream2 = null;
            XMLEncoder xmlEncoder = null;
            try {
                outputStream2 = new BufferedOutputStream(outputStream, PersistenceService.BUFSIZE);
                xmlEncoder = new XMLEncoder(outputStream2);
                installPersistenceDelegates(xmlEncoder);
                xmlEncoder.setExceptionListener(createExceptionListener());
                xmlEncoder.writeObject(o);
            }
            finally {
                if (null != xmlEncoder) {
                    try {
                        xmlEncoder.close();
                        return;
                    }
                    catch (Throwable t) {
                        outputStream2.close();
                        throw t;
                    }
                }
                if (null != outputStream2) {
                    outputStream2.close();
                }
                else {
                    outputStream.close();
                }
            }
        }
        catch (UndeclaredThrowableException ex) {
            throw new PersistenceServiceException(ex.getCause());
        }
        catch (Throwable t2) {
            throw new PersistenceServiceException(t2);
        }
    }
    
    public static void store(Object var0, File var1) throws PersistenceServiceException {
        if (null == var1) {
            throw new NullPointerException();
        } else {
            File var2 = null;
            boolean var3 = false;

            try {
                var2 = getRenamedFile(var1);
                var3 = var1.renameTo(var2);
                store(var0, (OutputStream)(new FileOutputStream(var1)));
                if (var3) {
                    var2.delete();
                }

            } catch (Throwable var8) {
                Throwable var4 = var8;
                if (var3) {
                    try {
                        var1.delete();
                    } catch (Throwable var7) {
                        var4 = var7;
                    }

                    try {
                        var2.renameTo(var1);
                    } catch (Throwable var6) {
                        var4 = var6;
                    }
                }

                throw var4 instanceof PersistenceServiceException ? (PersistenceServiceException)var4 : new PersistenceServiceException(var4);
            }
        }
    }
    
    private static File getRenamedFile(final File file) {
        final StringBuilder sb = new StringBuilder(file.getPath());
        File file2;
        do {
            file2 = new File(sb.append('~').toString());
        } while (file2.exists());
        return file2;
    }
    
    public static byte[] store2ByteArray(final Object o) throws PersistenceServiceException {
        try {
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            store(o, byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        }
        catch (PersistenceServiceException ex) {
            throw ex;
        }
        catch (Throwable t) {
            throw new PersistenceServiceException(t);
        }
    }
    
    public static String store2String(final Object o) throws PersistenceServiceException {
        try {
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            store(o, byteArrayOutputStream);
            return byteArrayOutputStream.toString("UTF-8");
        }
        catch (UnsupportedEncodingException ex) {
            throw new AssertionError((Object)ex);
        }
        catch (PersistenceServiceException ex2) {
            throw ex2;
        }
        catch (Throwable t) {
            throw new PersistenceServiceException(t);
        }
    }
    
    public static Object load(final InputStream inputStream) throws PersistenceServiceException {
        if (null == inputStream) {
            throw new NullPointerException();
        }
        XMLDecoder xmlDecoder = null;
        try {
            xmlDecoder = new XMLDecoder(new BufferedInputStream(inputStream, PersistenceService.BUFSIZE), null, createExceptionListener());
            return xmlDecoder.readObject();
        }
        catch (UndeclaredThrowableException ex) {
            throw new PersistenceServiceException(ex.getCause());
        }
        catch (Throwable t) {
            throw new PersistenceServiceException(t);
        }
        finally {
            if (null != xmlDecoder) {
                try {
                    xmlDecoder.close();
                }
                catch (Throwable t2) {
                    throw new PersistenceServiceException(t2);
                }
            }
        }
    }
    
    public static Object load(final File file) throws PersistenceServiceException {
        if (null == file) {
            throw new NullPointerException();
        }
        try {
            return load(new FileInputStream(file));
        }
        catch (PersistenceServiceException ex) {
            throw ex;
        }
        catch (Throwable t) {
            throw new PersistenceServiceException(t);
        }
    }
    
    public static Object load(final byte[] array) throws PersistenceServiceException {
        if (null == array) {
            throw new NullPointerException();
        }
        try {
            return load(new ByteArrayInputStream(array));
        }
        catch (PersistenceServiceException ex) {
            throw ex;
        }
        catch (Throwable t) {
            throw new PersistenceServiceException(t);
        }
    }
    
    public static Object load(final String s) throws PersistenceServiceException {
        if (null == s) {
            throw new NullPointerException();
        }
        try {
            return load(s.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException ex) {
            throw new AssertionError((Object)ex);
        }
        catch (PersistenceServiceException ex2) {
            throw ex2;
        }
        catch (Throwable t) {
            throw new PersistenceServiceException(t);
        }
    }
    
    static {
        allPDs = new HashMap();
        PersistenceService.BUFSIZE = 8192;
    }
}
