Here you can find the source of generateIds(Node root, String attrName)
Parameter | Description |
---|---|
root | the root element |
attrName | the name of the attribute, e.g. "id" |
public static void generateIds(Node root, String attrName)
//package com.java2s; /*//from w ww . j av a 2 s. co m * ==================================================================== * This software is subject to the terms of the Common Public License * Agreement, available at the following URL: * http://www.opensource.org/licenses/cpl.html . * Copyright (C) 2003-2004 TONBELLER AG. * All Rights Reserved. * You must accept the terms of that agreement to use this software. * ==================================================================== * * */ import java.util.Random; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { static final Random random = new Random(); /** * adds an attribute with random value to all elements, that do not * have an attribute with the specified name. * @param root the root element * @param attrName the name of the attribute, e.g. "id" */ public static void generateIds(Node root, String attrName) { if (root.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) root; String id = e.getAttribute(attrName); if (id == null || id.length() == 0) e.setAttribute(attrName, randomId()); } NodeList list = root.getChildNodes(); int N = list.getLength(); for (int i = 0; i < N; i++) generateIds(list.item(i), attrName); } public static void generateIds(Node root) { generateIds(root, "id"); } public static synchronized String randomId() { return "wcf" + Integer.toHexString(random.nextInt()); } }