On 2001.10.31 13:34 Stephane Hillion wrote: > > From what you said it sounds like it is not possible and batik won't > let me > > do it. > > I could not repeat the npe, it must be a combination of a number of > > factors.
I found out that the npe occurs when I serialize the document to an object stream and then read it back. I have attached the code that displays the two problems.
import java.io.*; import java.util.*; import org.apache.batik.dom.svg.SVGDOMImplementation; import org.apache.batik.dom.util.DOMUtilities; import org.w3c.dom.*; import org.w3c.dom.svg.SVGSVGElement; import org.w3c.dom.svg.SVGDocument; public class DOMImport { static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; public static void main(String[] args) { Document doc = null; try { InputStream is = new FileInputStream("foo.xml"); javax.xml.parsers.DocumentBuilderFactory fact = javax.xml.parsers.DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); doc = fact. newDocumentBuilder().parse(is); } catch (Exception e) { e.printStackTrace(); } Element root = null; root = doc.getDocumentElement(); DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); Document svgDocument = impl.createDocument(svgNS, "svg", null); Element svgRoot = svgDocument.getDocumentElement(); System.out.println("trying with DOMUtilities.importNode()"); try { Document svgDoc1 = getSVGDocument1(root, "xmlns"); SVGSVGElement svg = ((SVGDocument) svgDoc1).getRootElement(); Node newsvg = svgDocument.importNode(svg, true); svgRoot.appendChild(newsvg); } catch (Throwable e) { e.printStackTrace(); } System.out.println("trying with DOMUtilities.importNode()"); try { Document svgDoc1 = getSVGDocument1(root, "xmlns:svg"); SVGSVGElement svg = ((SVGDocument) svgDoc1).getRootElement(); Node newsvg = svgDocument.importNode(svg, true); svgRoot.appendChild(newsvg); } catch (Throwable e) { e.printStackTrace(); } System.out.println("trying with modified importNode()"); try { Document svgDoc2 = getSVGDocument2(root, "xmlns"); SVGSVGElement svg = ((SVGDocument) svgDoc2).getRootElement(); Node newsvg = svgDocument.importNode(svg, true); svgRoot.appendChild(newsvg); } catch (Throwable e) { e.printStackTrace(); } System.out.println("trying with modified importNode()"); try { Document svgDoc2 = getSVGDocument2(root, "xmlns:svg"); SVGSVGElement svg = ((SVGDocument) svgDoc2).getRootElement(); Node newsvg = svgDocument.importNode(svg, true); svgRoot.appendChild(newsvg); } catch (Throwable e) { e.printStackTrace(); } // this is causes an npe when importing the node try { Document svgDoc2 = getSVGDocument2(root, "xmlns"); ObjectOutputStream tempstream = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("temp.ser"))); tempstream.writeObject(svgDoc2); tempstream.close(); File temp = new File("temp.ser"); System.out.println("page serialized to: " + temp.length()); temp = null; ObjectInputStream in = new ObjectInputStream( new BufferedInputStream(new FileInputStream("temp.ser"))); svgDoc2 = (Document) in.readObject(); in.close(); SVGSVGElement svg = ((SVGDocument) svgDoc2).getRootElement(); Element view = svgDocument.createElementNS(svgNS, "svg"); Node newsvg = svgDocument.importNode(svg, true); view.appendChild(newsvg); svgRoot.appendChild(view); } catch (Throwable e) { e.printStackTrace(); } try { PrintWriter writer = new PrintWriter(System.out); DOMUtilities.writeDocument(svgDocument, writer); writer.flush(); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } static Document getSVGDocument1(Element ele, String attr) { Document doc; NodeList childs = ele.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node obj = childs.item(i); if (obj instanceof Element) { //System.out.println(obj.getNodeName()); Element rootEle = (Element) obj; String space = rootEle.getAttribute(attr); if (svgNS.equals(space)) { try { doc = javax.xml.parsers.DocumentBuilderFactory.newInstance(). newDocumentBuilder().newDocument(); Node node = doc.importNode(obj, true); doc.appendChild(node); DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); // due to namespace problem attributes are not cloned doc = DOMUtilities.deepCloneDocument(doc, impl); return doc; } catch (Exception e) { e.printStackTrace(); } } else { doc = getSVGDocument1(rootEle, attr); if (doc != null) { return doc; } } } } return null; } static Document getSVGDocument2(Element ele, String attr) { Document doc; NodeList childs = ele.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node obj = childs.item(i); if (obj instanceof Element) { //System.out.println(obj.getNodeName()); Element rootEle = (Element) obj; String space = rootEle.getAttribute(attr); if (svgNS.equals(space)) { try { doc = javax.xml.parsers.DocumentBuilderFactory.newInstance(). newDocumentBuilder().newDocument(); Node node = doc.importNode(obj, true); doc.appendChild(node); DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); // due to namespace problem attributes are not cloned doc = deepCloneDocument(doc, impl); return doc; } catch (Exception e) { e.printStackTrace(); } } else { doc = getSVGDocument2(rootEle, attr); if (doc != null) { return doc; } } } } return null; } public static Document deepCloneDocument(Document doc, DOMImplementation impl) { Element root = doc.getDocumentElement(); Document result = impl.createDocument(svgNS, root.getNodeName(), null); Element rroot = result.getDocumentElement(); boolean before = true; for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) { if (n == root) { before = false; if (root.hasAttributes()) { NamedNodeMap attr = root.getAttributes(); int len = attr.getLength(); for (int i = 0; i < len; i++) { rroot.setAttributeNode( (Attr) result.importNode(attr.item(i), true)); } } for (Node c = root.getFirstChild(); c != null; c = c.getNextSibling()) { rroot.appendChild(result.importNode(c, true)); } } else { if (n.getNodeType() != Node.DOCUMENT_TYPE_NODE) { if (before) { result.insertBefore(result.importNode(n, true), rroot); } else { result.appendChild(result.importNode(n, true)); } } } } return result; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]