bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/util/LazyNameUtil.java

77 lines
2.9 KiB
Java
Raw Normal View History

2021-03-30 20:01:41 +00:00
package the.bytecode.club.bytecodeviewer.util;
2021-06-28 07:02:44 +00:00
import java.util.HashMap;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
2021-03-30 20:01:41 +00:00
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* Prevents name path collisions by allowing the same name to be used in multiple resource containers.
2021-06-28 07:02:44 +00:00
*
2021-03-30 20:01:41 +00:00
* @author Konloch
*/
2021-06-28 07:02:44 +00:00
public class LazyNameUtil
{
2021-03-30 20:01:41 +00:00
public static boolean SAME_NAME_JAR_WORKSPACE = false;
private static final HashMap<String, SeqAndCount> nameMap = new HashMap<>();
2021-04-12 20:19:12 +00:00
public static void reset() {
2021-03-30 20:01:41 +00:00
nameMap.clear();
}
2021-06-28 07:02:44 +00:00
public static String applyNameChanges(String name)
{
if (nameMap.containsKey(name))
{
if (!SAME_NAME_JAR_WORKSPACE)
2021-03-30 20:01:41 +00:00
SAME_NAME_JAR_WORKSPACE = true;
SeqAndCount seqAndCount = nameMap.get(name);
nameMap.put(name, seqAndCount.incrSeqAndCount());
return FilenameUtils.removeExtension(name) + "#" + seqAndCount.getSeq() + "." + FilenameUtils.getExtension(name);
2021-06-28 07:02:44 +00:00
}
else
{
2021-03-30 20:01:41 +00:00
nameMap.put(name, SeqAndCount.init());
}
return name;
}
2021-06-28 07:02:44 +00:00
public static void removeName(String name)
{
if (StringUtils.isBlank(name))
2021-03-30 20:01:41 +00:00
return;
2021-06-28 07:02:44 +00:00
if (name.contains("#"))
2021-03-30 20:01:41 +00:00
name = name.substring(0, name.indexOf("#")) + name.substring(name.indexOf("."));
SeqAndCount seqAndCount = nameMap.get(name);
2021-06-28 07:02:44 +00:00
if (seqAndCount == null)
2021-03-30 20:01:41 +00:00
return;
// sequence remain the same and decrease the count
// still the count become 1
2021-06-28 07:02:44 +00:00
if (seqAndCount.getCount() == 1)
2021-03-30 20:01:41 +00:00
nameMap.remove(name);
2021-06-28 07:02:44 +00:00
else
2021-03-30 20:01:41 +00:00
nameMap.put(name, seqAndCount.decrCount());
}
}