Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 96659 invoked from network); 24 Mar 2004 22:52:22 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 24 Mar 2004 22:52:22 -0000 Received: (qmail 22369 invoked by uid 500); 24 Mar 2004 22:52:06 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 22273 invoked by uid 500); 24 Mar 2004 22:52:05 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 22225 invoked by uid 500); 24 Mar 2004 22:52:05 -0000 Received: (qmail 22210 invoked from network); 24 Mar 2004 22:52:05 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 24 Mar 2004 22:52:05 -0000 Received: (qmail 96535 invoked by uid 1289); 24 Mar 2004 22:52:14 -0000 Date: 24 Mar 2004 22:52:14 -0000 Message-ID: <20040324225214.96534.qmail@minotaur.apache.org> From: rdonkin@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy BaseMixedContentEncodingStrategy.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N rdonkin 2004/03/24 14:52:14 Added: betwixt/src/java/org/apache/commons/betwixt/strategy BaseMixedContentEncodingStrategy.java Log: Basic implementation for mxied content encoding strategy. Revision Changes Path 1.1 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/BaseMixedContentEncodingStrategy.java Index: BaseMixedContentEncodingStrategy.java =================================================================== /* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.betwixt.strategy; import org.apache.commons.betwixt.ElementDescriptor; import org.apache.commons.betwixt.XMLUtils; /** *

Basic implementation for {@link MixedContentEncodingStrategy} * supports variations of most common use case. *

*

This supports subclasses that choose to encode body content * either as a CDATA section or by escaping the characters. * Implementations should override {@link #encodeAsCDATA} * with an appropriate decision algorithm. *

* @author Jakarta Commons Team * @version $Revision: 1.1 $ */ public abstract class BaseMixedContentEncodingStrategy extends MixedContentEncodingStrategy { /** * Escapes a sequence of body content. * @param bodyContent the content whose character data should be escaped, * not null * @return the escaped character data, not null */ protected String escapeCharacters(String bodyContent) { return XMLUtils.escapeBodyValue(bodyContent); } /** * Wraps the given content into a CDATA section. * @param bodyContent the content to be encoded into a CDATA * section * @return the content wrapped inside a CDATA section, not null */ protected String encodeInCDATA(String bodyContent) { StringBuffer buffer = new StringBuffer(bodyContent); buffer.ensureCapacity(12); XMLUtils.escapeCDATAContent(buffer); return buffer.insert(0, "").toString(); } /** * Encodes the given body content by either escaping the character data * or by encoding within a CDATA section. * The algorithm used to decide whether a particular element's mixed * should be escaped is delegated to the concrete subclass through * {@link #encodeAsCDATA} * @see org.apache.commons.betwixt.strategy.MixedContentEncodingStrategy#encode(java.lang.String, org.apache.commons.betwixt.ElementDescriptor) */ public String encode(String bodyContent, ElementDescriptor element) { if (encodeAsCDATA(element)) { return encodeInCDATA(bodyContent); } return escapeCharacters(bodyContent); } /** *

Should the element described by the given * ElementDescriptor be encoded as a CDATA * section? *

*

Usage: subclasses should provide a strategy * to determine whether an element should be encoded using a * CDATA section. *

* * @param element ElementDescriptor, not null * @return true if the element should be encoded * as a CDATA section */ protected abstract boolean encodeAsCDATA(ElementDescriptor element); } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org