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. /**
  19. * <p>Encodes body content.
  20. * </p><p>
  21. * <strong>Usage:</strong>
  22. * Used by {@link BeanWriter} to encode body content before it is written
  23. * into the textual output.
  24. * This gives flexibility in this stage allowing (for example)
  25. * some properties to use character escaping whilst others
  26. * use <code>CDATA</code> wrapping.
  27. * </p>
  28. * <p><strong>Note:</strong> the word <code>encoding</code> here is used
  29. * in the sense of escaping a sequence of character data.
  30. * </p>
  31. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  32. * @since 0.5
  33. */
  34. public abstract class MixedContentEncodingStrategy {
  35. /**
  36. * The name of the option used to specify encoding on a per-element
  37. * basis is
  38. * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
  39. */
  40. public static final String ENCODING_OPTION_NAME
  41. = "org.apache.commons.betwixt.mixed-content-encoding";
  42. /** The option value for CDATA */
  43. public static final String CDATA_ENCODING = "CDATA";
  44. /**
  45. * The standard implementation used by Betwixt by default.
  46. * The default is to escape as character data unless
  47. * the <code>ElementDescriptor</code> contains
  48. * an option with name
  49. * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
  50. * and value <code>CDATA</code>.
  51. * This is a singleton.
  52. */
  53. public static final MixedContentEncodingStrategy DEFAULT
  54. = new BaseMixedContentEncodingStrategy() {
  55. /**
  56. * Encode by escaping character data unless
  57. * the <code>ElementDescriptor</code> contains
  58. * an option with name
  59. * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
  60. * and value <code>CDATA</code>.
  61. */
  62. protected boolean encodeAsCDATA(ElementDescriptor element) {
  63. boolean result = false;
  64. if (element != null ) {
  65. String optionValue = element.getOptions().getValue(ENCODING_OPTION_NAME);
  66. result = CDATA_ENCODING.equals(optionValue);
  67. }
  68. return result;
  69. }
  70. };
  71. /**
  72. * Encodes element content within a <code>CDATA</code> section.
  73. * This is a singleton.
  74. */
  75. public static final MixedContentEncodingStrategy CDATA
  76. = new BaseMixedContentEncodingStrategy() {
  77. /**
  78. * Always encode by escaping character data.
  79. */
  80. protected boolean encodeAsCDATA(ElementDescriptor element) {
  81. return true;
  82. }
  83. };
  84. /**
  85. * Encodes by escaping character data.
  86. * This is a singleton.
  87. */
  88. public static final MixedContentEncodingStrategy ESCAPED_CHARACTERS
  89. = new BaseMixedContentEncodingStrategy() {
  90. /**
  91. * Always encode by escaping character data.
  92. */
  93. protected boolean encodeAsCDATA(ElementDescriptor element) {
  94. return false;
  95. }
  96. };
  97. /**
  98. * Encodes the body content into a form suitable for output as
  99. * (textual) xml.
  100. * @param bodyContent the raw (unescaped) character data, not null
  101. * @param element the <code>ElementDescriptor</code> describing the element
  102. * whose content is being encoded.
  103. * @return the encoded (escaped) character data, not null
  104. */
  105. public abstract String encode(String bodyContent, ElementDescriptor element);
  106. }