1 /*------------------------------------------------------------------------------
2 Name: SvgIdMapper.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Creates an hashtable containing the dynamic svg elements
6 Version: $Id: SvgIdMapper.java 16476 2007-09-06 22:36:52Z laghi $
7 ------------------------------------------------------------------------------*/
8 package javaclients.svg.batik;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12 import org.xmlBlaster.util.XmlBlasterException;
13 import java.util.Hashtable;
14 import java.util.Enumeration;
15 import java.io.File;
16
17 // It would make sense to use DOM2, but with Crimson this is not possible
18 /*
19 import org.w3c.dom.traversal.NodeFilter;
20 import org.w3c.dom.traversal.DocumentTraversal;
21 import org.w3c.dom.traversal.NodeIterator;
22 */
23 import org.w3c.dom.NodeList;
24
25 import org.w3c.dom.Node;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Document;
28
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.DocumentBuilder;
31
32 /**
33 * This class implements the NodeFilter interface to handle only nodes which
34 * are of the type "element" whith an attribute "id" which is not empty.
35 * @author $Author: laghi $ (michele@laghi.eu)
36 */
37 public class SvgIdMapper /*implements NodeFilter*/
38 {
39 private final static String ME = "SvgIdMapper";
40 private static Logger log = Logger.getLogger(SvgIdMapper.class.getName());
41
42 /**
43 * The table containing the pairs
44 */
45 private Hashtable idTable = null;
46
47 public SvgIdMapper ()
48 {
49
50 idTable = new Hashtable();
51 }
52
53
54 /**
55 * does a check to see if the given id string fullfills the requirement
56 * needed to be a dynamic element. At this stage it accepts all id which
57 * start with the prefix 'xmlBlaster.'. For example 'xmlBlaster.rect1'.
58 */
59 public static boolean isDynamic (String id)
60 {
61 if (id == null) return false;
62 if (id.startsWith("xmlBlaster.")) return true;
63 return false;
64 }
65
66
67 /*
68 public short acceptNode(Node node)
69 {
70 log.info(".acceptNode " + node.toString());
71 // this is probably not needed
72 if (!(node instanceof Element)) return FILTER_REJECT;
73 String idText = ((Element)node).getAttribute("id");
74 if ((idText == null) || (idText.length() < 1)) return FILTER_SKIP;
75 // add it to the map ...
76 this.idTable.put(idText, node);
77 return FILTER_ACCEPT;
78 }
79 */
80
81
82 /**
83 * This method can be used with the crimson parser since this has no
84 * transverse implemented.
85 */
86 protected void scanNode (Node node)
87 {
88 log.fine(".scanNode started");
89 if (node instanceof Document) {
90 node = ((Document)node).getDocumentElement();
91 }
92 if (node instanceof Element) {
93 Element el = (Element)node;
94 String idText = el.getAttribute("id");
95 if (isDynamic(idText)) {
96 this.idTable.put(idText, node);
97 log.fine(".scanNode: " + idText);
98 }
99 // scan the child nodes if any
100 NodeList nodeList = el.getChildNodes();
101 if (nodeList != null) {
102 for (int i=0; i < nodeList.getLength(); i++) {
103 scanNode(nodeList.item(i));
104 }
105 }
106 }
107 log.fine(".scanNode ended");
108 }
109
110
111
112 public Hashtable createIdTable (Document document)
113 {
114 log.info("createIdTable");
115 this.idTable = new Hashtable();
116
117 this.scanNode(document);
118 /*
119 NodeIterator nodeIterator = ((DocumentTraversal)document)
120 .createNodeIterator(document, SHOW_ELEMENT, this, true);
121
122 while (nodeIterator.nextNode() != null) {}
123 */
124
125
126 return this.idTable;
127 }
128
129
130
131 public static void main(String[] args)
132 {
133 SvgIdMapper mapper = new SvgIdMapper();
134 try {
135 File file = new File("simple.svg");
136 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
137 Document doc = builder.parse(file);
138 Hashtable idTable = mapper.createIdTable(doc);
139 Enumeration keys = idTable.keys();
140 while (keys.hasMoreElements()) {
141 String key = (String)keys.nextElement();
142 System.out.println(key);
143 }
144 }
145 catch (Exception ex) {
146 System.err.println(ex.toString());
147 }
148 }
149 }
syntax highlighted by Code2HTML, v. 0.9.1