/*
 * Copyright (c) 2008. All rights reserved.
 */
package ro.isdc.wro.model.resource.processor.impl.js;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import org.apache.commons.io.input.ProxyInputStream;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.io.output.ProxyOutputStream;
import org.apache.commons.io.output.WriterOutputStream;

import ro.isdc.wro.config.jmx.WroConfiguration;
import ro.isdc.wro.model.group.Inject;
import ro.isdc.wro.model.group.processor.Minimize;
import ro.isdc.wro.model.resource.Resource;
import ro.isdc.wro.model.resource.ResourceType;
import ro.isdc.wro.model.resource.SupportedResourceType;
import ro.isdc.wro.model.resource.processor.ResourcePostProcessor;
import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
import ro.isdc.wro.model.resource.processor.support.JSMin;


/**
 * Use JSMin utility for js compression. This processor is annotated with {@link Minimize} because it performs
 * minimization.
 *
 * @author Alex Objelean
 * @created Created on Nov 28, 2008
 */
@Minimize
@SupportedResourceType(ResourceType.JS)
public class JSMinProcessor implements ResourcePreProcessor,
    ResourcePostProcessor {
  public static final String ALIAS = "jsMin";
  @Inject
  private WroConfiguration config;
  private String encoding;
  
  /**
   * {@inheritDoc}
   */
  public void process(final Resource resource, final Reader reader, final Writer writer)
      throws IOException {
    try {
      final InputStream is = new ProxyInputStream(new ReaderInputStream(reader, getEncoding())) {};
      final OutputStream os = new ProxyOutputStream(new WriterOutputStream(writer, getEncoding()));
      
      new JSMin(is, os).jsmin();
      
      is.close();
      os.close();
		} catch (final Exception e) {
      throw new IOException(e);
    } 
  }

  /**
   * {@inheritDoc}
   */
  public void process(final Reader reader,
      final Writer writer) throws IOException {
    // resource Uri doesn't matter.
    process(null, reader, writer);
  }

  /**
   * @return the encoding
   */
  private String getEncoding() {
    if (encoding == null) {
      //use config is available to get encoding
      this.encoding = config == null ? WroConfiguration.DEFAULT_ENCODING : config.getEncoding();
    }
    return encoding;
  }

  /**
   * @param encoding the encoding to set
   */
  public JSMinProcessor setEncoding(final String encoding) {
    this.encoding = encoding;
    return this;
  }
}

@@ -1460,32 +1460,65 @@
 ritDoc%7D%0D%0A   */%0D%0A
+  @SuppressWarnings(%22resource%22)%0D%0A
   public void pr
@@ -1568,16 +1568,21 @@
  reader,
+%0D%0A   
  final W
@@ -1594,23 +1594,16 @@
  writer)
-%0D%0A     
  throws 
@@ -1621,19 +1621,8 @@
  %7B%0D%0A
-    try %7B%0D%0A
     
@@ -1815,34 +1815,37 @@
 oding()));%0D%0A    
-  
+try %7B
 %0D%0A      new JSMi
@@ -1959,14 +1959,18 @@
 row 
-new IO
+WroRuntime
 Exce
@@ -1974,16 +1974,21 @@
 xception
+.wrap
 (e);%0D%0A  
@@ -1991,16 +1991,91 @@
 %0D%0A    %7D 
+finally %7B%0D%0A      IOUtils.closeQuietly(is);%0D%0A      IOUtils.closeQuietly(os);
 %0D%0A  %7D%0D%0A%0D

fix merge conflicts