diff --git a/modules/book/src/main/java/me/ag2s/epublib/domain/Resources.java b/modules/book/src/main/java/me/ag2s/epublib/domain/Resources.java index 519156061..33cda3253 100644 --- a/modules/book/src/main/java/me/ag2s/epublib/domain/Resources.java +++ b/modules/book/src/main/java/me/ag2s/epublib/domain/Resources.java @@ -31,6 +31,8 @@ public class Resources implements Serializable { private Map resources = new HashMap<>(); + private Map resourcesById = new HashMap<>(); + /** * Adds a resource to the resources. *

@@ -43,6 +45,7 @@ public class Resources implements Serializable { fixResourceHref(resource); fixResourceId(resource); this.resources.put(resource.getHref(), resource); + resourcesById.put(resource.getId(), resource); return resource; } @@ -129,12 +132,7 @@ public class Resources implements Serializable { if (StringUtil.isBlank(id)) { return false; } - for (Resource resource : resources.values()) { - if (id.equals(resource.getId())) { - return true; - } - } - return false; + return resourcesById.containsKey(id); } /** @@ -147,12 +145,7 @@ public class Resources implements Serializable { if (StringUtil.isBlank(id)) { return null; } - for (Resource resource : resources.values()) { - if (id.equals(resource.getId())) { - return resource; - } - } - return null; + return resourcesById.get(id); } public Resource getByProperties(String properties) { @@ -267,6 +260,7 @@ public class Resources implements Serializable { */ public void set(Collection resources) { this.resources.clear(); + resourcesById.clear(); addAll(resources); } @@ -279,6 +273,7 @@ public class Resources implements Serializable { for (Resource resource : resources) { fixResourceHref(resource); this.resources.put(resource.getHref(), resource); + resourcesById.put(resource.getId(), resource); } } @@ -289,6 +284,10 @@ public class Resources implements Serializable { */ public void set(Map resources) { this.resources = new HashMap<>(resources); + resourcesById.clear(); + for (Resource resource : resources.values()) { + resourcesById.put(resource.getId(), resource); + } } @@ -321,6 +320,10 @@ public class Resources implements Serializable { } href = StringUtil.substringBefore(href, Constants.FRAGMENT_SEPARATOR_CHAR); + if (!StringUtil.startsWithIgnoreCase(href, "data")) { + return resources.get(href); + } + Matcher dataUriMatcher = dataUriRegex.matcher(href); if (dataUriMatcher.find()) { String dataUriMediaTypeString = dataUriMatcher.group(1); diff --git a/modules/book/src/main/java/me/ag2s/epublib/epub/NCXDocumentV2.java b/modules/book/src/main/java/me/ag2s/epublib/epub/NCXDocumentV2.java index 982c2e8eb..1a0bbcb91 100644 --- a/modules/book/src/main/java/me/ag2s/epublib/epub/NCXDocumentV2.java +++ b/modules/book/src/main/java/me/ag2s/epublib/epub/NCXDocumentV2.java @@ -148,9 +148,9 @@ public class NCXDocumentV2 { if (resource == null) { Log.e(TAG, "Resource with href " + href + " in NCX document not found"); } - Log.v(TAG, "label:" + label); - Log.v(TAG, "href:" + href); - Log.v(TAG, "fragmentId:" + fragmentId); + //Log.v(TAG, "label:" + label); + //Log.v(TAG, "href:" + href); + //Log.v(TAG, "fragmentId:" + fragmentId); TOCReference result = new TOCReference(label, resource, fragmentId); List childTOCReferences = readTOCReferences( navpointElement.getChildNodes(), book); diff --git a/modules/book/src/main/java/me/ag2s/epublib/util/StringUtil.java b/modules/book/src/main/java/me/ag2s/epublib/util/StringUtil.java index e9fff3136..4ab1762df 100644 --- a/modules/book/src/main/java/me/ag2s/epublib/util/StringUtil.java +++ b/modules/book/src/main/java/me/ag2s/epublib/util/StringUtil.java @@ -111,6 +111,20 @@ public class StringUtil { .toLowerCase().endsWith(suffix.toLowerCase()); } + public static boolean startsWithIgnoreCase(String source, String prefix) { + if (isEmpty(prefix)) { + return true; + } + if (isEmpty(source)) { + return false; + } + if (prefix.length() > source.length()) { + return false; + } + return source.substring(0, prefix.length()) + .toLowerCase().startsWith(prefix.toLowerCase()); + } + /** * If the given text is null return "", the original text otherwise. *