001: package net.sf.jdec.settings;
002:
003: import java.util.HashMap;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import net.sf.jdec.commonutil.Objects;
010:
011: /*******************************************************************************
012: * @author swaroop belur
013: */
014:
015: public class VariableSettings {
016:
017: /***
018: * !!! IMP !!!
019: *
020: * When u add a variable check whether it has to be cleaned
021: * up after decompiling a class. In that case add it to reset
022: * method
023: */
024:
025: private Set registeredVariables = new HashSet();
026:
027: private Map prefixOccurences = new HashMap();
028:
029: VariableSettings() {
030: }
031:
032: /**
033: * CALLERS : - Need to check for null return .
034: *
035: * @param packageName
036: * @return
037: */
038: public String getPrefixName(String packageName, String className,
039: String dataType) {
040:
041: if (className != null)
042: className = className.trim();
043: if (dataType != null)
044: dataType = dataType.trim();
045: if (packageName != null)
046: packageName = packageName.trim();
047: Iterator iterator = registeredVariables.iterator();
048: while (iterator.hasNext()) {
049: Variable variable = (Variable) iterator.next();
050:
051: if (!Objects.isEmpty(variable.getPkg())) {
052: if (variable.getPkg().equals(packageName)) {
053: if (variable.getClassName().equals(className)) {
054: return variable.getPrefix();
055: }
056: }
057: } else {
058: if (Objects.isTrue(variable.getDefaultPackage())
059: && packageName == null) {
060: if (variable.getClassName().equals(className)) {
061: return variable.getPrefix();
062: }
063: }
064: }
065:
066: /***
067: * Primitive check
068: */
069: if (Objects.isEmpty(className)) {
070: if (Objects.isPrimitive(dataType)) {
071: if (variable.getClassName().equals(dataType)) {
072: return variable.getPrefix();
073: }
074: }
075: }
076:
077: }
078: if (dataType != null && dataType.indexOf("[]") != -1) {
079: if (className == null)
080: className = "";
081: if (className.indexOf("[]") == -1)
082: className = className + "array";
083: else {
084: className = className.substring(0, className
085: .indexOf("[]"));
086: if (className.length() > 0)
087: className = className.trim();
088: }
089: }
090: if (!Objects.isEmpty(className))
091: return "a" + className;
092: return "a" + dataType;
093: }
094:
095: /***************************************************************************
096: * UI side : Should catch ConfigurationException and show message to user
097: * AND then exit. Console side: Should catch and exit.
098: *
099: * @param v
100: */
101: public void addVariable(Variable v) {
102:
103: if (!Objects.isEmpty(v.getIsPrimitive())
104: && Objects.isTrue(v.getIsPrimitive())) {
105: if (!Objects.isEmpty(v.getPrefix())) {
106: registeredVariables.add(v);
107: return;
108: }
109: }
110:
111: if (Objects.isEmpty(v.getPkg())
112: && (Objects.isEmpty(v.getDefaultPackage()) || !v
113: .getDefaultPackage().trim().equalsIgnoreCase(
114: "true"))) {
115:
116: /*******************************************************************
117: * TODO : add a log statement here that this variable was ignored
118: */
119: return;
120: }
121: if (Objects.isEmpty(v.getClassName())) {
122: /*******************************************************************
123: * TODO : add a log statement here that this variable was ignored
124: */
125: return;
126: }
127: if (Objects.isEmpty(v.getPrefix())) {
128: /*******************************************************************
129: * TODO : add a log statement here that this variable was ignored
130: */
131: return;
132: }
133: if (!Objects.isTrue(v.getIsPrimitive())) {
134: registeredVariables.add(v);
135: }
136: }
137:
138: public void reset() {
139: prefixOccurences = new HashMap();
140: }
141:
142: public int getPrefixOccurence(String prefix) {
143: if (prefixOccurences.get(prefix) != null) {
144: Integer in = (Integer) prefixOccurences.get(prefix);
145: prefixOccurences
146: .put(prefix, new Integer(in.intValue() + 1));
147: } else {
148: prefixOccurences.put(prefix, new Integer(1));
149: }
150: return ((Integer) prefixOccurences.get(prefix)).intValue();
151: }
152: }
|