From bd32335e71b416f81ea79c111e4dc8834d6c9323 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Wed, 29 Jan 2020 15:54:06 +0100 Subject: [PATCH] Refactoring --- .../java/writer2latex/xhtml/Converter.java | 94 +++---- src/main/java/writer2latex/xhtml/Parser.java | 36 +-- .../content/XhtmlBibliographyGenerator.java | 4 +- .../xhtml/style/FrameStyleParser.java | 6 +- .../xhtml/style/HeadingStyleParser.java | 74 ++--- ...FamilyParser.java => ListStyleParser.java} | 88 +++--- .../xhtml/style/PageStyleParser.java | 10 +- .../xhtml/style/PresentationStyleParser.java | 6 +- .../xhtml/style/StyleFamilyParser.java | 125 --------- .../writer2latex/xhtml/style/StyleParser.java | 263 ++++++------------ .../style/StyleWithPropertiesParser.java | 7 +- .../java/writer2latex/xhtml/style/Styles.java | 226 +++++++++++++++ .../xhtml/style/TextStyleParser.java | 12 +- 13 files changed, 476 insertions(+), 475 deletions(-) rename src/main/java/writer2latex/xhtml/style/{ListStyleFamilyParser.java => ListStyleParser.java} (76%) delete mode 100644 src/main/java/writer2latex/xhtml/style/StyleFamilyParser.java create mode 100644 src/main/java/writer2latex/xhtml/style/Styles.java diff --git a/src/main/java/writer2latex/xhtml/Converter.java b/src/main/java/writer2latex/xhtml/Converter.java index a08089e..a77de02 100644 --- a/src/main/java/writer2latex/xhtml/Converter.java +++ b/src/main/java/writer2latex/xhtml/Converter.java @@ -62,7 +62,7 @@ import writer2latex.xhtml.content.PageContainer; import writer2latex.xhtml.content.TableParser; import writer2latex.xhtml.content.TextParser; import writer2latex.xhtml.l10n.L10n; -import writer2latex.xhtml.style.StyleParser; +import writer2latex.xhtml.style.Styles; /** *

This class converts an OpenDocument file to an XHTML(+MathML) or EPUB document.

@@ -84,11 +84,11 @@ public class Converter extends BasicConverter { private L10n l10n; // The helpers - private StyleParser styleCv; - private TextParser textCv; - private TableParser tableCv; - private DrawParser drawCv; - private MathParser mathCv; + private Styles styles; + private TextParser textParser; + private TableParser tableParser; + private DrawParser drawParser; + private MathParser mathParser; // The template private XhtmlDocument template = null; @@ -189,15 +189,15 @@ public class Converter extends BasicConverter { return contentWidth.size()==1; } - public StyleParser getStyleCv() { return styleCv; } + public Styles getStylesParser() { return styles; } - public TextParser getTextCv() { return textCv; } + public TextParser getTextParser() { return textParser; } - public TableParser getTableCv() { return tableCv; } + public TableParser getTableParser() { return tableParser; } - public DrawParser getDrawCv() { return drawCv; } + public DrawParser getDrawParser() { return drawParser; } - public MathParser getMathCv() { return mathCv; } + public MathParser getMathParser() { return mathParser; } public int getType() { return nType; } @@ -276,11 +276,11 @@ public class Converter extends BasicConverter { imageConverter.setDefaultVectorFormat(MIMETypes.SVG); } - styleCv = new StyleParser(ofr,config,this,nType); - textCv = new TextParser(ofr,config,this); - tableCv = new TableParser(ofr,config,this); - drawCv = new DrawParser(ofr,config,this); - mathCv = new MathParser(ofr,config,this,nType!=XhtmlDocument.XHTML10 && nType!=XhtmlDocument.XHTML11); + styles = new Styles(ofr,config,this,nType); + textParser = new TextParser(ofr,config,this); + tableParser = new TableParser(ofr,config,this); + drawParser = new DrawParser(ofr,config,this); + mathParser = new MathParser(ofr,config,this,nType!=XhtmlDocument.XHTML10 && nType!=XhtmlDocument.XHTML11); // Set locale to document language StyleWithProperties style = ofr.isSpreadsheet() ? ofr.getDefaultCellStyle() : ofr.getDefaultParStyle(); @@ -295,13 +295,13 @@ public class Converter extends BasicConverter { } // Set the main content width - pushContentWidth(getStyleCv().getPageSc().getTextWidth()); + pushContentWidth(getStylesParser().getPageSc().getTextWidth()); // Traverse the body Element body = ofr.getContent(); - if (ofr.isSpreadsheet()) { tableCv.convertTableContent(body); } - else if (ofr.isPresentation()) { drawCv.convertDrawContent(body); } - else { textCv.convertTextContent(body); } + if (ofr.isSpreadsheet()) { tableParser.convertTableContent(body); } + else if (ofr.isPresentation()) { drawParser.convertDrawContent(body); } + else { textParser.convertTextContent(body); } // Set the title page and text page entries if (converterResult.getContent().isEmpty()) { @@ -351,9 +351,9 @@ public class Converter extends BasicConverter { for (int i=0; i<=outFileIndex; i++) { Element head = outFiles.get(i).getHeadNode(); if (head!=null) { - Node styles = styleCv.getStyleSelectors(outFiles.get(i).getContentDOM()); - if (styles!=null) { - head.appendChild(styles); + Node styleNode = styles.getStylesNode(outFiles.get(i).getContentDOM()); + if (styleNode!=null) { + head.appendChild(styleNode); } } } @@ -402,15 +402,15 @@ public class Converter extends BasicConverter { footerPar.appendChild(dom.createTextNode(" ")); } // Add links to all sheets: - int nSheets = tableCv.sheetNames.size(); + int nSheets = tableParser.sheetNames.size(); for (int j=0; j=0) { - addNavigationLink(dom,header,l10n.get(L10n.CONTENTS),textCv.getTocIndex()); + if (textParser.getTocIndex()>=0) { + addNavigationLink(dom,header,l10n.get(L10n.CONTENTS),textParser.getTocIndex()); } - if (textCv.getAlphabeticalIndex()>=0) { - addNavigationLink(dom,header,l10n.get(L10n.INDEX),textCv.getAlphabeticalIndex()); + if (textParser.getAlphabeticalIndex()>=0) { + addNavigationLink(dom,header,l10n.get(L10n.INDEX),textParser.getAlphabeticalIndex()); } } @@ -465,11 +465,11 @@ public class Converter extends BasicConverter { addNavigationLink(dom,footer,l10n.get(L10n.PREVIOUS),i-1); addNavigationLink(dom,footer,l10n.get(L10n.NEXT),i+1); addNavigationLink(dom,footer,l10n.get(L10n.LAST),outFileIndex); - if (textCv.getTocIndex()>=0) { - addNavigationLink(dom,footer,l10n.get(L10n.CONTENTS),textCv.getTocIndex()); + if (textParser.getTocIndex()>=0) { + addNavigationLink(dom,footer,l10n.get(L10n.CONTENTS),textParser.getTocIndex()); } - if (textCv.getAlphabeticalIndex()>=0) { - addNavigationLink(dom,footer,l10n.get(L10n.INDEX),textCv.getAlphabeticalIndex()); + if (textParser.getAlphabeticalIndex()>=0) { + addNavigationLink(dom,footer,l10n.get(L10n.INDEX),textParser.getAlphabeticalIndex()); } } } @@ -504,12 +504,12 @@ public class Converter extends BasicConverter { if (config.xhtmlFormatting()>XhtmlConfig.IGNORE_STYLES) { if (isOPS()) { // EPUB CssDocument cssDoc = new CssDocument(EPUB_STYLESHEET); - cssDoc.read(styleCv.allStyleSelectors(false)); + cssDoc.read(styles.allStyleSelectors(false)); converterResult.addDocument(cssDoc); } else if (config.separateStylesheet()) { // XHTML CssDocument cssDoc = new CssDocument(sTargetFileName+"-styles.css"); - cssDoc.read(styleCv.allStyleSelectors(false)); + cssDoc.read(styles.allStyleSelectors(false)); converterResult.addDocument(cssDoc); } } @@ -660,7 +660,7 @@ public class Converter extends BasicConverter { // Create head + body htmlDOM = htmlDoc.getContentDOM(); Element rootElement = htmlDOM.getDocumentElement(); - styleCv.applyDefaultLanguage(rootElement); + styles.applyDefaultLanguage(rootElement); addEpubNs(rootElement); rootElement.insertBefore(htmlDOM.createComment( "This file was converted to xhtml by " @@ -672,8 +672,8 @@ public class Converter extends BasicConverter { // Apply default writing direction if (!ofr.isPresentation()) { StyleInfo pageInfo = new StyleInfo(); - styleCv.getPageSc().applyDefaultWritingDirection(pageInfo); - styleCv.getPageSc().applyStyle(pageInfo,htmlDoc.getContentNode()); + styles.getPageSc().applyDefaultWritingDirection(pageInfo); + styles.getPageSc().applyStyle(pageInfo,htmlDoc.getContentNode()); } // Add title (required by xhtml) @@ -779,8 +779,8 @@ public class Converter extends BasicConverter { } // Recreate nested sections, if any - if (!textCv.sections.isEmpty()) { - Iterator iter = textCv.sections.iterator(); + if (!textParser.sections.isEmpty()) { + Iterator iter = textParser.sections.iterator(); while (iter.hasNext()) { Element section = (Element) iter.next(); String sStyleName = Misc.getAttribute(section,XMLString.TEXT_STYLE_NAME); @@ -788,8 +788,8 @@ public class Converter extends BasicConverter { htmlDoc.getContentNode().appendChild(div); htmlDoc.setContentNode(div); StyleInfo sectionInfo = new StyleInfo(); - styleCv.getSectionSc().applyStyle(sStyleName,sectionInfo); - styleCv.getSectionSc().applyStyle(sectionInfo,div); + styles.getSectionSc().applyStyle(sStyleName,sectionInfo); + styles.getSectionSc().applyStyle(sectionInfo,div); } } @@ -883,8 +883,8 @@ public class Converter extends BasicConverter { String sStyleName = onode.getAttribute(XMLString.TEXT_STYLE_NAME); String sVisitedStyleName = onode.getAttribute(XMLString.TEXT_VISITED_STYLE_NAME); StyleInfo anchorInfo = new StyleInfo(); - styleCv.getTextSc().applyAnchorStyle(sStyleName,sVisitedStyleName,anchorInfo); - styleCv.getTextSc().applyStyle(anchorInfo,anchor); + styles.getTextSc().applyAnchorStyle(sStyleName,sVisitedStyleName,anchorInfo); + styles.getTextSc().applyStyle(anchorInfo,anchor); return anchor; } diff --git a/src/main/java/writer2latex/xhtml/Parser.java b/src/main/java/writer2latex/xhtml/Parser.java index 90f8c52..2cb23a4 100644 --- a/src/main/java/writer2latex/xhtml/Parser.java +++ b/src/main/java/writer2latex/xhtml/Parser.java @@ -35,13 +35,13 @@ import writer2latex.xhtml.content.TextParser; import writer2latex.xhtml.style.CellStyleParser; import writer2latex.xhtml.style.FrameStyleParser; import writer2latex.xhtml.style.HeadingStyleParser; -import writer2latex.xhtml.style.ListStyleFamilyParser; +import writer2latex.xhtml.style.ListStyleParser; import writer2latex.xhtml.style.PageStyleParser; import writer2latex.xhtml.style.ParStyleParser; import writer2latex.xhtml.style.PresentationStyleParser; import writer2latex.xhtml.style.RowStyleParser; import writer2latex.xhtml.style.SectionStyleParser; -import writer2latex.xhtml.style.StyleParser; +import writer2latex.xhtml.style.Styles; import writer2latex.xhtml.style.TableStyleParser; import writer2latex.xhtml.style.TextStyleParser; @@ -68,37 +68,37 @@ public class Parser { // Convenience accessor methods to other converter helpers (only needed to save some typing) - protected StyleParser getStyleCv() { return converter.getStyleCv(); } + protected Styles getStyleCv() { return converter.getStylesParser(); } - protected TextStyleParser getTextSc() { return converter.getStyleCv().getTextSc(); } + protected TextStyleParser getTextSc() { return converter.getStylesParser().getTextSc(); } - protected ParStyleParser getParSc() { return converter.getStyleCv().getParSc(); } + protected ParStyleParser getParSc() { return converter.getStylesParser().getParSc(); } - protected HeadingStyleParser getHeadingSc() { return converter.getStyleCv().getHeadingSc(); } + protected HeadingStyleParser getHeadingSc() { return converter.getStylesParser().getHeadingSc(); } - protected ListStyleFamilyParser getListSc() { return converter.getStyleCv().getListSc(); } + protected ListStyleParser getListSc() { return converter.getStylesParser().getListSc(); } - protected SectionStyleParser getSectionSc() { return converter.getStyleCv().getSectionSc(); } + protected SectionStyleParser getSectionSc() { return converter.getStylesParser().getSectionSc(); } - protected TableStyleParser getTableSc() { return converter.getStyleCv().getTableSc(); } + protected TableStyleParser getTableSc() { return converter.getStylesParser().getTableSc(); } - protected RowStyleParser getRowSc() { return converter.getStyleCv().getRowSc(); } + protected RowStyleParser getRowSc() { return converter.getStylesParser().getRowSc(); } - protected CellStyleParser getCellSc() { return converter.getStyleCv().getCellSc(); } + protected CellStyleParser getCellSc() { return converter.getStylesParser().getCellSc(); } - protected FrameStyleParser getFrameSc() { return converter.getStyleCv().getFrameSc(); } + protected FrameStyleParser getFrameSc() { return converter.getStylesParser().getFrameSc(); } - protected PresentationStyleParser getPresentationSc() { return converter.getStyleCv().getPresentationSc(); } + protected PresentationStyleParser getPresentationSc() { return converter.getStylesParser().getPresentationSc(); } - protected PageStyleParser getPageSc() { return converter.getStyleCv().getPageSc(); } + protected PageStyleParser getPageSc() { return converter.getStylesParser().getPageSc(); } - protected TextParser getTextCv() { return converter.getTextCv(); } + protected TextParser getTextCv() { return converter.getTextParser(); } - protected TableParser getTableCv() { return converter.getTableCv(); } + protected TableParser getTableCv() { return converter.getTableParser(); } - protected DrawParser getDrawCv() { return converter.getDrawCv(); } + protected DrawParser getDrawCv() { return converter.getDrawParser(); } - protected MathParser getMathCv() { return converter.getMathCv(); } + protected MathParser getMathCv() { return converter.getMathParser(); } /** Apply style information to an XHTML node * diff --git a/src/main/java/writer2latex/xhtml/content/XhtmlBibliographyGenerator.java b/src/main/java/writer2latex/xhtml/content/XhtmlBibliographyGenerator.java index 5ec0aaa..2a91cd9 100644 --- a/src/main/java/writer2latex/xhtml/content/XhtmlBibliographyGenerator.java +++ b/src/main/java/writer2latex/xhtml/content/XhtmlBibliographyGenerator.java @@ -57,12 +57,12 @@ class XhtmlBibliographyGenerator extends BibliographyGenerator { converter.addTarget(li, "bib"+sKey); converter.addEpubType(li, "biblioentry"); ul.appendChild(li); - currentPar = converter.getTextCv().createParagraph(li, sStyleName); + currentPar = converter.getTextParser().createParagraph(li, sStyleName); } @Override protected void insertBibliographyItemElement(String sStyleName, String sText) { if (sStyleName!=null) { - converter.getTextCv().createInline(currentPar, sStyleName).appendChild(converter.createTextNode(sText)); + converter.getTextParser().createInline(currentPar, sStyleName).appendChild(converter.createTextNode(sText)); } else { currentPar.appendChild(converter.createTextNode(sText)); diff --git a/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java b/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java index 1d26c43..b102e3e 100644 --- a/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/FrameStyleParser.java @@ -61,10 +61,10 @@ public class FrameStyleParser extends StyleWithPropertiesParser { /** Convert style information for used styles * @param sIndent a String of spaces to add before each line */ - public String getStyleSelectors(String sIndent) { + public String getStyleSelectors() { if (bConvertStyles) { StringBuilder buf = new StringBuilder(); - buf.append(super.getStyleSelectors(sIndent)); + buf.append(super.getStyleSelectors()); Enumeration names = styleNames.keys(); while (names.hasMoreElements()) { String sDisplayName = names.nextElement(); @@ -76,7 +76,7 @@ public class FrameStyleParser extends StyleWithPropertiesParser { getParSc().cssPar(style, props, true); getTextSc().cssTextCommon(style, props, true); if (!props.isEmpty()) { - buf.append(sIndent); + buf.append(indent); buf.append(getDefaultTagName(null)); buf.append("."); buf.append(getClassNamePrefix()); diff --git a/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java b/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java index 2c171da..5803817 100644 --- a/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/HeadingStyleParser.java @@ -38,7 +38,7 @@ import writer2latex.xhtml.StyleInfo; import writer2latex.xhtml.XhtmlConfig; import writer2latex.xhtml.XhtmlStyleMapItem; -public class HeadingStyleParser extends StyleFamilyParser { +public class HeadingStyleParser extends StyleParser { // Sets of additional styles (other than the main heading style for the level) private List> otherLevelStyles; @@ -56,43 +56,43 @@ public class HeadingStyleParser extends StyleFamilyParser { } @Override - public String getStyleSelectors(String sIndent) { - if (bConvertStyles) { - StringBuilder buf = new StringBuilder(); - for (int i = 1; i <= 6; i++) { - // Convert main style for this level - if (ofr.getHeadingStyle(i) != null) { - CSVList props = new CSVList(";"); - getParSc().applyProperties(ofr.getHeadingStyle(i), props, true); - props.addValue("clear", "left"); - buf.append(sIndent); - buf.append("h"); - buf.append(i); - buf.append(" {"); - buf.append(props.toString()); - buf.append("}"); - buf.append(config.prettyPrint() ? "\n" : " "); - } - // Convert other styles for this level - for (String sDisplayName : otherLevelStyles.get(i)) { - StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(sDisplayName); - CSVList props = new CSVList(";"); - getParSc().applyProperties(style, props, true); - props.addValue("clear", "left"); - buf.append(sIndent); - buf.append("h"); - buf.append(i); - buf.append("."); - buf.append(styleNames.addToExport(sDisplayName)); - buf.append(" {"); - buf.append(props.toString()); - buf.append("}"); - buf.append(config.prettyPrint() ? "\n" : " "); - } - } - return buf.toString(); + public String getStyleSelectors() { + if (!bConvertStyles) { + return ""; } - return ""; + StringBuilder result = new StringBuilder(); + for (int i = 1; i <= 6; i++) { + // Convert main style for this level + if (ofr.getHeadingStyle(i) != null) { + CSVList props = new CSVList(";"); + getParSc().applyProperties(ofr.getHeadingStyle(i), props, true); + props.addValue("clear", "left"); + result.append(indent); + result.append("h"); + result.append(i); + result.append(" {"); + result.append(props.toString()); + result.append("}"); + result.append(config.prettyPrint() ? "\n" : " "); + } + // Convert other styles for this level + for (String sDisplayName : otherLevelStyles.get(i)) { + StyleWithProperties style = (StyleWithProperties) getStyles().getStyleByDisplayName(sDisplayName); + CSVList props = new CSVList(";"); + getParSc().applyProperties(style, props, true); + props.addValue("clear", "left"); + result.append(indent); + result.append("h"); + result.append(i); + result.append("."); + result.append(styleNames.addToExport(sDisplayName)); + result.append(" {"); + result.append(props.toString()); + result.append("}"); + result.append(config.prettyPrint() ? "\n" : " "); + } + } + return result.toString(); } @Override diff --git a/src/main/java/writer2latex/xhtml/style/ListStyleFamilyParser.java b/src/main/java/writer2latex/xhtml/style/ListStyleParser.java similarity index 76% rename from src/main/java/writer2latex/xhtml/style/ListStyleFamilyParser.java rename to src/main/java/writer2latex/xhtml/style/ListStyleParser.java index a867455..2f37b65 100644 --- a/src/main/java/writer2latex/xhtml/style/ListStyleFamilyParser.java +++ b/src/main/java/writer2latex/xhtml/style/ListStyleParser.java @@ -44,7 +44,7 @@ import writer2latex.xhtml.XhtmlStyleMapItem; * This class converts OpenDocument list styles to * CSS2 styles (currently, actually CSS1). */ -public class ListStyleFamilyParser extends StyleFamilyParser { +public class ListStyleParser extends StyleParser { /** Create a new ListStyleConverter * @param ofr an OfficeReader to read style information from @@ -52,7 +52,7 @@ public class ListStyleFamilyParser extends StyleFamilyParser { * @param converter the main Converter class * @param nType the type of xhtml to use */ - public ListStyleFamilyParser(OfficeReader ofr, XhtmlConfig config, Converter converter, int nType) { + public ListStyleParser(OfficeReader ofr, XhtmlConfig config, Converter converter, int nType) { super(ofr,config,converter,nType); this.styleMap = config.getXListStyleMap(); this.bConvertStyles = config.xhtmlFormatting()==XhtmlConfig.CONVERT_ALL || config.xhtmlFormatting()==XhtmlConfig.IGNORE_HARD; @@ -90,49 +90,47 @@ public class ListStyleFamilyParser extends StyleFamilyParser { /**

Convert style information for used styles

* @param sIndent a String of spaces to add before each line */ - public String getStyleSelectors(String sIndent) { - if (bConvertStyles) { - StringBuilder buf = new StringBuilder(); - Enumeration names = styleNames.keys(); - while (names.hasMoreElements()) { - String sDisplayName = names.nextElement(); - ListStyle style = (ListStyle) - getStyles().getStyleByDisplayName(sDisplayName); - if (!style.isAutomatic()) { - for (int nLevel=1; nLevel<10; nLevel++) { - CSVList props = new CSVList(";"); - cssList(style,nLevel,props); - buf.append(sIndent); - buf.append(".listlevel"); - buf.append(nLevel); - buf.append(styleNames.addToExport(sDisplayName)); - buf.append(" {"); - buf.append(props.toString()); - buf.append("}"); - buf.append(config.prettyPrint() ? "\n" : " "); - if (config.listFormatting()==XhtmlConfig.HARD_LABELS) { - // Apply left margin and text indent to the paragraphs contained in the list - CSVList parProps = new CSVList(";"); - cssListParMargins(style,nLevel,parProps); - if (!parProps.isEmpty()) { - buf.append(sIndent); - buf.append(".listlevel"); - buf.append(nLevel); - buf.append(styleNames.addToExport(sDisplayName)); - buf.append(" p {"); - buf.append(parProps.toString()); - buf.append("}"); - buf.append(config.prettyPrint() ? "\n" : " "); - } - } - } - } - } - return buf.toString(); - } - else { - return ""; - } + public String getStyleSelectors() { + if (!bConvertStyles) { + return ""; + } + StringBuilder buf = new StringBuilder(); + Enumeration names = styleNames.keys(); + while (names.hasMoreElements()) { + String sDisplayName = names.nextElement(); + ListStyle style = (ListStyle) getStyles().getStyleByDisplayName(sDisplayName); + if (!style.isAutomatic()) { + for (int nLevel = 1; nLevel < 10; nLevel++) { + CSVList props = new CSVList(";"); + cssList(style, nLevel, props); + buf.append(indent); + buf.append(".listlevel"); + buf.append(nLevel); + buf.append(styleNames.addToExport(sDisplayName)); + buf.append(" {"); + buf.append(props.toString()); + buf.append("}"); + buf.append(config.prettyPrint() ? "\n" : " "); + if (config.listFormatting() == XhtmlConfig.HARD_LABELS) { + // Apply left margin and text indent to the paragraphs contained in + // the list + CSVList parProps = new CSVList(";"); + cssListParMargins(style, nLevel, parProps); + if (!parProps.isEmpty()) { + buf.append(indent); + buf.append(".listlevel"); + buf.append(nLevel); + buf.append(styleNames.addToExport(sDisplayName)); + buf.append(" p {"); + buf.append(parProps.toString()); + buf.append("}"); + buf.append(config.prettyPrint() ? "\n" : " "); + } + } + } + } + } + return buf.toString(); } /** Get the family of list styles diff --git a/src/main/java/writer2latex/xhtml/style/PageStyleParser.java b/src/main/java/writer2latex/xhtml/style/PageStyleParser.java index 0e17e0a..1a1ab78 100644 --- a/src/main/java/writer2latex/xhtml/style/PageStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/PageStyleParser.java @@ -48,7 +48,7 @@ import writer2latex.xhtml.XhtmlConfig; * In a presentation document we export the full page style. * In a text document we export the writing direction, background color and footnote rule for the first master page only */ -public class PageStyleParser extends StyleFamilyParser { +public class PageStyleParser extends StyleParser { private boolean bHasFootnoteRules = false; @@ -131,7 +131,7 @@ public class PageStyleParser extends StyleFamilyParser { /** Convert style information for used styles * @param sIndent a String of spaces to add before each line */ - public String getStyleSelectors(String sIndent) { + public String getStyleSelectors() { StringBuilder buf = new StringBuilder(); // This will be master pages for presentations only @@ -154,7 +154,7 @@ public class PageStyleParser extends StyleFamilyParser { cssDrawBackground(drawingPage,info.props,true); } // Then export the results - buf.append(sIndent); + buf.append(indent); buf.append(".masterpage"); buf.append(styleNames.addToExport(sDisplayName)); buf.append(" {"); @@ -177,7 +177,7 @@ public class PageStyleParser extends StyleFamilyParser { pageInfo.props.addValue("margin", "0"); }*/ if (pageInfo.hasAttributes()) { - buf.append(sIndent); + buf.append(indent); buf.append("body {"); buf.append(pageInfo.props.toString()); buf.append("}"); @@ -188,7 +188,7 @@ public class PageStyleParser extends StyleFamilyParser { if (bHasFootnoteRules) { StyleInfo ruleInfo = new StyleInfo(); cssFootnoteRule(pageLayout,ruleInfo.props); - buf.append(sIndent); + buf.append(indent); buf.append("hr.footnoterule {"); buf.append(ruleInfo.props.toString()); buf.append("}"); diff --git a/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java b/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java index 20afa6b..52b5214 100644 --- a/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/PresentationStyleParser.java @@ -86,10 +86,10 @@ public class PresentationStyleParser extends FrameStyleParser { /**

Convert style information for used styles

* @param sIndent a String of spaces to add before each line */ - public String getStyleSelectors(String sIndent) { + public String getStyleSelectors() { if (bConvertStyles) { StringBuilder buf = new StringBuilder(); - buf.append(super.getStyleSelectors(sIndent)); + buf.append(super.getStyleSelectors()); Enumeration names = outlineStyleNames.keys(); while (names.hasMoreElements()) { String sDisplayName = names.nextElement(); @@ -102,7 +102,7 @@ public class PresentationStyleParser extends FrameStyleParser { getParSc().cssPar(style,props,true); getTextSc().cssTextCommon(style,props,true); if (!props.isEmpty()) { - buf.append(sIndent); + buf.append(indent); buf.append("li.outline"); buf.append(styleNames.addToExport(sDisplayName)); buf.append(" p {"); diff --git a/src/main/java/writer2latex/xhtml/style/StyleFamilyParser.java b/src/main/java/writer2latex/xhtml/style/StyleFamilyParser.java deleted file mode 100644 index b1e6ff7..0000000 --- a/src/main/java/writer2latex/xhtml/style/StyleFamilyParser.java +++ /dev/null @@ -1,125 +0,0 @@ -/************************************************************************ - * - * StyleConverterHelper.java - * - * Copyright: 2002-2011 by Henrik Just - * - * This file is part of Writer2LaTeX. - * - * Writer2LaTeX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Writer2LaTeX is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Writer2LaTeX. If not, see . - * - * Version 1.2 (2011-03-10) - * - */ - -package writer2latex.xhtml.style; - -import writer2latex.office.OfficeReader; -import writer2latex.office.OfficeStyleFamily; -import writer2latex.office.StyleWithProperties; -import writer2latex.office.XMLString; -import writer2latex.util.Calc; -import writer2latex.util.ExportNameCollection; -import writer2latex.xhtml.Converter; -import writer2latex.xhtml.Parser; -import writer2latex.xhtml.StyleInfo; -import writer2latex.xhtml.XhtmlConfig; -import writer2latex.xhtml.XhtmlStyleMap; - -/** - *

This is an abstract base class to convert an OpenDocument style family to - * CSS2 styles.

- */ -public abstract class StyleFamilyParser extends Parser { - - // Translation of OpenDocument style names to CSS class names - protected ExportNameCollection styleNames = new ExportNameCollection(true); - - // Style map to use - protected XhtmlStyleMap styleMap; - - // Should we convert styles resp. hard formatting? - protected boolean bConvertStyles = true; - protected boolean bConvertHard = true; - - // The type of xhtml document - protected int nType; - - // Scaling and unit transformation to use - private String sScale; - private String sColScale; - private boolean bConvertToPx; - - /** Create a new StyleConverterHelper - * @param ofr an OfficeReader to read style information from - * @param config the configuration to use - * @param converter the main Converter class - * @param nType the type of xhtml to use - */ - public StyleFamilyParser(OfficeReader ofr, XhtmlConfig config, Converter converter, int nType) { - super(ofr,config,converter); - this.nType = nType; - sScale = config.getXhtmlScaling(); - sColScale = config.getXhtmlColumnScaling(); - bConvertToPx = config.xhtmlConvertToPx(); - } - - public String scale(String s) { - if (bConvertToPx) { - return Calc.length2px(Calc.multiply(sScale,s)); - } - else { - return Calc.length2rem(Calc.multiply(sScale,s)); - } - } - - public String colScale(String s) { - return scale(Calc.multiply(sColScale,s)); - } - - /** Apply the writing direction (ltr or rtl) attribute from a style - * @param style the OpenDocument style to use - * @param info the StyleInfo object to add information to - */ - protected static void applyDirection(StyleWithProperties style, StyleInfo info) { - String sDir = style.getProperty(XMLString.STYLE_WRITING_MODE); - if ("lr-tb".equals(sDir)) { info.sDir="ltr"; } - else if ("rl-tb".equals(sDir)) { info.sDir="rtl"; } - } - - /** Apply language+country from a style - * @param style the OpenDocument style to use - * @param info the StyleInfo object to add information to - */ - protected static void applyLang(StyleWithProperties style, StyleInfo info) { - String sLang = style.getProperty(XMLString.FO_LANGUAGE); - String sCountry = style.getProperty(XMLString.FO_COUNTRY); - if (sLang!=null) { - if (sCountry==null || sCountry.equals("none")) { info.sLang = sLang; } - else { info.sLang = sLang+"-"+sCountry; } - } - } - - /** Get the OpenDocument style family associated with this - * StyleConverterHelper - * @return the style family - */ - public abstract OfficeStyleFamily getStyles(); - - /**

Convert style information for used styles

- * @param sIndent a String of spaces to add before each line - */ - public abstract String getStyleSelectors(String sIndent); - -} diff --git a/src/main/java/writer2latex/xhtml/style/StyleParser.java b/src/main/java/writer2latex/xhtml/style/StyleParser.java index 7ccf724..71f9cdc 100644 --- a/src/main/java/writer2latex/xhtml/style/StyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/StyleParser.java @@ -1,6 +1,6 @@ /************************************************************************ * - * StyleConverter.java + * StyleConverterHelper.java * * Copyright: 2002-2015 by Henrik Just * @@ -19,207 +19,110 @@ * You should have received a copy of the GNU General Public License * along with Writer2LaTeX. If not, see . * - * Version 1.6 (2015-06-15) + * Version 1.2 (2011-03-10) * */ package writer2latex.xhtml.style; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; - -import writer2latex.office.*; -import writer2latex.util.*; +import writer2latex.office.OfficeReader; +import writer2latex.office.OfficeStyleFamily; +import writer2latex.office.StyleWithProperties; +import writer2latex.office.XMLString; +import writer2latex.util.Calc; +import writer2latex.util.ExportNameCollection; import writer2latex.xhtml.Converter; import writer2latex.xhtml.Parser; import writer2latex.xhtml.StyleInfo; import writer2latex.xhtml.XhtmlConfig; +import writer2latex.xhtml.XhtmlStyleMap; -/** This class converts OpenDocument styles to CSS2 styles. - * Note that some elements in OpenDocument has attributes that also maps to CSS2 properties. - * Example: the width of a text box. - * Also note, that some OpenDocument style properties cannot be mapped to CSS2 without creating an additional inline element. - * The class uses one helper class per OpenDocument style family (paragraph, frame etc.) +/** + *

This is an abstract base class to convert an OpenDocument style family to + * CSS2 styles.

*/ -public class StyleParser extends Parser { +public abstract class StyleParser extends Parser { - // Helpers for text styles - private TextStyleParser textSc; - private ParStyleParser parSc; - private HeadingStyleParser headingSc; - private ListStyleFamilyParser listSc; - private SectionStyleParser sectionSc; - - // Helpers for table styles - private TableStyleParser tableSc; - private RowStyleParser rowSc; - private CellStyleParser cellSc; - - // Helpers for drawing styles - private FrameStyleParser frameSc; - private PresentationStyleParser presentationSc; + // Translation of OpenDocument style names to CSS class names + protected ExportNameCollection styleNames = new ExportNameCollection(true); - // Helper for page styles - private PageStyleParser pageSc; - - /** Create a new StyleConverter - * - * @param ofr the office reader used to access the source document - * @param config the configuration to use - * @param converter the main converter - * @param nType the XHTML type + // Style map to use + protected XhtmlStyleMap styleMap; + + // Should we convert styles resp. hard formatting? + protected boolean bConvertStyles = true; + protected boolean bConvertHard = true; + + // The type of xhtml document + protected int nType; + private boolean prettyPrint = true; + String indent = null; + // Scaling and unit transformation to use + private String sScale; + private String sColScale; + private boolean bConvertToPx; + + /** Create a new StyleConverterHelper + * @param ofr an OfficeReader to read style information from + * @param config the configuration to use + * @param converter the main Converter class + * @param nType the type of xhtml to use */ public StyleParser(OfficeReader ofr, XhtmlConfig config, Converter converter, int nType) { super(ofr,config,converter); - // Create the helpers - textSc = new TextStyleParser(ofr,config,converter,nType); - parSc = new ParStyleParser(ofr,config,converter,nType); - headingSc = new HeadingStyleParser(ofr,config,converter,nType); - listSc = new ListStyleFamilyParser(ofr,config,converter,nType); - sectionSc = new SectionStyleParser(ofr,config,converter,nType); - tableSc = new TableStyleParser(ofr,config,converter,nType); - rowSc = new RowStyleParser(ofr,config,converter,nType); - cellSc = new CellStyleParser(ofr,config,converter,nType); - frameSc = new FrameStyleParser(ofr,config,converter,nType); - presentationSc = new PresentationStyleParser(ofr,config,converter,nType); - pageSc = new PageStyleParser(ofr,config,converter,nType); + this.nType = nType; + this.prettyPrint = config.prettyPrint(); + this.indent = prettyPrint ? " " : ""; + sScale = config.getXhtmlScaling(); + sColScale = config.getXhtmlColumnScaling(); + bConvertToPx = config.xhtmlConvertToPx(); } - - // Accessor methods for helpers: We need to override the style helper accessors - - public TextStyleParser getTextSc() { return textSc; } - public ParStyleParser getParSc() { return parSc; } - - public HeadingStyleParser getHeadingSc() { return headingSc; } - - public ListStyleFamilyParser getListSc() { return listSc; } - - public SectionStyleParser getSectionSc() { return sectionSc; } - - public TableStyleParser getTableSc() { return tableSc; } - - public RowStyleParser getRowSc() { return rowSc; } - - public CellStyleParser getCellSc() { return cellSc; } - - public FrameStyleParser getFrameSc() { return frameSc; } - - public PresentationStyleParser getPresentationSc() { return presentationSc; } - - public PageStyleParser getPageSc() { return pageSc; } - - /** Apply the default language of the source document on an XHTML element - * - * @param node the XHTML element - */ - public void applyDefaultLanguage(Element node) { - StyleWithProperties style = getDefaultStyle(); - if (style!=null) { - StyleInfo info = new StyleInfo(); - StyleFamilyParser.applyLang(style,info); - applyStyle(info,node); - } - } - - /** Export style information as a string of plain CSS code - * - * @param bIndent true if the CSS code should be indented - * @return the CSS code - */ - public String allStyleSelectors(boolean bIndent) { - String sIndent = bIndent ? " " : ""; - StringBuilder buf = new StringBuilder(); - - exportDefaultStyle(buf,sIndent); - - // Export declarations from helpers - // For OpenDocument documents created with OOo only some will generate content: - // Text documents: text, par, list, frame - // Spreadsheet documents: cell - // Presentation documents: frame, presentation, page - buf.append(textSc.getStyleSelectors(sIndent)); - buf.append(parSc.getStyleSelectors(sIndent)); - buf.append(headingSc.getStyleSelectors(sIndent)); - buf.append(listSc.getStyleSelectors(sIndent)); - buf.append(sectionSc.getStyleSelectors(sIndent)); - buf.append(cellSc.getStyleSelectors(sIndent)); - buf.append(tableSc.getStyleSelectors(sIndent)); - buf.append(rowSc.getStyleSelectors(sIndent)); - buf.append(frameSc.getStyleSelectors(sIndent)); - buf.append(presentationSc.getStyleSelectors(sIndent)); - buf.append(pageSc.getStyleSelectors(sIndent)); - return buf.toString(); - } - - /** Export style information as an XHTML style element - * - * @param htmlDOM the XHTML DOM to which the generated element belongs - * @return the style element - */ - public Node getStyleSelectors(Document htmlDOM) { - String styleSelectors = allStyleSelectors(config.prettyPrint()); - - // Create node - if (styleSelectors.length()>0) { - Element styleElement = htmlDOM.createElement("style"); - styleElement.setAttribute("media","all"); - styleElement.setAttribute("type","text/css"); - styleElement.appendChild(htmlDOM.createTextNode(config.prettyPrint() ? "\n" : " ")); - styleElement.appendChild(htmlDOM.createTextNode(styleSelectors)); - if (config.prettyPrint()) { styleElement.appendChild(htmlDOM.createTextNode(" ")); } - return styleElement; + public String scale(String s) { + if (bConvertToPx) { + return Calc.length2px(Calc.multiply(sScale,s)); } else { - return null; - } - } - - // Private helper methods - - private void exportDefaultStyle(StringBuilder buf, String sIndent) { - // Export default style - if (config.xhtmlCustomStylesheet().length()==0 && - (config.xhtmlFormatting()==XhtmlConfig.CONVERT_ALL || - config.xhtmlFormatting()==XhtmlConfig.IGNORE_HARD)) { - CSVList props = new CSVList(";"); - - // Default paragraph/cell/frame style is applied to the body element - StyleWithProperties defaultStyle = getDefaultStyle(); - if (defaultStyle!=null) { - // text properties only! - getTextSc().cssTextCommon(defaultStyle,props,true); - if (config.useDefaultFont() && config.defaultFontName().length()>0) { - props.addValue("font-family", "'"+config.defaultFontName()+"'"); - } - } - - // For text documents (XHTML only), also set maximum width - if (ofr.isText() && !converter.isOPS()) { - String sMaxWidth = config.getMaxWidth().trim(); - if (sMaxWidth.length()>0) { - props.addValue("max-width", sMaxWidth); - props.addValue("margin-left","auto"); - props.addValue("margin-right","auto"); - } - } - - // Apply properties to body - if (!props.isEmpty()) { - buf.append(sIndent); - buf.append("body {"); - buf.append(props.toString()); - buf.append("}"); - buf.append(config.prettyPrint() ? "\n" : " "); - } + return Calc.length2rem(Calc.multiply(sScale,s)); } } - private StyleWithProperties getDefaultStyle() { - if (ofr.isSpreadsheet()) return ofr.getDefaultCellStyle(); - else if (ofr.isPresentation()) return ofr.getDefaultFrameStyle(); - else return ofr.getDefaultParStyle(); + public String colScale(String s) { + return scale(Calc.multiply(sColScale,s)); + } + + /** Apply the writing direction (ltr or rtl) attribute from a style + * @param style the OpenDocument style to use + * @param info the StyleInfo object to add information to + */ + protected static void applyDirection(StyleWithProperties style, StyleInfo info) { + String sDir = style.getProperty(XMLString.STYLE_WRITING_MODE); + if ("lr-tb".equals(sDir)) { info.sDir="ltr"; } + else if ("rl-tb".equals(sDir)) { info.sDir="rtl"; } } -} \ No newline at end of file + /** Apply language+country from a style + * @param style the OpenDocument style to use + * @param info the StyleInfo object to add information to + */ + protected static void applyLang(StyleWithProperties style, StyleInfo info) { + String sLang = style.getProperty(XMLString.FO_LANGUAGE); + String sCountry = style.getProperty(XMLString.FO_COUNTRY); + if (sLang!=null) { + if (sCountry==null || sCountry.equals("none")) { info.sLang = sLang; } + else { info.sLang = sLang+"-"+sCountry; } + } + } + + /** Get the OpenDocument style family associated with this + * StyleConverterHelper + * @return the style family + */ + public abstract OfficeStyleFamily getStyles(); + + /**

Convert style information for used styles

+ * @param sIndent a String of spaces to add before each line + */ + public abstract String getStyleSelectors(); + +} diff --git a/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java b/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java index 8b96fc0..947276c 100644 --- a/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java +++ b/src/main/java/writer2latex/xhtml/style/StyleWithPropertiesParser.java @@ -41,8 +41,7 @@ import writer2latex.xhtml.XhtmlStyleMapItem; *

This is an abstract class to convert an OpenDocument style family * represented by StyleWithProperties to CSS2 styles.

*/ -public abstract class StyleWithPropertiesParser - extends StyleFamilyParser { +public abstract class StyleWithPropertiesParser extends StyleParser { /** Create a new StyleWithPropertiesConverterHelper * @param ofr an OfficeReader to read style information from @@ -95,7 +94,7 @@ public abstract class StyleWithPropertiesParser /** Convert style information for used styles * @param sIndent a String of spaces to add before each line */ - public String getStyleSelectors(String sIndent) { + public String getStyleSelectors() { if (bConvertStyles) { StringBuilder buf = new StringBuilder(); Enumeration names = styleNames.keys(); @@ -106,7 +105,7 @@ public abstract class StyleWithPropertiesParser if (!style.isAutomatic()) { CSVList props = new CSVList(";"); applyProperties(style,props,true); - buf.append(sIndent); + buf.append(indent); buf.append(getDefaultTagName(null)); buf.append("."); buf.append(getClassNamePrefix()); diff --git a/src/main/java/writer2latex/xhtml/style/Styles.java b/src/main/java/writer2latex/xhtml/style/Styles.java new file mode 100644 index 0000000..aed9cf8 --- /dev/null +++ b/src/main/java/writer2latex/xhtml/style/Styles.java @@ -0,0 +1,226 @@ +/************************************************************************ + * + * StyleConverter.java + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * Copyright: 2002-2015 by Henrik Just + * + * All Rights Reserved. + * + * Version 1.6 (2015-06-15) + * + */ + +package writer2latex.xhtml.style; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import writer2latex.office.*; +import writer2latex.util.*; +import writer2latex.xhtml.Converter; +import writer2latex.xhtml.Parser; +import writer2latex.xhtml.StyleInfo; +import writer2latex.xhtml.XhtmlConfig; + +/** This class converts OpenDocument styles to CSS2 styles. + * Note that some elements in OpenDocument has attributes that also maps to CSS2 properties. + * Example: the width of a text box. + * Also note, that some OpenDocument style properties cannot be mapped to CSS2 without creating an additional inline element. + * The class uses one helper class per OpenDocument style family (paragraph, frame etc.) + */ +public class Styles extends Parser { + + // Helpers for text styles + private TextStyleParser textStyleParser; + private ParStyleParser parStyleParser; + private HeadingStyleParser headingStyleParser; + private ListStyleParser listStyleParser; + private SectionStyleParser sectionStyleParser; + + // Helpers for table styles + private TableStyleParser tableStyleParser; + private RowStyleParser rowStyleParser; + private CellStyleParser cellStyleParser; + + // Helpers for drawing styles + private FrameStyleParser frameStyleParser; + private PresentationStyleParser presentationStyleParser; + + // Helper for page styles + private PageStyleParser pageStyleParser; + + /** Create a new StyleConverter + * + * @param ofr the office reader used to access the source document + * @param config the configuration to use + * @param converter the main converter + * @param nType the XHTML type + */ + public Styles(OfficeReader ofr, XhtmlConfig config, Converter converter, int nType) { + super(ofr,config,converter); + // Create the helpers + textStyleParser = new TextStyleParser(ofr,config,converter,nType); + parStyleParser = new ParStyleParser(ofr,config,converter,nType); + headingStyleParser = new HeadingStyleParser(ofr,config,converter,nType); + listStyleParser = new ListStyleParser(ofr,config,converter,nType); + sectionStyleParser = new SectionStyleParser(ofr,config,converter,nType); + tableStyleParser = new TableStyleParser(ofr,config,converter,nType); + rowStyleParser = new RowStyleParser(ofr,config,converter,nType); + cellStyleParser = new CellStyleParser(ofr,config,converter,nType); + frameStyleParser = new FrameStyleParser(ofr,config,converter,nType); + presentationStyleParser = new PresentationStyleParser(ofr,config,converter,nType); + pageStyleParser = new PageStyleParser(ofr,config,converter,nType); + } + + // Accessor methods for helpers: We need to override the style helper accessors + + public TextStyleParser getTextSc() { return textStyleParser; } + + public ParStyleParser getParSc() { return parStyleParser; } + + public HeadingStyleParser getHeadingSc() { return headingStyleParser; } + + public ListStyleParser getListSc() { return listStyleParser; } + + public SectionStyleParser getSectionSc() { return sectionStyleParser; } + + public TableStyleParser getTableSc() { return tableStyleParser; } + + public RowStyleParser getRowSc() { return rowStyleParser; } + + public CellStyleParser getCellSc() { return cellStyleParser; } + + public FrameStyleParser getFrameSc() { return frameStyleParser; } + + public PresentationStyleParser getPresentationSc() { return presentationStyleParser; } + + public PageStyleParser getPageSc() { return pageStyleParser; } + + /** Apply the default language of the source document on an XHTML element + * + * @param node the XHTML element + */ + public void applyDefaultLanguage(Element node) { + StyleWithProperties style = getDefaultStyle(); + if (style!=null) { + StyleInfo info = new StyleInfo(); + StyleParser.applyLang(style,info); + applyStyle(info,node); + } + } + + /** Export style information as a string of plain CSS code + * + * @param bIndent true if the CSS code should be indented + * @return the CSS code + */ + public String allStyleSelectors(boolean bIndent) { + String sIndent = bIndent ? " " : ""; + StringBuilder buf = new StringBuilder(); + + exportDefaultStyle(buf,sIndent); + + // Export declarations from helpers + // For OpenDocument documents created with OOo only some will generate content: + // Text documents: text, par, list, frame + // Spreadsheet documents: cell + // Presentation documents: frame, presentation, page + buf.append(textStyleParser.getStyleSelectors()); + buf.append(parStyleParser.getStyleSelectors()); + buf.append(headingStyleParser.getStyleSelectors()); + buf.append(listStyleParser.getStyleSelectors()); + buf.append(sectionStyleParser.getStyleSelectors()); + buf.append(cellStyleParser.getStyleSelectors()); + buf.append(tableStyleParser.getStyleSelectors()); + buf.append(rowStyleParser.getStyleSelectors()); + buf.append(frameStyleParser.getStyleSelectors()); + buf.append(presentationStyleParser.getStyleSelectors()); + buf.append(pageStyleParser.getStyleSelectors()); + return buf.toString(); + } + + /** Export style information as an XHTML style element + * + * @param htmlDOM the XHTML DOM to which the generated element belongs + * @return the style element + */ + public Node getStylesNode(Document htmlDOM) { + String styleSelectors = allStyleSelectors(config.prettyPrint()); + + // Create node + if (styleSelectors.length()>0) { + Element styleElement = htmlDOM.createElement("style"); + styleElement.setAttribute("media","all"); + styleElement.setAttribute("type","text/css"); + styleElement.appendChild(htmlDOM.createTextNode(config.prettyPrint() ? "\n" : " ")); + styleElement.appendChild(htmlDOM.createTextNode(styleSelectors)); + if (config.prettyPrint()) { styleElement.appendChild(htmlDOM.createTextNode(" ")); } + return styleElement; + } + else { + return null; + } + } + + // Private helper methods + + private void exportDefaultStyle(StringBuilder buf, String sIndent) { + // Export default style + if (config.xhtmlCustomStylesheet().length()==0 && + (config.xhtmlFormatting()==XhtmlConfig.CONVERT_ALL || + config.xhtmlFormatting()==XhtmlConfig.IGNORE_HARD)) { + CSVList props = new CSVList(";"); + + // Default paragraph/cell/frame style is applied to the body element + StyleWithProperties defaultStyle = getDefaultStyle(); + if (defaultStyle!=null) { + // text properties only! + getTextSc().cssTextCommon(defaultStyle,props,true); + if (config.useDefaultFont() && config.defaultFontName().length()>0) { + props.addValue("font-family", "'"+config.defaultFontName()+"'"); + } + } + + // For text documents (XHTML only), also set maximum width + if (ofr.isText() && !converter.isOPS()) { + String sMaxWidth = config.getMaxWidth().trim(); + if (sMaxWidth.length()>0) { + props.addValue("max-width", sMaxWidth); + props.addValue("margin-left","auto"); + props.addValue("margin-right","auto"); + } + } + + // Apply properties to body + if (!props.isEmpty()) { + buf.append(sIndent); + buf.append("body {"); + buf.append(props.toString()); + buf.append("}"); + buf.append(config.prettyPrint() ? "\n" : " "); + } + } + } + + private StyleWithProperties getDefaultStyle() { + if (ofr.isSpreadsheet()) return ofr.getDefaultCellStyle(); + else if (ofr.isPresentation()) return ofr.getDefaultFrameStyle(); + else return ofr.getDefaultParStyle(); + } + +} \ No newline at end of file diff --git a/src/main/java/writer2latex/xhtml/style/TextStyleParser.java b/src/main/java/writer2latex/xhtml/style/TextStyleParser.java index f004805..5f7850b 100644 --- a/src/main/java/writer2latex/xhtml/style/TextStyleParser.java +++ b/src/main/java/writer2latex/xhtml/style/TextStyleParser.java @@ -153,8 +153,8 @@ public class TextStyleParser extends StyleWithPropertiesParser { * @param sIndent a String of spaces to add before each line */ StringBuilder buf = new StringBuilder(); - public String getStyleSelectors(String sIndent) { - buf.append(super.getStyleSelectors(sIndent)); + public String getStyleSelectors() { + buf.append(super.getStyleSelectors()); if (bConvertStyles) { // Export anchor styles // Default is always the styles "Internet link" and "Visited Internet Link"(?) @@ -164,7 +164,7 @@ public class TextStyleParser extends StyleWithPropertiesParser { CSVList props = new CSVList(";"); cssText(defaultLinkStyle,props,true); cssHyperlink(defaultLinkStyle,props); - buf.append(sIndent); + buf.append(indent); buf.append("a:link {"); buf.append(props.toString()); buf.append("}\n"); @@ -176,7 +176,7 @@ public class TextStyleParser extends StyleWithPropertiesParser { CSVList props = new CSVList(";"); cssText(defaultLinkStyle,props,true); cssHyperlink(defaultLinkStyle,props); - buf.append(sIndent); + buf.append(indent); buf.append("a:visited {"); buf.append(props.toString()); buf.append("}\n"); @@ -195,7 +195,7 @@ public class TextStyleParser extends StyleWithPropertiesParser { CSVList props = new CSVList(";"); cssText(style,props,true); cssHyperlink(style,props); - buf.append(sIndent); + buf.append(indent); buf.append("a."); buf.append(sExportName); buf.append(":link {"); @@ -208,7 +208,7 @@ public class TextStyleParser extends StyleWithPropertiesParser { CSVList props = new CSVList(";"); cssText(style,props,true); cssHyperlink(style,props); - buf.append(sIndent); + buf.append(indent); buf.append("a."); buf.append(sExportName); buf.append(":visited {");