/**
   * Minifies CSS file.
   *
   * @param mergedFile input file resulting from the merged step
   * @param minifiedFile output file resulting from the minify step
   */
  @Override
  protected void minify(File mergedFile, File minifiedFile) throws IOException {
    if (minifiedFile != null) {
      try {
        log.info(
            "Creating minified file ["
                + ((debug) ? minifiedFile.getPath() : minifiedFile.getName())
                + "].");

        InputStream in = new FileInputStream(mergedFile);
        OutputStream out = new FileOutputStream(minifiedFile);
        InputStreamReader reader;
        OutputStreamWriter writer;
        if (charset == null) {
          reader = new InputStreamReader(in);
          writer = new OutputStreamWriter(out);
        } else {
          reader = new InputStreamReader(in, charset);
          writer = new OutputStreamWriter(out, charset);
        }

        CssCompressor compressor = new CssCompressor(reader);
        compressor.compress(writer, linebreak);

        IOUtil.close(reader);
        IOUtil.close(writer);
        IOUtil.close(in);
        IOUtil.close(out);
      } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw e;
      }
    }
  }
 /** {@inheritDoc} */
 @Override
 protected InputStream wrapWithMinify(InputStream is) throws IOException {
   CssCompressor compressor = new CssCompressor(new InputStreamReader(is));
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   PrintWriter writer = new PrintWriter(baos);
   compressor.compress(writer, LINEBREAK);
   writer.close();
   return new ByteArrayInputStream(baos.toByteArray());
 }
示例#3
0
  @Override
  protected void doProcess(Asset asset, Reader reader, Writer writer) throws Exception {

    try {
      CssCompressor compressor = new CssCompressor(reader);
      compressor.compress(writer, -1);
    } catch (IOException e) {
      LOG.error("YUI compressor can't access to the content of {}", asset.toLog());
      throw DandelionException.wrap(e);
    }
  }
示例#4
0
  @Override
  public void transform(InputStream input, OutputStream output) throws IOException {

    // prepare input reader
    Reader reader = new InputStreamReader(input, charset);
    CssCompressor compressor = new CssCompressor(reader);

    // write compressed output
    OutputStreamWriter writer = new OutputStreamWriter(output, charset);
    compressor.compress(writer, 0);
    writer.flush();
  }
示例#5
0
  private String _minifyCss(String content) {
    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    try {
      CssCompressor cssCompressor = new CssCompressor(new UnsyncStringReader(content));

      cssCompressor.compress(unsyncStringWriter, PropsValues.YUI_COMPRESSOR_CSS_LINE_BREAK);
    } catch (Exception e) {
      _log.error("CSS Minifier failed for\n" + content);

      unsyncStringWriter.append(content);
    }

    return unsyncStringWriter.toString();
  }
 /** {@inheritDoc} */
 public void process(final Resource resource, final Reader reader, final Writer writer)
     throws IOException {
   try {
     final CssCompressor compressor = new CssCompressor(reader);
     compressor.compress(writer, linebreakpos);
   } catch (IOException e) {
     LOG.error(
         "Exception occured while processing resource: "
             + resource
             + " using processor: "
             + ALIAS);
     onException(
         new WroRuntimeException(
             "Exception during YuiCss processing of resource: " + resource, e));
   } finally {
     reader.close();
     writer.close();
   }
 }
  /**
   * 压缩css,js帮助
   *
   * @param isCss
   * @param out
   * @return
   */
  private static boolean compressorHelper(List<String> fileList, boolean isCss, Writer out) {
    Reader in = null;
    try {
      if (isCss) {
        for (String path : fileList) {
          String filePath = PathKit.getWebRootPath() + path;
          in = new InputStreamReader(new FileInputStream(filePath), CHARSET);
          if (path.indexOf(".min.") > 0) { // 对.min.css的css放弃压缩
            out.append(repairCss(IOUtils.toString(in), path));
          } else {
            CssCompressor css =
                new CssCompressor(new StringReader(repairCss(IOUtils.toString(in), path)));
            in.close();
            in = null;
            css.compress(out, -1);
          }
        }
      } else {
        // nomunge: 混淆,verbose:显示信息消息和警告,preserveAllSemiColons:保留所有的分号 ,disableOptimizations 禁止优化
        boolean munge = true,
            verbose = false,
            preserveAllSemiColons = false,
            disableOptimizations = false;
        for (String path : fileList) {
          String filePath = PathKit.getWebRootPath() + path;
          in = new InputStreamReader(new FileInputStream(filePath), CHARSET);
          if (path.indexOf(".min.") > 0) { // 对.min.js的js放弃压缩
            out.append(IOUtils.toString(in));
          } else {
            JavaScriptCompressor compressor =
                new JavaScriptCompressor(
                    in,
                    new ErrorReporter() {
                      public void warning(
                          String message,
                          String sourceName,
                          int line,
                          String lineSource,
                          int lineOffset) {
                        if (line < 0) {
                          log.error("\n[WARNING] " + message);
                        } else {
                          log.error("\n[WARNING] " + line + ':' + lineOffset + ':' + message);
                        }
                      }

                      public void error(
                          String message,
                          String sourceName,
                          int line,
                          String lineSource,
                          int lineOffset) {
                        if (line < 0) {
                          log.error("\n[ERROR] " + message);
                        } else {
                          log.error("\n[ERROR] " + line + ':' + lineOffset + ':' + message);
                        }
                      }

                      public EvaluatorException runtimeError(
                          String message,
                          String sourceName,
                          int line,
                          String lineSource,
                          int lineOffset) {
                        error(message, sourceName, line, lineSource, lineOffset);
                        return new EvaluatorException(message);
                      }
                    });
            in.close();
            in = null;
            compressor.compress(
                out, -1, munge, verbose, preserveAllSemiColons, disableOptimizations);
          }
        }
      }
      out.flush();
      return true;
    } catch (IOException e) {
      log.error(e.getMessage());
      return false;
    } finally {
      IOUtils.closeQuietly(in);
      IOUtils.closeQuietly(out);
    }
  }