View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
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  
17  package org.apache.commons.betwixt.strategy;
18  
19  import org.apache.commons.betwixt.ElementDescriptor;
20  import org.apache.commons.betwixt.XMLUtils;
21  
22  /***
23   * <p>Basic implementation for {@link MixedContentEncodingStrategy} 
24   * supports variations of most common use case.
25   * </p>
26   * <p>This supports subclasses that choose to encode body content
27   * either as a <code>CDATA</code> section or by escaping the characters.
28   * Implementations should override {@link #encodeAsCDATA}
29   * with an appropriate decision algorithm.
30   * </p>
31   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
32   * @since 0.5
33   */
34  public abstract class BaseMixedContentEncodingStrategy
35      extends MixedContentEncodingStrategy {
36  
37      /***
38       * Escapes a sequence of body content.
39       * @param bodyContent the content whose character data should be escaped, 
40       * not null
41       * @return the escaped character data, not null
42       */
43      protected String escapeCharacters(String bodyContent) {
44          return XMLUtils.escapeBodyValue(bodyContent);
45      }
46      
47      /***
48       * Wraps the given content into a CDATA section.
49       * @param bodyContent the content to be encoded into a CDATA
50       * section
51       * @return the content wrapped inside a CDATA section, not null
52       */
53      protected String encodeInCDATA(String bodyContent) {
54          StringBuffer buffer = new StringBuffer(bodyContent);
55          buffer.ensureCapacity(12);
56          XMLUtils.escapeCDATAContent(buffer);
57          return buffer.insert(0, "<![CDATA[").append("]]>").toString();
58      }
59      
60      /***
61       * Encodes the given body content by either escaping the character data
62       * or by encoding within a <code>CDATA</code> section.
63       * The algorithm used to decide whether a particular element's mixed 
64       * should be escaped is delegated to the concrete subclass through
65       * {@link #encodeAsCDATA}
66       * @see org.apache.commons.betwixt.strategy.MixedContentEncodingStrategy#encode(java.lang.String, org.apache.commons.betwixt.ElementDescriptor)
67       */
68      public String encode(String bodyContent, ElementDescriptor element) {
69          if (encodeAsCDATA(element)) {
70              return encodeInCDATA(bodyContent);
71          }
72           
73          return escapeCharacters(bodyContent);
74      }
75  
76      /***
77       * <p>Should the element described by the given 
78       * <code>ElementDescriptor</code> be encoded as a <code>CDATA</code>
79       * section?
80       * </p>
81       * <p><strong>Usage:</strong> subclasses should provide a strategy
82       * to determine whether an element should be encoded using a 
83       * <code>CDATA</code> section.
84       * </p>
85       * 
86       * @param element <code>ElementDescriptor</code>, not null
87       * @return true if the element should be encoded 
88       * as a <code>CDATA</code> section
89       */
90      protected abstract boolean encodeAsCDATA(ElementDescriptor element);
91      
92  }