01: /*
02: * $Id: AutoIdUtils.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.config.spring.parsers.generic;
12:
13: import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
14: import org.mule.util.StringUtils;
15:
16: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
17:
18: import org.w3c.dom.Element;
19:
20: public class AutoIdUtils {
21:
22: public static final String ATTRIBUTE_ID = AbstractMuleBeanDefinitionParser.ATTRIBUTE_ID;
23: public static final String ATTRIBUTE_NAME = AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME;
24: private static final AtomicInteger counter = new AtomicInteger(0);
25: public static final String PREFIX = "org.mule.autogen.";
26:
27: public static boolean blankAttribute(Element element,
28: String attribute) {
29: return StringUtils.isBlank(element.getAttribute(attribute));
30: }
31:
32: public static void ensureUniqueId(Element element, String type) {
33: if (null != element && blankAttribute(element, ATTRIBUTE_ID)) {
34: if (blankAttribute(element, ATTRIBUTE_NAME)) {
35: element.setAttribute(ATTRIBUTE_ID, uniqueValue(PREFIX
36: + type));
37: } else {
38: element.setAttribute(ATTRIBUTE_ID, element
39: .getAttribute(ATTRIBUTE_NAME));
40: }
41: }
42: }
43:
44: public static String getUniqueName(Element element, String type) {
45: if (!blankAttribute(element, ATTRIBUTE_NAME)) {
46: return element.getAttribute(ATTRIBUTE_NAME);
47: } else if (!blankAttribute(element, ATTRIBUTE_ID)) {
48: return element.getAttribute(ATTRIBUTE_ID);
49: } else {
50: return uniqueValue(PREFIX + type);
51: }
52: }
53:
54: public static String uniqueValue(String value) {
55: return value + "." + counter.incrementAndGet();
56: }
57:
58: public static void forceUniqueId(Element element, String type) {
59: if (null != element) {
60: String id = uniqueValue(PREFIX + type);
61: element.setAttribute(ATTRIBUTE_ID, id);
62: element.setAttribute(ATTRIBUTE_NAME, id);
63: }
64: }
65:
66: }
|