/*******************************************************************************
 * Copyright (c) 2014 IBH SYSTEMS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package de.dentrassi.pm.osgi;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.osgi.framework.Version;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public final class ParserHelper
{
    private ParserHelper ()
    {
    }

    public static Map<String, Properties> loadLocalization ( final ZipFile file, final String loc ) throws IOException
    {
        final Map<String, Properties> locs = new HashMap<> ();

        final Pattern pattern = Pattern.compile ( Pattern.quote ( loc ) + "(|_[a-z]{2}-[A-Z]{2})\\.properties" );

        final Enumeration<? extends ZipEntry> en = file.entries ();
        while ( en.hasMoreElements () )
        {
            final ZipEntry ze = en.nextElement ();
            final Matcher m = pattern.matcher ( ze.getName () );
            if ( m.matches () )
            {
                final String locale = makeLocale ( m.group ( 1 ) );
                final Properties properties = loadProperties ( file, ze );
                locs.put ( locale, properties );
            }
        }
        return locs;
    }

    private static Properties loadProperties ( final ZipFile file, final ZipEntry ze ) throws IOException
    {
        final Properties p = new Properties ();
        p.load ( file.getInputStream ( ze ) );
        return p;
    }

    private static String makeLocale ( final String localeString )
    {
        if ( localeString.isEmpty () )
        {
            return "df_LT";
        }
        else
        {
            return localeString;
        }
    }

    private static final GsonBuilder gb;

    public static Gson newGson ()
    {
        return gb.create ();
    }

    static
    {
        gb = new GsonBuilder ();
        gb.registerTypeAdapter ( Version.class, new TypeAdapter<Version> () {

            @Override
            public Version read ( final JsonReader reader ) throws IOException
            {
                if ( reader.peek () == JsonToken.NULL )
                {
                    reader.nextNull ();
                    return null;
                }

                final String str = reader.nextString ();
                if ( str == null )
                {
                    return null;
                }
                return new Version ( str );
            }

            @Override
            public void write ( final JsonWriter writer, final Version value ) throws IOException
            {
                if ( value == null )
                {
                    writer.nullValue ();
                }
                else
                {
                    writer.value ( value.toString () );
                }
            }
        } );
    }
}

@@ -2962,32 +2962,1407 @@
              %7D%0A%0A
+                /* Begin compat */%0A                if ( reader.peek () == JsonToken.BEGIN_OBJECT )%0A                %7B%0A                    // old format%0A                    reader.beginObject ();%0A%0A                    int major = 0;%0A                    int minor = 0;%0A                    int micro = 0;%0A                    String qualifier = null;%0A%0A                    while ( reader.hasNext () )%0A                    %7B%0A                        final String name = reader.nextName ();%0A                        switch ( name )%0A                        %7B%0A                            case %22major%22:%0A                                major = reader.nextInt ();%0A                                break;%0A                            case %22minor%22:%0A                                minor = reader.nextInt ();%0A                                break;%0A                            case %22micro%22:%0A                                micro = reader.nextInt ();%0A                                break;%0A                            case %22qualifier%22:%0A                                qualifier = reader.peek () == JsonToken.NULL ? null : reader.nextString ();%0A                                break;%0A                        %7D%0A                    %7D%0A                    reader.endObject ();%0A                    return new Version ( major, minor, micro, qualifier );%0A                %7D%0A                /* end compat */%0A%0A
                 

allow processing old metadata