本文整理汇总了Java中nl.siegmann.epublib.domain.Resources类的典型用法代码示例。如果您正苦于以下问题:Java Resources类的具体用法?Java Resources怎么用?Java Resources使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Resources类属于nl.siegmann.epublib.domain包,在下文中一共展示了Resources类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readLazyResources
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
private static void readLazyResources(File root, Resources resources, File folder) throws IOException {
String hrefRoot = root.getAbsolutePath() + "/";
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
readLazyResources(root, resources, file);
continue;
}
if (file.getName().equals(".ready")) {
continue;
}
String path = file.getAbsolutePath();
String href = path.replace(hrefRoot, "");
Resource resource = new LazyResource(path, 0, href);
resources.add(resource);
}
}
示例2: parseChm
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
public static Book parseChm(FileObject chmRootDir, String inputHtmlEncoding)
throws IOException, ParserConfigurationException,
XPathExpressionException {
Book result = new Book();
result.getMetadata().addTitle(findTitle(chmRootDir));
FileObject hhcFileObject = findHhcFileObject(chmRootDir);
if(hhcFileObject == null) {
throw new IllegalArgumentException("No index file found in directory " + chmRootDir + ". (Looked for file ending with extension '.hhc'");
}
if(inputHtmlEncoding == null) {
inputHtmlEncoding = DEFAULT_CHM_HTML_INPUT_ENCODING;
}
Resources resources = findResources(chmRootDir, inputHtmlEncoding);
List<TOCReference> tocReferences = HHCParser.parseHhc(hhcFileObject.getContent().getInputStream(), resources);
result.setTableOfContents(new TableOfContents(tocReferences));
result.setResources(resources);
result.generateSpineFromTableOfContents();
return result;
}
示例3: findResources
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
private static Resources findResources(FileObject rootDir, String inputEncoding) throws IOException {
Resources result = new Resources();
FileObject[] allFiles = rootDir.findFiles(new AllFileSelector());
for(int i = 0; i < allFiles.length; i++) {
FileObject file = allFiles[i];
if (file.getType() == FileType.FOLDER) {
continue;
}
MediaType mediaType = MediatypeService.determineMediaType(file.getName().getBaseName());
if(mediaType == null) {
continue;
}
String href = file.getName().toString().substring(rootDir.getName().toString().length() + 1);
byte[] resourceData = IOUtils.toByteArray(file.getContent().getInputStream());
if(mediaType == MediatypeService.XHTML && ! nl.siegmann.epublib.Constants.CHARACTER_ENCODING.equalsIgnoreCase(inputEncoding)) {
resourceData = ResourceUtil.recode(inputEncoding, nl.siegmann.epublib.Constants.CHARACTER_ENCODING, resourceData);
}
Resource fileResource = new Resource(null, resourceData, href, mediaType);
result.add(fileResource);
}
return result;
}
示例4: processUlNode
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
private static List<TOCReference> processUlNode(Node ulNode, Resources resources) {
List<TOCReference> result = new ArrayList<TOCReference>();
NodeList children = ulNode.getChildNodes();
for(int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if(node.getNodeName().equals("li")) {
List<TOCReference> section = processLiNode(node, resources);
result.addAll(section);
} else if(node.getNodeName().equals("ul")) {
List<TOCReference> childTOCReferences = processUlNode(node, resources);
if(result.isEmpty()) {
result = childTOCReferences;
} else {
result.get(result.size() - 1).getChildren().addAll(childTOCReferences);
}
}
}
return result;
}
示例5: processLiNode
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
private static List<TOCReference> processLiNode(Node liNode, Resources resources) {
List<TOCReference> result = new ArrayList<TOCReference>();
NodeList children = liNode.getChildNodes();
for(int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if(node.getNodeName().equals("object")) {
TOCReference section = processObjectNode(node, resources);
if(section != null) {
result.add(section);
}
} else if(node.getNodeName().equals("ul")) {
List<TOCReference> childTOCReferences = processUlNode(node, resources);
if(result.isEmpty()) {
result = childTOCReferences;
} else {
result.get(result.size() - 1).getChildren().addAll(childTOCReferences);
}
}
}
return result;
}
示例6: read
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
public static void read(Resource packageResource, EpubReader epubReader, Book book, Resources resources) throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException {
Document packageDocument = ResourceUtil.getAsDocument(packageResource);
String packageHref = packageResource.getHref();
resources = fixHrefs(packageHref, resources);
readGuide(packageDocument, epubReader, book, resources);
// Books sometimes use non-identifier ids. We map these here to legal ones
Map<String, String> idMapping = new HashMap<String, String>();
resources = readManifest(packageDocument, packageHref, epubReader, resources, idMapping);
book.setResources(resources);
readCover(packageDocument, book);
book.setMetadata(PackageDocumentMetadataReader.readMetadata(packageDocument));
book.setSpine(readSpine(packageDocument, epubReader, book.getResources(), idMapping));
// if we did not find a cover page then we make the first page of the book the cover page
if (book.getCoverPage() == null && book.getSpine().size() > 0) {
book.setCoverPage(book.getSpine().getResource(0));
}
}
示例7: fixHrefs
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
/**
* Strips off the package prefixes up to the href of the packageHref.
*
* Example:
* If the packageHref is "OEBPS/content.opf" then a resource href like "OEBPS/foo/bar.html" will be turned into "foo/bar.html"
*
* @param packageHref
* @param resourcesByHref
* @return The stipped package href
*/
private static Resources fixHrefs(String packageHref,
Resources resourcesByHref) {
int lastSlashPos = packageHref.lastIndexOf('/');
if(lastSlashPos < 0) {
return resourcesByHref;
}
Resources result = new Resources();
for(Resource resource: resourcesByHref.getAll()) {
if(StringUtil.isNotBlank(resource.getHref())
|| resource.getHref().length() > lastSlashPos) {
resource.setHref(resource.getHref().substring(lastSlashPos + 1));
}
result.add(resource);
}
return result;
}
示例8: generateSpineFromResources
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
/**
* Creates a spine out of all resources in the resources.
* The generated spine consists of all XHTML pages in order of their href.
*
* @param resources
* @return a spine created out of all resources in the resources.
*/
private static Spine generateSpineFromResources(Resources resources) {
Spine result = new Spine();
List<String> resourceHrefs = new ArrayList<String>();
resourceHrefs.addAll(resources.getAllHrefs());
Collections.sort(resourceHrefs, String.CASE_INSENSITIVE_ORDER);
for (String resourceHref: resourceHrefs) {
Resource resource = resources.getByHref(resourceHref);
if (resource.getMediaType() == MediatypeService.NCX) {
result.setTocResource(resource);
} else if (resource.getMediaType() == MediatypeService.XHTML) {
result.addSpineReference(new SpineReference(resource));
}
}
return result;
}
示例9: loadResources
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
/**
* Loads all entries from the ZipInputStream as Resources.
*
* Loads the contents of all ZipEntries into memory.
* Is fast, but may lead to memory problems when reading large books on devices with small amounts of memory.
*
* @param zipInputStream
* @param defaultHtmlEncoding
* @return
* @throws IOException
*/
public static Resources loadResources(ZipInputStream zipInputStream, String defaultHtmlEncoding) throws IOException {
Resources result = new Resources();
ZipEntry zipEntry;
do {
// get next valid zipEntry
zipEntry = getNextZipEntry(zipInputStream);
if((zipEntry == null) || (zipEntry == ERROR_ZIP_ENTRY) || zipEntry.isDirectory()) {
continue;
}
// store resource
Resource resource = ResourceUtil.createResource(zipEntry, zipInputStream);
if(resource.getMediaType() == MediatypeService.XHTML) {
resource.setInputEncoding(defaultHtmlEncoding);
}
result.add(resource);
} while(zipEntry != null);
return result;
}
示例10: getPackageResourceHref
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
private String getPackageResourceHref(Resources resources) {
String defaultResult = "OEBPS/content.opf";
String result = defaultResult;
Resource containerResource = resources.remove("META-INF/container.xml");
if(containerResource == null) {
return result;
}
try {
Document document = ResourceUtil.getAsDocument(containerResource);
Element rootFileElement = (Element) ((Element) document.getDocumentElement().getElementsByTagName("rootfiles").item(0)).getElementsByTagName("rootfile").item(0);
result = rootFileElement.getAttribute("full-path");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
if(StringUtil.isBlank(result)) {
result = defaultResult;
}
return result;
}
示例11: parseHhc
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
public static List<TOCReference> parseHhc(InputStream hhcFile, Resources resources) throws IOException, ParserConfigurationException, XPathExpressionException {
HtmlCleaner htmlCleaner = new HtmlCleaner();
CleanerProperties props = htmlCleaner.getProperties();
TagNode node = htmlCleaner.clean(hhcFile);
Document hhcDocument = new DomSerializer(props).createDOM(node);
XPath xpath = XPathFactory.newInstance().newXPath();
Node ulNode = (Node) xpath.evaluate("body/ul", hhcDocument
.getDocumentElement(), XPathConstants.NODE);
List<TOCReference> sections = processUlNode(ulNode, resources);
return sections;
}
示例12: readManifest
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
/**
* Reads the manifest containing the resource ids, hrefs and mediatypes.
*
* @param packageDocument
* @param packageHref
* @param epubReader
* @param book
* @param resourcesByHref
* @return a Map with resources, with their id's as key.
*/
private static Resources readManifest(Document packageDocument, String packageHref,
EpubReader epubReader, Resources resources, Map<String, String> idMapping) {
Element manifestElement = DOMUtil.getFirstElementByTagNameNS(packageDocument.getDocumentElement(), NAMESPACE_OPF, OPFTags.manifest);
Resources result = new Resources();
if(manifestElement == null) {
log.error("Package document does not contain element " + OPFTags.manifest);
return result;
}
NodeList itemElements = manifestElement.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.item);
for(int i = 0; i < itemElements.getLength(); i++) {
Element itemElement = (Element) itemElements.item(i);
String id = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.id);
String href = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.href);
try {
href = URLDecoder.decode(href, Constants.CHARACTER_ENCODING);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage());
}
String mediaTypeName = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.media_type);
Resource resource = resources.remove(href);
if(resource == null) {
log.error("resource with href '" + href + "' not found");
continue;
}
resource.setId(id);
MediaType mediaType = MediatypeService.getMediaTypeByName(mediaTypeName);
if(mediaType != null) {
resource.setMediaType(mediaType);
}
result.add(resource);
idMapping.put(id, resource.getId());
}
return result;
}
示例13: findTableOfContentsResource
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
/**
* The spine tag should contain a 'toc' attribute with as value the resource id of the 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.
*
* @param spineElement
* @param resourcesById
* @return the Resource containing the table of contents
*/
private static Resource findTableOfContentsResource(Element spineElement, Resources resources) {
String tocResourceId = DOMUtil.getAttribute(spineElement, NAMESPACE_OPF, OPFAttributes.toc);
Resource tocResource = null;
if (StringUtil.isNotBlank(tocResourceId)) {
tocResource = resources.getByIdOrHref(tocResourceId);
}
if (tocResource != null) {
return tocResource;
}
for (int i = 0; i < POSSIBLE_NCX_ITEM_IDS.length; i++) {
tocResource = resources.getByIdOrHref(POSSIBLE_NCX_ITEM_IDS[i]);
if (tocResource != null) {
return tocResource;
}
tocResource = resources.getByIdOrHref(POSSIBLE_NCX_ITEM_IDS[i].toUpperCase());
if (tocResource != null) {
return tocResource;
}
}
// get the first resource with the NCX mediatype
tocResource = resources.findFirstResourceByMediaType(MediatypeService.NCX);
if (tocResource == null) {
log.error("Could not find table of contents resource. Tried resource with id '" + tocResourceId + "', " + Constants.DEFAULT_TOC_ID + ", " + Constants.DEFAULT_TOC_ID.toUpperCase() + " and any NCX resource.");
}
return tocResource;
}
示例14: processPackageResource
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
private Resource processPackageResource(String packageResourceHref, Book book, Resources resources) {
Resource packageResource = resources.remove(packageResourceHref);
try {
PackageDocumentReader.read(packageResource, this, book, resources);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return packageResource;
}
示例15: readUncompressedBook
import nl.siegmann.epublib.domain.Resources; //导入依赖的package包/类
static Book readUncompressedBook(File folder) throws IOException {
Resources resources = readLazyResources(folder);
return new EpubReader().readEpub(resources);
}