Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main/doctypes/simplewpml/simplewpml-base.rng
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,23 @@
</choice>
</attribute>
</optional>

<ref name="style-atts"/>
<optional>
<attribute name="align">
<a:documentation>
<p>Specifies the alignment of the table with respect to the text margins, but not the text inside the table. The default is implementation-dependent, but 'start' is typical.</p>
</a:documentation>
<choice>
<value type="string">start</value>
<value type="string">center</value>
<value type="string">end</value>
</choice>
</attribute>
</optional>

<ref name="style-atts"/>
</element>
</define>

<define name="cols">
<element name="cols" ns="urn:ns:wordinator:simplewpml">
<a:documentation>
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TableRowAlign;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFAbstractFootnoteEndnote;
import org.apache.poi.xwpf.usermodel.XWPFAbstractNum;
Expand Down Expand Up @@ -2707,8 +2708,7 @@ private void makeTable(XWPFTable table, XmlObject xml) throws DocxGenerationExce

setTableIndents(table, cursor);
setTableLayout(table, cursor);


setTableAlign(table, cursor);

String styleName = cursor.getAttributeText(DocxConstants.QNAME_STYLE_ATT);
String styleId = cursor.getAttributeText(DocxConstants.QNAME_STYLEID_ATT);
Expand Down Expand Up @@ -2854,6 +2854,26 @@ private void setTableIndents(XWPFTable table, XmlCursor cursor) {

}

private void setTableAlign(XWPFTable table, XmlCursor cursor) throws DocxGenerationException {
String align = cursor.getAttributeText(DocxConstants.QNAME_ALIGN_ATT);
if (align == null) {
return;
}

TableRowAlign alignObj;
if (align.equals("start")) {
alignObj = TableRowAlign.LEFT;
} else if (align.equals("center")) {
alignObj = TableRowAlign.CENTER;
} else if (align.equals("end")) {
alignObj = TableRowAlign.RIGHT;
} else {
throw new DocxGenerationException("Unknown value for table align: '" +
align + "'");
}

table.setTableAlignment(alignObj);
}

/**
* Sets the w:tblLayout to fixed or auto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public static void convertMath(XWPFParagraph para, XmlObject indoc) throws DocxG
CTOMathPara ctOMathPara = CTOMathPara.Factory.parse(convertToOOML(indoc));

CTP ctp = para.getCTP();
ctp.setOMathArray(ctOMathPara.getOMathArray());

int oMathCount = ctp.getOMathList().size();

// add empty OMath
ctp.addNewOMath();

// then overwrite that empty OMath with ours
ctp.setOMathArray(oMathCount, ctOMathPara.getOMathArray()[0]);
} catch (XmlException e) {
// we seem to have produced bad OOXML, but this was done by the
// stylesheet the user supplied, so treating as user error
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.IRunElement;
import org.apache.poi.xwpf.usermodel.TableRowAlign;
import org.apache.poi.xwpf.usermodel.XWPFAbstractNum;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
Expand Down Expand Up @@ -862,6 +863,29 @@ public void testMultiSectionPageProps() throws Exception {
assertEquals(BigInteger.valueOf(16838), pageSz.getH());
}

public void testTableAlign() throws Exception {
XWPFDocument doc = convert("simplewp/simplewpml-table-align.swpx", "out/table-align.docx");
List<IBodyElement> contents = doc.getBodyElements();
assertEquals(2, contents.size());

// first table
Iterator<IBodyElement> it = contents.iterator();
IBodyElement elem = it.next();
assertEquals(BodyElementType.TABLE, elem.getElementType());

XWPFTable t = (XWPFTable) elem;
TableRowAlign align = t.getTableAlignment();
assertEquals(TableRowAlign.LEFT, align);

// second table
elem = it.next();
assertEquals(BodyElementType.TABLE, elem.getElementType());

t = (XWPFTable) elem;
align = t.getTableAlignment();
assertEquals(TableRowAlign.CENTER, align);
}

// ===== INTERNAL UTILITIES

private XWPFDocument convert(String infile, String outfile) throws Exception {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/wordinator/xml2docx/TestMathML.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ public void testEndingStep() throws Exception {
assertEquals("And it is possible for inline equations to be in the middle of paragraphs.", p.getText());
}

@Test
public void testTwoMathMLInOnePara() throws Exception {
XWPFDocument doc = convert("simplewp/simplewpml-mathml-03.xml", "out/testMathML.docx");

// first para of text
Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
XWPFParagraph p = iterator.next();
assertNotNull("Expected a paragraph", p);
assertEquals("When 12 test specimens of size  mm ×  mm are tested in accordance with Appendix B, the following apply:", p.getText());

// check the math in the para
CTP ctp = p.getCTP();
CTOMath[] maths = ctp.getOMathArray();
assertEquals(2, maths.length);

// there's no point in looking into the maths to see what's there,
// since that would amount to testing the stylesheet, but the
// stylesheet is not part of the Wordinator
}

private XWPFDocument convert(String infile, String outfile) throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File inFile = new File(classLoader.getResource(infile).getFile());
Expand Down
50 changes: 50 additions & 0 deletions src/test/resources/simplewp/simplewpml-mathml-03.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<wp:document xmlns:wp="urn:ns:wordinator:simplewpml">
<wp:body>
<wp:p style="Normal">
<wp:run>When 12 test specimens of size </wp:run>
<wp:run>
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" id="mml_m54" xlink:href="./mml_m54.png" display="inline">
<mrow>
<msubsup>
<mrow>
<mn>300</mn>
</mrow>
<mrow>
<mo>−</mo>
<mn>0</mn>
</mrow>
<mrow>
<mo>+</mo>
<mn>10</mn>
</mrow>
</msubsup>
</mrow>
</math>
</wp:run>
<wp:run> mm × </wp:run>
<wp:run>
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" id="mml_m55" xlink:href="./mml_m55.png" display="inline">
<mrow>
<msubsup>
<mrow>
<mn>300</mn>
</mrow>
<mrow>
<mo>−</mo>
<mn>0</mn>
</mrow>
<mrow>
<mo>+</mo>
<mn>10</mn>
</mrow>
</msubsup>
</mrow>
</math>
</wp:run>
<wp:run> mm are tested in accordance with </wp:run>
<wp:run>Appendix B</wp:run>
<wp:run>, the following apply:</wp:run>
</wp:p>
</wp:body>
</wp:document>
40 changes: 40 additions & 0 deletions src/test/resources/simplewp/simplewpml-table-align.swpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<wp:document xmlns:wp="urn:ns:wordinator:simplewpml">
<wp:body>
<!-- left-aligned table -->
<wp:table layout="auto" align="start">
<wp:tbody>
<wp:tr>
<wp:td>
<wp:p>
<wp:run>Left-aligned</wp:run>
</wp:p>
</wp:td>
<wp:td>
<wp:p>
<wp:run>table</wp:run>
</wp:p>
</wp:td>
</wp:tr>
</wp:tbody>
</wp:table>

<!-- centered table -->
<wp:table layout="auto" align="center">
<wp:tbody>
<wp:tr>
<wp:td>
<wp:p>
<wp:run>Centered</wp:run>
</wp:p>
</wp:td>
<wp:td>
<wp:p>
<wp:run>table</wp:run>
</wp:p>
</wp:td>
</wp:tr>
</wp:tbody>
</wp:table>
</wp:body>
</wp:document>