001: /* ===========================================================================
002: * $RCSfile: Pk.java,v $
003: * ===========================================================================
004: *
005: * RetroGuard -- an obfuscation package for Java classfiles.
006: *
007: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
008: *
009: * This program can be redistributed and/or modified under the terms of the
010: * Version 2 of the GNU General Public License as published by the Free
011: * Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: */
019:
020: package COM.rl.obf;
021:
022: import java.io.*;
023: import java.lang.reflect.*;
024: import java.util.*;
025: import COM.rl.util.*;
026: import COM.rl.obf.classfile.*;
027:
028: /**
029: * Tree item representing a package.
030: *
031: * @author Mark Welsh
032: */
033: public class Pk extends PkCl {
034: // Constants -------------------------------------------------------------
035:
036: // Fields ----------------------------------------------------------------
037: private Hashtable pks = new Hashtable(); // Owns a list of sub-package levels
038: private String repackageName = null; // Compact name for this package
039:
040: // Class Methods ---------------------------------------------------------
041: /** Create the root entry for a tree. */
042: public static Pk createRoot(ClassTree classTree) {
043: return new Pk(classTree);
044: }
045:
046: // Instance Methods ------------------------------------------------------
047: /** Constructor for default package level. */
048: public Pk(ClassTree classTree) {
049: this (null, "");
050: this .classTree = classTree;
051: }
052:
053: /** Constructor for regular package levels. */
054: public Pk(TreeItem parent, String name) {
055: super (parent, name);
056: if (parent == null && !name.equals("")) {
057: System.err
058: .println("Internal error: only the default package has no parent");
059: } else if (parent != null && name.equals("")) {
060: System.err
061: .println("Internal error: the default package cannot have a parent");
062: }
063: }
064:
065: /** Set the repackage name of the entry. */
066: public void setRepackageName(String repackageName) {
067: this .repackageName = repackageName;
068: }
069:
070: /** Return the repackage name of the entry. */
071: public String getRepackageName() {
072: return this .repackageName;
073: }
074:
075: /** Get a package level by name. */
076: public Pk getPackage(String name) throws Exception {
077: return (Pk) pks.get(name);
078: }
079:
080: /** Get a package level by obfuscated name. */
081: public Pk getObfPackage(String name) throws Exception {
082: for (Enumeration enm = pks.elements(); enm.hasMoreElements();) {
083: Pk pk = (Pk) enm.nextElement();
084: if (name.equals(pk.getOutName())) {
085: return pk;
086: }
087: }
088: return null;
089: }
090:
091: /** Get a package level by obfuscated repackage name. */
092: public Pk getObfRepackage(String name) throws Exception {
093: for (Enumeration enm = pks.elements(); enm.hasMoreElements();) {
094: Pk pk = (Pk) enm.nextElement();
095: if (name.equals(pk.getRepackageName())) {
096: return pk;
097: }
098: Pk sub = pk.getObfRepackage(name);
099: if (sub != null) {
100: return sub;
101: }
102: }
103: return null;
104: }
105:
106: /** Get an Enumeration of packages. */
107: public Enumeration getPackageEnum() throws Exception {
108: return pks.elements();
109: }
110:
111: /** Return number of packages. */
112: public int getPackageCount() {
113: return pks.size();
114: }
115:
116: /** Add a sub-package level. */
117: public Pk addPackage(String name) throws Exception {
118: Pk pk = getPackage(name);
119: if (pk == null) {
120: pk = new Pk(this , name);
121: pks.put(name, pk);
122: }
123: return pk;
124: }
125:
126: /** Add a class. */
127: public Cl addClass(String name, String super Name,
128: String[] interfaceNames, int access) throws Exception {
129: return addClass(false, name, super Name, interfaceNames, access);
130: }
131:
132: /** Add a placeholder class. */
133: public Cl addPlaceholderClass(String name) throws Exception {
134: return addPlaceholderClass(false, name);
135: }
136:
137: /** Generate unique obfuscated names for this namespace. */
138: public void generateNames() throws Exception {
139: super .generateNames();
140: generateNames(pks);
141: }
142:
143: /** Generate unique-across-run obfuscated repackage name. */
144: public void repackageName(NameMaker nm) throws Exception {
145: if (!isFixed()) {
146: setRepackageName(nm.nextName(null));
147: setOutName(getInName());
148: }
149: }
150:
151: /** Construct and return the full obfuscated name of the entry. */
152: public String getFullOutName() {
153: if (getRepackageName() == null) {
154: return super.getFullOutName();
155: } else {
156: return getRepackageName();
157: }
158: }
159: }
|