001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.betwixt.strategy;
019
020 import org.apache.commons.betwixt.ElementDescriptor;
021 import org.apache.commons.betwixt.XMLUtils;
022
023 /**
024 * <p>Basic implementation for {@link MixedContentEncodingStrategy}
025 * supports variations of most common use case.
026 * </p>
027 * <p>This supports subclasses that choose to encode body content
028 * either as a <code>CDATA</code> section or by escaping the characters.
029 * Implementations should override {@link #encodeAsCDATA}
030 * with an appropriate decision algorithm.
031 * </p>
032 * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
033 * @since 0.5
034 */
035 public abstract class BaseMixedContentEncodingStrategy
036 extends MixedContentEncodingStrategy {
037
038 /**
039 * Escapes a sequence of body content.
040 * @param bodyContent the content whose character data should be escaped,
041 * not null
042 * @return the escaped character data, not null
043 */
044 protected String escapeCharacters(String bodyContent) {
045 return XMLUtils.escapeBodyValue(bodyContent);
046 }
047
048 /**
049 * Wraps the given content into a CDATA section.
050 * @param bodyContent the content to be encoded into a CDATA
051 * section
052 * @return the content wrapped inside a CDATA section, not null
053 */
054 protected String encodeInCDATA(String bodyContent) {
055 StringBuffer buffer = new StringBuffer(bodyContent);
056 buffer.ensureCapacity(12);
057 XMLUtils.escapeCDATAContent(buffer);
058 return buffer.insert(0, "<![CDATA[").append("]]>").toString();
059 }
060
061 /**
062 * Encodes the given body content by either escaping the character data
063 * or by encoding within a <code>CDATA</code> section.
064 * The algorithm used to decide whether a particular element's mixed
065 * should be escaped is delegated to the concrete subclass through
066 * {@link #encodeAsCDATA}
067 * @see org.apache.commons.betwixt.strategy.MixedContentEncodingStrategy#encode(java.lang.String, org.apache.commons.betwixt.ElementDescriptor)
068 */
069 public String encode(String bodyContent, ElementDescriptor element) {
070 if (encodeAsCDATA(element)) {
071 return encodeInCDATA(bodyContent);
072 }
073
074 return escapeCharacters(bodyContent);
075 }
076
077 /**
078 * <p>Should the element described by the given
079 * <code>ElementDescriptor</code> be encoded as a <code>CDATA</code>
080 * section?
081 * </p>
082 * <p><strong>Usage:</strong> subclasses should provide a strategy
083 * to determine whether an element should be encoded using a
084 * <code>CDATA</code> section.
085 * </p>
086 *
087 * @param element <code>ElementDescriptor</code>, not null
088 * @return true if the element should be encoded
089 * as a <code>CDATA</code> section
090 */
091 protected abstract boolean encodeAsCDATA(ElementDescriptor element);
092
093 }