package com.github.manouti.normalize;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;

@Component( role = Normalizer.class, hint = "default" )
public class DefaultNormalizer implements Normalizer {

	private ManifestTransformer manifestTransformer = new ManifestTransformer();
	private PomPropertiesTransformer pomPropertiesTransformer = new PomPropertiesTransformer();

	@Override
	public void normalize(File jarFile, Date timestamp, File outputFile) throws MojoExecutionException {
		JarOutputStream jarOutputStream = null;
		try {
			JarFile jar = new JarFile(jarFile);
			outputFile.getParentFile().mkdirs();
			FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
			jarOutputStream = new JarOutputStream( new BufferedOutputStream(fileOutputStream));
			for (Enumeration<JarEntry> en = jar.entries(); en.hasMoreElements();) {
				JarEntry entry = en.nextElement();
				entry.setTime(timestamp.getTime());
				
				String resource = entry.getName();
				if (resource.equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
					manifestTransformer.normalizeManifest(jarOutputStream, jar, entry, timestamp);
				} else if(resource.endsWith("/pom.properties")) {
					pomPropertiesTransformer.normalizePropertiesFile(jarOutputStream, jar, entry, timestamp);
				} else if (!entry.isDirectory()) {
					InputStream inputStream = jar.getInputStream(entry);
					jarOutputStream.putNextEntry(entry);
					IOUtil.copy(inputStream, jarOutputStream);
				}
			}
		} catch (Throwable th) {
			outputFile.delete();
			throw new MojoExecutionException( "Error in default normalizer", th );
		} finally {
			IOUtil.close(jarOutputStream);
		}
	}
	
}

@@ -977,20 +977,25 @@
 va.util.
-Date
+ArrayList
 ;%0Aimport
@@ -1005,27 +1005,106 @@
 va.util.
-Enumeration
+Collections;%0Aimport java.util.Comparator;%0Aimport java.util.Date;%0Aimport java.util.Iterator
 ;%0Aimport
@@ -2046,73 +2046,332 @@
 %0A%09%09%09
-for (Enumeration%3CJarEntry%3E en = jar.entries(); en.hasMoreElements
+ArrayList%3CJarEntry%3E entries = Collections.list(jar.entries());%0A%09%09%09Collections.sort(entries, new Comparator%3CJarEntry%3E() %7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09public int compare(JarEntry e1, JarEntry e2) %7B%0A%09%09%09%09%09return e1.getName().compareTo(e2.getName());%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09%09for (Iterator%3CJarEntry%3E iterator = entries.iterator(); iterator.hasNext
 ();)
@@ -2398,21 +2398,20 @@
 y = 
-en.nextElemen
+iterator.nex
 t();

Reorder entries