Differences between revisions 6 and 7
Revision 6 as of 2009-09-20 23:47:36
Size: 4440
Editor: localhost
Comment: converted to 1.6 markup
Revision 7 as of 2012-06-22 14:17:38
Size: 4452
Editor: drool
Comment: display java code with syntax highlighting
Deletions are marked like this. Additions are marked like this.
Line 28: Line 28:
{{{ {{{#!java
Line 58: Line 58:
{{{ {{{#!java

Using DOM 3 functionality from Java

The latest Batik SVN has support for DOM Level 3 Core and Events. However, JDKs < 1.5 include the DOM 2 interfaces and this can cause problems when trying to use the DOM 3 versions of these same interface files (org.w3c.dom). Though the concrete Batik DOM classes implement the DOM 3 functionality, you won't be able to access those methods through the org.w3c.dom interfaces.

There are two ways to overcome this problem. The first is to install the DOM Level 3 interfaces using the Endorsed Standards Override Mechanism. Copy the file lib/xml-apis-dom3.jar into the endorsed standards override directory and the DOM 3 interfaces will be visible. You can then write code against them (for example, call Document.renameNode directly). However, this will mean that other people cannot run or compile your code unless they have JDK 1.5 or they have also installed the xml-apis-dom3.jar in the same way.

The second method, which requires less messing about with the JDK, is to cast your DOM objects to the concrete Batik DOM objects and call your DOM 3 methods directly on them. The Batik DOM classes are in the org.apache.batik.dom package. The classes named "Abstract*" implement the DOM interfaces, and also contain the DOM 3 methods. The advantage of this method is that for your code to compile and run in others' environments, they need not mess around with the endorsed standards overrides.

An example:

   1   import org.apache.batik.dom.AbstractDocument;
   2   import org.apache.batik.dom.svg.SVGDOMImplementation;
   3   import org.w3c.dom.DOMImplementation;
   4   import org.w3c.dom.Document;
   5   import org.w3c.dom.Element;
   6 
   7   public class C {
   8       public void f() {
   9           // Create a new SVG document
  10           DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  11           Document doc = impl.createDocument("http://www.w3.org/2000/svg", "svg", null);
  12 
  13           // Create a 'g' element and append it to the root 'svg' element
  14           Element e = doc.createElementNS("http://www.w3.org/2000/svg", "g");
  15           doc.getDocumentElement().appendChild(e);
  16 
  17           // Cast the document object to org.apache.batik.dom.AbstractDocument,
  18           // so that DOM 3 methods will be guaranteed to be visible
  19           AbstractDocument document = (AbstractDocument) doc;
  20 
  21           // Now a DOM 3 method can be used
  22           document.renameNode(e, "http://www.w3.org/2000/svg", "text");
  23       }
  24   }

For cases where the DOM Level 3 versions of these interfaces contain constants that you wish to use, the constants have been copied into the Batik DOM classes. For example:

   1   import org.apache.batik.dom.AbstractNode;
   2   import org.apache.batik.dom.svg.SVGDOMImplementation;
   3   import org.w3c.dom.DOMImplementation;
   4   import org.w3c.dom.Document;
   5   import org.w3c.dom.Element;
   6 
   7   public class C {
   8       public void f() {
   9           // Create a new SVG document
  10           DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  11           Document doc = impl.createDocument("http://www.w3.org/2000/svg", "svg", null);
  12 
  13           // Create a 'g' element and append it to the root 'svg' element
  14           Element svg = doc.getDocumentElement();
  15           Element e = doc.createElementNS("http://www.w3.org/2000/svg", "g");
  16           svg.appendChild(e);
  17 
  18           AbstractNode n1 = (AbstractNode) svg;
  19           AbstractNode n2 = (AbstractNode) e;
  20           int position = n1.compareDocumentPosition(n2);
  21           if (position == AbstractNode.DOCUMENT_POSITION_PRECEDING
  22                         | AbstractNode.DOCUMENT_POSITION_CONTAINS) {
  23               System.out.println("The svg element contains the g element.");
  24           } else {
  25               System.out.println("Something is wrong!");
  26           }
  27       }
  28   }

Note that using these org.apache.batik.dom interfaces is only needed for the DOM 3 Core and DOM 3 Events interfaces. There were no earlier versions of the DOM XPath interfaces to conflict with, so these can be used directly (org.w3c.dom.xpath).

Of course, none of this matters if you are just using the DOM 3 functionality in ECMAScript, as the matter of interfaces is hidden from the scripting environment.

UsingDOM3 (last edited 2012-06-22 14:17:38 by drool)