Code is working, but ugly.
This commit is contained in:
parent
525aa8de1d
commit
c13cb3abce
6 changed files with 91 additions and 23 deletions
|
@ -432,6 +432,9 @@ public class OfficeReader {
|
|||
public StyleWithProperties getParStyle(String sName) {
|
||||
return (StyleWithProperties) par.getStyle(sName);
|
||||
}
|
||||
public String cloneParStyle(String sName) {
|
||||
return par.cloneStyle(sName);
|
||||
}
|
||||
public StyleWithProperties getDefaultParStyle() {
|
||||
return (StyleWithProperties) par.getDefaultStyle();
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.w3c.dom.Node;
|
|||
import writer2latex.util.Misc;
|
||||
|
||||
/** <p> Abstract class representing a style in OOo </p> */
|
||||
public abstract class OfficeStyle {
|
||||
public abstract class OfficeStyle implements Cloneable {
|
||||
// These attributes are defined by OfficeStyleFamily upon collection of styles
|
||||
protected String sName;
|
||||
protected OfficeStyleFamily family;
|
||||
|
@ -53,6 +53,18 @@ public abstract class OfficeStyle {
|
|||
public OfficeStyle getParentStyle() {
|
||||
return family.getStyle(sParentName);
|
||||
}
|
||||
|
||||
public OfficeStyle Clone() { return null; }
|
||||
|
||||
public void loadFromStyle(OfficeStyle original) {
|
||||
sDisplayName = original.getDisplayName() + "-clone";
|
||||
sName = original.getName() + "-clone";
|
||||
family = original.family;
|
||||
bAutomatic = original.isAutomatic();
|
||||
sParentName = original.getParentName();
|
||||
sListStyleName = original.getListStyleName();
|
||||
sMasterPageName = original.getMasterPageName();
|
||||
}
|
||||
|
||||
public String getListStyleName() { return sListStyleName; }
|
||||
|
||||
|
|
|
@ -71,6 +71,14 @@ public class OfficeStyleFamily {
|
|||
if (sName==null) { return null; }
|
||||
else { return styles.get(sName); }
|
||||
}
|
||||
public String cloneStyle(String sName) {
|
||||
OfficeStyle origStyle = styles.get(sName);
|
||||
OfficeStyle newStyle = origStyle.Clone();
|
||||
String newName = sName+"-clone";
|
||||
styles.put(sName+"-clone", newStyle);
|
||||
displayNames.put(newStyle.getDisplayName(), newStyle.getName());
|
||||
return newName;
|
||||
}
|
||||
|
||||
/** Get a style by display name. Automatic styles does not have a display
|
||||
* name, so only common styles can be retrieved with this method
|
||||
|
|
|
@ -33,7 +33,7 @@ import java.util.Hashtable;
|
|||
/** <p> Class representing a set of style properties in OOo (actually this
|
||||
is simply the set of attributes of an element). </p>
|
||||
*/
|
||||
public class PropertySet {
|
||||
public class PropertySet implements Cloneable {
|
||||
private Hashtable<String, String> properties = new Hashtable<String, String>();
|
||||
private String sName;
|
||||
|
||||
|
@ -41,6 +41,10 @@ public class PropertySet {
|
|||
properties = new Hashtable<String, String>();
|
||||
sName="";
|
||||
}
|
||||
public PropertySet(String name, Hashtable<String,String> properties ) {
|
||||
this.properties = (Hashtable<String, String>) properties.clone();
|
||||
sName = new String(name);
|
||||
}
|
||||
|
||||
public String getProperty(String sPropName) {
|
||||
if (sPropName!=null) {
|
||||
|
@ -57,6 +61,10 @@ public class PropertySet {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public PropertySet Clone() {
|
||||
return new PropertySet(sName, properties);
|
||||
}
|
||||
|
||||
public String getName() { return sName; }
|
||||
|
||||
|
@ -70,6 +78,7 @@ public class PropertySet {
|
|||
for (int i=0; i<nLen; i++){
|
||||
Node attr = attrNodes.item(i);
|
||||
properties.put(attr.getNodeName(),attr.getNodeValue());
|
||||
// System.out.println(attr.getNodeName()+" " + attr.getNodeValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,7 +64,23 @@ public class StyleWithProperties extends OfficeStyle {
|
|||
properties[i] = new PropertySet();
|
||||
}
|
||||
}
|
||||
|
||||
public StyleWithProperties(PropertySet[] props) {
|
||||
for (int i=0; i < props.length; i++) {
|
||||
properties[i] = props[i].Clone();
|
||||
}
|
||||
}
|
||||
public OfficeStyle Clone() {
|
||||
StyleWithProperties clone = new StyleWithProperties(properties);
|
||||
clone.loadFromStyle(this);
|
||||
return clone;
|
||||
}
|
||||
public void setPropertiesFromStyle(StyleWithProperties baseStyle)
|
||||
{
|
||||
for (int i=0; i<COUNT; i++) {
|
||||
properties[i] = new PropertySet();
|
||||
}
|
||||
|
||||
}
|
||||
public void loadStyleFromDOM(Node node) {
|
||||
super.loadStyleFromDOM(node);
|
||||
// read the properties of the style, if any
|
||||
|
@ -150,6 +166,11 @@ public class StyleWithProperties extends OfficeStyle {
|
|||
return null; // no value
|
||||
}
|
||||
|
||||
protected void setProperty(int nIndex, String sName, String sValue) {
|
||||
int nRealIndex = bIsOldProps ? OLDPROPS : nIndex;
|
||||
properties[nRealIndex].setProperty(sName, sValue);
|
||||
}
|
||||
|
||||
public String getTextProperty(String sName, boolean bInherit) {
|
||||
return getProperty(TEXT,sName,bInherit);
|
||||
}
|
||||
|
@ -157,8 +178,12 @@ public class StyleWithProperties extends OfficeStyle {
|
|||
public String getParProperty(String sName, boolean bInherit) {
|
||||
return getProperty(PAR,sName,bInherit);
|
||||
}
|
||||
public void setParProperty(String sName, String sValue) {
|
||||
setProperty(PAR,sName, sValue);
|
||||
}
|
||||
|
||||
public String getSectionProperty(String sName, boolean bInherit) {
|
||||
|
||||
public String getSectionProperty(String sName, boolean bInherit) {
|
||||
return getProperty(SECTION,sName,bInherit);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,21 +8,24 @@ import org.w3c.dom.Node;
|
|||
import org.w3c.dom.NodeList;
|
||||
|
||||
import writer2latex.office.OfficeReader;
|
||||
import writer2latex.office.OfficeStyle;
|
||||
import writer2latex.office.StyleWithProperties;
|
||||
import writer2latex.office.XMLString;
|
||||
import writer2latex.util.Misc;
|
||||
|
||||
public class PageSplitter {
|
||||
static Node truncatedListItemNodeContent = null;
|
||||
static OfficeReader officeReader = null;
|
||||
|
||||
protected static Node splitSoftPageBreak(Node onode,OfficeReader ofr){
|
||||
//Find par node with soft page break inside and split it
|
||||
officeReader = ofr;
|
||||
Document document = onode.getOwnerDocument();
|
||||
Element softPageBreak = document.createElement(XMLString.TEXT_SOFT_PAGE_BREAK);
|
||||
NodeList nodes = onode.getChildNodes();
|
||||
|
||||
int i = 0;
|
||||
//Loop through the content nodes and split paragraph nodes with soft page break
|
||||
//Loop through the content nodes and split nodes with soft page break
|
||||
while (i < nodes.getLength()){
|
||||
Node child = nodes.item(i);
|
||||
//System.out.println("splitSoftPageBreak ");
|
||||
|
@ -30,8 +33,6 @@ public class PageSplitter {
|
|||
//Necessary check if node is an Element
|
||||
if ((child.getNodeType() == Node.ELEMENT_NODE) && containsSPB(child)){
|
||||
String nodeName = child.getNodeName();
|
||||
//DEBUG
|
||||
//System.out.println("----------CURRENT NODE IS-------" + nodeName);
|
||||
//Create Duplicate Node!
|
||||
Element childFirstPart = (Element) child.cloneNode(false);
|
||||
StyleWithProperties style = null;
|
||||
|
@ -39,7 +40,8 @@ public class PageSplitter {
|
|||
//If SPB not the first node
|
||||
if (handleParagraph(childFirstPart, child)){
|
||||
onode.insertBefore(childFirstPart, child);
|
||||
style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TEXT_STYLE_NAME));
|
||||
style = ofr.getParStyle(Misc.getAttribute(child, XMLString.TEXT_STYLE_NAME));
|
||||
// OfficeStyle styleCopy = style.Clone();
|
||||
}
|
||||
} else if (nodeName.equals(XMLString.TABLE_TABLE)) {
|
||||
if (handleTableTable(childFirstPart, child)){
|
||||
|
@ -50,14 +52,13 @@ public class PageSplitter {
|
|||
} else if (nodeName.equals(XMLString.TEXT_LIST)) {
|
||||
if (handleList(childFirstPart, child)){
|
||||
onode.insertBefore(childFirstPart, child);
|
||||
style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TEXT_LIST_STYLE));
|
||||
}
|
||||
} else if (nodeName.equals(XMLString.TEXT_SECTION)) {
|
||||
//System.out.println("Get into Section ");
|
||||
|
||||
if (handleSection(childFirstPart, child)){
|
||||
onode.insertBefore(childFirstPart, child);
|
||||
style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TEXT_SECTION));
|
||||
style = ofr.getSectionStyle(Misc.getAttribute(child, XMLString.TEXT_SECTION));
|
||||
//System.out.println("Data moved");
|
||||
}
|
||||
} else if (nodeName.equals(XMLString.TEXT_TABLE_OF_CONTENT)){
|
||||
|
@ -524,25 +525,21 @@ public class PageSplitter {
|
|||
}
|
||||
|
||||
|
||||
private static boolean handleParagraph(Node paraFirstPart, Node paraNode) {
|
||||
private static boolean handleParagraph(Node paraBefore, Node paraAfter) {
|
||||
boolean dataMoved = false;
|
||||
int i = 0;
|
||||
NodeList paraChildNodes = paraNode.getChildNodes();
|
||||
NodeList paraChildNodes = paraAfter.getChildNodes();
|
||||
while (paraChildNodes.getLength() > i) {
|
||||
Node paraChildNode = paraChildNodes.item(i);
|
||||
//NOT TEXT NODES
|
||||
if ((paraChildNode.getNodeType() == Node.ELEMENT_NODE)) {
|
||||
String nodeName = paraChildNode.getNodeName();
|
||||
//System.out.println(nodeName);
|
||||
//SPB FOUND
|
||||
if (containsSPB(paraChildNode)){
|
||||
if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){
|
||||
|
||||
|
||||
|
||||
//Next node in paragraph. If it is text node go further
|
||||
Node paraNextNode = paraChildNodes.item(i+1);
|
||||
Node paraPrevNode = paraFirstPart.getLastChild();
|
||||
Node paraPrevNode = paraBefore.getLastChild();
|
||||
String nextText = null;
|
||||
String prevText = null;
|
||||
if (paraNextNode != null && paraPrevNode != null ){
|
||||
|
@ -576,13 +573,13 @@ public class PageSplitter {
|
|||
}
|
||||
// In case paragraph is empty add space to prevent it's removing
|
||||
if (paraNextNode == null && paraPrevNode == null){
|
||||
Document doc = paraNode.getOwnerDocument();
|
||||
Document doc = paraAfter.getOwnerDocument();
|
||||
Node space = doc.createTextNode(" ");
|
||||
paraNode.insertBefore(space, paraChildNode);
|
||||
paraAfter.insertBefore(space, paraChildNode);
|
||||
}
|
||||
|
||||
// remove inner soft page break node
|
||||
paraNode.removeChild(paraChildNode);
|
||||
paraAfter.removeChild(paraChildNode);
|
||||
|
||||
/* Check if next node in para is text and first char is a letter
|
||||
* Check if last node in paraFirstPart is text and last char is a letter
|
||||
|
@ -597,19 +594,33 @@ public class PageSplitter {
|
|||
break;
|
||||
//ELEMENT WITHOUT SPB
|
||||
} else if (nodeName.equals(XMLString.TEXT_BOOKMARK_START)){
|
||||
paraFirstPart.appendChild(paraChildNode.cloneNode(true));
|
||||
paraBefore.appendChild(paraChildNode.cloneNode(true));
|
||||
i++;
|
||||
} else {
|
||||
paraFirstPart.appendChild(paraChildNode);
|
||||
paraBefore.appendChild(paraChildNode);
|
||||
dataMoved = true;
|
||||
}
|
||||
//TEXT NODES
|
||||
} else {
|
||||
paraFirstPart.appendChild(paraChildNode);
|
||||
paraBefore.appendChild(paraChildNode);
|
||||
dataMoved = true;
|
||||
}
|
||||
|
||||
}
|
||||
//StyleWithProperties style = officeReader.getParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
|
||||
String newStyleName = officeReader.cloneParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
|
||||
|
||||
Node styleAttr = paraAfter.getAttributes().getNamedItem(XMLString.TEXT_STYLE_NAME);
|
||||
styleAttr.setTextContent(newStyleName);
|
||||
StyleWithProperties newStyle = officeReader.getParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
|
||||
//String propVal = style.getParProperty(XMLString.FO_TEXT_INDENT, true);
|
||||
//System.out.println(propVal);
|
||||
newStyle.setParProperty(XMLString.FO_TEXT_INDENT, "0");
|
||||
//propVal = newStyle.getParProperty(XMLString.FO_TEXT_INDENT, true);
|
||||
//System.out.println(propVal);
|
||||
|
||||
|
||||
//System.out.println(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
|
||||
return dataMoved;
|
||||
}
|
||||
// Returns true if soft-page-break found. Removes it if removeFound = true
|
||||
|
|
Loading…
Add table
Reference in a new issue