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:
022: /**
023: * <p>Encodes body content.
024: * </p><p>
025: * <strong>Usage:</strong>
026: * Used by {@link org.apache.commons.betwixt.io.BeanWriter} to encode body content before it is written
027: * into the textual output.
028: * This gives flexibility in this stage allowing (for example)
029: * some properties to use character escaping whilst others
030: * use <code>CDATA</code> wrapping.
031: * </p>
032: * <p><strong>Note:</strong> the word <code>encoding</code> here is used
033: * in the sense of escaping a sequence of character data.
034: * </p>
035: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
036: * @since 0.5
037: */
038: public abstract class MixedContentEncodingStrategy {
039:
040: /**
041: * The name of the option used to specify encoding on a per-element
042: * basis is
043: * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
044: */
045: public static final String ENCODING_OPTION_NAME = "org.apache.commons.betwixt.mixed-content-encoding";
046: /** The option value for CDATA */
047: public static final String CDATA_ENCODING = "CDATA";
048:
049: /**
050: * The standard implementation used by Betwixt by default.
051: * The default is to escape as character data unless
052: * the <code>ElementDescriptor</code> contains
053: * an option with name
054: * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
055: * and value <code>CDATA</code>.
056: * This is a singleton.
057: */
058: public static final MixedContentEncodingStrategy DEFAULT = new BaseMixedContentEncodingStrategy() {
059: /**
060: * Encode by escaping character data unless
061: * the <code>ElementDescriptor</code> contains
062: * an option with name
063: * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
064: * and value <code>CDATA</code>.
065: */
066: protected boolean encodeAsCDATA(ElementDescriptor element) {
067: boolean result = false;
068: if (element != null) {
069: String optionValue = element.getOptions().getValue(
070: ENCODING_OPTION_NAME);
071: result = CDATA_ENCODING.equals(optionValue);
072: }
073: return result;
074: }
075: };
076:
077: /**
078: * Encodes element content within a <code>CDATA</code> section.
079: * This is a singleton.
080: */
081: public static final MixedContentEncodingStrategy CDATA = new BaseMixedContentEncodingStrategy() {
082: /**
083: * Always encode by escaping character data.
084: */
085: protected boolean encodeAsCDATA(ElementDescriptor element) {
086: return true;
087: }
088: };
089:
090: /**
091: * Encodes by escaping character data.
092: * This is a singleton.
093: */
094: public static final MixedContentEncodingStrategy ESCAPED_CHARACTERS = new BaseMixedContentEncodingStrategy() {
095: /**
096: * Always encode by escaping character data.
097: */
098: protected boolean encodeAsCDATA(ElementDescriptor element) {
099: return false;
100: }
101: };
102:
103: /**
104: * Encodes the body content into a form suitable for output as
105: * (textual) xml.
106: * @param bodyContent the raw (unescaped) character data, not null
107: * @param element the <code>ElementDescriptor</code> describing the element
108: * whose content is being encoded.
109: * @return the encoded (escaped) character data, not null
110: */
111: public abstract String encode(String bodyContent,
112: ElementDescriptor element);
113: }
|