git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1767057 13f79535-47bb-0310-9956-ffa450edef68tags/REL_3_16_BETA1
import java.util.Map; | import java.util.Map; | ||||
import java.util.Vector; | import java.util.Vector; | ||||
import javax.xml.parsers.ParserConfigurationException; | |||||
import javax.xml.transform.OutputKeys; | import javax.xml.transform.OutputKeys; | ||||
import javax.xml.transform.Source; | import javax.xml.transform.Source; | ||||
import javax.xml.transform.Transformer; | import javax.xml.transform.Transformer; | ||||
* Exports the data in an XML stream | * Exports the data in an XML stream | ||||
* | * | ||||
* @param os OutputStream in which will contain the output XML | * @param os OutputStream in which will contain the output XML | ||||
* @param validate if true, validates the XML againts the XML Schema | |||||
* @throws SAXException | |||||
* @throws TransformerException | |||||
* @throws ParserConfigurationException | |||||
* @param validate if true, validates the XML against the XML Schema | |||||
* @throws SAXException If validating the document fails | |||||
* @throws TransformerException If transforming the document fails | |||||
*/ | */ | ||||
public void exportToXML(OutputStream os, boolean validate) throws SAXException, ParserConfigurationException, TransformerException { | |||||
public void exportToXML(OutputStream os, boolean validate) throws SAXException, TransformerException { | |||||
exportToXML(os, "UTF-8", validate); | exportToXML(os, "UTF-8", validate); | ||||
} | } | ||||
* | * | ||||
* @param os OutputStream in which will contain the output XML | * @param os OutputStream in which will contain the output XML | ||||
* @param encoding the output charset encoding | * @param encoding the output charset encoding | ||||
* @param validate if true, validates the XML againts the XML Schema | |||||
* @throws SAXException | |||||
* @throws ParserConfigurationException | |||||
* @throws TransformerException | |||||
* @param validate if true, validates the XML against the XML Schema | |||||
* @throws SAXException If validating the document fails | |||||
* @throws TransformerException If transforming the document fails | |||||
*/ | */ | ||||
public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, ParserConfigurationException, TransformerException{ | |||||
public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, TransformerException{ | |||||
List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell(); | List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell(); | ||||
List<XSSFTable> tables = map.getRelatedTables(); | List<XSSFTable> tables = map.getRelatedTables(); | ||||
* | * | ||||
* @param xml the XML to validate | * @param xml the XML to validate | ||||
* @return true, if document is valid | * @return true, if document is valid | ||||
* @throws SAXException If validating the document fails | |||||
*/ | */ | ||||
private boolean isValid(Document xml) throws SAXException{ | private boolean isValid(Document xml) throws SAXException{ | ||||
try{ | try{ | ||||
String[] leftTokens = leftXpath.split("/"); | String[] leftTokens = leftXpath.split("/"); | ||||
String[] rightTokens = rightXpath.split("/"); | String[] rightTokens = rightXpath.split("/"); | ||||
int minLenght = leftTokens.length< rightTokens.length? leftTokens.length : rightTokens.length; | |||||
int minLength = leftTokens.length< rightTokens.length? leftTokens.length : rightTokens.length; | |||||
Node localComplexTypeRootNode = xmlSchema; | Node localComplexTypeRootNode = xmlSchema; | ||||
for(int i =1;i <minLenght; i++) { | |||||
for(int i =1;i <minLength; i++) { | |||||
String leftElementName = leftTokens[i]; | String leftElementName = leftTokens[i]; | ||||
String rightElementName = rightTokens[i]; | String rightElementName = rightTokens[i]; | ||||
} | } | ||||
private int indexOfElementInComplexType(String elementName,Node complexType) { | private int indexOfElementInComplexType(String elementName,Node complexType) { | ||||
if(complexType == null) { | |||||
return -1; | |||||
} | |||||
NodeList list = complexType.getChildNodes(); | NodeList list = complexType.getChildNodes(); | ||||
int indexOf = -1; | int indexOf = -1; | ||||
private String getComplexTypeNameFromChildren(Node localComplexTypeRootNode, | private String getComplexTypeNameFromChildren(Node localComplexTypeRootNode, | ||||
String elementNameWithoutNamespace) { | String elementNameWithoutNamespace) { | ||||
if(localComplexTypeRootNode == null) { | |||||
return ""; | |||||
} | |||||
NodeList list = localComplexTypeRootNode.getChildNodes(); | NodeList list = localComplexTypeRootNode.getChildNodes(); | ||||
String complexTypeName = ""; | String complexTypeName = ""; | ||||
package org.apache.poi.xssf.extractor; | package org.apache.poi.xssf.extractor; | ||||
import java.io.ByteArrayInputStream; | |||||
import java.io.ByteArrayOutputStream; | |||||
import java.io.IOException; | |||||
import java.util.Date; | |||||
import java.util.regex.Matcher; | |||||
import java.util.regex.Pattern; | |||||
import javax.xml.parsers.DocumentBuilder; | |||||
import javax.xml.parsers.DocumentBuilderFactory; | |||||
import javax.xml.parsers.ParserConfigurationException; | |||||
import junit.framework.TestCase; | |||||
import org.apache.poi.POIXMLDocumentPart; | import org.apache.poi.POIXMLDocumentPart; | ||||
import org.apache.poi.ss.usermodel.Cell; | import org.apache.poi.ss.usermodel.Cell; | ||||
import org.apache.poi.ss.usermodel.CellType; | import org.apache.poi.ss.usermodel.CellType; | ||||
import org.apache.poi.xssf.model.MapInfo; | import org.apache.poi.xssf.model.MapInfo; | ||||
import org.apache.poi.xssf.usermodel.XSSFMap; | import org.apache.poi.xssf.usermodel.XSSFMap; | ||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||||
import org.junit.Test; | |||||
import org.xml.sax.EntityResolver; | import org.xml.sax.EntityResolver; | ||||
import org.xml.sax.InputSource; | import org.xml.sax.InputSource; | ||||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
import junit.framework.TestCase; | |||||
import javax.xml.parsers.DocumentBuilder; | |||||
import javax.xml.parsers.DocumentBuilderFactory; | |||||
import javax.xml.parsers.ParserConfigurationException; | |||||
import java.io.ByteArrayInputStream; | |||||
import java.io.ByteArrayOutputStream; | |||||
import java.io.IOException; | |||||
import java.util.Collection; | |||||
import java.util.Date; | |||||
import java.util.regex.Matcher; | |||||
import java.util.regex.Pattern; | |||||
/** | /** | ||||
* @author Roberto Manicardi | * @author Roberto Manicardi | ||||
XSSFWorkbook wb = XSSFTestDataSamples | XSSFWorkbook wb = XSSFTestDataSamples | ||||
.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); | .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); | ||||
MapInfo mapInfo = null; | |||||
boolean found = false; | boolean found = false; | ||||
for (POIXMLDocumentPart p : wb.getRelations()) { | for (POIXMLDocumentPart p : wb.getRelations()) { | ||||
if (!(p instanceof MapInfo)) { | if (!(p instanceof MapInfo)) { | ||||
continue; | continue; | ||||
} | } | ||||
mapInfo = (MapInfo) p; | |||||
MapInfo mapInfo = (MapInfo) p; | |||||
XSSFMap map = mapInfo.getXSSFMapById(1); | XSSFMap map = mapInfo.getXSSFMapById(1); | ||||
XSSFExportToXml exporter = new XSSFExportToXml(map); | XSSFExportToXml exporter = new XSSFExportToXml(map); | ||||
XSSFWorkbook wb = XSSFTestDataSamples | XSSFWorkbook wb = XSSFTestDataSamples | ||||
.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); | .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); | ||||
MapInfo mapInfo = null; | |||||
boolean found = false; | boolean found = false; | ||||
for (POIXMLDocumentPart p : wb.getRelations()) { | for (POIXMLDocumentPart p : wb.getRelations()) { | ||||
if (p instanceof MapInfo) { | if (p instanceof MapInfo) { | ||||
mapInfo = (MapInfo) p; | |||||
MapInfo mapInfo = (MapInfo) p; | |||||
XSSFMap map = mapInfo.getXSSFMapById(1); | XSSFMap map = mapInfo.getXSSFMapById(1); | ||||
XSSFExportToXml exporter = new XSSFExportToXml(map); | XSSFExportToXml exporter = new XSSFExportToXml(map); | ||||
assertTrue(found); | assertTrue(found); | ||||
} | } | ||||
@Test | |||||
public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception { | public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception { | ||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); | XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); | ||||
} | } | ||||
assertTrue(found); | assertTrue(found); | ||||
} | } | ||||
public void testBug59026() throws Exception { | |||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx"); | |||||
Collection<XSSFMap> mappings = wb.getCustomXMLMappings(); | |||||
assertTrue(mappings.size() > 0); | |||||
for (XSSFMap map : mappings) { | |||||
XSSFExportToXml exporter = new XSSFExportToXml(map); | |||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |||||
exporter.exportToXML(os, false); | |||||
assertNotNull(os.toString("UTF-8")); | |||||
} | |||||
} | |||||
} | } |