Hello,
We are integrating Birt for a Project. I need to retrieve all Images existing in rptdesign. Actually I have found and implemented a Solution for finding all Images in Report. But I think it can not ensure to retrieve all Images in Report. Because i'm iterating over grids rows and then cells and Listing recursively. the Problem is that a Image does not have to exist in a Grid. so what happens if a Image is added in a Table or Data. or am I wrong? is there any simple solution for finding all Images in a Report File? Thanks in advance
SlotHandle shBody = reportHandle.getBody();
Iterator allImagesList = shBody.iterator();
while (allImagesList.hasNext()) {
Object obj = allImagesList.next();
if (obj instanceof ImageHandle) {
ImageHandle imgHandle = (ImageHandle) obj;
} else if (obj instanceof GridHandle) {
GridHandle gridHandle = (GridHandle) obj;
parseGrid(gridHandle);
} else if (obj instanceof ListingHandle) {
ListingHandle list = (ListingHandle) obj;
parseList(list);
}
}
private void parseGrid(GridHandle grid) {
SlotHandle grRows = grid.getRows();
Iterator rowIterator = grRows.iterator();
parseRow(rowIterator);
}
private void parseRow(Iterator rowIterator) {
while (rowIterator.hasNext()) {
// Get RowHandle objects.
Object rowSlotContents = rowIterator.next();
System.out.println("Row ID: " + ((RowHandle) rowSlotContents).getID());
// To find the image element, iterate over the grid.
SlotHandle cellSlot
= ((RowHandle) rowSlotContents).getCells();
Iterator cellIterator = cellSlot.iterator();
while (cellIterator.hasNext()) {
// Get a CellHandle object.
Object cellSlotContents = cellIterator.next();
System.out.println("Cell ID: " + ((CellHandle) cellSlotContents).getID());
SlotHandle cellContentSlot
= ((CellHandle) cellSlotContents).getContent();
Iterator cellContentIterator
= cellContentSlot.iterator();
while (cellContentIterator.hasNext()) {
// Get a DesignElementHandle object.
Object cellContents
= cellContentIterator.next();
// Check that the element is an image.
if (cellContents instanceof ImageHandle) {
// String imageSource = ((ImageHandle) cellContents).getSource();
ImageHandle ih = (ImageHandle) cellContents;
// System.out.println(ih.getImageName());
// Check that the image has a URI.
//System.out.println("Type Expression: " + ih.getExpressionProperty("uri").getStringValue());
if (ih.getSource() != null) {
HashMap<String, Serializable> imageMap = new HashMap<String, Serializable>();
imageMap.put(BirtProperties.IMAGE_ID, ih.getID());
imageMap.put(BirtProperties.IMAGE_HEIGHT, ih.getHeight().getStringValue());
imageMap.put(BirtProperties.IMAGE_WIDTH, ih.getWidth().getStringValue());
imageMap.put(BirtProperties.IMAGE_SOURCE, ih.getSource());
if (ih.getSource().equals("embed") && ih.getImageName() != null) {
imageMap.put(BirtProperties.IMAGE_NAME, ih.getImageName());
imageMap.put(BirtProperties.IMAGE_IDENTIFIER, ih.getImageName());
List allEmbeddedImages = reportHandle.getAllImages();
for (Object embedImageHandle : allEmbeddedImages) {
EmbeddedImageHandle img = (EmbeddedImageHandle) embedImageHandle;
if (img.getName().equals(imageMap.get(BirtProperties.IMAGE_NAME))) {
imageMap.put(BirtProperties.IMAGE_BASE64, img.getData());
imageMap.put(BirtProperties.IMAGE_TYPE, img.getType());
}
}
} else {
imageMap.put(BirtProperties.IMAGE_NAME, ih.getExpressionProperty("uri").getStringValue());
imageMap.put(BirtProperties.IMAGE_IDENTIFIER, ih.getExpressionProperty("uri").getStringValue());
imageMap.put(BirtProperties.IMAGE_BASE64, ih.getExpressionProperty("uri").getStringValue());
}
parameters.put(String.valueOf(ih.getID()), imageMap);
}
} else if (cellContents instanceof GridHandle) {
parseGrid((GridHandle) cellContents);
} else if (cellContents instanceof ListingHandle) {
parseList((ListingHandle) cellContents);
}
}
}
}
}
private void parseList(ListingHandle list) {
SlotHandle grRows = list.getHeader();
Iterator rowIterator = grRows.iterator();
parseRow(rowIterator);
grRows = list.getDetail();
rowIterator = grRows.iterator();
parseRow(rowIterator);
grRows = list.getFooter();
rowIterator = grRows.iterator();
parseRow(rowIterator);
grRows = list.getGroups();
rowIterator = grRows.iterator();
parseRow(rowIterator);
}
[Updated on: Tue, 26 August 2014 11:53]
Report message to a moderator