From 20c6326b5ef6955c1f58b3df3eeacbeba92d8ddf Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Mon, 3 Feb 2020 10:51:42 +0100 Subject: [PATCH] Refactoring styles conversion --- .../java/writer2latex/xhtml/Converter.java | 8 ++--- src/main/java/writer2latex/xhtml/Parser.java | 2 +- .../xhtml/content/DrawParser.java | 16 +++++----- .../xhtml/content/FootnoteParser.java | 2 +- .../xhtml/content/IndexParser.java | 6 ++-- .../xhtml/content/NoteParser.java | 4 +-- .../xhtml/content/TableParser.java | 12 +++---- .../xhtml/content/TextParser.java | 31 ++++++++++--------- .../xhtml/style/FrameStyleParser.java | 4 +-- .../xhtml/style/PresentationStyleParser.java | 4 +-- .../style/StyleWithPropertiesParser.java | 14 ++++++--- .../java/writer2latex/xhtml/style/Styles.java | 2 +- 12 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/main/java/writer2latex/xhtml/Converter.java b/src/main/java/writer2latex/xhtml/Converter.java index 0be2504..38638e3 100644 --- a/src/main/java/writer2latex/xhtml/Converter.java +++ b/src/main/java/writer2latex/xhtml/Converter.java @@ -675,7 +675,7 @@ public class Converter extends BasicConverter { if (!ofr.isPresentation()) { StyleInfo pageInfo = new StyleInfo(); styles.getPageSc().applyDefaultWritingDirection(pageInfo); - styles.getPageSc().applyStyle(pageInfo,htmlDoc.getContentNode()); + styles.getPageSc().writeStyle(pageInfo,htmlDoc.getContentNode()); } // Add title (required by xhtml) @@ -790,8 +790,8 @@ public class Converter extends BasicConverter { htmlDoc.getContentNode().appendChild(div); htmlDoc.setContentNode(div); StyleInfo sectionInfo = new StyleInfo(); - styles.getSectionSc().applyStyle(sStyleName,sectionInfo); - styles.getSectionSc().applyStyle(sectionInfo,div); + styles.getSectionSc().readStyle(sStyleName,sectionInfo); + styles.getSectionSc().writeStyle(sectionInfo,div); } } @@ -888,7 +888,7 @@ public class Converter extends BasicConverter { String sVisitedStyleName = onode.getAttribute(XMLString.TEXT_VISITED_STYLE_NAME); StyleInfo anchorInfo = new StyleInfo(); styles.getTextSc().applyAnchorStyle(sStyleName,sVisitedStyleName,anchorInfo); - styles.getTextSc().applyStyle(anchorInfo,anchor); + styles.getTextSc().writeStyle(anchorInfo,anchor); return anchor; } diff --git a/src/main/java/writer2latex/xhtml/Parser.java b/src/main/java/writer2latex/xhtml/Parser.java index 2cb23a4..6d0ecb7 100644 --- a/src/main/java/writer2latex/xhtml/Parser.java +++ b/src/main/java/writer2latex/xhtml/Parser.java @@ -105,7 +105,7 @@ public class Parser { * @param info the style to apply * @param hnode the XHTML node */ - public void applyStyle(StyleInfo info, Element hnode) { + public void writeStyle(StyleInfo info, Element hnode) { if (info.sClass!=null) { hnode.setAttribute("class",info.sClass); } diff --git a/src/main/java/writer2latex/xhtml/content/DrawParser.java b/src/main/java/writer2latex/xhtml/content/DrawParser.java index 5fc4034..dbfbab4 100644 --- a/src/main/java/writer2latex/xhtml/content/DrawParser.java +++ b/src/main/java/writer2latex/xhtml/content/DrawParser.java @@ -170,7 +170,7 @@ public class DrawParser extends Parser { info.props.addValue("top","40px"); // Somewhat arbitrary info.props.addValue("left","0"); info.props.addValue("position","absolute"); - applyStyle(info,div); + writeStyle(info,div); // Traverse the draw:page if (!onode.hasChildNodes()) { return; } @@ -624,13 +624,13 @@ public class DrawParser extends Parser { // Now style it StyleInfo info = new StyleInfo(); String sStyleName = Misc.getAttribute(frame, XMLString.DRAW_STYLE_NAME); - if (nMode!=FULL_SCREEN) { getFrameSc().applyStyle(sStyleName,info); } + if (nMode!=FULL_SCREEN) { getFrameSc().readStyle(sStyleName,info); } applyImageSize(frame,info.props,nMode,false); // Apply placement applyPlacement(frame, hnodeBlock, hnodeInline, nMode, imageElement, info); - applyStyle(info,imageElement); + writeStyle(info,imageElement); addLink(onode,imageElement); } } @@ -654,7 +654,7 @@ public class DrawParser extends Parser { // Draw frame style String sStyleName = Misc.getAttribute(frame, XMLString.DRAW_STYLE_NAME); if (sStyleName!=null) { - getFrameSc().applyStyle(sStyleName,info); + getFrameSc().readStyle(sStyleName,info); } // Presentation frame style String sPresentationStyleName = Misc.getAttribute(frame, XMLString.PRESENTATION_STYLE_NAME); @@ -662,7 +662,7 @@ public class DrawParser extends Parser { if ("outline".equals(Misc.getAttribute(frame, XMLString.PRESENTATION_CLASS))) { getPresentationSc().enterOutline(sPresentationStyleName); } - getPresentationSc().applyStyle(sPresentationStyleName,info); + getPresentationSc().readStyle(sPresentationStyleName,info); } // Additional text formatting String sTextStyleName = Misc.getAttribute(frame, XMLString.DRAW_TEXT_STYLE_NAME); @@ -730,7 +730,7 @@ public class DrawParser extends Parser { } //Finish - applyStyle(info,textbox); + writeStyle(info,textbox); if (sContentWidth!=null) { converter.pushContentWidth(sContentWidth); } @@ -805,10 +805,10 @@ public class DrawParser extends Parser { if (hcontrol!=null) { Element frame = onode; // controls are *not* contained in a draw:frame! StyleInfo info = new StyleInfo(); - getFrameSc().applyStyle(frame.getAttribute(XMLString.DRAW_STYLE_NAME),info); + getFrameSc().readStyle(frame.getAttribute(XMLString.DRAW_STYLE_NAME),info); applySize(frame,info.props,false); applyPlacement(frame,hnodeBlock,hnodeInline,nMode,hcontrol,info); - applyStyle(info,hcontrol); + writeStyle(info,hcontrol); } } diff --git a/src/main/java/writer2latex/xhtml/content/FootnoteParser.java b/src/main/java/writer2latex/xhtml/content/FootnoteParser.java index 3ff734d..60f86a5 100644 --- a/src/main/java/writer2latex/xhtml/content/FootnoteParser.java +++ b/src/main/java/writer2latex/xhtml/content/FootnoteParser.java @@ -62,7 +62,7 @@ class FootnoteParser extends NoteParser { Element rule = converter.createElement("hr"); StyleInfo info = new StyleInfo(); getPageSc().applyFootnoteRuleStyle(info); - getPageSc().applyStyle(info, rule); + getPageSc().writeStyle(info, rule); sectionElement.appendChild(rule); flushAllNotes(sectionElement,"footnote"); diff --git a/src/main/java/writer2latex/xhtml/content/IndexParser.java b/src/main/java/writer2latex/xhtml/content/IndexParser.java index 608c98f..c822555 100644 --- a/src/main/java/writer2latex/xhtml/content/IndexParser.java +++ b/src/main/java/writer2latex/xhtml/content/IndexParser.java @@ -95,8 +95,8 @@ abstract class IndexParser extends Parser { String sStyleName = source.getAttribute(XMLString.TEXT_STYLE_NAME); if (sStyleName!=null) { StyleInfo sectionInfo = new StyleInfo(); - getSectionSc().applyStyle(sStyleName,sectionInfo); - applyStyle(sectionInfo,container); + getSectionSc().readStyle(sStyleName,sectionInfo); + writeStyle(sectionInfo,container); } return container; } @@ -111,7 +111,7 @@ abstract class IndexParser extends Parser { StyleInfo info = new StyleInfo(); info.sTagName = "h1"; getHeadingSc().applyStyle(1, sStyleName, info); - applyStyle(info,h1); + writeStyle(info,h1); getTextCv().traversePCDATA(title,h1); } } diff --git a/src/main/java/writer2latex/xhtml/content/NoteParser.java b/src/main/java/writer2latex/xhtml/content/NoteParser.java index 1109bd4..4833218 100644 --- a/src/main/java/writer2latex/xhtml/content/NoteParser.java +++ b/src/main/java/writer2latex/xhtml/content/NoteParser.java @@ -186,8 +186,8 @@ class NoteParser extends Parser { // Style it String sCitStyle = noteConfig.getProperty(XMLString.TEXT_CITATION_STYLE_NAME); StyleInfo linkInfo = new StyleInfo(); - getTextSc().applyStyle(sCitStyle,linkInfo); - applyStyle(linkInfo,link); + getTextSc().readStyle(sCitStyle,linkInfo); + writeStyle(linkInfo,link); // Add prefix String sPrefix = noteConfig.getProperty(XMLString.STYLE_NUM_PREFIX); diff --git a/src/main/java/writer2latex/xhtml/content/TableParser.java b/src/main/java/writer2latex/xhtml/content/TableParser.java index 3b4facb..6c2156f 100644 --- a/src/main/java/writer2latex/xhtml/content/TableParser.java +++ b/src/main/java/writer2latex/xhtml/content/TableParser.java @@ -341,7 +341,7 @@ public class TableParser extends Parser { private void applyTableStyle(String sStyleName, Element table, boolean bIsSubTable) { StyleInfo info = new StyleInfo(); - getTableSc().applyStyle(sStyleName,info); + getTableSc().readStyle(sStyleName,info); if (config.tableSize()!=XhtmlConfig.NONE) { StyleWithProperties style = ofr.getTableStyle(sStyleName); @@ -384,12 +384,12 @@ public class TableParser extends Parser { info.props.addValue("width","100%"); info.props.addValue("margin","0"); } - applyStyle(info,table); + writeStyle(info,table); } private void applyRowStyle(String sStyleName, Element row) { StyleInfo info = new StyleInfo(); - getRowSc().applyStyle(sStyleName,info); + getRowSc().readStyle(sStyleName,info); if (config.tableSize()!=XhtmlConfig.NONE) { StyleWithProperties style = ofr.getRowStyle(sStyleName); @@ -405,12 +405,12 @@ public class TableParser extends Parser { } } - applyStyle(info,row); + writeStyle(info,row); } private void applyCellStyle(String sStyleName, boolean bIsRelative, String sTotalWidth, String sValueType, Element cell, boolean bIsSubTable) { StyleInfo info = new StyleInfo(); - getCellSc().applyStyle(sStyleName,info); + getCellSc().readStyle(sStyleName,info); StyleWithProperties style = ofr.getCellStyle(sStyleName); if (style!=null) { @@ -470,7 +470,7 @@ public class TableParser extends Parser { info.props.addValue("padding","0"); } - applyStyle(info,cell); + writeStyle(info,cell); } // TODO: Move me to a more logical place! diff --git a/src/main/java/writer2latex/xhtml/content/TextParser.java b/src/main/java/writer2latex/xhtml/content/TextParser.java index 3151183..84f986d 100644 --- a/src/main/java/writer2latex/xhtml/content/TextParser.java +++ b/src/main/java/writer2latex/xhtml/content/TextParser.java @@ -676,7 +676,7 @@ public class TextParser extends Parser { // add root element Element heading = converter.createElement(info.sTagName); hnode.appendChild(heading); - applyStyle(info, heading); + writeStyle(info, heading); traverseFloats(onode, hnode, heading); // Apply writing direction /* @@ -707,7 +707,7 @@ public class TextParser extends Parser { if (innerInfo.sTagName != null && innerInfo.sTagName.length() > 0) { content = converter.createElement(innerInfo.sTagName); heading.appendChild(content); - applyStyle(innerInfo, content); + writeStyle(innerInfo, content); } traverseInlineText(onode, content); @@ -836,14 +836,14 @@ public class TextParser extends Parser { StyleInfo info = new StyleInfo(); if (style!=null) { String sTextStyleName = style.getLevelProperty(nLevel,TEXT_STYLE_NAME); - getTextSc().applyStyle(sTextStyleName, info); + getTextSc().readStyle(sTextStyleName, info); } if (info.sTagName==null) { info.sTagName = "span"; } if (info.sClass==null) { info.sClass = sDefaultStyle; } Element content = converter.createElement(info.sTagName); - getTextSc().applyStyle(info, content); + getTextSc().writeStyle(info, content); hnode.appendChild(content); content.appendChild( converter.createTextNode(sLabel) ); } @@ -873,7 +873,7 @@ public class TextParser extends Parser { Element list = converter.createElement("ol"); StyleInfo listInfo = new StyleInfo(); getListSc().applyStyle(nLevel,sStyleName,listInfo); - applyStyle(listInfo,list); + writeStyle(listInfo,list); hnode.appendChild(list); traverseList(onode,nLevel,sStyleName,list); } @@ -888,7 +888,7 @@ public class TextParser extends Parser { Element list = converter.createElement("ul"); StyleInfo listInfo = new StyleInfo(); getListSc().applyStyle(nLevel,sStyleName,listInfo); - applyStyle(listInfo,list); + writeStyle(listInfo,list); hnode.appendChild(list); traverseList(onode,nLevel,sStyleName,list); } @@ -967,7 +967,7 @@ public class TextParser extends Parser { Element item = converter.createElement("li"); StyleInfo info = new StyleInfo(); getPresentationSc().applyOutlineStyle(nLevel,info); - applyStyle(info,item); + writeStyle(info,item); hnode.appendChild(item); if (config.listFormatting()==XhtmlConfig.CSS1_HACK) { boolean bRestart = "true".equals(Misc.getAttribute(child, @@ -1644,16 +1644,17 @@ public class TextParser extends Parser { /* Create a styled paragraph node */ protected Element createParagraph(Element node, String sStyleName) { StyleInfo info = new StyleInfo(); - getParSc().applyStyle(sStyleName,info); - Element par = converter.createElement(info.sTagName); - node.appendChild(par); - applyStyle(info,par); StyleWithProperties style = ofr.getParStyle(sStyleName); + getParSc().readStyle(sStyleName,info); + Element para = converter.createElement(info.sTagName); + node.appendChild(para); + + writeStyle(info,para); if (style!=null && style.isAutomatic()) { - return applyAttributes(par,style); + return applyAttributes(para,style); } else { - return par; + return para; } } @@ -1677,12 +1678,12 @@ public class TextParser extends Parser { /* Create a styled inline node */ protected Element createInline(Element node, String sStyleName) { StyleInfo info = new StyleInfo(); - getTextSc().applyStyle(sStyleName,info); + getTextSc().readStyle(sStyleName,info); Element newNode = node; if (info.hasAttributes() || !"span".equals(info.sTagName)) { // We (probably) need to create a new element newNode = converter.createElement(info.sTagName); - applyStyle(info,newNode); + writeStyle(info,newNode); // But we may want to merge it with the previous element Node prev = node.getLastChild(); if (prev!=null && Misc.isElement(prev, info.sTagName)) { diff --git a/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java b/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java index 28cd898..f79be7e 100644 --- a/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java @@ -76,7 +76,7 @@ public class FrameStyleParser extends StyleWithPropertiesParser { } - private String composeDeclaration(String displayName) { + public String composeDeclaration(String displayName) { String declaration = ""; StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(displayName); String declarationBlock = composeDeclarationBlock(displayName); @@ -92,7 +92,7 @@ public class FrameStyleParser extends StyleWithPropertiesParser { return getStyleTag() + "." + getClassNamePrefix() + styleNames.getName(displayName) + " p "; } - private String composeDeclarationBlock( String sDisplayName) { + public String composeDeclarationBlock( String sDisplayName) { StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(sDisplayName); CSVList props = new CSVList(";"); getFrameSc().cssMargins(style, props, true); diff --git a/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java b/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java index 38710bd..f818c25 100644 --- a/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java @@ -101,7 +101,7 @@ public class PresentationStyleParser extends FrameStyleParser { } - private String composeDeclaration(String displayName) { + public String composeDeclaration(String displayName) { String declaration = ""; StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(displayName); String declarationBlock = composeDeclarationBlock(displayName); @@ -118,7 +118,7 @@ public class PresentationStyleParser extends FrameStyleParser { return "li.outline" + styleNames.getName(displayName) + " p "; } - private String composeDeclarationBlock(String displayName) { + public String composeDeclarationBlock(String displayName) { StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(displayName); CSVList props = new CSVList(";"); getFrameSc().cssMargins(style, props, true); diff --git a/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java b/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java index 756036c..a993816 100644 --- a/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java +++ b/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java @@ -59,7 +59,7 @@ public abstract class StyleWithPropertiesParser extends StyleParser { * @param sStyleName name of the OpenDocument style * @param info the StyleInfo object to add information to */ - public void applyStyle(String sStyleName, StyleInfo info) { + public void readStyle(String sStyleName, StyleInfo info) { StyleWithProperties style = (StyleWithProperties) getStyles().getStyle(sStyleName); info.sTagName = getStyleTag(); if (style!=null) { @@ -67,7 +67,7 @@ public abstract class StyleWithPropertiesParser extends StyleParser { applyDirection(style,info); if (style.isAutomatic()) { // Apply parent style + hard formatting - applyStyle(style.getParentName(),info); + readStyle(style.getParentName(),info); if (bConvertHard) { applyProperties(style,info.props,false); } } else { @@ -108,12 +108,12 @@ public abstract class StyleWithPropertiesParser extends StyleParser { return buf.toString(); } - private String composeDeclaration( String displayName) { + public String composeDeclaration( String displayName) { String declaration = ""; StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(displayName); String selector = composeSelector(displayName); String declarationBlock = composeDeclarationBlock(displayName); - if (!style.isAutomatic() && !selector.isEmpty() && !declarationBlock.isEmpty()) { + if (style!= null && !style.isAutomatic() && !selector.isEmpty() && !declarationBlock.isEmpty()) { declaration = createDeclaration(selector, declarationBlock); // TODO: Create a method "getStyleDeclarationsInner" // to be used by eg. FrameStyleConverter @@ -121,9 +121,13 @@ public abstract class StyleWithPropertiesParser extends StyleParser { return declaration.toString(); } - private String composeDeclarationBlock(String displayName) { + public String composeDeclarationBlock(String displayName) { StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(displayName); + if (style == null) { + return ""; + } CSVList props = new CSVList(";"); + applyProperties(style, props, true); return props.toString(); } diff --git a/src/main/java/writer2latex/xhtml/style/Styles.java b/src/main/java/writer2latex/xhtml/style/Styles.java index 9f1d7fb..31d92ed 100644 --- a/src/main/java/writer2latex/xhtml/style/Styles.java +++ b/src/main/java/writer2latex/xhtml/style/Styles.java @@ -120,7 +120,7 @@ public class Styles extends Parser { if (style!=null) { StyleInfo info = new StyleInfo(); StyleParser.applyLang(style,info); - applyStyle(info,node); + writeStyle(info,node); } }