/*
 * Copyright 2017 Volker Berlin (i-net software)
 *
 * Licensed 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.
 */
package de.inetsoftware.jwebassembly;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.text.TextModuleWriter;

/**
 * The main class of the compiler.
 * 
 * @author Volker Berlin
 */
public class JWebAssembly {

    private List<File> classFiles = new ArrayList<File>();

    /**
     * Create a instance.
     */
    public JWebAssembly() {
    }

    /**
     * Add a classFile to compile
     * 
     * @param classFile
     *            the file
     */
    public void addFile( @Nonnull File classFile ) {
        classFiles.add( classFile );
    }

    /**
     * Convert the added files to a WebAssembly module in text representation.
     * 
     * @return the module as string
     * @throws WasmException
     *             if any conversion error occurs
     */
    public String compileToText() throws WasmException {
        StringBuilder output = new StringBuilder();
        compileToText( output );
        return output.toString();
    }

    /**
     * Convert the added files to a WebAssembly module in text representation.
     * 
     * @param output
     *            the target for the module data
     * @throws WasmException
     *             if any conversion error occurs
     */
    public void compileToText( Appendable output ) throws WasmException {
        try (TextModuleWriter writer = new TextModuleWriter( output )) {
            compile( writer );
        } catch( Exception ex ) {
            throw WasmException.create( ex );
        }
    }

    /**
     * Convert the added files to a WebAssembly module in binary representation.
     * 
     * @return the module as string
     * @throws WasmException
     *             if any conversion error occurs
     */
    public byte[] compileToBinary() throws WasmException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        compileToBinary( output );
        return output.toByteArray();
    }

    /**
     * Convert the added files to a WebAssembly module in binary representation.
     * 
     * @param file
     *            the target for the module data
     * @throws WasmException
     *             if any conversion error occurs
     */
    public void compileToBinary( File file ) throws WasmException {
        try (FileOutputStream output = new FileOutputStream( file )) {
            compileToBinary( output );
        } catch( Exception ex ) {
            throw WasmException.create( ex );
        }
    }

    /**
     * Convert the added files to a WebAssembly module in binary representation.
     * 
     * @param output
     *            the target for the module data
     * @throws WasmException
     *             if any conversion error occurs
     */
    public void compileToBinary( OutputStream output ) throws WasmException {
        try (BinaryModuleWriter writer = new BinaryModuleWriter( output )) {
            compile( writer );
        } catch( Exception ex ) {
            throw WasmException.create( ex );
        }
    }

    /**
     * Convert the added files to a WebAssembly module.
     * 
     * @param writer
     *            the formatter
     * @throws IOException
     *             if any I/O error occur
     * @throws WasmException
     *             if any conversion error occurs
     */
    private void compile( ModuleWriter writer ) throws IOException, WasmException {
        for( File file : classFiles ) {
            ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) );
            writer.write( classFile );
        }
    }
}

@@ -746,40 +746,8 @@
 le;%0A
-import java.io.FileInputStream;%0A
 impo
@@ -767,32 +767,32 @@
 leOutputStream;%0A
+
 import java.io.I
@@ -824,32 +824,92 @@
 o.OutputStream;%0A
+import java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 import java.util
@@ -1322,20 +1322,19 @@
 te List%3C
-File
+URL
 %3E classF
@@ -1354,20 +1354,16 @@
 rayList%3C
-File
 %3E();%0A%0A  
@@ -1538,32 +1538,32 @@
 he file%0A     */%0A
-
     public void 
@@ -1595,24 +1595,373 @@
 assFile ) %7B%0A
+        try %7B%0A            classFiles.add( classFile.toURI().toURL() );%0A        %7D catch( MalformedURLException ex ) %7B%0A            throw new IllegalArgumentException( ex );%0A        %7D%0A    %7D%0A%0A    /**%0A     * Add a classFile to compile%0A     * %0A     * @param classFile%0A     *            the file%0A     */%0A    public void addFile( @Nonnull URL classFile ) %7B%0A
         clas
@@ -4788,25 +4788,23 @@
    for( 
-File file
+URL url
  : class
@@ -4891,34 +4891,23 @@
 am( 
-new FileInput
+url.open
 Stream(
- file 
 ) ) 

add method addFile(URL)