This commit is contained in:
Horis 2023-12-09 17:50:26 +08:00
parent 5b31137db1
commit 73f160a9d7
3 changed files with 32 additions and 15 deletions

View File

@ -31,6 +31,8 @@ public class Resources implements Serializable {
private Map<String, Resource> resources = new HashMap<>();
private Map<String, Resource> resourcesById = new HashMap<>();
/**
* Adds a resource to the resources.
* <p>
@ -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<Resource> 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<String, Resource> 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);

View File

@ -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<TOCReference> childTOCReferences = readTOCReferences(
navpointElement.getChildNodes(), book);

View File

@ -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.
*