2017-05-17 19:20:42 +02:00
|
|
|
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;
|
2017-05-17 19:20:42 +02:00
|
|
|
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;
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
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
|
2017-05-17 19:20:42 +02:00
|
|
|
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();
|
2017-05-17 19:20:42 +02:00
|
|
|
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);
|
2017-05-17 19:20:42 +02:00
|
|
|
if (metaFile == null) {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("No meta ");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
WoomyMeta meta = WoomyMetaParser.parseMeta(zipFile.getInputStream(metaFile));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
|
|
|
* Currently we will only use the first entry in the metadata.xml
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
if (meta.getEntries().isEmpty()) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
WoomyEntry entry = meta.getEntries().get(0);
|
2017-05-17 19:20:42 +02:00
|
|
|
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);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
} catch (ZipException e) {
|
|
|
|
log.info("Caught Execption : " + e.getMessage());
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
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();
|
2017-05-17 19:20:42 +02:00
|
|
|
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();
|
2017-05-17 19:20:42 +02:00
|
|
|
if (!entry.isDirectory()) {
|
2016-12-12 21:01:12 +01:00
|
|
|
String entryName = entry.getName();
|
|
|
|
Matcher matcher = pattern.matcher(entryName);
|
2017-05-17 19:20:42 +02:00
|
|
|
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];
|
2017-05-17 19:20:42 +02:00
|
|
|
result.put(filename.toLowerCase(Locale.ENGLISH), entry);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2016-12-12 21:01:12 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|