package folioxml.export.plugins;
import folioxml.config.*;
import folioxml.core.InvalidMarkupException;
import folioxml.export.FileNode;
import folioxml.export.InfobaseSetPlugin;
import folioxml.export.LogStreamProvider;
import folioxml.export.html.RenameImages;
import folioxml.slx.ISlxTokenReader;
import folioxml.slx.SlxRecord;
import folioxml.xml.NodeList;
import folioxml.xml.XmlRecord;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class RenameFiles implements InfobaseSetPlugin {
RenameImages renamer;
ExportLocations loc;
Boolean useHighslide;
@Override
public void beginInfobaseSet(InfobaseSet set, ExportLocations export, LogStreamProvider logs) throws IOException {
renamer = new RenameImages(set, export);
loc = export;
System.out.println("Exporting resources...");
if (useHighslide == null) useHighslide = set.getBool("use_highslide");
if (useHighslide == null) useHighslide = true;
if (useHighslide) {
InputStream zipStream = RenameFiles.class.getResourceAsStream("/highslide.zip");
if (zipStream == null) zipStream = RenameFiles.class.getResourceAsStream("..\\..\\..\\highslide.zip");
if (zipStream == null) throw new IOException("Failed to locate resource highslide.zip");
unzip(zipStream, loc.getLocalPath("highslide", AssetType.Javascript, FolderCreation.CreateParents).toString());
}
}
@Override
public void beginInfobase(InfobaseConfig infobase) throws IOException {
}
@Override
public ISlxTokenReader wrapSlxReader(ISlxTokenReader reader) {
return reader;
}
@Override
public void onSlxRecordParsed(SlxRecord clean_slx) throws InvalidMarkupException, IOException {
}
@Override
public void onRecordTransformed(XmlRecord xr, SlxRecord dirty_slx) throws InvalidMarkupException, IOException {
}
@Override
public FileNode assignFileNode(XmlRecord xr, SlxRecord dirty_slx) throws InvalidMarkupException, IOException {
return null;
}
@Override
public void onRecordComplete(XmlRecord xr, FileNode file) throws InvalidMarkupException, IOException {
if (!xr.isRootRecord()) {
renamer.process(new NodeList(xr), file);
}
}
@Override
public void endInfobase(InfobaseConfig infobase) throws IOException, InvalidMarkupException {
}
@Override
public void endInfobaseSet(InfobaseSet set) throws IOException, InvalidMarkupException {
System.out.println("Exporting asset manifest...");
renamer.ExportAssetInventory();
System.out.println("Copying/converting referenced files...");
renamer.CopyConvertFiles();
}
/**
* Extracts a zip file specified by the zipFilePath to a directory specified by
* destDirectory (will be created if does not exists)
*
* @param destDirectory
* @throws IOException
*/
public static void unzip(InputStream zipStream, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(zipStream);
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
//Extract file
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
}