001// Copyright 2007, 2008, 2009 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.dom;
016
017import java.io.PrintWriter;
018import java.util.Map;
019
020/**
021 * A node that stores parsed character content (CDATA).  For XML documents (as per {@link MarkupModel#isXML()}, this
022 * will be writtens as a CDATA section. For non-XML documents, the content is filtered as it is written out.
023 */
024public class CData extends Node
025{
026    private final String content;
027
028    public CData(Element container, String content)
029    {
030        super(container);
031
032        this.content = content;
033    }
034
035    @Override
036    void toMarkup(Document document, PrintWriter writer, Map<String, String> namespaceURIToPrefix)
037    {
038        MarkupModel model = document.getMarkupModel();
039
040        if (model.isXML())
041        {
042            writer.print("<![CDATA[");
043            writer.print(content);
044            writer.print("]]>");
045            return;
046        }
047
048        // CDATA not supported, so write it normally, with entities escaped.
049
050        writer.print(model.encode(content));
051    }
052}