KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > IdGenerator


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 /*
14  * Created on 14.11.2003
15  *
16  * To change the template for this generated file go to
17  * Window>Preferences>Java>Code Generation>Code and Comments
18  */

19 package com.tonbeller.wcf.utils;
20
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25
26 public class IdGenerator {
27   int counter = 0;
28   public void generate(Node JavaDoc root, String JavaDoc prefix) {
29     if (root.getNodeType() == Node.ELEMENT_NODE) {
30       Element JavaDoc e = (Element JavaDoc) root;
31       String JavaDoc 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 JavaDoc 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