KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > ImportNodeTest


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.svg;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.w3c.dom.DOMImplementation JavaDoc;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NamedNodeMap JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28
29 import org.apache.batik.test.AbstractTest;
30 import org.apache.batik.test.DefaultTestReport;
31 import org.apache.batik.test.TestReport;
32 import org.apache.batik.util.XMLResourceDescriptor;
33
34 /**
35  * This class tests the importNode method.
36  *
37  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
38  * @version $Id: ImportNodeTest.java,v 1.4 2004/08/18 07:16:41 vhardy Exp $
39  */

40 public class ImportNodeTest extends AbstractTest {
41     protected String JavaDoc testFileName;
42     protected String JavaDoc targetId;
43
44     public ImportNodeTest(String JavaDoc file, String JavaDoc id) {
45         testFileName = file;
46         targetId = id;
47     }
48     
49     public TestReport runImpl() throws Exception JavaDoc {
50         String JavaDoc parser =
51             XMLResourceDescriptor.getXMLParserClassName();
52
53         SAXSVGDocumentFactory df = new SAXSVGDocumentFactory(parser);
54
55         File JavaDoc f = (new File JavaDoc(testFileName));
56         URL JavaDoc url = f.toURL();
57         Document JavaDoc doc = df.createDocument(url.toString(),
58                                          url.openStream());
59         
60         Element JavaDoc e = doc.getElementById(targetId);
61
62         if (e == null){
63             DefaultTestReport report = new DefaultTestReport(this);
64             report.setErrorCode("error.get.element.by.id.failed");
65             report.addDescriptionEntry("entry.key.id", targetId);
66             report.setPassed(false);
67             return report;
68         }
69
70         DOMImplementation JavaDoc di = SVGDOMImplementation.getDOMImplementation();
71         Document JavaDoc d = di.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
72                                        "svg", null);
73         
74
75         Element JavaDoc celt = (Element JavaDoc)d.importNode(e, true);
76
77         NamedNodeMap JavaDoc attrs = e.getAttributes();
78
79         for (int i = 0; i < attrs.getLength(); i++) {
80             Node JavaDoc attr = attrs.item(i);
81             String JavaDoc ns = attr.getNamespaceURI();
82             String JavaDoc name = (ns == null)
83                 ? attr.getNodeName()
84                 : attr.getLocalName();
85             String JavaDoc val = attr.getNodeValue();
86             String JavaDoc val2 = celt.getAttributeNS(ns, name);
87             if (!val.equals(val2)) {
88                 DefaultTestReport report = new DefaultTestReport(this);
89                 report.setErrorCode("error.attr.comparison.failed");
90                 report.addDescriptionEntry("entry.attr.name", name);
91                 report.addDescriptionEntry("entry.attr.value1", val);
92                 report.addDescriptionEntry("entry.attr.value2", val2);
93                 report.setPassed(false);
94                 return report;
95             }
96         }
97
98         return reportSuccess();
99     }
100 }
101
Popular Tags