1 13 19 package com.tonbeller.wcf.utils; 20 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 25 26 public class IdGenerator { 27 int counter = 0; 28 public void generate(Node root, String prefix) { 29 if (root.getNodeType() == Node.ELEMENT_NODE) { 30 Element e = (Element ) root; 31 String id = e.getAttribute("id"); 32 if (id == null || id.length() == 0) { 33 id = prefix + counter; 34 e.setAttribute("id", id); 35 counter += 1; 36 } else if (id.startsWith("$id.")) { 37 id = prefix + id.substring(4); 38 e.setAttribute("id", id); 39 } 40 } 41 NodeList list = root.getChildNodes(); 42 int N = list.getLength(); 43 for (int i = 0; i < N; i++) 44 generate(list.item(i), prefix); 45 } 46 } | Popular Tags |