Skip to content

Commit 42ada12

Browse files
committed
Restructure Aspose.Words Java for Apache POI WP
1 parent 03df0da commit 42ada12

File tree

100 files changed

+2394
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2394
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.aspose</groupId>
5+
<artifactId>words-java</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<maven.compiler.source>1.7</maven.compiler.source>
10+
<maven.compiler.target>1.7</maven.compiler.target>
11+
</properties>
12+
<dependencies>
13+
<dependency>
14+
<groupId>com.aspose</groupId>
15+
<artifactId>aspose-words</artifactId>
16+
<version>15.6.0</version>
17+
<classifier>jdk16</classifier>
18+
</dependency>
19+
<dependency>
20+
<groupId>javax.media.jai</groupId>
21+
<artifactId>com.springsource.javax.media.jai.core</artifactId>
22+
<version>1.1.3</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.apache.poi</groupId>
26+
<artifactId>poi-ooxml</artifactId>
27+
<version>3.13</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.poi</groupId>
31+
<artifactId>poi-scratchpad</artifactId>
32+
<version>3.13</version>
33+
</dependency>
34+
</dependencies>
35+
<repositories>
36+
<repository>
37+
<id>aspose-maven-repository</id>
38+
<url>http://maven.aspose.com/repository/repo/</url>
39+
</repository>
40+
<repository>
41+
<id>com.springsource.repository.bundles.external</id>
42+
<name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
43+
<url>http://repository.springsource.com/maven/bundles/external</url>
44+
</repository>
45+
</repositories>
46+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.aspose.words.examples;
2+
3+
import com.aspose.words.License;
4+
5+
import java.io.File;
6+
7+
public class Utils {
8+
9+
public static String getDataDir(Class c) {
10+
File dir = new File(System.getProperty("user.dir"));
11+
dir = new File(dir, "src");
12+
dir = new File(dir, "main");
13+
dir = new File(dir, "resources");
14+
15+
for (String s : c.getName().split("\\.")) {
16+
dir = new File(dir, s);
17+
if (dir.isDirectory() == false)
18+
dir.mkdir();
19+
}
20+
21+
System.out.println("Using data directory: " + dir.toString());
22+
return dir.toString() + File.separator;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.aspose.words.examples.asposefeatures.mailmerge.mailmergefromxmldatasource;
2+
3+
import javax.xml.parsers.DocumentBuilder;
4+
import javax.xml.parsers.DocumentBuilderFactory;
5+
6+
import com.aspose.words.*;
7+
import com.aspose.words.examples.Utils;
8+
9+
/**
10+
* This sample demonstrates how to execute mail merge with data from an XML data source. The XML file is read into memory,
11+
* stored in a DOM and passed to a custom data source implementing IMailMergeDataSource. This returns each value from XML when
12+
* called by the mail merge engine.
13+
*/
14+
public class XMLMailMerge
15+
{
16+
public static void main(String[] args) throws Exception
17+
{
18+
// The path to the documents directory.
19+
String dataDir = Utils.getDataDir(XMLMailMerge.class);
20+
21+
// Use DocumentBuilder from the javax.xml.parsers package and Document class from the org.w3c.dom package to read
22+
// the XML data file and store it in memory.
23+
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
24+
25+
// Parse the XML data.
26+
org.w3c.dom.Document xmlData = db.parse(dataDir + "Customers.xml");
27+
28+
// Open a template document.
29+
Document doc = new Document(dataDir + "mergeDoc.doc");
30+
31+
// Note that this class also works with a single repeatable region (and any nested regions).
32+
// To merge multiple regions at the same time from a single XML data source, use the XmlMailMergeDataSet class.
33+
// e.g doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));
34+
doc.getMailMerge().execute(new XmlMailMergeDataTable(xmlData, "customer"));
35+
36+
// Save the output document.
37+
doc.save(dataDir + "AsposeMailMerge.doc");
38+
39+
System.out.println("Process Completed Successfully.");
40+
}
41+
}
42+
//ExEnd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2001-2014 Aspose Pty Ltd. All Rights Reserved.
3+
*
4+
* This file is part of Aspose.Words. The source code in this file
5+
* is only intended as a supplement to the documentation, and is provided
6+
* "as is", without warranty of any kind, either expressed or implied.
7+
*/
8+
package com.aspose.words.examples.asposefeatures.mailmerge.mailmergefromxmldatasource;
9+
10+
import com.aspose.words.*;
11+
12+
/**
13+
* Use this class when using mail merge with regions and nested mail merge with numerous regions. All regions in the document
14+
* are called here and data from XML.
15+
*/
16+
public class XmlMailMergeDataSet implements IMailMergeDataSourceRoot
17+
{
18+
/**
19+
* Creates a new XmlMailMergeDataSet for the specified XML document. All regions in the document can be
20+
* merged at once using this class.
21+
*
22+
* @param xmlDoc The DOM object which contains the parsed XML data.
23+
*/
24+
public XmlMailMergeDataSet(org.w3c.dom.Document xmlDoc)
25+
{
26+
mXmlDoc = xmlDoc;
27+
}
28+
29+
public IMailMergeDataSource getDataSource(String tableName) throws Exception
30+
{
31+
return new XmlMailMergeDataTable(mXmlDoc, tableName);
32+
}
33+
34+
private org.w3c.dom.Document mXmlDoc;
35+
}
36+
//ExEnd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright 2001-2014 Aspose Pty Ltd. All Rights Reserved.
3+
*
4+
* This file is part of Aspose.Words. The source code in this file
5+
* is only intended as a supplement to the documentation, and is provided
6+
* "as is", without warranty of any kind, either expressed or implied.
7+
*/
8+
package com.aspose.words.examples.asposefeatures.mailmerge.mailmergefromxmldatasource;
9+
10+
import com.aspose.words.*;
11+
import org.w3c.dom.Element;
12+
import org.w3c.dom.Node;
13+
14+
import javax.xml.xpath.XPath;
15+
import javax.xml.xpath.XPathConstants;
16+
import javax.xml.xpath.XPathExpression;
17+
import javax.xml.xpath.XPathFactory;
18+
import java.util.HashMap;
19+
20+
/**
21+
* A custom mail merge data source that allows you to merge data from an XML document into Word templates.
22+
* This class demonstrates how data can be read from a custom data source (XML parsed and loaded into a DOM) and merged
23+
* into a document using the IMailMergeDataSource interface.
24+
*
25+
* An instance of this class represents a single table in the data source and in the template.
26+
* Note: We are using the Document and Node class from the org.w3c.dom package here and not from Aspose.Words.
27+
*/
28+
public class XmlMailMergeDataTable implements IMailMergeDataSource
29+
{
30+
/**
31+
* Creates a new XmlMailMergeDataSource for the specified XML document and table name.
32+
*
33+
* @param xmlDoc The DOM object which contains the parsed XML data.
34+
* @param tableName The name of the element in the data source where the data of the region is extracted from.
35+
*/
36+
public XmlMailMergeDataTable(org.w3c.dom.Document xmlDoc, String tableName) throws Exception
37+
{
38+
this(xmlDoc.getDocumentElement(), tableName);
39+
}
40+
41+
/**
42+
* Private constructor that is also called by GetChildDataSource.
43+
*/
44+
private XmlMailMergeDataTable(Node rootNode, String tableName) throws Exception
45+
{
46+
mTableName = tableName;
47+
48+
// Get the first element on this level matching the table name.
49+
mCurrentNode = (Node)retrieveExpression("./" + tableName).evaluate(rootNode, XPathConstants.NODE);
50+
}
51+
52+
/**
53+
* The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
54+
*/
55+
public String getTableName()
56+
{
57+
return mTableName;
58+
}
59+
60+
/**
61+
* Aspose.Words calls this method to get a value for every data field.
62+
*/
63+
public boolean getValue(String fieldName, Object[] fieldValue) throws Exception
64+
{
65+
// Attempt to retrieve the child node matching the field name by using XPath.
66+
Node value = (Node)retrieveExpression(fieldName).evaluate(mCurrentNode, XPathConstants.NODE);
67+
// We also look for the field name in attributes of the element node.
68+
Element nodeAsElement = (Element)mCurrentNode;
69+
70+
if (value != null)
71+
{
72+
// Field exists in the data source as a child node, pass the value and return true.
73+
// This merges the data into the document.
74+
fieldValue[0] = value.getTextContent();
75+
return true;
76+
}
77+
else if (nodeAsElement.hasAttribute(fieldName))
78+
{
79+
// Field exists in the data source as an attribute of the current node, pass the value and return true.
80+
// This merges the data into the document.
81+
fieldValue[0] = nodeAsElement.getAttribute(fieldName);
82+
return true;
83+
}
84+
else
85+
{
86+
// Field does not exist in the data source, return false.
87+
// No value will be merged for this field and it is left over in the document.
88+
return false;
89+
}
90+
}
91+
92+
/**
93+
* Moves to the next record in a collection. This method is a little different then the regular implementation as
94+
* we are walking over an XML document stored in a DOM.
95+
*/
96+
public boolean moveNext()
97+
{
98+
if (!isEof())
99+
{
100+
// Don't move to the next node if this the first record to be merged.
101+
if (!mIsFirstRecord)
102+
{
103+
// Find the next node which is an element and matches the table name represented by this class.
104+
// This skips any text nodes and any elements which belong to a different table.
105+
do
106+
{
107+
mCurrentNode = mCurrentNode.getNextSibling();
108+
}
109+
while ((mCurrentNode != null) && !(mCurrentNode.getNodeName().equals(mTableName) && (mCurrentNode.getNodeType() == Node.ELEMENT_NODE)));
110+
}
111+
else
112+
{
113+
mIsFirstRecord = false;
114+
}
115+
}
116+
117+
return (!isEof());
118+
}
119+
120+
/**
121+
* If the data source contains nested data this method will be called to retrieve the data for
122+
* the child table. In the XML data source nested data this should look like this:
123+
*
124+
* <Tables>
125+
* <ParentTable>
126+
* <Name>ParentName</Name>
127+
* <ChildTable>
128+
* <Text>Content</Text>
129+
* </ChildTable>
130+
* </ParentTable>
131+
* </Tables>
132+
*/
133+
public IMailMergeDataSource getChildDataSource(String tableName) throws Exception
134+
{
135+
return new XmlMailMergeDataTable(mCurrentNode, tableName);
136+
}
137+
138+
private boolean isEof()
139+
{
140+
return (mCurrentNode == null);
141+
}
142+
143+
/**
144+
* Returns a cached version of a compiled XPathExpression if available, otherwise creates a new expression.
145+
*/
146+
private XPathExpression retrieveExpression(String path) throws Exception
147+
{
148+
XPathExpression expression;
149+
150+
if(mExpressionSet.containsKey(path))
151+
{
152+
expression = (XPathExpression)mExpressionSet.get(path);
153+
}
154+
else
155+
{
156+
expression = mXPath.compile(path);
157+
mExpressionSet.put(path, expression);
158+
}
159+
return expression;
160+
}
161+
162+
/**
163+
* Instance variables.
164+
*/
165+
private Node mCurrentNode;
166+
private boolean mIsFirstRecord = true;
167+
private final String mTableName;
168+
private final HashMap mExpressionSet = new HashMap();
169+
private final XPath mXPath = XPathFactory.newInstance().newXPath();
170+
}
171+
//ExEnd

0 commit comments

Comments
 (0)