185 lines
4.8 KiB
Java
185 lines
4.8 KiB
Java
package w2phtml.rdf;
|
|
|
|
import java.io.StringWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import javax.xml.transform.OutputKeys;
|
|
import javax.xml.transform.Result;
|
|
import javax.xml.transform.Transformer;
|
|
import javax.xml.transform.TransformerConfigurationException;
|
|
import javax.xml.transform.TransformerException;
|
|
import javax.xml.transform.TransformerFactory;
|
|
import javax.xml.transform.dom.DOMSource;
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.NamedNodeMap;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
import org.w3c.dom.ls.DOMImplementationLS;
|
|
import org.w3c.dom.ls.LSSerializer;
|
|
|
|
import w2phtml.util.Misc;
|
|
import w2phtml.xhtml.XhtmlDocument;
|
|
|
|
public class DocumentPart {
|
|
|
|
private static final String DC_TITLE = "dc.Title";
|
|
private XhtmlDocument excerptDoc;
|
|
private String path;
|
|
private String itemNumber;
|
|
private String body;
|
|
private String parentPath;
|
|
private String name = null;
|
|
private String order = null;
|
|
private Metadata metadata = null;
|
|
|
|
public DocumentPart(XhtmlDocument document,Metadata metadata) {
|
|
this.excerptDoc = document;
|
|
this.metadata = metadata;
|
|
extractPath();
|
|
extractName();
|
|
extractNumber();
|
|
extractBody();
|
|
calculateParentPath();
|
|
extractAnnotationMetadata();
|
|
}
|
|
|
|
public DocumentPart(String path, Metadata metadata) {
|
|
this.path = path;
|
|
this.metadata = metadata;
|
|
this.name = "";
|
|
extractNumber();
|
|
this.body = "";
|
|
calculateParentPath();
|
|
}
|
|
|
|
private void extractAnnotationMetadata() {
|
|
if (excerptDoc == null) {
|
|
System.out.println("Error. Excerpt doc is null!");
|
|
return;
|
|
}
|
|
Element head = excerptDoc.getHeadNode();
|
|
if (head == null) {
|
|
System.out.println("Error. Head node is null!");
|
|
return;
|
|
}
|
|
NodeList metas = head.getElementsByTagName("meta");
|
|
for (int i = 0; i < metas.getLength();i++) {
|
|
Node meta = metas.item(i);
|
|
if (Misc.isElement(meta)) {
|
|
Element metaElements = (Element) meta;
|
|
if (metaElements.hasAttribute("name") && metaElements.hasAttribute("content")) {
|
|
String metaName = metaElements.getAttribute("name");
|
|
String metaValue = metaElements.getAttribute("content");
|
|
metadata.addMetadata(order, metaName, metaValue);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void extractName() {
|
|
Element excerptContentNode = excerptDoc.getContentNode();
|
|
this.name = excerptContentNode.getAttribute("name");
|
|
}
|
|
private void extractNumber() {
|
|
itemNumber = path.replaceAll("([0-9]+ )+", "");
|
|
}
|
|
public String getPath() {
|
|
return path;
|
|
}
|
|
public String getSafePath(){
|
|
return path.replaceAll(" ","_");
|
|
}
|
|
public String getNumber() {
|
|
return itemNumber;
|
|
}
|
|
public String getBody() {
|
|
return body;
|
|
}
|
|
public void setOrder(String order) {
|
|
this.order = order;
|
|
}
|
|
public String getOrder() {
|
|
return order;
|
|
}
|
|
public String getParentPath() {
|
|
return parentPath;
|
|
}
|
|
public boolean isMasterPart() {
|
|
if (path.isEmpty()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public String getName() {
|
|
if (metadata != null && order != null) {
|
|
String nameFromMeta = metadata.getFirstValue(order,DC_TITLE);
|
|
if (!nameFromMeta.isEmpty()) {
|
|
return nameFromMeta;
|
|
}
|
|
}
|
|
if (!name.isEmpty()) {
|
|
return name;
|
|
}
|
|
return "No name";
|
|
}
|
|
|
|
private void extractPath() {
|
|
Element excerptContentNode = excerptDoc.getContentNode();
|
|
this.path = excerptContentNode.getAttribute("path");
|
|
}
|
|
|
|
private void calculateParentPath() {
|
|
if(path.length() == 1) {
|
|
parentPath = "";
|
|
} else {
|
|
parentPath = path.replaceAll(" [0-9]+$", "");
|
|
}
|
|
}
|
|
private void extractBody() {
|
|
Element excerptContentNode = excerptDoc.getContentNode();
|
|
StringBuilder bodyBuilder;
|
|
Document document = excerptContentNode.getOwnerDocument();
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
Transformer transformer = null;
|
|
try {
|
|
transformer = transformerFactory.newTransformer();
|
|
transformer.setOutputProperty(OutputKeys.METHOD, "html");
|
|
} catch (TransformerConfigurationException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
|
|
bodyBuilder = new StringBuilder();
|
|
NodeList excerptContentNodes = excerptContentNode.getChildNodes();
|
|
int i = 0;
|
|
while (excerptContentNodes.getLength() > i) {
|
|
Node child = excerptContentNodes.item(i);
|
|
DOMSource source = new DOMSource(child);
|
|
StringWriter sw = new StringWriter();
|
|
Result streamResult = new StreamResult(sw);
|
|
try {
|
|
transformer.transform(source, streamResult );
|
|
} catch (TransformerException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
bodyBuilder.append(sw.toString());
|
|
i++;
|
|
|
|
}
|
|
this.body = bodyBuilder.toString();
|
|
}
|
|
public boolean isEmpty() {
|
|
if (metadata == null && body.isEmpty()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|