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> resources = new HashMap<>();
private Map<String, Resource> resourcesById = new HashMap<>();
/** /**
* Adds a resource to the resources. * Adds a resource to the resources.
* <p> * <p>
@ -43,6 +45,7 @@ public class Resources implements Serializable {
fixResourceHref(resource); fixResourceHref(resource);
fixResourceId(resource); fixResourceId(resource);
this.resources.put(resource.getHref(), resource); this.resources.put(resource.getHref(), resource);
resourcesById.put(resource.getId(), resource);
return resource; return resource;
} }
@ -129,12 +132,7 @@ public class Resources implements Serializable {
if (StringUtil.isBlank(id)) { if (StringUtil.isBlank(id)) {
return false; return false;
} }
for (Resource resource : resources.values()) { return resourcesById.containsKey(id);
if (id.equals(resource.getId())) {
return true;
}
}
return false;
} }
/** /**
@ -147,12 +145,7 @@ public class Resources implements Serializable {
if (StringUtil.isBlank(id)) { if (StringUtil.isBlank(id)) {
return null; return null;
} }
for (Resource resource : resources.values()) { return resourcesById.get(id);
if (id.equals(resource.getId())) {
return resource;
}
}
return null;
} }
public Resource getByProperties(String properties) { public Resource getByProperties(String properties) {
@ -267,6 +260,7 @@ public class Resources implements Serializable {
*/ */
public void set(Collection<Resource> resources) { public void set(Collection<Resource> resources) {
this.resources.clear(); this.resources.clear();
resourcesById.clear();
addAll(resources); addAll(resources);
} }
@ -279,6 +273,7 @@ public class Resources implements Serializable {
for (Resource resource : resources) { for (Resource resource : resources) {
fixResourceHref(resource); fixResourceHref(resource);
this.resources.put(resource.getHref(), 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) { public void set(Map<String, Resource> resources) {
this.resources = new HashMap<>(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); href = StringUtil.substringBefore(href, Constants.FRAGMENT_SEPARATOR_CHAR);
if (!StringUtil.startsWithIgnoreCase(href, "data")) {
return resources.get(href);
}
Matcher dataUriMatcher = dataUriRegex.matcher(href); Matcher dataUriMatcher = dataUriRegex.matcher(href);
if (dataUriMatcher.find()) { if (dataUriMatcher.find()) {
String dataUriMediaTypeString = dataUriMatcher.group(1); String dataUriMediaTypeString = dataUriMatcher.group(1);

View File

@ -148,9 +148,9 @@ public class NCXDocumentV2 {
if (resource == null) { if (resource == null) {
Log.e(TAG, "Resource with href " + href + " in NCX document not found"); Log.e(TAG, "Resource with href " + href + " in NCX document not found");
} }
Log.v(TAG, "label:" + label); //Log.v(TAG, "label:" + label);
Log.v(TAG, "href:" + href); //Log.v(TAG, "href:" + href);
Log.v(TAG, "fragmentId:" + fragmentId); //Log.v(TAG, "fragmentId:" + fragmentId);
TOCReference result = new TOCReference(label, resource, fragmentId); TOCReference result = new TOCReference(label, resource, fragmentId);
List<TOCReference> childTOCReferences = readTOCReferences( List<TOCReference> childTOCReferences = readTOCReferences(
navpointElement.getChildNodes(), book); navpointElement.getChildNodes(), book);

View File

@ -111,6 +111,20 @@ public class StringUtil {
.toLowerCase().endsWith(suffix.toLowerCase()); .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. * If the given text is null return "", the original text otherwise.
* *