1 18 package org.apache.batik.dom; 19 20 import org.w3c.dom.*; 21 22 import java.io.*; 23 import java.net.*; 24 import org.apache.batik.dom.util.*; 25 import org.apache.batik.util.*; 26 27 import org.apache.batik.test.*; 28 29 35 public class ReplaceChildTest extends AbstractTest { 36 public static String ERROR_GET_ELEMENT_BY_ID_FAILED 37 = "error.get.element.by.id.failed"; 38 39 public static String ENTRY_KEY_ID 40 = "entry.key.id"; 41 42 protected String testFileName; 43 protected String rootTag; 44 protected String targetId; 45 46 public ReplaceChildTest(String file, 47 String root, 48 String id) { 49 testFileName = file; 50 rootTag = root; 51 targetId = id; 52 } 53 54 public TestReport runImpl() throws Exception { 55 String parser = 56 XMLResourceDescriptor.getXMLParserClassName(); 57 58 DocumentFactory df 59 = new SAXDocumentFactory 60 (GenericDOMImplementation.getDOMImplementation(), parser); 61 62 File f = (new File(testFileName)); 63 URL url = f.toURL(); 64 Document doc = df.createDocument(null, 65 rootTag, 66 url.toString(), 67 url.openStream()); 68 69 70 Element e = doc.getElementById(targetId); 71 72 if (e == null){ 73 DefaultTestReport report = new DefaultTestReport(this); 74 report.setErrorCode(ERROR_GET_ELEMENT_BY_ID_FAILED); 75 report.addDescriptionEntry(ENTRY_KEY_ID, 76 targetId); 77 report.setPassed(false); 78 return report; 79 } 80 81 Element fc = null; 82 for (Node n = e.getFirstChild(); 83 n != null; 84 n = n.getNextSibling()) { 85 if (n.getNodeType() == Node.ELEMENT_NODE) { 86 fc = (Element)n; 87 break; 88 } 89 } 90 Element ne = doc.createElementNS(null, "elt4"); 91 e.replaceChild(ne, fc); 92 93 if (ne.getParentNode() != e || fc.getParentNode() != null) { 94 DefaultTestReport report = new DefaultTestReport(this); 95 report.setErrorCode(TestReport.ERROR_TEST_FAILED); 96 report.setPassed(false); 97 return report; 98 } 99 return reportSuccess(); 100 } 101 } 102 | Popular Tags |