1 18 package org.apache.batik.dom; 19 20 import java.io.File ; 21 import java.net.URL ; 22 23 import org.apache.batik.dom.util.SAXDocumentFactory; 24 import org.apache.batik.test.AbstractTest; 25 import org.apache.batik.test.DefaultTestReport; 26 import org.apache.batik.test.TestReport; 27 import org.apache.batik.util.XMLResourceDescriptor; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 33 34 40 public class CloneElementTest extends AbstractTest { 41 protected String testFileName; 42 protected String rootTag; 43 protected String targetId; 44 45 public CloneElementTest(String file, String root, String id) { 46 testFileName = file; 47 rootTag = root; 48 targetId = id; 49 } 50 51 public TestReport runImpl() throws Exception { 52 String parser = 53 XMLResourceDescriptor.getXMLParserClassName(); 54 55 SAXDocumentFactory df = 56 new SAXDocumentFactory 57 (GenericDOMImplementation.getDOMImplementation(), parser); 58 59 File f = (new File (testFileName)); 60 URL url = f.toURL(); 61 Document doc = df.createDocument(null, 62 rootTag, 63 url.toString(), 64 url.openStream()); 65 66 Element e = doc.getElementById(targetId); 67 68 if (e == null){ 69 DefaultTestReport report = new DefaultTestReport(this); 70 report.setErrorCode("error.get.element.by.id.failed"); 71 report.addDescriptionEntry("entry.key.id", targetId); 72 report.setPassed(false); 73 return report; 74 } 75 76 Element celt = (Element )e.cloneNode(false); 77 78 NamedNodeMap attrs = e.getAttributes(); 79 80 for (int i = 0; i < attrs.getLength(); i++) { 81 Node attr = attrs.item(i); 82 String ns = attr.getNamespaceURI(); 83 String name = (ns == null) 84 ? attr.getNodeName() 85 : attr.getLocalName(); 86 String val = attr.getNodeValue(); 87 String val2 = celt.getAttributeNS(ns, name); 88 if (!val.equals(val2)) { 89 DefaultTestReport report = new DefaultTestReport(this); 90 report.setErrorCode("error.attr.comparison.failed"); 91 report.addDescriptionEntry("entry.attr.name", name); 92 report.addDescriptionEntry("entry.attr.value1", val); 93 report.addDescriptionEntry("entry.attr.value2", val2); 94 report.setPassed(false); 95 return report; 96 } 97 } 98 99 return reportSuccess(); 100 } 101 } 102 | Popular Tags |