Removed uses of internal JDK classes.
Removed @author tag. Dealt with exceprions properly. General reformatting. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@827905 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ebc7f3cb26
commit
3e82ec8035
@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@ -38,29 +39,23 @@ import org.apache.poi.xssf.usermodel.XSSFMap;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRow;
|
||||
import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
|
||||
import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
|
||||
import org.apache.xml.utils.PrefixResolver;
|
||||
import org.apache.xml.utils.PrefixResolverDefault;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import com.sun.org.apache.xml.internal.utils.PrefixResolver;
|
||||
import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Imports data from an external XML to an XLSX to an XML according to one of the mapping defined.
|
||||
*
|
||||
* The output XML Schema must respect this limitations:
|
||||
*
|
||||
* - the input XML must be valid according to the XML Schema used in the mapping
|
||||
* - denormalized table mapping is not supported (see OpenOffice part 4: chapter 3.5.1.7)
|
||||
* - all the namespaces used in the document must be declared in the root node
|
||||
*
|
||||
*
|
||||
* @author Roberto Manicardi
|
||||
*
|
||||
* Imports data from an external XML to an XLSX according to one of the mappings
|
||||
* defined.The output XML Schema must respect this limitations:
|
||||
* <ul>
|
||||
* <li>the input XML must be valid according to the XML Schema used in the mapping</li>
|
||||
* <li>denormalized table mapping is not supported (see OpenOffice part 4: chapter 3.5.1.7)</li>
|
||||
* <li>all the namespaces used in the document must be declared in the root node</li>
|
||||
* </ul>
|
||||
*/
|
||||
public class XSSFImportFromXML {
|
||||
|
||||
@ -68,23 +63,20 @@ public class XSSFImportFromXML {
|
||||
|
||||
private static POILogger logger = POILogFactory.getLogger(XSSFImportFromXML.class);
|
||||
|
||||
public XSSFImportFromXML(XSSFMap map){
|
||||
|
||||
public XSSFImportFromXML(XSSFMap map) {
|
||||
this.map = map;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports an XML into the XLSX using the Custom XML mapping defined
|
||||
*
|
||||
* @param xmlInputString the XML to import
|
||||
* @throws SAXException raised if error occurs during XML parsing
|
||||
* @throws XPathExpressionException raised if error occurs during XML navigation
|
||||
* @throws SAXException if error occurs during XML parsing
|
||||
* @throws XPathExpressionException if error occurs during XML navigation
|
||||
* @throws ParserConfigurationException if there are problems with XML parser configuration
|
||||
* @throws IOException if there are problems reading the input string
|
||||
*/
|
||||
public void importFromXML(String xmlInputString) throws SAXException, XPathExpressionException{
|
||||
|
||||
try{
|
||||
public void importFromXML(String xmlInputString) throws SAXException, XPathExpressionException, ParserConfigurationException, IOException {
|
||||
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setNamespaceAware(true);
|
||||
@ -99,10 +91,9 @@ public class XSSFImportFromXML {
|
||||
XPathFactory xpathFactory = XPathFactory.newInstance();
|
||||
XPath xpath = xpathFactory.newXPath();
|
||||
|
||||
|
||||
|
||||
// Setting namespace context to XPath
|
||||
// Assuming that the namespace prefix in the mapping xpath is the same as the one used in the document
|
||||
// Assuming that the namespace prefix in the mapping xpath is the
|
||||
// same as the one used in the document
|
||||
final PrefixResolver resolver = new PrefixResolverDefault(doc.getDocumentElement());
|
||||
|
||||
NamespaceContext ctx = new NamespaceContext() {
|
||||
@ -110,10 +101,12 @@ public class XSSFImportFromXML {
|
||||
public String getNamespaceURI(String prefix) {
|
||||
return resolver.getNamespaceForPrefix(prefix);
|
||||
}
|
||||
|
||||
// Dummy implementation - not used!
|
||||
public Iterator getPrefixes(String val) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Dummy implemenation - not used!
|
||||
public String getPrefix(String uri) {
|
||||
return null;
|
||||
@ -121,89 +114,63 @@ public class XSSFImportFromXML {
|
||||
};
|
||||
xpath.setNamespaceContext(ctx);
|
||||
|
||||
|
||||
|
||||
|
||||
for(XSSFSingleXmlCell singleXmlCell :singleXmlCells ){
|
||||
|
||||
|
||||
for (XSSFSingleXmlCell singleXmlCell : singleXmlCells) {
|
||||
|
||||
String xpathString = singleXmlCell.getXpath();
|
||||
|
||||
Node result = (Node) xpath.evaluate(xpathString,doc, XPathConstants.NODE);
|
||||
Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
|
||||
String textContent = result.getTextContent();
|
||||
logger.log(POILogger.DEBUG,"Extracting with xpath "+xpathString+" : value is '"+textContent+"'");
|
||||
logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
|
||||
XSSFCell cell = singleXmlCell.getReferencedCell();
|
||||
logger.log(POILogger.DEBUG,"Setting '"+textContent+"' to cell "+cell.getColumnIndex()+"-"+cell.getRowIndex()+" in sheet "+cell.getSheet().getSheetName());
|
||||
logger.log(POILogger.DEBUG, "Setting '" + textContent + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
|
||||
+ cell.getSheet().getSheetName());
|
||||
cell.setCellValue(textContent);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
for(Table table : tables){
|
||||
for (Table table : tables) {
|
||||
|
||||
String commonXPath = table.getCommonXpath();
|
||||
NodeList result = (NodeList) xpath.evaluate(commonXPath, doc, XPathConstants.NODESET);
|
||||
int rowOffset = table.getStartCellReference().getRow() + 1;// the first row contains the table header
|
||||
int columnOffset = table.getStartCellReference().getCol() - 1;
|
||||
|
||||
NodeList result = (NodeList) xpath.evaluate(commonXPath,doc, XPathConstants.NODESET);
|
||||
for (int i = 0; i < result.getLength(); i++) {
|
||||
|
||||
int rowOffset = table.getStartCellReference().getRow()+1;//the first row contains the table header
|
||||
int columnOffset = table.getStartCellReference().getCol()-1;
|
||||
|
||||
for(int i = 0; i< result.getLength();i++){
|
||||
|
||||
// TODO: implement support for denormalized XMLs (see OpenOffice part 4: chapter 3.5.1.7)
|
||||
|
||||
for(XSSFXmlColumnPr xmlColumnPr: table.getXmlColumnPrs()){
|
||||
|
||||
int localColumnId = (int)xmlColumnPr.getId();
|
||||
|
||||
int rowId = rowOffset+i;
|
||||
int columnId = columnOffset+localColumnId;
|
||||
// TODO: implement support for denormalized XMLs (see
|
||||
// OpenOffice part 4: chapter 3.5.1.7)
|
||||
|
||||
for (XSSFXmlColumnPr xmlColumnPr : table.getXmlColumnPrs()) {
|
||||
|
||||
int localColumnId = (int) xmlColumnPr.getId();
|
||||
int rowId = rowOffset + i;
|
||||
int columnId = columnOffset + localColumnId;
|
||||
String localXPath = xmlColumnPr.getLocalXPath();
|
||||
localXPath = localXPath.substring(localXPath.substring(1).indexOf('/')+1);
|
||||
|
||||
// Build an XPath to select the right node (assuming that the commonXPath != "/")
|
||||
String nodeXPath = commonXPath+"["+(i+1)+"]"+localXPath;
|
||||
|
||||
localXPath = localXPath.substring(localXPath.substring(1).indexOf('/') + 1);
|
||||
|
||||
// Build an XPath to select the right node (assuming
|
||||
// that the commonXPath != "/")
|
||||
String nodeXPath = commonXPath + "[" + (i + 1) + "]" + localXPath;
|
||||
|
||||
// TODO: convert the data to the cell format
|
||||
String value = (String) xpath.evaluate(nodeXPath,result.item(i), XPathConstants.STRING);
|
||||
logger.log(POILogger.DEBUG,"Extracting with xpath "+nodeXPath+" : value is '"+value+"'");
|
||||
String value = (String) xpath.evaluate(nodeXPath, result.item(i), XPathConstants.STRING);
|
||||
logger.log(POILogger.DEBUG, "Extracting with xpath " + nodeXPath + " : value is '" + value + "'");
|
||||
XSSFRow row = table.getXSSFSheet().getRow(rowId);
|
||||
if(row==null){
|
||||
if (row == null) {
|
||||
row = table.getXSSFSheet().createRow(rowId);
|
||||
}
|
||||
|
||||
XSSFCell cell = row.getCell(columnId);
|
||||
if(cell==null){
|
||||
if (cell == null) {
|
||||
cell = row.createCell(columnId);
|
||||
}
|
||||
logger.log(POILogger.DEBUG,"Setting '"+value+"' to cell "+cell.getColumnIndex()+"-"+cell.getRowIndex()+" in sheet "+table.getXSSFSheet().getSheetName());
|
||||
logger.log(POILogger.DEBUG, "Setting '" + value + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
|
||||
+ table.getXSSFSheet().getSheetName());
|
||||
cell.setCellValue(value.trim());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}catch(IOException e){
|
||||
//Thrown by StringReader
|
||||
e.printStackTrace();
|
||||
}catch(ParserConfigurationException e){
|
||||
//Thrown by DocumentBuilderFactory
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user