Commit 6ca2f662 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

glyphs are added to the project in a zip parser

parent 883722f2
......@@ -21,6 +21,7 @@ import org.apache.log4j.Logger;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidClassException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.converter.zip.GlyphZipEntryFile;
import lcsb.mapviewer.converter.zip.ImageZipEntryFile;
import lcsb.mapviewer.converter.zip.LayoutZipEntryFile;
import lcsb.mapviewer.converter.zip.ModelZipEntryFile;
......@@ -122,7 +123,8 @@ public class ComplexZipConverter {
continue;
} else if (zef instanceof ImageZipEntryFile) {
continue;
// imageEntries.add((ImageZipEntryFile) zef);
} else if (zef instanceof GlyphZipEntryFile) {
continue;
} else if (!isIgnoredFile(entry.getName())) {
throw new NotImplementedException("Unknwon entry type: " + zef.getClass());
}
......
package lcsb.mapviewer.converter;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import lcsb.mapviewer.converter.zip.GlyphZipEntryFile;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.map.layout.graphics.Glyph;
/**
* Parser used to extract data about {@link Glyph} from zip file.
*
* @author Piotr Gawron
*
*/
public class GlyphParser {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private final Logger logger = Logger.getLogger(GlyphParser.class);
/**
* Method that parse zip entry and creates a {@link Glyph} from it.
*
* @param entry
* zip entry to parse
* @param zipFile
* original file where zip entry comes from
* @throws IOException
* thrown when there is a problem with zip file
* @throws InvalidGlyphFile
* thrown when the zip file contains invalid data
*/
public Glyph parseGlyph(GlyphZipEntryFile entry, ZipFile zipFile) throws InvalidGlyphFile, IOException {
String filename = FilenameUtils.getName(entry.getFilename());
if (filename.toLowerCase().endsWith("png")) {
InputStream is = zipFile.getInputStream(zipFile.getEntry(entry.getFilename()));
UploadedFileEntry file = new UploadedFileEntry();
file.setFileContent(IOUtils.toByteArray(is));
file.setOriginalFileName(entry.getFilename());
file.setLength(file.getFileContent().length);
Glyph result = new Glyph();
result.setFile(file);
return result;
} else {
throw new InvalidGlyphFile("Unknown file in overview images zip archive: " + filename);
}
}
}
package lcsb.mapviewer.converter;
/**
* Exception thrown when the zip file containing data about
* {@link lcsb.mapviewer.model.map.layout.graphics.Glyph} is invalid.
*
* @author Piotr Gawron
*
*/
public class InvalidGlyphFile extends InvalidInputDataExecption {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*
* @param e
* parent exception (reason)
*/
public InvalidGlyphFile(Exception e) {
super(e);
}
/**
* Default constructor.
*
* @param message
* exception message
* @param e
* parent exception (reason)
*/
public InvalidGlyphFile(String message, Exception e) {
super(message, e);
}
/**
* Default constructor.
*
* @param message
* exception message
*/
public InvalidGlyphFile(String message) {
super(message);
}
}
......@@ -12,6 +12,7 @@ import java.util.zip.ZipFile;
import org.apache.log4j.Logger;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.converter.zip.GlyphZipEntryFile;
import lcsb.mapviewer.converter.zip.ImageZipEntryFile;
import lcsb.mapviewer.converter.zip.LayoutZipEntryFile;
import lcsb.mapviewer.converter.zip.ZipEntryFile;
......@@ -21,13 +22,15 @@ import lcsb.mapviewer.model.map.model.Model;
public class ProjectFactory {
/**
* Deafult class clogger.
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(ProjectFactory.class);
private ComplexZipConverter converter;
private GlyphParser glyphParser = new GlyphParser();
public ProjectFactory(ComplexZipConverter converter) {
this.converter = converter;
}
......@@ -59,6 +62,8 @@ public class ProjectFactory {
ZipEntryFile zef = params.getEntry(entry.getName());
if (zef instanceof ImageZipEntryFile) {
imageEntries.add((ImageZipEntryFile) zef);
} else if (zef instanceof GlyphZipEntryFile) {
project.addGlyph(glyphParser.parseGlyph((GlyphZipEntryFile) zef, zipFile));
} else if (zef instanceof LayoutZipEntryFile) {
project.addLayout(
converter.layoutZipEntryFileToLayout(params, zipFile, entry, (LayoutZipEntryFile) zef, overlayOrder++));
......
package lcsb.mapviewer.converter.zip;
import java.io.IOException;
import java.io.Serializable;
/**
* Structure used to describe a file in a zip archive with single entry about
* {@link lcsb.mapviewer.model.map.layout.graphics.Glyph}.
*
* @author Piotr Gawron
*
*/
public class GlyphZipEntryFile extends ZipEntryFile implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public GlyphZipEntryFile() {
}
/**
* Default constructor.
*
* @param filename
* {@link ZipEntryFile#filename}
* @param inputStream
* input stream with the data for this entry.
* @see #baos
* @throws IOException
* thrown when there is a problem with accessing input stream
*/
public GlyphZipEntryFile(String filename) {
super(filename);
}
}
......@@ -38,6 +38,13 @@ public class ZipEntryFileFactory {
*/
private static final String IMAGES_DIRECTORY = "images/";
/**
* Directory in a zip file where information about
* {@link lcsb.mapviewer.model.map.layout.graphics.Glyph} is stored. These
* entries should be by default transformed into {@link GlyphZipEntryFile}.
*/
private static final String GLYPHS_DIRECTORY = "glyphs/";
/**
* Directory in a zip file where information about
* {@link lcsb.mapviewer.model.map.layout.Layout Layout} is stored. These
......@@ -128,6 +135,9 @@ public class ZipEntryFileFactory {
} else if (directory.equals(IMAGES_DIRECTORY)) {
ImageZipEntryFile result = new ImageZipEntryFile(entry.getName());
return result;
} else if (directory.equals(GLYPHS_DIRECTORY)) {
GlyphZipEntryFile result = new GlyphZipEntryFile(entry.getName());
return result;
} else if (directory.equals(LAYOUT_DIRECTORY)) {
LayoutZipEntryFile result = createLayoutZipEntryFile(entry.getName(), zipFile.getInputStream(entry));
return result;
......
......@@ -10,12 +10,14 @@ import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import lcsb.mapviewer.converter.ComplexZipConverterTest.MockConverter;
import lcsb.mapviewer.converter.zip.ModelZipEntryFile;
import lcsb.mapviewer.converter.zip.ZipEntryFileFactory;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.OverviewImage;
......@@ -24,6 +26,7 @@ import lcsb.mapviewer.model.map.OverviewModelLink;
import lcsb.mapviewer.model.map.model.ModelData;
public class ProjectFactoryTest {
Logger logger = Logger.getLogger(ProjectFactoryTest.class);
@AfterClass
public static void tearDownAfterClass() throws Exception {
......@@ -126,4 +129,36 @@ public class ProjectFactoryTest {
}
@Test
public void testParseGlyphs() throws Exception {
try {
ComplexZipConverter converter = new ComplexZipConverter(MockConverter.class);
ProjectFactory projectFactory = new ProjectFactory(converter);
ComplexZipConverterParams params = new ComplexZipConverterParams();
params.zipFile(new ZipFile("testFiles/complex_with_glyphs.zip"));
params.entry(new ModelZipEntryFile("main.xml", "main", true, false, null));
ZipFile zipFile = new ZipFile("testFiles/complex_with_glyphs.zip");
ZipEntryFileFactory factory = new ZipEntryFileFactory();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
params.entry(factory.createZipEntryFile(entry, zipFile));
}
}
Project project = projectFactory.create(params);
assertNotNull(project);
assertEquals(1, project.getGlyphs().size());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
......@@ -698,4 +698,9 @@ public class Project implements Serializable {
this.glyphs = glyphs;
}
public void addGlyph(Glyph parseGlyph) {
this.glyphs.add(parseGlyph);
parseGlyph.setProject(this);
}
}
......@@ -43,7 +43,7 @@ public class UploadedFileEntry extends FileEntry implements Serializable {
* Constructor that copies data from the parameter.
*
* @param original
* original object from which data will bbe copied
* original object from which data will be copied
*/
public UploadedFileEntry(UploadedFileEntry original) {
super(original);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment