public class Repeats
{
private static final String ITEM1_XML
= "/customXml/item1.xml";
private static final String OUTPUT_FILE_PATH
= System.
getProperty("user.dir") + "/repeats-out.docx";
private static final String DATA_XML_FILE
= System.
getProperty("user.dir") + "/input_docs/invoice-data.xml";
private static final String DXA_TEXT
= "dxa";
private static final String OD_REPEAT_X2
= "od:repeat=x2";
private static final String STORE_ITEM_ID_OF_DATA_PART
= "{8B049945-9DFE-4726-9DE9-CF5691E53858}";
//private static final String STORE_ITEM_ID_OF_XPATHS_PART = "{5E015971-2421-4FFC-AD0A-CF7BDB8701E6}";
private static final String INVOICE_ITEMS_NAME_XPATH
= "/invoice[1]/items/item[1]/name";
private static final String INVOICE_ITEMS_PRICE_XPATH
= "/invoice[1]/items/item[1]/price";
private static final String ITEM_TEXT
= "Item";
private static final String PRICE_TEXT
= "Price";
private static final String REPEAT_TEXT
= "Repeat";
private static final int RANDOM_ID_2
= 1418037945;
private static final int TABLE_WIDTH_6771
= 6771;
private static final int TABLE_WIDTH_2268
= 2268;
private static final String DESCRIPTION_TEXT
= "Description";
private static final int RANDOM_ID_1
= 1418037946;
private static final String DESCRIPTION_OD_XPATH_X3
= "Description=Description&od:xpath=x3";
private static final String APPLES_TEXT
= "apples";
private static final String PRICE_OD_XPATH_X4
= "price=price&od:xpath=x4";
private static final int RANDOM_ID_3
= 1418037951;
private static final String TWENTY_DOLLARS
= "$20";
private static final String TABLE_GRID
= "TableGrid";
private static final String AUTO_TEXT
= "auto";
private static final String TABLE_LOOK_VALUE
= "04A0";
private static final int ZERO
= 0;
private static final String PLACEHOLDER_VALUE
= "E918F341A2F54C0A9743EE2731AB58B8";
private static CustomXmlDataStoragePart customXMLDataStoragePart
;
private Document document
;
private WordprocessingMLPackage
wordprocessingMLPackage;
private XPathsPart xPathsPart
;
public static void main
(String[] args
) throws Exception {
Repeats repeats
= new Repeats
();
repeats.
createDocumentWithRepeats();
// Add the data part
CustomXmlDataStoragePart customXmlDataStoragePart
= repeats.
injectCustomXmlDataStoragePart(repeats.
getMainDocumentPart(),
new File(DATA_XML_FILE
));
repeats.
addPropertiesPart(customXmlDataStoragePart, STORE_ITEM_ID_OF_DATA_PART
);
// Add the XPaths Part
repeats.
addXpathsPart();
repeats.
save();
}
public Repeats
() throws Docx4JException
{
document
= getWmlObjectFactory
().
createDocument();
wordprocessingMLPackage
= (WordprocessingMLPackage
) WordprocessingMLPackage.
load(new File(System.
getProperty("user.dir") + "/input_docs/empty.docx")).
clone();
}
private ObjectFactory getWmlObjectFactory
() {
return new ObjectFactory();
}
private MainDocumentPart getMainDocumentPart
(){
return wordprocessingMLPackage.
getMainDocumentPart();
}
private void save
() throws Docx4JException
{
String outputFilePath
= OUTPUT_FILE_PATH
;
wordprocessingMLPackage.
save(new File(outputFilePath
));
}
private void addXpathsPart
() throws InvalidFormatException
{
xPathsPart
= new XPathsPart
(new PartName
(ITEM1_XML
));
getMainDocumentPart
().
addTargetPart(xPathsPart, AddPartBehaviour.
RENAME_IF_NAME_EXISTS);
xPathsPart.
setJaxbElement(new Xpaths
());
// for Repeat
Xpaths.
Xpath xp
= createXpath
();
Xpaths.
Xpath.
DataBinding db
= createDataBinding
();
xp.
setId("x2");
db.
setXpath("/invoice[1]/items/item[1]");
db.
setStoreItemID(STORE_ITEM_ID_OF_DATA_PART
);
xp.
setDataBinding(db
);
xPathsPart.
getJaxbElement().
getXpath().
add(xp
);
// for Price
Xpaths.
Xpath xp2
= createXpath
();
Xpaths.
Xpath.
DataBinding db2
= createDataBinding
();
xp2.
setId("x4");
db2.
setXpath(INVOICE_ITEMS_PRICE_XPATH
);
db2.
setStoreItemID(STORE_ITEM_ID_OF_DATA_PART
);
xp2.
setDataBinding(db2
);
xPathsPart.
getJaxbElement().
getXpath().
add(xp2
);
// for item Name
Xpaths.
Xpath xp1
= createXpath
();
Xpaths.
Xpath.
DataBinding db1
= createDataBinding
();
xp1.
setId("x3");
db1.
setXpath(INVOICE_ITEMS_NAME_XPATH
);
db1.
setStoreItemID(STORE_ITEM_ID_OF_DATA_PART
);
xp1.
setDataBinding(db1
);
xPathsPart.
getJaxbElement().
getXpath().
add(xp1
);
//addPropertiesPart(xPathsPart, STORE_ITEM_ID_OF_XPATHS_PART);
}
private CustomXmlDataStoragePart injectCustomXmlDataStoragePart
(Part parent
) throws Exception {
org.
docx4j.
openpackaging.
parts.
CustomXmlDataStoragePart customXmlDataStoragePart
=
new org.
docx4j.
openpackaging.
parts.
CustomXmlDataStoragePart();
// Defaults to /customXml/item1.xml
CustomXmlDataStorage data
= new CustomXmlDataStorageImpl
();
data.
setDocument(createCustomXmlDocument
(new File(DATA_XML_FILE
)));
customXmlDataStoragePart.
setData(data
);
parent.
addTargetPart(customXmlDataStoragePart, RelationshipsPart.
AddPartBehaviour.
RENAME_IF_NAME_EXISTS);
return customXmlDataStoragePart
;
}
private void addPropertiesPart
(Part customXmlDataStoragePart,
String itemID
) throws InvalidFormatException
{
CustomXmlDataStoragePropertiesPart part
= new CustomXmlDataStoragePropertiesPart
();
org.
docx4j.
customXmlProperties.
ObjectFactory of
= new org.
docx4j.
customXmlProperties.
ObjectFactory();
DatastoreItem dsi
= of.
createDatastoreItem();
String newItemId
= itemID
;
dsi.
setItemID(newItemId
);
part.
setJaxbElement(dsi
);
customXmlDataStoragePart.
addTargetPart(part
);
}
private Xpaths.
Xpath.
DataBinding createDataBinding
() {
return new org.
opendope.
xpaths.
ObjectFactory().
createXpathsXpathDataBinding();
}
private Xpaths.
Xpath createXpath
() {
return new org.
opendope.
xpaths.
ObjectFactory().
createXpathsXpath();
}
public void createDocumentWithRepeats
() {
ObjectFactory wmlObjectFactory
= new ObjectFactory();
Body body
= getMainDocumentPart
().
getJaxbElement().
getBody();
// Create object for p
P p
= wmlObjectFactory.
createP();
body.
getContent().
add( p
);
// Create object for tbl (wrapped in JAXBElement)
Tbl tbl
= wmlObjectFactory.
createTbl();
JAXBElement
<org.
docx4j.
wml.
Tbl> tblWrapped
= wmlObjectFactory.
createBodyTbl(tbl
);
body.
getContent().
add( tblWrapped
);
// Create object for tr
Tr tr
= wmlObjectFactory.
createTr();
tbl.
getContent().
add( tr
);
// Create object for tc (wrapped in JAXBElement)
Tc tc
= wmlObjectFactory.
createTc();
JAXBElement
<org.
docx4j.
wml.
Tc> tcWrapped
= wmlObjectFactory.
createTrTc(tc
);
tr.
getContent().
add( tcWrapped
);
// Create object for p
P p2
= wmlObjectFactory.
createP();
tc.
getContent().
add( p2
);
// Create object for r
R r
= wmlObjectFactory.
createR();
p2.
getContent().
add( r
);
// Create object for t (wrapped in JAXBElement)
Text text
= wmlObjectFactory.
createText();
JAXBElement
<org.
docx4j.
wml.
Text> textWrapped
= wmlObjectFactory.
createRT(text
);
r.
getContent().
add( textWrapped
);
text.
setValue(ITEM_TEXT
);
// Create object for tcPr
TcPr tcpr
= wmlObjectFactory.
createTcPr();
tc.
setTcPr(tcpr
);
// Create object for tcW
TblWidth tblwidth
= wmlObjectFactory.
createTblWidth();
tcpr.
setTcW(tblwidth
);
tblwidth.
setW( BigInteger.
valueOf(TABLE_WIDTH_6771
) );
tblwidth.
setType(DXA_TEXT
);
// Create object for tc (wrapped in JAXBElement)
Tc tc2
= wmlObjectFactory.
createTc();
JAXBElement
<org.
docx4j.
wml.
Tc> tcWrapped2
= wmlObjectFactory.
createTrTc(tc2
);
tr.
getContent().
add( tcWrapped2
);
// Create object for p
P p3
= wmlObjectFactory.
createP();
tc2.
getContent().
add( p3
);
// Create object for r
R r2
= wmlObjectFactory.
createR();
p3.
getContent().
add( r2
);
// Create object for t (wrapped in JAXBElement)
Text text2
= wmlObjectFactory.
createText();
JAXBElement
<org.
docx4j.
wml.
Text> textWrapped2
= wmlObjectFactory.
createRT(text2
);
r2.
getContent().
add( textWrapped2
);
text2.
setValue(PRICE_TEXT
);
// Create object for tcPr
TcPr tcpr2
= wmlObjectFactory.
createTcPr();
tc2.
setTcPr(tcpr2
);
// Create object for tcW
TblWidth tblwidth2
= wmlObjectFactory.
createTblWidth();
tcpr2.
setTcW(tblwidth2
);
tblwidth2.
setW( BigInteger.
valueOf(TABLE_WIDTH_2268
) );
tblwidth2.
setType(DXA_TEXT
);
// The repeat
// Create object for sdt (wrapped in JAXBElement)
CTSdtRow sdtrow
= wmlObjectFactory.
createCTSdtRow();
JAXBElement
<org.
docx4j.
wml.
CTSdtRow> sdtrowWrapped
= wmlObjectFactory.
createTblSdt(sdtrow
);
tbl.
getContent().
add( sdtrowWrapped
);
// Create object for sdtPr
SdtPr sdtpr
= wmlObjectFactory.
createSdtPr();
sdtrow.
setSdtPr(sdtpr
);
// Create object for alias (wrapped in JAXBElement)
SdtPr.
Alias sdtpralias
= wmlObjectFactory.
createSdtPrAlias();
JAXBElement
<org.
docx4j.
wml.
SdtPr.
Alias> sdtpraliasWrapped
= wmlObjectFactory.
createSdtPrAlias(sdtpralias
);
sdtpr.
getRPrOrAliasOrLock().
add( sdtpraliasWrapped
);
sdtpralias.
setVal(REPEAT_TEXT
);
// Create object for tag
Tag tag
= wmlObjectFactory.
createTag();
sdtpr.
getRPrOrAliasOrLock().
add( tag
);
tag.
setVal(OD_REPEAT_X2
);
// Create object for id
Id id
= wmlObjectFactory.
createId();
sdtpr.
getRPrOrAliasOrLock().
add( id
);
id.
setVal( BigInteger.
valueOf(RANDOM_ID_2
) );
// Create object for tag
Tag tag2
= wmlObjectFactory.
createTag();
sdtpr.
setTag(tag2
);
tag2.
setVal(OD_REPEAT_X2
);
// Create object for id
Id id2
= wmlObjectFactory.
createId();
sdtpr.
setId(id2
);
id2.
setVal( BigInteger.
valueOf(RANDOM_ID_2
) );
// The repeat's contents
// Create object for sdtContent
CTSdtContentRow sdtcontentrow
= wmlObjectFactory.
createCTSdtContentRow();
sdtrow.
setSdtContent(sdtcontentrow
);
// Create object for tr
Tr tr2
= wmlObjectFactory.
createTr();
sdtcontentrow.
getContent().
add( tr2
);
// Create object for sdt (wrapped in JAXBElement)
CTSdtCell sdtcell
= wmlObjectFactory.
createCTSdtCell();
JAXBElement
<org.
docx4j.
wml.
CTSdtCell> sdtcellWrapped
= wmlObjectFactory.
createTrSdt(sdtcell
);
tr2.
getContent().
add( sdtcellWrapped
);
// Create object for sdtPr
SdtPr sdtpr2
= wmlObjectFactory.
createSdtPr();
sdtcell.
setSdtPr(sdtpr2
);
// Create object for dataBinding
CTDataBinding databinding
= wmlObjectFactory.
createCTDataBinding();
sdtpr2.
setDataBinding(databinding
);
databinding.
setStoreItemID(STORE_ITEM_ID_OF_DATA_PART
); // you'll need to alter this
databinding.
setXpath(INVOICE_ITEMS_NAME_XPATH
);
// Create object for alias (wrapped in JAXBElement)
SdtPr.
Alias sdtpralias2
= wmlObjectFactory.
createSdtPrAlias();
JAXBElement
<org.
docx4j.
wml.
SdtPr.
Alias> sdtpraliasWrapped2
= wmlObjectFactory.
createSdtPrAlias(sdtpralias2
);
sdtpr2.
getRPrOrAliasOrLock().
add( sdtpraliasWrapped2
);
sdtpralias2.
setVal(DESCRIPTION_TEXT
);
// Create object for tag
Tag tag3
= wmlObjectFactory.
createTag();
sdtpr2.
getRPrOrAliasOrLock().
add( tag3
);
tag3.
setVal(DESCRIPTION_OD_XPATH_X3
);
// Create object for id
Id id3
= wmlObjectFactory.
createId();
sdtpr2.
getRPrOrAliasOrLock().
add( id3
);
id3.
setVal( BigInteger.
valueOf(RANDOM_ID_1
) );
// Create object for text (wrapped in JAXBElement)
CTSdtText sdttext
= wmlObjectFactory.
createCTSdtText();
JAXBElement
<org.
docx4j.
wml.
CTSdtText> sdttextWrapped
= wmlObjectFactory.
createSdtPrText(sdttext
);
sdtpr2.
getRPrOrAliasOrLock().
add( sdttextWrapped
);
// Create object for tag
Tag tag4
= wmlObjectFactory.
createTag();
sdtpr2.
setTag(tag4
);
tag4.
setVal(DESCRIPTION_OD_XPATH_X3
); // you'll need to alter this
// Create object for id
Id id4
= wmlObjectFactory.
createId();
sdtpr2.
setId(id4
);
id4.
setVal( BigInteger.
valueOf(RANDOM_ID_1
) );
// Create object for sdtContent
CTSdtContentCell sdtcontentcell
= wmlObjectFactory.
createCTSdtContentCell();
sdtcell.
setSdtContent(sdtcontentcell
);
// Create object for tc (wrapped in JAXBElement)
Tc tc3
= wmlObjectFactory.
createTc();
JAXBElement
<org.
docx4j.
wml.
Tc> tcWrapped3
= wmlObjectFactory.
createCTSdtContentCellTc(tc3
);
sdtcontentcell.
getContent().
add( tcWrapped3
);
// Create object for p
P p4
= wmlObjectFactory.
createP();
tc3.
getContent().
add( p4
);
// Create object for r
R r3
= wmlObjectFactory.
createR();
p4.
getContent().
add( r3
);
// Create object for t (wrapped in JAXBElement)
Text text3
= wmlObjectFactory.
createText();
JAXBElement
<org.
docx4j.
wml.
Text> textWrapped3
= wmlObjectFactory.
createRT(text3
);
r3.
getContent().
add( textWrapped3
);
text3.
setValue(APPLES_TEXT
);
// Create object for tcPr
TcPr tcpr3
= wmlObjectFactory.
createTcPr();
tc3.
setTcPr(tcpr3
);
// Create object for tcW
TblWidth tblwidth3
= wmlObjectFactory.
createTblWidth();
tcpr3.
setTcW(tblwidth3
);
tblwidth3.
setW( BigInteger.
valueOf( TABLE_WIDTH_6771
) );
tblwidth3.
setType(DXA_TEXT
);
// Create object for sdt (wrapped in JAXBElement)
CTSdtCell sdtcell2
= wmlObjectFactory.
createCTSdtCell();
JAXBElement
<org.
docx4j.
wml.
CTSdtCell> sdtcellWrapped2
= wmlObjectFactory.
createTrSdt(sdtcell2
);
tr2.
getContent().
add( sdtcellWrapped2
);
// Create object for sdtPr
SdtPr sdtpr3
= wmlObjectFactory.
createSdtPr();
sdtcell2.
setSdtPr(sdtpr3
);
// Create object for dataBinding
CTDataBinding databinding3
= wmlObjectFactory.
createCTDataBinding();
sdtpr3.
setDataBinding(databinding3
);
databinding3.
setStoreItemID(STORE_ITEM_ID_OF_DATA_PART
);
databinding3.
setXpath(INVOICE_ITEMS_PRICE_XPATH
);
// Create object for alias (wrapped in JAXBElement)
SdtPr.
Alias sdtpralias3
= wmlObjectFactory.
createSdtPrAlias();
JAXBElement
<org.
docx4j.
wml.
SdtPr.
Alias> sdtpraliasWrapped3
= wmlObjectFactory.
createSdtPrAlias(sdtpralias3
);
sdtpr3.
getRPrOrAliasOrLock().
add( sdtpraliasWrapped3
);
sdtpralias3.
setVal(PRICE_TEXT
);
// Create object for tag
Tag tag5
= wmlObjectFactory.
createTag();
sdtpr3.
getRPrOrAliasOrLock().
add( tag5
);
tag5.
setVal(PRICE_OD_XPATH_X4
);
// Create object for id
Id id5
= wmlObjectFactory.
createId();
sdtpr3.
getRPrOrAliasOrLock().
add( id5
);
id5.
setVal( BigInteger.
valueOf(RANDOM_ID_3
) );
// Create object for placeholder (wrapped in JAXBElement)
CTPlaceholder placeholder3
= wmlObjectFactory.
createCTPlaceholder();
JAXBElement
<org.
docx4j.
wml.
CTPlaceholder> placeholderWrapped3
= wmlObjectFactory.
createSdtPrPlaceholder(placeholder3
);
sdtpr3.
getRPrOrAliasOrLock().
add( placeholderWrapped3
);
// Create object for docPart
CTPlaceholder.
DocPart placeholderdocpart3
= wmlObjectFactory.
createCTPlaceholderDocPart();
placeholder3.
setDocPart(placeholderdocpart3
);
placeholderdocpart3.
setVal(PLACEHOLDER_VALUE
);
// Create object for text (wrapped in JAXBElement)
CTSdtText sdttext2
= wmlObjectFactory.
createCTSdtText();
JAXBElement
<org.
docx4j.
wml.
CTSdtText> sdttextWrapped2
= wmlObjectFactory.
createSdtPrText(sdttext2
);
sdtpr3.
getRPrOrAliasOrLock().
add( sdttextWrapped2
);
// Create object for tag
Tag tag6
= wmlObjectFactory.
createTag();
sdtpr3.
setTag(tag6
);
tag6.
setVal(PRICE_OD_XPATH_X4
);
// Create object for id
Id id6
= wmlObjectFactory.
createId();
sdtpr3.
setId(id6
);
id6.
setVal( BigInteger.
valueOf(RANDOM_ID_3
) );
// Create object for sdtContent
CTSdtContentCell sdtcontentcell2
= wmlObjectFactory.
createCTSdtContentCell();
sdtcell2.
setSdtContent(sdtcontentcell2
);
// Create object for tc (wrapped in JAXBElement)
Tc tc4
= wmlObjectFactory.
createTc();
JAXBElement
<org.
docx4j.
wml.
Tc> tcWrapped4
= wmlObjectFactory.
createCTSdtContentCellTc(tc4
);
sdtcontentcell2.
getContent().
add( tcWrapped4
);
// Create object for p
P p5
= wmlObjectFactory.
createP();
tc4.
getContent().
add( p5
);
// Create object for r
R r4
= wmlObjectFactory.
createR();
p5.
getContent().
add( r4
);
// Create object for t (wrapped in JAXBElement)
Text text4
= wmlObjectFactory.
createText();
JAXBElement
<org.
docx4j.
wml.
Text> textWrapped4
= wmlObjectFactory.
createRT(text4
);
r4.
getContent().
add( textWrapped4
);
text4.
setValue(TWENTY_DOLLARS
);
// Create object for tcPr
TcPr tcpr4
= wmlObjectFactory.
createTcPr();
tc4.
setTcPr(tcpr4
);
// Create object for tcW
TblWidth tblwidth4
= wmlObjectFactory.
createTblWidth();
tcpr4.
setTcW(tblwidth4
);
tblwidth4.
setW( BigInteger.
valueOf(TABLE_WIDTH_2268
) );
tblwidth4.
setType(DXA_TEXT
);
// Create object for tblPr
TblPr tblpr
= wmlObjectFactory.
createTblPr();
tbl.
setTblPr(tblpr
);
// Create object for tblStyle
CTTblPrBase.
TblStyle tblprbasetblstyle
= wmlObjectFactory.
createCTTblPrBaseTblStyle();
tblpr.
setTblStyle(tblprbasetblstyle
);
tblprbasetblstyle.
setVal(TABLE_GRID
);
// Create object for tblW
TblWidth tblwidth5
= wmlObjectFactory.
createTblWidth();
tblpr.
setTblW(tblwidth5
);
tblwidth5.
setW( BigInteger.
valueOf(ZERO
) );
tblwidth5.
setType(AUTO_TEXT
);
// Create object for tblLook
CTTblLook tbllook
= wmlObjectFactory.
createCTTblLook();
tblpr.
setTblLook(tbllook
);
tbllook.
setVal(TABLE_LOOK_VALUE
);
tbllook.
setFirstColumn(org.
docx4j.
sharedtypes.
STOnOff.
ONE);
tbllook.
setFirstRow(org.
docx4j.
sharedtypes.
STOnOff.
ONE);
tbllook.
setLastColumn(org.
docx4j.
sharedtypes.
STOnOff.
ZERO);
tbllook.
setLastRow(org.
docx4j.
sharedtypes.
STOnOff.
ZERO);
tbllook.
setNoHBand(org.
docx4j.
sharedtypes.
STOnOff.
ZERO);
tbllook.
setNoVBand(org.
docx4j.
sharedtypes.
STOnOff.
ONE);
// Create object for tblGrid
TblGrid tblgrid
= wmlObjectFactory.
createTblGrid();
tbl.
setTblGrid(tblgrid
);
// Create object for gridCol
TblGridCol tblgridcol
= wmlObjectFactory.
createTblGridCol();
tblgrid.
getGridCol().
add( tblgridcol
);
tblgridcol.
setW( BigInteger.
valueOf( TABLE_WIDTH_6771
) );
// Create object for gridCol
TblGridCol tblgridcol2
= wmlObjectFactory.
createTblGridCol();
tblgrid.
getGridCol().
add( tblgridcol2
);
tblgridcol2.
setW( BigInteger.
valueOf(TABLE_WIDTH_2268
) );
// Create object for p
P p6
= wmlObjectFactory.
createP();
body.
getContent().
add( p6
);
}
private CustomXmlDataStoragePart injectCustomXmlDataStoragePart
(Part parent,
File inputXmlFile
) throws Exception {
org.
docx4j.
openpackaging.
parts.
CustomXmlDataStoragePart customXmlDataStoragePart
=
new org.
docx4j.
openpackaging.
parts.
CustomXmlDataStoragePart();
// Defaults to /customXml/item1.xml
CustomXmlDataStorage data
= new CustomXmlDataStorageImpl
();
data.
setDocument(createCustomXmlDocument
(inputXmlFile
));
customXmlDataStoragePart.
setData(data
);
parent.
addTargetPart(customXmlDataStoragePart, RelationshipsPart.
AddPartBehaviour.
RENAME_IF_NAME_EXISTS);
return customXmlDataStoragePart
;
}
private org.
w3c.
dom.
Document createCustomXmlDocument
(File xmlFile
) throws ParserConfigurationException,
IOException, SAXException
{
DocumentBuilderFactory documentBuilderFactory
= DocumentBuilderFactory.
newInstance();
documentBuilderFactory.
setNamespaceAware(true);
DocumentBuilder documentBuilder
= documentBuilderFactory.
newDocumentBuilder();
org.
w3c.
dom.
Document document
= documentBuilder.
parse(xmlFile
);
return document
;
}
}
Parsed in 0.060 seconds, using
GeSHi 1.0.8.4