diff --git a/dynamictable/Employee.java b/dynamictable/Employee.java new file mode 100644 index 0000000..803a75a --- /dev/null +++ b/dynamictable/Employee.java @@ -0,0 +1,119 @@ +import javax.xml.bind.annotation.*; +public class Employee +{ + private String Name; + private String Title; + private String Address; + private String HomePhone; + private String Photo; + @XmlElement(name = "Name") + /** + * Gets the employee name. + */ + public String getName() + { + return Name; + } + /** + * Sets the employee name. + * + * @param name Name of the employee. + */ + public void setName(String name) + { + this.Name = name; + } + @XmlElement(name = "Title") + /** + * Gets the designation of the employee. + */ + public String getTitle() + { + return Title; + } + /** + * Sets the designation of the employee. + * + * @param title Designation of the employee. + */ + public void setTitle(String title) + { + this.Title = title; + } + @XmlElement(name = "Address") + /** + * Gets the address of the employee. + */ + public String getAddress() + { + return Address; + } + /** + * Sets the address of the employee. + * + * @param address Address of the employee. + */ + public void setAddress(String address) + { + this.Address = address; + } + @XmlElement(name = "HomePhone") + /** + * Gets the contact number of the employee. + */ + public String getHomePhone() + { + return HomePhone; + } + /** + * Sets the contact number of the employee. + * + * @param homePhone Contact number of the employee. + */ + public void setHomePhone(String homePhone) + { + this.HomePhone = homePhone; + } + @XmlElement(name = "Photo") + /** + * Gets the photo of the employee. + */ + public String getPhoto() + { + return Photo; + } + /** + * Sets the photo of the employee. + * + * @param photo Photo of the employee. + */ + public void setPhoto(String photo) + { + this.Photo = photo; + } + /** + * Initializes a new instance of the Employee class with the specified name, + * title, address, contact number and photo. + * + * @param name Name of the employee. + * @param title Designation of the employee. + * @param address Address of the employee. + * @param homePhone Contact number of the employee. + * @param photo Photo of the employee. + * + */ + public Employee(String name, String title, String address, String homePhone, String photo) + { + this.Name = name; + this.Title = title; + this.Address = address; + this.HomePhone = homePhone; + this.Photo = photo; + } + /** + * Initializes a new instance of the Employee class. + */ + public Employee() + { + } +} diff --git a/dynamictable/Employees.java b/dynamictable/Employees.java index 47f524a..d16e5c5 100644 --- a/dynamictable/Employees.java +++ b/dynamictable/Employees.java @@ -1,76 +1,40 @@ +import java.util.List; +import javax.xml.bind.annotation.*; +@XmlRootElement(name = "Employees") public class Employees { - private String m_name; - private String m_title; - private String m_address; - private String m_homePhone; - private String m_photo; - // Gets the employee name. - public String getName() throws Exception { - return m_name; - } - // Sets the employee name. - public String setName(String value) throws Exception { - m_name = value; - return value; - } - // Gets the designation of the employee. - public String getTitle() throws Exception { - return m_title; - } - // Sets the designation of the employee. - public String setTitle(String value) throws Exception { - m_title = value; - return value; - } - // Gets the address of the employee. - public String getAddress() throws Exception { - return m_address; - } - // Sets the address of the employee. - public String setAddress(String value) throws Exception { - m_address = value; - return value; - } - // Gets the contact number of the employee. - public String getHomePhone() throws Exception { - return m_homePhone; - } - // Sets the contact number of the employee. - public String setHomePhone(String value) throws Exception { - m_homePhone = value; - return value; - } - // Gets the photo of the employee. - public String getPhoto() throws Exception { - return m_photo; - } - // Sets the photo of the employee. - public String setPhoto(String value) throws Exception { - m_photo = value; - return value; + private List Employee; + /** + * Gets the list of employees. + */ + @XmlElement(name = "Employee") + public List getEmployees() + { + return Employee; } /** - * Initializes a new instance of the Employees class with the specified name, - * title, address, contact number and photo. + * Sets the list of employees. * - * @param name Name of the employee. - * @param title Designation of the employee. - * @param address Address of the employee. - * @param homePhone Contact number of the employee. - * @param photo Photo of the employee. - * @throws Exception + * @param employee List of employee. */ - public Employees(String name, String title, String address, String homePhone, String photo) throws Exception { - m_name = name; - m_title = title; - m_address = address; - m_homePhone = homePhone; - m_photo = photo; + public void setEmployees(List employee) + { + this.Employee = employee; } /** * Initializes a new instance of the Employees class. */ - public Employees() throws Exception { + public Employees() + { + } + /** + * Initializes a new instance of the Employees class with the specified list of + * employee. + * + * @param employees List of employee. + */ + public Employees(List employees) + { + this.Employee = employees; } } diff --git a/dynamictable/WordTable.java b/dynamictable/WordTable.java index 02efbc6..ffd3e24 100644 --- a/dynamictable/WordTable.java +++ b/dynamictable/WordTable.java @@ -1,25 +1,32 @@ import java.io.*; +import java.util.List; +import javax.xml.bind.*; import com.syncfusion.docio.*; import com.syncfusion.javahelper.system.*; -import com.syncfusion.javahelper.system.collections.generic.ListSupport; -import com.syncfusion.javahelper.system.io.*; -import com.syncfusion.javahelper.system.xml.*; -public class WordTable{ - public static void main(String[] args) throws Exception { +public class WordTable +{ + public static void main(String[] args) throws Exception + { + // Loads the XML file. + File file = new File(getDataDir("EmployeesList.xml")); + // Create a new instance for the JAXBContext. + JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class); + // Reads the XML file. + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + Employees employees = (Employees) jaxbUnmarshaller.unmarshal(file); + // Gets the list of employee details. + List employeeList = employees.getEmployees(); // Loads the template document. WordDocument document = new WordDocument(getDataDir("WordTable_Template.docx")); - // Creates a list of employee details. - ListSupport employeeDetails = getEmployeeDetails(); // Iterates each item in the list. - for (Employees employee : employeeDetails) + for (Employee employee : employeeList) { // Accesses the table in the document. IWTable table = document.getSections().get(0).getTables().get(0); // Initializes the paragraph and add new row to the table. IWParagraph paragraph = null; - WTableRow newRow = null; - newRow = table.addRow(); + table.addRow(); // Gets the employee photo and convert that base64 string to bytes. byte[] bytes = ConvertSupport.fromBase64String(employee.getPhoto()); ByteArrayInputStream stream = new ByteArrayInputStream(bytes); @@ -36,101 +43,6 @@ public static void main(String[] args) throws Exception { document.close(); } - /** - * - * Gets the list of employee details. - * - */ - private static ListSupport getEmployeeDetails() throws Exception { - // Gets list of employee details. - ListSupport employees = new ListSupport(Employees.class); - // Reads the xml document. - FileStreamSupport fs = new FileStreamSupport(getDataDir("EmployeesList.xml"), FileMode.Open, FileAccess.Read); - XmlReaderSupport reader = XmlReaderSupport.create(fs); - if (reader == null) - throw new Exception("reader"); - while (reader.getNodeType() != XmlNodeType.Element) - reader.read(); - if (reader.getLocalName() != "Employees") - throw new Exception(StringSupport.concat("Unexpected xml tag ", reader.getLocalName())); - reader.read(); - while (reader.getNodeType() == XmlNodeType.Whitespace) - reader.read(); - // Iterates to add the employee details in list. - while (reader.getLocalName() != "Employees") - { - if (reader.getNodeType() == XmlNodeType.Element) - { - switch (reader.getLocalName()) - { - case "Employee": - employees.add(getEmployees(reader)); - break; - } - } - else - { - reader.read(); - if ((reader.getLocalName() == "Employees") && reader.getNodeType() == XmlNodeType.EndElement) - break; - } - } - return employees; - } - - /** - * - * Gets the employees. - * - * @param reader Syncfusion's XML reader to read the XML files.. - */ - private static Employees getEmployees(XmlReaderSupport reader) throws Exception { - if (reader == null) - throw new Exception("reader"); - while (reader.getNodeType() != XmlNodeType.Element) - reader.read(); - if (reader.getLocalName() != "Employee") - throw new Exception(StringSupport.concat("Unexpected xml tag ", reader.getLocalName())); - reader.read(); - while (reader.getNodeType() == XmlNodeType.Whitespace) - reader.read(); - Employees employee = new Employees(); - while (reader.getLocalName() != "Employee") - { - if (reader.getNodeType() == XmlNodeType.Element) - { - switch (reader.getLocalName()) - { - case "Name": - employee.setName(reader.readContentAsString()); - break; - case "Title": - employee.setTitle(reader.readContentAsString()); - break; - case "Address": - employee.setAddress(reader.readContentAsString()); - break; - case "HomePhone": - employee.setHomePhone(reader.readContentAsString()); - break; - case "Photo": - employee.setPhoto(reader.readContentAsString()); - break; - default: - reader.skip(); - break; - } - } - else - { - reader.read(); - if ((reader.getLocalName() == "Employee") && reader.getNodeType() == XmlNodeType.EndElement) - break; - } - } - return employee; - } - /** * Get the file path * diff --git a/dynamictableusingmailmerge/DynamicTableUsingMailmerge.java b/dynamictableusingmailmerge/DynamicTableUsingMailmerge.java index ccec183..18cfd4d 100644 --- a/dynamictableusingmailmerge/DynamicTableUsingMailmerge.java +++ b/dynamictableusingmailmerge/DynamicTableUsingMailmerge.java @@ -1,124 +1,38 @@ import java.io.File; +import java.util.List; import com.syncfusion.docio.*; import com.syncfusion.javahelper.system.collections.generic.ListSupport; -import com.syncfusion.javahelper.system.io.*; -import com.syncfusion.javahelper.system.xml.*; +import javax.xml.bind.*; -public class DynamicTableUsingMailmerge { - - public static void main(String[] args) throws Exception { +public class DynamicTableUsingMailmerge +{ + public static void main(String[] args) throws Exception + { + // Loads the XML file. + File file = new File(getDataDir("StockDetails.xml")); + // Create a new instance for the JAXBContext. + JAXBContext jaxbContext = JAXBContext.newInstance(StockMarket.class); + // Reads the XML file. + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + StockMarket stockMarket = (StockMarket) jaxbUnmarshaller.unmarshal(file); + // Gets the list of stock details. + List list = stockMarket.getStockDetails(); + ListSupport stockDetails = new ListSupport(StockDetails.class); + for(StockDetails stock : list) + { + //Add all items in the list to ListSupport to perform mail merge. + stockDetails.add(stock); + } // Loads the template document. WordDocument document = new WordDocument(getDataDir("Template.docx"), FormatType.Docx); - // Creates MailMergeDataTable. - MailMergeDataTable mailMergeDataTableStock = getMailMergeDataTableStock(); + MailMergeDataTable dataTable = new MailMergeDataTable("StockDetails", stockDetails); // Executes Mail Merge with group. - document.getMailMerge().executeGroup(mailMergeDataTableStock); + document.getMailMerge().executeGroup(dataTable); // Saves and closes the document. document.save("Result.docx", FormatType.Docx); document.close(); } - /** - * - * Gets the mail merge data table. - * - */ - private static MailMergeDataTable getMailMergeDataTableStock() throws Exception { - // Gets list of stock details. - ListSupport stockDetails = new ListSupport(StockDetails.class); - FileStreamSupport fs = new FileStreamSupport(getDataDir("StockDetails.xml"), FileMode.Open, FileAccess.Read); - // Reads the xml document. - XmlReaderSupport reader = XmlReaderSupport.create(fs); - if (reader == null) - throw new Exception("reader"); - while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue()) - reader.read(); - if (!(reader.getLocalName() == "StockMarket")) - throw new Exception("Unexpected xml tag " + reader.getLocalName()); - reader.read(); - while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue()) - reader.read(); - // Iterates to add the stock details in list. - while (!(reader.getLocalName() == "StockMarket")) - { - if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue()) - { - switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName())) - { - case "StockDetails": - stockDetails.add(getStockDetails(reader)); - break; - } - } - else - { - reader.read(); - if ((reader.getLocalName() == "StockMarket") - && reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue()) - break; - } - } - // Creates an instance of MailMergeDataTable by specifying mail merge group name and IEnumerable collection. - MailMergeDataTable dataTable = new MailMergeDataTable("StockDetails", stockDetails); - reader.close(); - fs.close(); - return dataTable; - } - - /** - * - * Gets the StockDetails. - * - * @param reader Syncfusion's XML reader to read the XML files. - */ - private static StockDetails getStockDetails(XmlReaderSupport reader) throws Exception { - if (reader == null) - throw new Exception("reader"); - while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue()) - reader.read(); - if (!(reader.getLocalName() == "StockDetails")) - throw new Exception("Unexpected xml tag " + reader.getLocalName()); - reader.read(); - while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue()) - reader.read(); - StockDetails stockDetails = new StockDetails(); - while (!(reader.getLocalName() == "StockDetails")) - { - if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue()) - { - switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName())) - { - case "TradeNo": - stockDetails.setTradeNo(reader.readContentAsString()); - break; - case "CompanyName": - stockDetails.setCompanyName(reader.readContentAsString()); - break; - case "CostPrice": - stockDetails.setCostPrice(reader.readContentAsString()); - break; - case "SharesCount": - stockDetails.setSharesCount(reader.readContentAsString()); - break; - case "SalesPrice": - stockDetails.setSalesPrice(reader.readContentAsString()); - break; - default: - reader.skip(); - break; - } - } - else - { - reader.read(); - if ((reader.getLocalName() == "StockDetails") - && reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue()) - break; - } - } - return stockDetails; - } - /** * Get the file path * diff --git a/dynamictableusingmailmerge/StockDetails.java b/dynamictableusingmailmerge/StockDetails.java index afa4669..70c97e5 100644 --- a/dynamictableusingmailmerge/StockDetails.java +++ b/dynamictableusingmailmerge/StockDetails.java @@ -1,75 +1,101 @@ +import javax.xml.bind.annotation.XmlElement; public class StockDetails { - private String m_tradeNo; - private String m_companyName; - private String m_costPrice; - private String m_sharesCount; - private String m_salesPrice; - + private String TradeNo; + private String CompanyName; + private String CostPrice; + private String SharesCount; + private String SalesPrice; + @XmlElement(name = "TradeNo") /** * Gets the trade number of the share. */ - public String getTradeNo() throws Exception { - return m_tradeNo; + public String getTradeNo() + { + return TradeNo; } /** * Sets the trade number of the share. + * + * @param tradeNo Trade number of the share. */ - public String setTradeNo(String value) throws Exception { - m_tradeNo = value; - return value; + public void setTradeNo(String tradeNo) + { + this.TradeNo = tradeNo; } + @XmlElement(name = "CompanyName") /** * Gets the company name of the share. */ - public String getCompanyName() throws Exception { - return m_companyName; + public String getCompanyName() + { + return CompanyName; } /** - * Sets the company name of the share. + * Sets company name of the share. + * + * @param companyName Company name of the share. */ - public String setCompanyName(String value) throws Exception { - m_companyName = value; - return value; + public void setCompanyName(String companyName) + { + this.CompanyName = companyName; } + @XmlElement(name = "SharesCount") /** - * Gets the cost price of the share. + * Gets the total shares count. */ - public String getCostPrice() throws Exception { - return m_costPrice; + public String getSharesCount() + { + return SharesCount; } /** - * Sets the cost price of the share. + * Sets the total shares count. + * + * @param sharesCount Total shares count. */ - public String setCostPrice(String value) throws Exception { - m_costPrice = value; - return value; + public void setSharesCount(String sharesCount) + { + this.SharesCount = sharesCount; } + @XmlElement(name = "CostPrice") /** - * Gets the total number of the share. + * Gets the cost price of the share. */ - public String getSharesCount() throws Exception { - return m_sharesCount; + public String getCostPrice() + { + return CostPrice; } /** - * Sets the total number of the share. + * Sets the cost price of the share. + * + * @param costPrice Cost price of the share. */ - public String setSharesCount(String value) throws Exception { - m_sharesCount = value; - return value; + public void setCostPrice(String costPrice) + { + this.CostPrice = costPrice; } + @XmlElement(name = "SalesPrice") /** * Gets the sales price of the share. */ - public String getSalesPrice() throws Exception { - return m_salesPrice; + public String getSalesPrice() + { + return SalesPrice; } /** * Sets the sales price of the share. + * + * @param salesPrice Sales price of the share. + */ + public void setSalesPrice(String salesPrice) + { + this.SalesPrice = salesPrice; + } + /** + * Initializes a new instance of the StockDetails class. */ - public String setSalesPrice(String value) throws Exception { - m_salesPrice = value; - return value; + public StockDetails() + { } /** * Initializes a new instance of the StockDetails class with the specified trade @@ -82,16 +108,11 @@ public String setSalesPrice(String value) throws Exception { * @param salesPrice Sales price of the share. */ public StockDetails(String tradeNo, String companyName, String costPrice, String sharesCount, String salesPrice) - throws Exception { - m_tradeNo = tradeNo; - m_companyName = companyName; - m_costPrice = costPrice; - m_sharesCount = sharesCount; - m_salesPrice = salesPrice; - } - /** - * Initializes a new instance of the StockDetails class. - */ - public StockDetails() throws Exception { + { + this.TradeNo = tradeNo; + this.CompanyName = companyName; + this.CostPrice = costPrice; + this.SharesCount = sharesCount; + this.SalesPrice = salesPrice; } } diff --git a/dynamictableusingmailmerge/StockMarket.java b/dynamictableusingmailmerge/StockMarket.java new file mode 100644 index 0000000..ac094e7 --- /dev/null +++ b/dynamictableusingmailmerge/StockMarket.java @@ -0,0 +1,40 @@ +import java.util.List; +import javax.xml.bind.annotation.*; +@XmlRootElement(name = "StockMarket") +public class StockMarket +{ + private List StockDetails; + @XmlElement(name = "StockDetails") + /** + * Gets the list of stock details. + */ + public List getStockDetails() + { + return StockDetails; + } + /** + * Sets the list of stock details. + * + * @param stockDetails List of stock details. + */ + public void setStockDetails(List listStockDetails) + { + this.StockDetails = listStockDetails; + } + /** + * Initializes a new instance of the StockMarket class. + */ + public StockMarket() + { + } + /** + * Initializes a new instance of the StockMarket class with specified list of + * stock details. + * + * @param stockDetails List of stock details. + */ + public StockMarket(List stockDetails) + { + this.StockDetails = stockDetails; + } +}