/*
 * 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.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

import de.inetsoftware.classparser.ClassFile;
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();
        try (TextModuleWriter writer = new TextModuleWriter( output )) {
            for( File file : classFiles ) {
                ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) );
                writer.write( classFile );
            }
        } catch( Exception ex ) {
            throw WasmException.create( ex );
        }
        return output.toString();
    }
}

@@ -748,32 +748,61 @@
 leInputStream;%0D%0A
+import java.io.IOException;%0D%0A
 import java.util
@@ -919,24 +919,82 @@
 ClassFile;%0D%0A
+import de.inetsoftware.jwebassembly.module.ModuleWriter;%0D%0A
 import de.in
@@ -1860,370 +1860,1036 @@
     
-try (TextModuleWriter writer = new TextModuleWriter( output )) %7B%0D%0A            for( File file : classFiles ) %7B%0D%0A                ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) );%0D%0A                writer.write( classFile );
+compileToText( output );%0D%0A        return output.toString();%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Convert the added files to a WebAssembly module in text representation.%0D%0A     * %0D%0A     * @param output%0D%0A     *            the target for the module data%0D%0A     * @throws WasmException%0D%0A     *             if any conversion error occurs%0D%0A     */%0D%0A    public void compileToText( Appendable output ) throws WasmException %7B%0D%0A        try (TextModuleWriter writer = new TextModuleWriter( output )) %7B%0D%0A            compile( writer );%0D%0A        %7D catch( Exception ex ) %7B%0D%0A            throw WasmException.create( ex );%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Convert the added files to a WebAssembly module.%0D%0A     * %0D%0A     * @param writer%0D%0A     *            the formatter%0D%0A     * @throws IOException
 %0D%0A     
+*
        
-%7D%0D%0A
       
-  %7D catch( Exception ex ) %7B%0D%0A            throw WasmException.create( ex );
+if any I/O error occur%0D%0A     * @throws WasmException%0D%0A     *             if any conversion error occurs%0D%0A     */%0D%0A    private void compile( ModuleWriter writer ) throws IOException, WasmException %7B%0D%0A        for( File file : classFiles ) %7B
 %0D%0A  
@@ -2898,44 +2898,157 @@
     
-%7D%0D%0A
     
-    return output.toString();
+ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) );%0D%0A            writer.write( classFile );%0D%0A        %7D
 %0D%0A  

Split function to more generic use