1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.update.schema.program.staticsource;
22  
23  import java.io.BufferedReader;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.InputStreamReader;
27  import java.io.OutputStreamWriter;
28  
29  import org.efaps.ci.CIAdminProgram;
30  import org.efaps.ci.CIType;
31  import org.efaps.db.Checkout;
32  import org.efaps.db.Instance;
33  import org.efaps.update.schema.program.staticsource.CSSCompiler.OneCSS;
34  import org.efaps.util.EFapsException;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import com.yahoo.platform.yui.compressor.CssCompressor;
39  
40  /**
41   * TODO description!
42   *
43   * @author The eFaps Team
44   * @version $Id$
45   */
46  public class CSSCompiler
47      extends AbstractStaticSourceCompiler<OneCSS>
48  {
49  
50      /**
51       * Logging instance used in this class.
52       */
53      private static final Logger LOG = LoggerFactory.getLogger(CSSCompiler.class);
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      protected CIType getClassName4Type()
60      {
61          return CIAdminProgram.CSS;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      protected CIType getClassName4Type2Type()
69      {
70          return CIAdminProgram.CSS2CSS;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected CIType getClassName4TypeCompiled()
78      {
79          return  CIAdminProgram.CSSCompiled;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      protected String getCompiledString(final Instance _instance)
87      {
88          String ret = "";
89          try {
90              final Checkout checkout = new Checkout(_instance);
91              final BufferedReader in = new BufferedReader(new InputStreamReader(checkout.execute(), "UTF-8"));
92  
93              final CssCompressor compressor = new CssCompressor(in);
94              in.close();
95              checkout.close();
96              final ByteArrayOutputStream byteout = new ByteArrayOutputStream();
97              final OutputStreamWriter out = new OutputStreamWriter(byteout, "UTF-8");
98              compressor.compress(out, 2000);
99              out.flush();
100             ret = byteout.toString("UTF-8");
101             ret += "\n";
102         } catch (final EFapsException e) {
103             CSSCompiler.LOG.error("error during checkout of Instance:" + _instance, e);
104             e.printStackTrace();
105         } catch (final IOException e) {
106             CSSCompiler.LOG.error("error during reqding of the Inputstram of Instance with oid:" + _instance, e);
107         }
108         return ret;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public OneCSS getNewSource(final String _name,
116                                        final Instance _instance)
117     {
118         return new OneCSS(_name, _instance);
119     }
120 
121     /**
122      * Class represents on stylesheet.
123      */
124     public static class OneCSS
125         extends AbstractStaticSourceCompiler.AbstractSource
126     {
127         /**
128          * @param _name     Name
129          * @param _instance Instance
130          */
131         public OneCSS(final String _name,
132                       final Instance _instance)
133         {
134             super(_name, _instance);
135         }
136     }
137 
138 }