commit
dea0ab7a2d
@ -0,0 +1,38 @@ |
|||||||
|
package me.ag2s.epublib.domain; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
|
||||||
|
/** |
||||||
|
* 用于创建epub,添加大文件(如大量图片)时容易OOM,使用LazyResource,避免OOM. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
public class FileResourceProvider implements LazyResourceProvider { |
||||||
|
//需要导入资源的父目录
|
||||||
|
String dir; |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建一个文件夹里面文件夹的LazyResourceProvider,用于LazyResource。 |
||||||
|
* @param dir 文件的目录 |
||||||
|
*/ |
||||||
|
public FileResourceProvider(String dir) { |
||||||
|
this.dir = dir; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建一个文件夹里面文件夹的LazyResourceProvider,用于LazyResource。 |
||||||
|
* @param dirfile 文件夹 |
||||||
|
*/ |
||||||
|
@SuppressWarnings("unused") |
||||||
|
public FileResourceProvider(File dirfile) { |
||||||
|
this.dir = dirfile.getPath(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public InputStream getResourceStream(String href) throws IOException { |
||||||
|
return new FileInputStream(new File(dir, href)); |
||||||
|
} |
||||||
|
} |
@ -1,135 +1,177 @@ |
|||||||
package me.ag2s.epublib.epub; |
package me.ag2s.epublib.epub; |
||||||
|
|
||||||
import me.ag2s.epublib.util.StringUtil; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
|
||||||
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
||||||
import org.w3c.dom.Element; |
import org.w3c.dom.Element; |
||||||
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
||||||
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
||||||
import org.w3c.dom.Text; |
import org.w3c.dom.Text; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import me.ag2s.epublib.util.StringUtil; |
||||||
|
|
||||||
/** |
/** |
||||||
* Utility methods for working with the DOM. |
* Utility methods for working with the DOM. |
||||||
* |
* |
||||||
* @author paul |
* @author paul |
||||||
* |
|
||||||
*/ |
*/ |
||||||
// package
|
// package
|
||||||
class DOMUtil { |
class DOMUtil { |
||||||
|
|
||||||
/** |
/** |
||||||
* First tries to get the attribute value by doing an getAttributeNS on the element, if that gets an empty element it does a getAttribute without namespace. |
* First tries to get the attribute value by doing an getAttributeNS on the element, if that gets an empty element it does a getAttribute without namespace. |
||||||
* |
* |
||||||
* @param element element |
* @param element element |
||||||
* @param namespace namespace |
* @param namespace namespace |
||||||
* @param attribute attribute |
* @param attribute attribute |
||||||
* @return String Attribute |
* @return String Attribute |
||||||
*/ |
*/ |
||||||
public static String getAttribute(Element element, String namespace, |
public static String getAttribute(Element element, String namespace, |
||||||
String attribute) { |
String attribute) { |
||||||
String result = element.getAttributeNS(namespace, attribute); |
String result = element.getAttributeNS(namespace, attribute); |
||||||
if (StringUtil.isEmpty(result)) { |
if (StringUtil.isEmpty(result)) { |
||||||
result = element.getAttribute(attribute); |
result = element.getAttribute(attribute); |
||||||
|
} |
||||||
|
return result; |
||||||
} |
} |
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Gets all descendant elements of the given parentElement with the given namespace and tagname and returns their text child as a list of String. |
* Gets all descendant elements of the given parentElement with the given namespace and tagname and returns their text child as a list of String. |
||||||
* |
* |
||||||
* @param parentElement parentElement |
* @param parentElement parentElement |
||||||
* @param namespace namespace |
* @param namespace namespace |
||||||
* @param tagName tagName |
* @param tagName tagName |
||||||
* @return List<String> |
* @return List<String> |
||||||
*/ |
*/ |
||||||
public static List<String> getElementsTextChild(Element parentElement, |
public static List<String> getElementsTextChild(Element parentElement, |
||||||
String namespace, String tagName) { |
String namespace, String tagName) { |
||||||
NodeList elements = parentElement |
NodeList elements = parentElement |
||||||
.getElementsByTagNameNS(namespace, tagName); |
.getElementsByTagNameNS(namespace, tagName); |
||||||
//ArrayList 初始化时指定长度提高性能
|
//ArrayList 初始化时指定长度提高性能
|
||||||
List<String> result = new ArrayList<>(elements.getLength()); |
List<String> result = new ArrayList<>(elements.getLength()); |
||||||
for (int i = 0; i < elements.getLength(); i++) { |
for (int i = 0; i < elements.getLength(); i++) { |
||||||
result.add(getTextChildrenContent((Element) elements.item(i))); |
result.add(getTextChildrenContent((Element) elements.item(i))); |
||||||
|
} |
||||||
|
return result; |
||||||
} |
} |
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Finds in the current document the first element with the given namespace and elementName and with the given findAttributeName and findAttributeValue. |
* Finds in the current document the first element with the given namespace and elementName and with the given findAttributeName and findAttributeValue. |
||||||
* It then returns the value of the given resultAttributeName. |
* It then returns the value of the given resultAttributeName. |
||||||
* |
* |
||||||
* @param document document |
* @param document document |
||||||
* @param namespace namespace |
* @param namespace namespace |
||||||
* @param elementName elementName |
* @param elementName elementName |
||||||
* @param findAttributeName findAttributeName |
* @param findAttributeName findAttributeName |
||||||
* @param findAttributeValue findAttributeValue |
* @param findAttributeValue findAttributeValue |
||||||
* @param resultAttributeName resultAttributeName |
* @param resultAttributeName resultAttributeName |
||||||
* @return String value |
* @return String value |
||||||
*/ |
*/ |
||||||
public static String getFindAttributeValue(Document document, |
public static String getFindAttributeValue(Document document, |
||||||
String namespace, String elementName, String findAttributeName, |
String namespace, String elementName, String findAttributeName, |
||||||
String findAttributeValue, String resultAttributeName) { |
String findAttributeValue, String resultAttributeName) { |
||||||
NodeList metaTags = document.getElementsByTagNameNS(namespace, elementName); |
NodeList metaTags = document.getElementsByTagNameNS(namespace, elementName); |
||||||
for (int i = 0; i < metaTags.getLength(); i++) { |
for (int i = 0; i < metaTags.getLength(); i++) { |
||||||
Element metaElement = (Element) metaTags.item(i); |
Element metaElement = (Element) metaTags.item(i); |
||||||
if (findAttributeValue |
if (findAttributeValue |
||||||
.equalsIgnoreCase(metaElement.getAttribute(findAttributeName)) |
.equalsIgnoreCase(metaElement.getAttribute(findAttributeName)) |
||||||
&& StringUtil |
&& StringUtil |
||||||
.isNotBlank(metaElement.getAttribute(resultAttributeName))) { |
.isNotBlank(metaElement.getAttribute(resultAttributeName))) { |
||||||
return metaElement.getAttribute(resultAttributeName); |
return metaElement.getAttribute(resultAttributeName); |
||||||
} |
} |
||||||
|
} |
||||||
|
return null; |
||||||
} |
} |
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Gets the first element that is a child of the parentElement and has the given namespace and tagName |
* Gets the first element that is a child of the parentElement and has the given namespace and tagName |
||||||
* |
* |
||||||
* @param parentElement parentElement |
* @param parentElement parentElement |
||||||
* @param namespace namespace |
* @param namespace namespace |
||||||
* @param tagName tagName |
* @param tagName tagName |
||||||
* @return Element |
* @return Element |
||||||
*/ |
*/ |
||||||
public static Element getFirstElementByTagNameNS(Element parentElement, |
public static NodeList getElementsByTagNameNS(Element parentElement, |
||||||
String namespace, String tagName) { |
String namespace, String tagName) { |
||||||
NodeList nodes = parentElement.getElementsByTagNameNS(namespace, tagName); |
NodeList nodes = parentElement.getElementsByTagNameNS(namespace, tagName); |
||||||
if (nodes.getLength() != 0) { |
if (nodes.getLength() != 0) { |
||||||
return (Element) nodes.item(0); |
return nodes; |
||||||
|
} |
||||||
|
nodes = parentElement.getElementsByTagName(tagName); |
||||||
|
if (nodes.getLength() == 0) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
return nodes; |
||||||
} |
} |
||||||
nodes= parentElement.getElementsByTagName(tagName); |
/** |
||||||
if (nodes.getLength()==0){ |
* Gets the first element that is a child of the parentElement and has the given namespace and tagName |
||||||
return null; |
* |
||||||
|
* @param parentElement parentElement |
||||||
|
* @param namespace namespace |
||||||
|
* @param tagName tagName |
||||||
|
* @return Element |
||||||
|
*/ |
||||||
|
public static NodeList getElementsByTagNameNS(Document parentElement, |
||||||
|
String namespace, String tagName) { |
||||||
|
NodeList nodes = parentElement.getElementsByTagNameNS(namespace, tagName); |
||||||
|
if (nodes.getLength() != 0) { |
||||||
|
return nodes; |
||||||
|
} |
||||||
|
nodes = parentElement.getElementsByTagName(tagName); |
||||||
|
if (nodes.getLength() == 0) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
return nodes; |
||||||
} |
} |
||||||
return (Element) nodes.item(0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* The contents of all Text nodes that are children of the given parentElement. |
* Gets the first element that is a child of the parentElement and has the given namespace and tagName |
||||||
* The result is trim()-ed. |
* |
||||||
* |
* @param parentElement parentElement |
||||||
* The reason for this more complicated procedure instead of just returning the data of the firstChild is that |
* @param namespace namespace |
||||||
* when the text is Chinese characters then on Android each Characater is represented in the DOM as |
* @param tagName tagName |
||||||
* an individual Text node. |
* @return Element |
||||||
* |
*/ |
||||||
* @param parentElement parentElement |
public static Element getFirstElementByTagNameNS(Element parentElement, |
||||||
* @return String value |
String namespace, String tagName) { |
||||||
*/ |
NodeList nodes = parentElement.getElementsByTagNameNS(namespace, tagName); |
||||||
public static String getTextChildrenContent(Element parentElement) { |
if (nodes.getLength() != 0) { |
||||||
if (parentElement == null) { |
return (Element) nodes.item(0); |
||||||
return null; |
} |
||||||
|
nodes = parentElement.getElementsByTagName(tagName); |
||||||
|
if (nodes.getLength() == 0) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
return (Element) nodes.item(0); |
||||||
} |
} |
||||||
StringBuilder result = new StringBuilder(); |
|
||||||
NodeList childNodes = parentElement.getChildNodes(); |
/** |
||||||
for (int i = 0; i < childNodes.getLength(); i++) { |
* The contents of all Text nodes that are children of the given parentElement. |
||||||
Node node = childNodes.item(i); |
* The result is trim()-ed. |
||||||
if ((node == null) || |
* <p> |
||||||
(node.getNodeType() != Node.TEXT_NODE)) { |
* The reason for this more complicated procedure instead of just returning the data of the firstChild is that |
||||||
continue; |
* when the text is Chinese characters then on Android each Characater is represented in the DOM as |
||||||
} |
* an individual Text node. |
||||||
result.append(((Text) node).getData()); |
* |
||||||
|
* @param parentElement parentElement |
||||||
|
* @return String value |
||||||
|
*/ |
||||||
|
public static String getTextChildrenContent(Element parentElement) { |
||||||
|
if (parentElement == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
StringBuilder result = new StringBuilder(); |
||||||
|
NodeList childNodes = parentElement.getChildNodes(); |
||||||
|
for (int i = 0; i < childNodes.getLength(); i++) { |
||||||
|
Node node = childNodes.item(i); |
||||||
|
if ((node == null) || |
||||||
|
(node.getNodeType() != Node.TEXT_NODE)) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
result.append(((Text) node).getData()); |
||||||
|
} |
||||||
|
return result.toString().trim(); |
||||||
} |
} |
||||||
return result.toString().trim(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue