1. Use JAXP for XML Parsing
http://www.javaworld.com/javaworld/jw-08-2005/jw-0808-xml.html?page=3
http://www.javaworld.com/javaworld/jw-08-2005/jw-0808-xml.html?page=4
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class XmlConfigReader {
public XmlConfigReader(String xmlFile) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setIgnoringComments(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
// Specify our own schema - this overrides the schemaLocation in the xml file
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:./test.xsd");
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler( new SimpleErrorHandler() );
Document xmlConfig = builder.parse(xmlFile);
Node rootNode = document.getFirstChild();
}
2. Use the below code to create a map of parsed XML, use the rootNode above as the input
/* * Recursive utility to represent an XML Document as a Set. *
* @param node is the root node to map to a Set
* @param basePath is the path to the root node *
* @return Set<Object> represents the XML Document as a Set */
private static Set<Object> getChildSet(Node node, String basePath) {
Set<Object> childSet = new HashSet<Object>();
if (!node.hasChildNodes() && !node.getTextContent().trim().equals("")) {
childSet.add(basePath + ":" + node.getTextContent());
}
else {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
childSet.add(getChildSet(children.item(i), basePath + "/" + node.getNodeName()));
}
}
return childSet; }
With this set, we can get the required config items
3. Set the boundary conditions
if(hssf.getPhysicalRowCount > xmlConfig.maxRowCount ||
hssf.rows(1).getPhysicalNumberOfCells > xmlConfig.maxColumnCount){
return error;
}
4. Read the file storage location from a properties file in class path
prop = new Properties();
try {
//load a properties file from class path, inside static method prop.load(getClass().getClassLoader().getResourceAsStream("config.properties");));
//get the property value and print it out
String uploadedFilePath = prop.getProperty("fileStorageLocation");
String templateFilePath = prop.getProperty("fileStorageLocation");
catch (IOException IOE) { IOE.printStackTrace(); }
catch (Exception ex) { ex.printStackTrace(); }
http://viralpatel.net/blogs/loading-java-properties-files/
http://www.mkyong.com/java/java-properties-file-examples/
5. Create the Excel template from XML for reference.
HSSFWorkbook wbTemplate = new HSSFWorkbook(new FileOutputStream(templateFilePath + "/" + configParser.getTemplateName());
SpreadsheetWriter sw = new SpreadsheetWriter(Writer out);
sw.beginSheet();
sw.insertRow();
for (columnCount = 0; columnCount < configParser.columnslist.getCols()){
sw.createCell(,columnCount,configParser.columnslist.get(columnCount),styleIndex);
}
sw.endRow();
sw.endSheet();
6. DOJO Editable Grid
var myStructure = [{
defaultCell: { width: 8, editable: true, type: dojox.grid.cells._Widget, styles: 'text-align: right;' },
rows: [
{ name: 'Id', field: 'id', editable: false /* Can't edit ID's of dojo.data items */ },
{ name: '', field: 'col8', width: 10,
type: dojox.grid.cells.DateTextBox,
formatter: formatDate,
constraint: {formatLength: 'long', selector: "date"}},
{ name: 'Priority', styles: 'text-align: center;', field: 'col1',
type: dojox.grid.cells.ComboBox,
options: ["normal", "note", "important"], width: 10},
{ name: 'Mark', field: 'col2', width: 3, styles: 'text-align: center;',
type: dojox.grid.cells.CheckBox},
{ field: 'col3', name: 'Status',
styles: 'text-align: center;',
type: dojox.grid.cells.Select,
options: [ "new", "read", "replied" ]
},
{ name: 'Message', field: 'col4', styles: '', width: 10,
type: dojox.grid.cells.Editor, editorToolbar: true },
{ name: 'Amount', field: 'col5', formatter: formatCurrency, constraint: {currency: 'EUR'},
widgetClass: dijit.form.CurrencyTextBox },
{ name: 'Amount', field: 'col5', formatter: formatCurrency, constraint: {currency: 'EUR'},
widgetClass: dijit.form.HorizontalSlider, width: 10}
]
}];
http://www.javaworld.com/javaworld/jw-08-2005/jw-0808-xml.html?page=3
http://www.javaworld.com/javaworld/jw-08-2005/jw-0808-xml.html?page=4
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class XmlConfigReader {
public XmlConfigReader(String xmlFile) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setIgnoringComments(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
// Specify our own schema - this overrides the schemaLocation in the xml file
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:./test.xsd");
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler( new SimpleErrorHandler() );
Document xmlConfig = builder.parse(xmlFile);
Node rootNode = document.getFirstChild();
}
2. Use the below code to create a map of parsed XML, use the rootNode above as the input
/* * Recursive utility to represent an XML Document as a Set. *
* @param node is the root node to map to a Set
* @param basePath is the path to the root node *
* @return Set<Object> represents the XML Document as a Set */
private static Set<Object> getChildSet(Node node, String basePath) {
Set<Object> childSet = new HashSet<Object>();
if (!node.hasChildNodes() && !node.getTextContent().trim().equals("")) {
childSet.add(basePath + ":" + node.getTextContent());
}
else {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
childSet.add(getChildSet(children.item(i), basePath + "/" + node.getNodeName()));
}
}
return childSet; }
With this set, we can get the required config items
3. Set the boundary conditions
if(hssf.getPhysicalRowCount > xmlConfig.maxRowCount ||
hssf.rows(1).getPhysicalNumberOfCells > xmlConfig.maxColumnCount){
return error;
}
4. Read the file storage location from a properties file in class path
prop = new Properties();
try {
//load a properties file from class path, inside static method prop.load(getClass().getClassLoader().getResourceAsStream("config.properties");));
//get the property value and print it out
String uploadedFilePath = prop.getProperty("fileStorageLocation");
String templateFilePath = prop.getProperty("fileStorageLocation");
catch (IOException IOE) { IOE.printStackTrace(); }
catch (Exception ex) { ex.printStackTrace(); }
http://viralpatel.net/blogs/loading-java-properties-files/
http://www.mkyong.com/java/java-properties-file-examples/
5. Create the Excel template from XML for reference.
HSSFWorkbook wbTemplate = new HSSFWorkbook(new FileOutputStream(templateFilePath + "/" + configParser.getTemplateName());
SpreadsheetWriter sw = new SpreadsheetWriter(Writer out);
sw.beginSheet();
sw.insertRow();
for (columnCount = 0; columnCount < configParser.columnslist.getCols()){
sw.createCell(,columnCount,configParser.columnslist.get(columnCount),styleIndex);
}
sw.endRow();
sw.endSheet();
6. DOJO Editable Grid
var myStructure = [{
defaultCell: { width: 8, editable: true, type: dojox.grid.cells._Widget, styles: 'text-align: right;' },
rows: [
{ name: 'Id', field: 'id', editable: false /* Can't edit ID's of dojo.data items */ },
{ name: '', field: 'col8', width: 10,
type: dojox.grid.cells.DateTextBox,
formatter: formatDate,
constraint: {formatLength: 'long', selector: "date"}},
{ name: 'Priority', styles: 'text-align: center;', field: 'col1',
type: dojox.grid.cells.ComboBox,
options: ["normal", "note", "important"], width: 10},
{ name: 'Mark', field: 'col2', width: 3, styles: 'text-align: center;',
type: dojox.grid.cells.CheckBox},
{ field: 'col3', name: 'Status',
styles: 'text-align: center;',
type: dojox.grid.cells.Select,
options: [ "new", "read", "replied" ]
},
{ name: 'Message', field: 'col4', styles: '', width: 10,
type: dojox.grid.cells.Editor, editorToolbar: true },
{ name: 'Amount', field: 'col5', formatter: formatCurrency, constraint: {currency: 'EUR'},
widgetClass: dijit.form.CurrencyTextBox },
{ name: 'Amount', field: 'col5', formatter: formatCurrency, constraint: {currency: 'EUR'},
widgetClass: dijit.form.HorizontalSlider, width: 10}
]
}];