|
|
@ -36,13 +36,12 @@ import me.ag2s.epublib.util.StringUtil; |
|
|
|
* Reads the opf package document as defined by namespace http://www.idpf.org/2007/opf
|
|
|
|
* Reads the opf package document as defined by namespace http://www.idpf.org/2007/opf
|
|
|
|
* |
|
|
|
* |
|
|
|
* @author paul |
|
|
|
* @author paul |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
|
|
|
|
|
|
|
|
private static final String TAG= PackageDocumentReader.class.getName(); |
|
|
|
private static final String TAG = PackageDocumentReader.class.getName(); |
|
|
|
private static final String[] POSSIBLE_NCX_ITEM_IDS = new String[]{"toc", |
|
|
|
private static final String[] POSSIBLE_NCX_ITEM_IDS = new String[]{"toc", |
|
|
|
"ncx", "ncxtoc","htmltoc"}; |
|
|
|
"ncx", "ncxtoc", "htmltoc"}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void read( |
|
|
|
public static void read( |
|
|
@ -56,7 +55,7 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
|
|
|
|
|
|
|
|
// Books sometimes use non-identifier ids. We map these here to legal ones
|
|
|
|
// Books sometimes use non-identifier ids. We map these here to legal ones
|
|
|
|
Map<String, String> idMapping = new HashMap<>(); |
|
|
|
Map<String, String> idMapping = new HashMap<>(); |
|
|
|
String version=DOMUtil.getAttribute(packageDocument.getDocumentElement(),PREFIX_OPF,PackageDocumentBase.version); |
|
|
|
String version = DOMUtil.getAttribute(packageDocument.getDocumentElement(), PREFIX_OPF, PackageDocumentBase.version); |
|
|
|
|
|
|
|
|
|
|
|
resources = readManifest(packageDocument, packageHref, epubReader, |
|
|
|
resources = readManifest(packageDocument, packageHref, epubReader, |
|
|
|
resources, idMapping); |
|
|
|
resources, idMapping); |
|
|
@ -119,18 +118,18 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
try { |
|
|
|
try { |
|
|
|
href = URLDecoder.decode(href, Constants.CHARACTER_ENCODING); |
|
|
|
href = URLDecoder.decode(href, Constants.CHARACTER_ENCODING); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
Log.e(TAG,e.getMessage()); |
|
|
|
Log.e(TAG, e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
String mediaTypeName = DOMUtil |
|
|
|
String mediaTypeName = DOMUtil |
|
|
|
.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.media_type); |
|
|
|
.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.media_type); |
|
|
|
Resource resource = resources.remove(href); |
|
|
|
Resource resource = resources.remove(href); |
|
|
|
if (resource == null) { |
|
|
|
if (resource == null) { |
|
|
|
Log.e(TAG,"resource with href '" + href + "' not found"); |
|
|
|
Log.e(TAG, "resource with href '" + href + "' not found"); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
resource.setId(id); |
|
|
|
resource.setId(id); |
|
|
|
//for epub3
|
|
|
|
//for epub3
|
|
|
|
String properties=DOMUtil.getAttribute(itemElement,NAMESPACE_OPF,OPFAttributes.properties); |
|
|
|
String properties = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.properties); |
|
|
|
resource.setProperties(properties); |
|
|
|
resource.setProperties(properties); |
|
|
|
|
|
|
|
|
|
|
|
MediaType mediaType = MediaTypes.getMediaTypeByName(mediaTypeName); |
|
|
|
MediaType mediaType = MediaTypes.getMediaTypeByName(mediaTypeName); |
|
|
@ -175,14 +174,14 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
Resource resource = resources.getByHref(StringUtil |
|
|
|
Resource resource = resources.getByHref(StringUtil |
|
|
|
.substringBefore(resourceHref, Constants.FRAGMENT_SEPARATOR_CHAR)); |
|
|
|
.substringBefore(resourceHref, Constants.FRAGMENT_SEPARATOR_CHAR)); |
|
|
|
if (resource == null) { |
|
|
|
if (resource == null) { |
|
|
|
Log.e(TAG,"Guide is referencing resource with href " + resourceHref |
|
|
|
Log.e(TAG, "Guide is referencing resource with href " + resourceHref |
|
|
|
+ " which could not be found"); |
|
|
|
+ " which could not be found"); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
String type = DOMUtil |
|
|
|
String type = DOMUtil |
|
|
|
.getAttribute(referenceElement, NAMESPACE_OPF, OPFAttributes.type); |
|
|
|
.getAttribute(referenceElement, NAMESPACE_OPF, OPFAttributes.type); |
|
|
|
if (StringUtil.isBlank(type)) { |
|
|
|
if (StringUtil.isBlank(type)) { |
|
|
|
Log.e(TAG,"Guide is referencing resource with href " + resourceHref |
|
|
|
Log.e(TAG, "Guide is referencing resource with href " + resourceHref |
|
|
|
+ " which is missing the 'type' attribute"); |
|
|
|
+ " which is missing the 'type' attribute"); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
@ -201,7 +200,7 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Strips off the package prefixes up to the href of the packageHref. |
|
|
|
* Strips off the package prefixes up to the href of the packageHref. |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* Example: |
|
|
|
* Example: |
|
|
|
* If the packageHref is "OEBPS/content.opf" then a resource href like "OEBPS/foo/bar.html" will be turned into "foo/bar.html" |
|
|
|
* If the packageHref is "OEBPS/content.opf" then a resource href like "OEBPS/foo/bar.html" will be turned into "foo/bar.html" |
|
|
|
* |
|
|
|
* |
|
|
@ -241,31 +240,32 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
.getFirstElementByTagNameNS(packageDocument.getDocumentElement(), |
|
|
|
.getFirstElementByTagNameNS(packageDocument.getDocumentElement(), |
|
|
|
NAMESPACE_OPF, OPFTags.spine); |
|
|
|
NAMESPACE_OPF, OPFTags.spine); |
|
|
|
if (spineElement == null) { |
|
|
|
if (spineElement == null) { |
|
|
|
Log.e(TAG,"Element " + OPFTags.spine |
|
|
|
Log.e(TAG, "Element " + OPFTags.spine |
|
|
|
+ " not found in package document, generating one automatically"); |
|
|
|
+ " not found in package document, generating one automatically"); |
|
|
|
return generateSpineFromResources(resources); |
|
|
|
return generateSpineFromResources(resources); |
|
|
|
} |
|
|
|
} |
|
|
|
Spine result = new Spine(); |
|
|
|
Spine result = new Spine(); |
|
|
|
String tocResourceId = DOMUtil |
|
|
|
String tocResourceId = DOMUtil.getAttribute(spineElement, NAMESPACE_OPF, OPFAttributes.toc); |
|
|
|
.getAttribute(spineElement, NAMESPACE_OPF, OPFAttributes.toc); |
|
|
|
Log.v(TAG,tocResourceId); |
|
|
|
result |
|
|
|
result.setTocResource(findTableOfContentsResource(tocResourceId, resources)); |
|
|
|
.setTocResource(findTableOfContentsResource(tocResourceId, resources)); |
|
|
|
NodeList spineNodes = DOMUtil.getElementsByTagNameNS(packageDocument, NAMESPACE_OPF, OPFTags.itemref); |
|
|
|
NodeList spineNodes = packageDocument |
|
|
|
if(spineNodes==null){ |
|
|
|
.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.itemref); |
|
|
|
Log.e(TAG,"spineNodes is null"); |
|
|
|
List<SpineReference> spineReferences = new ArrayList<>( |
|
|
|
return result; |
|
|
|
spineNodes.getLength()); |
|
|
|
} |
|
|
|
|
|
|
|
List<SpineReference> spineReferences = new ArrayList<>(spineNodes.getLength()); |
|
|
|
for (int i = 0; i < spineNodes.getLength(); i++) { |
|
|
|
for (int i = 0; i < spineNodes.getLength(); i++) { |
|
|
|
Element spineItem = (Element) spineNodes.item(i); |
|
|
|
Element spineItem = (Element) spineNodes.item(i); |
|
|
|
String itemref = DOMUtil |
|
|
|
String itemref = DOMUtil.getAttribute(spineItem, NAMESPACE_OPF, OPFAttributes.idref); |
|
|
|
.getAttribute(spineItem, NAMESPACE_OPF, OPFAttributes.idref); |
|
|
|
|
|
|
|
if (StringUtil.isBlank(itemref)) { |
|
|
|
if (StringUtil.isBlank(itemref)) { |
|
|
|
Log.e(TAG,"itemref with missing or empty idref"); // XXX
|
|
|
|
Log.e(TAG, "itemref with missing or empty idref"); // XXX
|
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
String id = idMapping.get(itemref); |
|
|
|
String id = idMapping.get(itemref); |
|
|
|
if (id == null) { |
|
|
|
if (id == null) { |
|
|
|
id = itemref; |
|
|
|
id = itemref; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Resource resource = resources.getByIdOrHref(id); |
|
|
|
Resource resource = resources.getByIdOrHref(id); |
|
|
|
if (resource == null) { |
|
|
|
if (resource == null) { |
|
|
|
Log.e(TAG, "resource with id '" + id + "' not found"); |
|
|
|
Log.e(TAG, "resource with id '" + id + "' not found"); |
|
|
@ -308,7 +308,7 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The spine tag should contain a 'toc' attribute with as value the resource id of the table of contents resource. |
|
|
|
* The spine tag should contain a 'toc' attribute with as value the resource id of the table of contents resource. |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* Here we try several ways of finding this table of contents resource. |
|
|
|
* Here we try several ways of finding this table of contents resource. |
|
|
|
* We try the given attribute value, some often-used ones and finally look through all resources for the first resource with the table of contents mimetype. |
|
|
|
* We try the given attribute value, some often-used ones and finally look through all resources for the first resource with the table of contents mimetype. |
|
|
|
* |
|
|
|
* |
|
|
@ -318,7 +318,13 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static Resource findTableOfContentsResource(String tocResourceId, |
|
|
|
static Resource findTableOfContentsResource(String tocResourceId, |
|
|
|
Resources resources) { |
|
|
|
Resources resources) { |
|
|
|
Resource tocResource = null; |
|
|
|
Resource tocResource; |
|
|
|
|
|
|
|
//一些epub3的文件为了兼容epub2,保留的epub2的目录文件,这里优先选择epub3的xml目录
|
|
|
|
|
|
|
|
tocResource = resources.getByProperties("nav"); |
|
|
|
|
|
|
|
if (tocResource != null) { |
|
|
|
|
|
|
|
return tocResource; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (StringUtil.isNotBlank(tocResourceId)) { |
|
|
|
if (StringUtil.isNotBlank(tocResourceId)) { |
|
|
|
tocResource = resources.getByIdOrHref(tocResourceId); |
|
|
|
tocResource = resources.getByIdOrHref(tocResourceId); |
|
|
|
} |
|
|
|
} |
|
|
@ -343,10 +349,7 @@ public class PackageDocumentReader extends PackageDocumentBase { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//For EPUB3
|
|
|
|
|
|
|
|
if (tocResource==null){ |
|
|
|
|
|
|
|
tocResource=resources.getByProperties("nav"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tocResource == null) { |
|
|
|
if (tocResource == null) { |
|
|
|
Log.e(TAG, |
|
|
|
Log.e(TAG, |
|
|
|