JNUSLib/src/de/mas/wiiu/jnus/implementations/woomy/WoomyParser.java

86 lines
2.9 KiB
Java
Raw Normal View History

package de.mas.wiiu.jnus.implementations.woomy;
2016-12-12 21:01:12 +01:00
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
2016-12-12 21:01:12 +01:00
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import de.mas.wiiu.jnus.Settings;
import de.mas.wiiu.jnus.implementations.woomy.WoomyMeta.WoomyEntry;
import lombok.NonNull;
2016-12-12 21:01:12 +01:00
import lombok.extern.java.Log;
/**
*
* @author Maschell
*
*/
@Log
public final class WoomyParser {
private WoomyParser() {
//
}
public static WoomyInfo createWoomyInfo(File woomyFile) throws IOException, ParserConfigurationException, SAXException {
2016-12-12 21:01:12 +01:00
WoomyInfo result = new WoomyInfo();
if (!woomyFile.exists()) {
2016-12-12 21:01:12 +01:00
log.info("File does not exist." + woomyFile.getAbsolutePath());
return null;
}
try (ZipFile zipFile = new ZipFile(woomyFile)) {
result.setWoomyFile(woomyFile);
ZipEntry metaFile = zipFile.getEntry(Settings.WOOMY_METADATA_FILENAME);
if (metaFile == null) {
2016-12-12 21:01:12 +01:00
log.info("No meta ");
return null;
}
WoomyMeta meta = WoomyMetaParser.parseMeta(zipFile.getInputStream(metaFile));
2016-12-12 21:01:12 +01:00
/**
* Currently we will only use the first entry in the metadata.xml
*/
if (meta.getEntries().isEmpty()) {
2016-12-12 21:01:12 +01:00
return null;
}
WoomyEntry entry = meta.getEntries().get(0);
String regEx = entry.getFolder() + ".*"; // We want all files in the entry fodler
Map<String, ZipEntry> contentFiles = loadFileList(zipFile, regEx);
2016-12-12 21:01:12 +01:00
result.setContentFiles(contentFiles);
} catch (ZipException e) {
log.info("Caught Execption : " + e.getMessage());
2016-12-12 21:01:12 +01:00
}
return result;
}
private static Map<String, ZipEntry> loadFileList(@NonNull ZipFile zipFile, @NonNull String regEx) {
2016-12-12 21:01:12 +01:00
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
Map<String, ZipEntry> result = new HashMap<>();
2016-12-12 21:01:12 +01:00
Pattern pattern = Pattern.compile(regEx);
while (zipEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) zipEntries.nextElement();
if (!entry.isDirectory()) {
2016-12-12 21:01:12 +01:00
String entryName = entry.getName();
Matcher matcher = pattern.matcher(entryName);
if (matcher.matches()) {
String[] tokens = entryName.split("[\\\\|/]"); // We only want the filename!
2016-12-12 21:01:12 +01:00
String filename = tokens[tokens.length - 1];
result.put(filename.toLowerCase(Locale.ENGLISH), entry);
2016-12-12 21:01:12 +01:00
}
}
}
2016-12-12 21:01:12 +01:00
return result;
}
}