1 /* 2 * Copyright (C) 2007 The Android Open Source Project 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 17 package org.apache.harmony.xml.dom; 18 19 import org.w3c.dom.CDATASection; 20 import org.w3c.dom.Node; 21 22 /** 23 * Provides a straightforward implementation of the corresponding W3C DOM 24 * interface. The class is used internally only, thus only notable members that 25 * are not in the original interface are documented (the W3C docs are quite 26 * extensive). Hope that's ok. 27 * <p> 28 * Some of the fields may have package visibility, so other classes belonging to 29 * the DOM implementation can easily access them while maintaining the DOM tree 30 * structure. 31 */ 32 public final class CDATASectionImpl extends TextImpl implements CDATASection { 33 CDATASectionImpl(DocumentImpl document, String data)34 public CDATASectionImpl(DocumentImpl document, String data) { 35 super(document, data); 36 } 37 38 @Override getNodeName()39 public String getNodeName() { 40 return "#cdata-section"; 41 } 42 43 @Override getNodeType()44 public short getNodeType() { 45 return Node.CDATA_SECTION_NODE; 46 } 47 48 /** 49 * Splits this CDATA node into parts that do not contain a "]]>" sequence. 50 * Any newly created nodes will be inserted before this node. 51 */ split()52 public void split() { 53 if (!needsSplitting()) { 54 return; 55 } 56 57 Node parent = getParentNode(); 58 String[] parts = getData().split("\\]\\]>"); 59 parent.insertBefore(new CDATASectionImpl(document, parts[0] + "]]"), this); 60 for (int p = 1; p < parts.length - 1; p++) { 61 parent.insertBefore(new CDATASectionImpl(document, ">" + parts[p] + "]]"), this); 62 } 63 setData(">" + parts[parts.length - 1]); 64 } 65 66 /** 67 * Returns true if this CDATA section contains the illegal character 68 * sequence "]]>". Such nodes must be {@link #split} before they are 69 * serialized. 70 */ needsSplitting()71 public boolean needsSplitting() { 72 return buffer.indexOf("]]>") != -1; 73 } 74 75 /** 76 * Replaces this node with a semantically equivalent text node. This node 77 * will be removed from the DOM tree and the new node inserted in its place. 78 * 79 * @return the replacement node. 80 */ replaceWithText()81 public TextImpl replaceWithText() { 82 TextImpl replacement = new TextImpl(document, getData()); 83 parent.insertBefore(replacement, this); 84 parent.removeChild(this); 85 return replacement; 86 } 87 } 88