Skip to content

Commit 57dff5b

Browse files
committed
Implementation of a CSS compressor
1 parent 1bf76af commit 57dff5b

4 files changed

Lines changed: 189 additions & 0 deletions

File tree

‎transform/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
<version>1.7R3</version>
3838
</dependency>
3939

40+
<dependency>
41+
<groupId>com.yahoo.platform.yui</groupId>
42+
<artifactId>yuicompressor</artifactId>
43+
<version>2.4.6</version>
44+
</dependency>
45+
4046
<!-- TODO: use ocpsoft commons -->
4147
<dependency>
4248
<groupId>commons-io</groupId>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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+
package org.ocpsoft.rewrite.transform.yui;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.InputStreamReader;
21+
import java.io.OutputStream;
22+
import java.io.OutputStreamWriter;
23+
import java.io.Reader;
24+
import java.nio.charset.Charset;
25+
26+
import org.ocpsoft.rewrite.transform.Transformer;
27+
28+
import com.yahoo.platform.yui.compressor.CssCompressor;
29+
30+
/**
31+
*
32+
* CSS compressor implementation based on yuicompressor
33+
*
34+
* @author Christian Kaltepoth
35+
*
36+
*/
37+
public class CssCompress implements Transformer
38+
{
39+
40+
private final Charset charset;
41+
42+
public CssCompress()
43+
{
44+
this(Charset.forName("UTF8"));
45+
}
46+
47+
public CssCompress(Charset charset)
48+
{
49+
this.charset = charset;
50+
}
51+
52+
@Override
53+
public void transform(InputStream input, OutputStream output) throws IOException
54+
{
55+
56+
// prepare input reader
57+
Reader reader = new InputStreamReader(input, charset);
58+
CssCompressor compressor = new CssCompressor(reader);
59+
60+
// write compressed output
61+
OutputStreamWriter writer = new OutputStreamWriter(output, charset);
62+
compressor.compress(writer, 0);
63+
writer.flush();
64+
65+
}
66+
67+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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+
package org.ocpsoft.rewrite.transform.yui;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.asset.StringAsset;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
28+
import org.ocpsoft.rewrite.test.HttpAction;
29+
import org.ocpsoft.rewrite.test.RewriteTest;
30+
31+
/**
32+
*
33+
* Integration test for {@link CssCompress}.
34+
*
35+
* @author Christian Kaltepoth
36+
*
37+
*/
38+
@RunWith(Arquillian.class)
39+
public class CssCompressTest extends RewriteTest
40+
{
41+
@Deployment(testable = false)
42+
public static WebArchive getDeployment()
43+
{
44+
return RewriteTest.getDeployment()
45+
.addAsWebResource(new StringAsset(".class {\n width : 100px;\n}"), "test.css")
46+
.addAsServiceProvider(ConfigurationProvider.class, CssCompressTestProvider.class);
47+
}
48+
49+
@Test
50+
public void testSimpleLessFileRendering() throws Exception
51+
{
52+
HttpAction<HttpGet> action = get("/test.css");
53+
assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
54+
assertEquals(".class{width:100px}", action.getResponseContent());
55+
}
56+
57+
@Test
58+
public void testNotExistingLessFile() throws Exception
59+
{
60+
HttpAction<HttpGet> action = get("/not-existing.css");
61+
assertEquals(404, action.getResponse().getStatusLine().getStatusCode());
62+
}
63+
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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+
package org.ocpsoft.rewrite.transform.yui;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
23+
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
24+
import org.ocpsoft.rewrite.transform.Transform;
25+
import org.ocpsoft.rewrite.transform.resolve.WebResourceResolver;
26+
27+
/**
28+
*
29+
* {@link ConfigurationProvider} for {@link CssCompressTest}.
30+
*
31+
* @author Christian Kaltepoth
32+
*
33+
*/
34+
public class CssCompressTestProvider extends HttpConfigurationProvider
35+
{
36+
@Override
37+
public int priority()
38+
{
39+
return 0;
40+
}
41+
42+
@Override
43+
public Configuration getConfiguration(final ServletContext context)
44+
{
45+
return ConfigurationBuilder
46+
.begin()
47+
.addRule(Transform.request(".css")
48+
.resolvedBy(WebResourceResolver.identity())
49+
.apply(CssCompress.class));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)