XlsLegendParser implements Parser { @Override public LegendClass[] parse(InputStream inputStream, boolean isRegional) { Workbook workbook = null; LegendClass[] classes = new LegendClass[0]; try { workbook = Workbook.getWorkbook(inputStream); Sheet sheet; if( isRegional ) { sheet = workbook.getSheet(REGIONAL_LEGEND_SHEET); if(sheet == null) { throw new IllegalArgumentException("Given xls-legend is not regional."); } } else { sheet = workbook.getSheet(GLOBAL_LEGEND_SHEET); } final int valueCol = sheet.findCell(VALUE).getColumn(); final int labelCol = sheet.findCell(LABEL).getColumn(); final int redCol = sheet.findCell(RED).getColumn(); final int blueCol = sheet.findCell(BLUE).getColumn(); final int greenCol = sheet.findCell(GREEN).getColumn(); classes = new LegendClass[ sheet.getRows() - 1 ]; for (int i = 1; i < sheet.getRows(); i++) { final Cell valueCell = sheet.getCell(valueCol, i); if(StringUtils.isNullOrEmpty(valueCell.getContents())) { continue; } final Cell labelCell = sheet.getCell(labelCol, i); final Cell redCell = sheet.getCell(redCol, i); final Cell greenCell = sheet.getCell(greenCol, i); final Cell blueCell = sheet.getCell(blueCol, i); final int value = Integer.parseInt(valueCell.getContents()); final String name = "Class_" + i; final String descr = labelCell.getContents().trim(); final Color color = new Color(Integer.parseInt(redCell.getContents()), Integer.parseInt(greenCell.getContents()), Integer.parseInt(blueCell.getContents())); final LegendClass legendClass = new LegendClass(value, descr, name, color); classes[ i - 1 ] = legendClass; } } catch (BiffException e) { Debug.trace(e); } catch (IOException e) { Debug.trace(e); } finally { if( workbook != null ) { workbook.close(); } } return classes; }  @Override LegendClass[] parse(InputStream inputStream, boolean isRegional);  }
@Test public void testRegionalLegendParsingRegionalSheet() { final InputStream stream = XlsLegendParserTest.class.getResourceAsStream("regional_Legend.xls"); final LegendClass[] regionalClasses = parser.parse(stream, true); assertEquals(41, regionalClasses.length); final LegendClass class1 = new LegendClass(11, "Post-flooding or irrigated croplands (or aquatic)", "Class_1", new Color(170, 240, 240)); assertLegendEquals(class1, regionalClasses[0]); final LegendClass class8 = new LegendClass(21, "Mosaic cropland (50-70%) / grassland or shrubland (20-50%)", "Class_8", new Color(220, 240, 100)); assertLegendEquals(class8, regionalClasses[7]); final LegendClass class41 = new LegendClass(230, "No data (burnt areas, clouds,…)", "Class_41", new Color(0, 0, 0)); assertLegendEquals(class41, regionalClasses[40]); }
@Test public void testRegionalLegendParsingGlobalSheet() { final InputStream stream = XlsLegendParserTest.class.getResourceAsStream("regional_Legend.xls"); final LegendClass[] globalClasses = parser.parse(stream, false); assertEquals(23, globalClasses.length); final LegendClass class1 = new LegendClass(11, "Post-flooding or irrigated croplands (or aquatic)", "Class_1", new Color(170, 240, 240)); assertLegendEquals(class1, globalClasses[0]); final LegendClass class8 = new LegendClass(70, "Closed (>40%) needleleaved evergreen forest (>5m)", "Class_8", new Color(0, 60, 0)); assertLegendEquals(class8, globalClasses[7]); final LegendClass class20 = new LegendClass(200, "Bare areas", "Class_20", new Color(255, 245, 215)); assertLegendEquals(class20, globalClasses[19]); final LegendClass class23 = new LegendClass(230, "No data (burnt areas, clouds,…)", "Class_23", new Color(0, 0, 0)); assertLegendEquals(class23, globalClasses[22]); }
@Test(expected = IllegalArgumentException.class) public void testGlobalLegendParsingRegionalSheet() { final InputStream stream = XlsLegendParserTest.class.getResourceAsStream("global_Legend.xls"); parser.parse(stream, true); }
@Test public void testGlobalLegendParsingGlobalSheet() { final InputStream stream = XlsLegendParserTest.class.getResourceAsStream("global_Legend.xls"); final LegendClass[] globalClasses = parser.parse(stream, false); assertEquals(23, globalClasses.length); final LegendClass class1 = new LegendClass(11, "Post-flooding or irrigated croplands (or aquatic)", "Class_1", new Color(170, 240, 240)); assertLegendEquals(class1, globalClasses[0]); final LegendClass class8 = new LegendClass(70, "Closed (>40%) needleleaved evergreen forest (>5m)", "Class_8", new Color(0, 60, 0)); assertLegendEquals(class8, globalClasses[7]); final LegendClass class20 = new LegendClass(200, "Bare areas", "Class_20", new Color(255, 245, 215)); assertLegendEquals(class20, globalClasses[19]); final LegendClass class23 = new LegendClass(230, "No data (burnt areas, clouds,…)", "Class_23", new Color(0, 0, 0)); assertLegendEquals(class23, globalClasses[22]); }