View Javadoc
1   /*
2    *    Copyright 2009-2025 the original author or authors.
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    *       https://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 com.googlecode.htmlcompressor.velocity;
17  
18  import com.googlecode.htmlcompressor.compressor.XmlCompressor;
19  
20  import java.io.IOException;
21  import java.io.StringWriter;
22  import java.io.Writer;
23  
24  import org.apache.velocity.context.InternalContextAdapter;
25  import org.apache.velocity.exception.MethodInvocationException;
26  import org.apache.velocity.exception.TemplateInitException;
27  import org.apache.velocity.runtime.RuntimeServices;
28  import org.apache.velocity.runtime.directive.Directive;
29  import org.apache.velocity.runtime.parser.node.Node;
30  
31  /**
32   * Velocity directive that compresses an XML content within #compressXml ... #end block. Compression parameters are set
33   * by default.
34   *
35   * @see XmlCompressor
36   */
37  public class XmlCompressorDirective extends Directive {
38  
39      /** The Constant xmlCompressor. */
40      private static final XmlCompressor xmlCompressor = new XmlCompressor();
41  
42      @Override
43      public String getName() {
44          return "compressXml";
45      }
46  
47      @Override
48      public int getType() {
49          return BLOCK;
50      }
51  
52      @Override
53      public void init(RuntimeServices rs, InternalContextAdapter context, Node node) throws TemplateInitException {
54          super.init(rs, context, node);
55          log = rs.getLog();
56  
57          // set compressor properties
58          xmlCompressor.setEnabled(rs.getBoolean("userdirective.compressXml.enabled", true));
59          xmlCompressor.setRemoveComments(rs.getBoolean("userdirective.compressXml.removeComments", true));
60          xmlCompressor.setRemoveIntertagSpaces(rs.getBoolean("userdirective.compressXml.removeIntertagSpaces", true));
61      }
62  
63      @Override
64      public boolean render(InternalContextAdapter context, Writer writer, Node node)
65              throws IOException, MethodInvocationException {
66  
67          // render content
68          StringWriter content = new StringWriter();
69          node.jjtGetChild(0).render(context, content);
70  
71          // compress
72          try {
73              writer.write(xmlCompressor.compress(content.toString()));
74          } catch (Exception e) {
75              writer.write(content.toString());
76              String msg = "Failed to compress content: " + content.toString();
77              log.error(msg, e);
78              throw new RuntimeException(msg, e);
79  
80          }
81          return true;
82  
83      }
84  
85  }