* @return a Map with resources, with their id's as key.
*/
private static Resources readManifest(Document packageDocument, String packageHref,
EpubReader epubReader, Resources resources, Map<String, String> idMapping) {
Element manifestElement = DOMUtil.getFirstElementByTagNameNS(packageDocument.getDocumentElement(), NAMESPACE_OPF, OPFTags.manifest);
Resources result = new Resources();
if(manifestElement == null) {
log.error("Package document does not contain element " + OPFTags.manifest);
return result;
}
NodeList itemElements = manifestElement.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.item);
for(int i = 0; i < itemElements.getLength(); i++) {
Element itemElement = (Element) itemElements.item(i);
String id = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.id);
String href = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.href);
try {
href = URLDecoder.decode(href, Constants.CHARACTER_ENCODING);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage());
}
String mediaTypeName = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.media_type);
Resource resource = resources.remove(href);
if(resource == null) {
log.error("resource with href '" + href + "' not found");
continue;
}
resource.setId(id);
MediaType mediaType = MediatypeService.getMediaTypeByName(mediaTypeName);
if(mediaType != null) {
resource.setMediaType(mediaType);
}
result.add(resource);
idMapping.put(id, resource.getId());
}
return result;
}