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