001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.codegen.builder;
020:
021: import java.util.TreeSet;
022:
023: public class DeclarationHelper {
024: /**
025: * Set of variable names.
026: */
027: private TreeSet m_declarations = new TreeSet();
028:
029: private String m_var_prefix;
030:
031: public DeclarationHelper(String prefix) {
032: m_var_prefix = prefix;
033: }
034:
035: public void addVariable(String varName) {
036: m_declarations.add(varName);
037: }
038:
039: public String getPrefix() {
040: if (m_var_prefix == null)
041: return "";
042: else
043: return m_var_prefix;
044: }
045:
046: public String createVariable(Class compClass, String suggestedName) {
047: String compName = getPrefix()
048: + toJavaVariable(compClass, suggestedName);
049: if (suggestedName == null)
050: suggestedName = "";
051: else
052: suggestedName = suggestedName.trim();
053:
054: if (suggestedName.length() == 0) {
055: String newname = compName + "1";
056: if (m_declarations.contains(newname)) {
057: for (int count = 2; count < 1000; count++) {
058: newname = compName + String.valueOf(count);
059: if (!m_declarations.contains(newname)) {
060: compName = newname;
061: break;
062: }
063: }
064: } else {
065: compName = newname;
066: }
067: } else {
068: if (m_declarations.contains(compName)) {
069: for (int count = 1; count < 1000; count++) {
070: String newname = compName + String.valueOf(count);
071: if (!m_declarations.contains(newname)) {
072: compName = newname;
073: break;
074: }
075: }
076: }
077: }
078: m_declarations.add(compName);
079: return compName;
080: }
081:
082: public static String trimPackage(Class c) {
083: if (c == null)
084: return "";
085: String cname = c.getName();
086: int pos = cname.lastIndexOf('.');
087: if (pos >= 0)
088: cname = cname.substring(pos + 1, cname.length());
089: return cname;
090: }
091:
092: /**
093: * Converts the given variable name to a valid Java variable. If any symbols
094: * are found that are invalid symbols, they are removed.
095: */
096: public static String toJavaVariable(Class compClass, String name) {
097: if (name != null)
098: name = name.trim();
099:
100: if (name == null || name.length() == 0) {
101: name = trimPackage(compClass);
102: name = name.toLowerCase();
103: }
104:
105: StringBuffer sbuff = new StringBuffer();
106: for (int index = 0; index < name.length(); index++) {
107: char c = name.charAt(index);
108: if (index == 0 && !Character.isJavaIdentifierStart(c)) {
109: c = '_';
110: } else if (!Character.isJavaIdentifierPart(c)) {
111: c = '_';
112: }
113: sbuff.append(c);
114: }
115: return sbuff.toString();
116: }
117: }
|