Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 9, 2023
1 parent 5b31137 commit 73f160a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
27 changes: 15 additions & 12 deletions modules/book/src/main/java/me/ag2s/epublib/domain/Resources.java
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -43,6 +45,7 @@ public Resource add(Resource resource) {
fixResourceHref(resource);
fixResourceId(resource);
this.resources.put(resource.getHref(), resource);
resourcesById.put(resource.getId(), resource);
return resource;
}

Expand Down Expand Up @@ -129,12 +132,7 @@ public boolean containsId(String id) {
if (StringUtil.isBlank(id)) {
return false;
}
for (Resource resource : resources.values()) {
if (id.equals(resource.getId())) {
return true;
}
}
return false;
return resourcesById.containsKey(id);
}

/**
Expand All @@ -147,12 +145,7 @@ public Resource getById(String id) {
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) {
Expand Down Expand Up @@ -267,6 +260,7 @@ public boolean containsByHref(String href) {
*/
public void set(Collection<Resource> resources) {
this.resources.clear();
resourcesById.clear();
addAll(resources);
}

Expand All @@ -279,6 +273,7 @@ public void addAll(Collection<Resource> resources) {
for (Resource resource : resources) {
fixResourceHref(resource);
this.resources.put(resource.getHref(), resource);
resourcesById.put(resource.getId(), resource);
}
}

Expand All @@ -289,6 +284,10 @@ public void addAll(Collection<Resource> resources) {
*/
public void set(Map<String, Resource> resources) {
this.resources = new HashMap<>(resources);
resourcesById.clear();
for (Resource resource : resources.values()) {
resourcesById.put(resource.getId(), resource);
}
}


Expand Down Expand Up @@ -321,6 +320,10 @@ public Resource getByHref(String href) {
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ static TOCReference readTOCReference(Element navpointElement, EpubBook book) {
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);
Expand Down
14 changes: 14 additions & 0 deletions modules/book/src/main/java/me/ag2s/epublib/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ public static boolean endsWithIgnoreCase(String source, String suffix) {
.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.
*
Expand Down

0 comments on commit 73f160a

Please sign in to comment.