poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1633404 - in /poi: site/src/documentation/content/xdocs/ trunk/src/ooxml/java/org/apache/poi/xssf/extractor/ trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/ trunk/test-data/spreadsheet/
Date Tue, 21 Oct 2014 15:56:45 GMT
Author: centic
Date: Tue Oct 21 15:56:45 2014
New Revision: 1633404

URL: http://svn.apache.org/r1633404
Log:
Apply patch for bug 55864 - XSSFImportFromXML.importFromXML() does not support optional elements

Added:
    poi/trunk/test-data/spreadsheet/55864.xlsx   (with props)
Modified:
    poi/site/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java

Modified: poi/site/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/status.xml?rev=1633404&r1=1633403&r2=1633404&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/status.xml (original)
+++ poi/site/src/documentation/content/xdocs/status.xml Tue Oct 21 15:56:45 2014
@@ -38,6 +38,7 @@
     </devs>
 
     <release version="3.11-beta3" date="2014-??-??">
+        <action dev="PD" type="fix" fixes-bug="55864">XSSFImportFromXML.importFromXML()
does not support optional elements</action>
         <action dev="PD" type="fix" fixes-bug="56835">Unreadable content when adding
multiple comments to cell</action>
         <action dev="PD" type="add">SheetUtil.getCellWithMerges for getting a cell
at a co-ordinate, or the primary one of it's merged region if it's a merged cell</action>
         <action dev="PD" type="add" fixes-bug="56836">XML signature support</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java?rev=1633404&r1=1633403&r2=1633404&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java Tue Oct
21 15:56:45 2014
@@ -97,12 +97,15 @@ public class XSSFImportFromXML {
 
             String xpathString = singleXmlCell.getXpath();
             Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
-            String textContent = result.getTextContent();
-            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());
-            cell.setCellValue(textContent);
+            // result can be null if value is optional (xsd:minOccurs=0), see bugzilla 55864
+            if (result != null) {
+	            String textContent = result.getTextContent();
+	            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());
+	            cell.setCellValue(textContent);
+            }
         }
 
         for (XSSFTable table : tables) {
@@ -160,6 +163,7 @@ public class XSSFImportFromXML {
             _docElem = doc.getDocumentElement();
         }
 
+        @Override
         public String getNamespaceURI(String prefix) {
             return getNamespaceForPrefix(prefix);
         }
@@ -214,11 +218,13 @@ public class XSSFImportFromXML {
         }
 
         // Dummy implementation - not used!
+        @Override
         public Iterator getPrefixes(String val) {
             return null;
         }
 
         // Dummy implementation - not used!
+        @Override
         public String getPrefix(String uri) {
             return null;
         }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java?rev=1633404&r1=1633403&r2=1633404&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java
(original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java
Tue Oct 21 15:56:45 2014
@@ -37,119 +37,162 @@ public class TestXSSFImportFromXML exten
 	public void  testImportFromXML() throws Exception{
 		
 		 XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
-		 
-		 String name = "name";
-		 String teacher = "teacher";
-		 String tutor = "tutor";
-		 String cdl = "cdl";
-		 String duration = "duration";
-		 String topic = "topic";
-		 String project = "project";
-		 String credits = "credits";
-		 
-		 String testXML = "<CORSO>"+
-						  "<NOME>"+name+"</NOME>"+
-						  "<DOCENTE>"+teacher+"</DOCENTE>"+ 
-						  "<TUTOR>"+tutor+"</TUTOR>"+ 
-						  "<CDL>"+cdl+"</CDL>"+
-		 				  "<DURATA>"+duration+"</DURATA>"+ 
-		 				  "<ARGOMENTO>"+topic+"</ARGOMENTO>"+ 
-		 				  "<PROGETTO>"+project+"</PROGETTO>"+
-		 				  "<CREDITI>"+credits+"</CREDITI>"+ 
-		 				  "</CORSO>\u0000";		
-		 
-		 XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
-		 assertNotNull(map);
-		 XSSFImportFromXML importer = new XSSFImportFromXML(map);
-		 
-		 importer.importFromXML(testXML);
-		 
-		 XSSFSheet sheet=wb.getSheetAt(0);
-		 
-		 XSSFRow row = sheet.getRow(0);
-		 assertTrue(row.getCell(0).getStringCellValue().equals(name));
-		 assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
-		 assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
-		 assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
-		 assertTrue(row.getCell(4).getStringCellValue().equals(duration));
-		 assertTrue(row.getCell(5).getStringCellValue().equals(topic));
-		 assertTrue(row.getCell(6).getStringCellValue().equals(project));
-		 assertTrue(row.getCell(7).getStringCellValue().equals(credits));	
+		 try {
+    		 String name = "name";
+    		 String teacher = "teacher";
+    		 String tutor = "tutor";
+    		 String cdl = "cdl";
+    		 String duration = "duration";
+    		 String topic = "topic";
+    		 String project = "project";
+    		 String credits = "credits";
+    		 
+    		 String testXML = "<CORSO>"+
+    						  "<NOME>"+name+"</NOME>"+
+    						  "<DOCENTE>"+teacher+"</DOCENTE>"+ 
+    						  "<TUTOR>"+tutor+"</TUTOR>"+ 
+    						  "<CDL>"+cdl+"</CDL>"+
+    		 				  "<DURATA>"+duration+"</DURATA>"+ 
+    		 				  "<ARGOMENTO>"+topic+"</ARGOMENTO>"+ 
+    		 				  "<PROGETTO>"+project+"</PROGETTO>"+
+    		 				  "<CREDITI>"+credits+"</CREDITI>"+ 
+    		 				  "</CORSO>\u0000";		
+    		 
+    		 XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
+    		 assertNotNull(map);
+    		 XSSFImportFromXML importer = new XSSFImportFromXML(map);
+    		 
+    		 importer.importFromXML(testXML);
+    		 
+    		 XSSFSheet sheet=wb.getSheetAt(0);
+    		 
+    		 XSSFRow row = sheet.getRow(0);
+    		 assertTrue(row.getCell(0).getStringCellValue().equals(name));
+    		 assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
+    		 assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
+    		 assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
+    		 assertTrue(row.getCell(4).getStringCellValue().equals(duration));
+    		 assertTrue(row.getCell(5).getStringCellValue().equals(topic));
+    		 assertTrue(row.getCell(6).getStringCellValue().equals(project));
+    		 assertTrue(row.getCell(7).getStringCellValue().equals(credits));
+		 } finally {
+		     wb.close();
+		 }
 	}
 	
 	
 	
 	
 	public void testMultiTable() throws Exception{
-		
-		
 		XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
 
-		
-		String cellC6 = "c6";
-		String cellC7 = "c7";
-		String cellC8 = "c8";
-		String cellC9 = "c9";
-		
-		String testXML = "<ns1:MapInfo xmlns:ns1=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"
SelectionNamespaces=\"\">" +
-						 "<ns1:Schema ID=\""+cellC6+"\" SchemaRef=\"a\" />"+ 
-						 "<ns1:Schema ID=\""+cellC7+"\" SchemaRef=\"b\" />"+ 
-						 "<ns1:Schema ID=\""+cellC8+"\" SchemaRef=\"c\" />"+ 
-						 "<ns1:Schema ID=\""+cellC9+"\" SchemaRef=\"d\" />"+ 
-						 "<ns1:Map ID=\"1\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\"
AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">"+
-						 "<ns1:DataBinding DataBindingLoadMode=\"\" />"+ 
-						 "</ns1:Map>"+
-						 "<ns1:Map ID=\"2\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\"
AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">"+
-						 "<ns1:DataBinding DataBindingLoadMode=\"\" />"+ 
-						 "</ns1:Map>"+
-						 "<ns1:Map ID=\"3\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\"
AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">"+
-						 "<ns1:DataBinding DataBindingLoadMode=\"\" />"+ 
-						 "</ns1:Map>"+
-						 "</ns1:MapInfo>\u0000";
-
-		XSSFMap map = wb.getMapInfo().getXSSFMapByName("MapInfo_mapping");
-		assertNotNull(map);
-		XSSFImportFromXML importer = new XSSFImportFromXML(map);
-		 
-		importer.importFromXML(testXML);
-		 
-		//Check for Schema element
-		XSSFSheet sheet=wb.getSheetAt(1);
-		 
-		assertEquals(cellC6,sheet.getRow(5).getCell(2).getStringCellValue());
-		assertEquals(cellC7,sheet.getRow(6).getCell(2).getStringCellValue());
-		assertEquals(cellC8,sheet.getRow(7).getCell(2).getStringCellValue());
-		assertEquals(cellC9,sheet.getRow(8).getCell(2).getStringCellValue());
-		
-		
+		try {
+    		String cellC6 = "c6";
+    		String cellC7 = "c7";
+    		String cellC8 = "c8";
+    		String cellC9 = "c9";
+    		
+    		String testXML = "<ns1:MapInfo xmlns:ns1=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"
SelectionNamespaces=\"\">" +
+    						 "<ns1:Schema ID=\""+cellC6+"\" SchemaRef=\"a\" />"+ 
+    						 "<ns1:Schema ID=\""+cellC7+"\" SchemaRef=\"b\" />"+ 
+    						 "<ns1:Schema ID=\""+cellC8+"\" SchemaRef=\"c\" />"+ 
+    						 "<ns1:Schema ID=\""+cellC9+"\" SchemaRef=\"d\" />"+ 
+    						 "<ns1:Map ID=\"1\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\"
AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">"+
+    						 "<ns1:DataBinding DataBindingLoadMode=\"\" />"+ 
+    						 "</ns1:Map>"+
+    						 "<ns1:Map ID=\"2\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\"
AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">"+
+    						 "<ns1:DataBinding DataBindingLoadMode=\"\" />"+ 
+    						 "</ns1:Map>"+
+    						 "<ns1:Map ID=\"3\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\"
AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">"+
+    						 "<ns1:DataBinding DataBindingLoadMode=\"\" />"+ 
+    						 "</ns1:Map>"+
+    						 "</ns1:MapInfo>\u0000";
+    
+    		XSSFMap map = wb.getMapInfo().getXSSFMapByName("MapInfo_mapping");
+    		assertNotNull(map);
+    		XSSFImportFromXML importer = new XSSFImportFromXML(map);
+    		 
+    		importer.importFromXML(testXML);
+    		 
+    		//Check for Schema element
+    		XSSFSheet sheet=wb.getSheetAt(1);
+    		 
+    		assertEquals(cellC6,sheet.getRow(5).getCell(2).getStringCellValue());
+    		assertEquals(cellC7,sheet.getRow(6).getCell(2).getStringCellValue());
+    		assertEquals(cellC8,sheet.getRow(7).getCell(2).getStringCellValue());
+    		assertEquals(cellC9,sheet.getRow(8).getCell(2).getStringCellValue());
+        } finally {
+            wb.close();
+        }
 	}
 
 	
 	public void testSingleAttributeCellWithNamespace() throws Exception{
-		
-		
 		XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
 
-		
-		String id = "a";
-		String displayName = "dispName";
-		String ref="19"; 
-		String count = "21";
-		
-		String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"+

-						 "<ns1:table xmlns:ns1=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"
id=\""+id+"\" displayName=\""+displayName+"\" ref=\""+ref+"\">"+
-						 "<ns1:tableColumns count=\""+count+"\" />"+ 
-						 "</ns1:table>\u0000"; 
-		XSSFMap map = wb.getMapInfo().getXSSFMapByName("table_mapping");
-		assertNotNull(map);
-		XSSFImportFromXML importer = new XSSFImportFromXML(map);
-		importer.importFromXML(testXML);
-		
-		//Check for Schema element
-		XSSFSheet sheet=wb.getSheetAt(0);
-		 
-		assertEquals(id,sheet.getRow(28).getCell(1).getStringCellValue());
-		assertEquals(displayName,sheet.getRow(11).getCell(5).getStringCellValue());
-		assertEquals(ref,sheet.getRow(14).getCell(7).getStringCellValue());
-		assertEquals(count,sheet.getRow(18).getCell(3).getStringCellValue());
-		
+		try {
+    		String id = "a";
+    		String displayName = "dispName";
+    		String ref="19"; 
+    		String count = "21";
+    		
+    		String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"+

+    						 "<ns1:table xmlns:ns1=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"
id=\""+id+"\" displayName=\""+displayName+"\" ref=\""+ref+"\">"+
+    						 "<ns1:tableColumns count=\""+count+"\" />"+ 
+    						 "</ns1:table>\u0000"; 
+    		XSSFMap map = wb.getMapInfo().getXSSFMapByName("table_mapping");
+    		assertNotNull(map);
+    		XSSFImportFromXML importer = new XSSFImportFromXML(map);
+    		importer.importFromXML(testXML);
+    		
+    		//Check for Schema element
+    		XSSFSheet sheet=wb.getSheetAt(0);
+    		 
+    		assertEquals(id,sheet.getRow(28).getCell(1).getStringCellValue());
+    		assertEquals(displayName,sheet.getRow(11).getCell(5).getStringCellValue());
+    		assertEquals(ref,sheet.getRow(14).getCell(7).getStringCellValue());
+    		assertEquals(count,sheet.getRow(18).getCell(3).getStringCellValue());
+        } finally {
+            wb.close();
+        }
+	}
+	
+	
+	public void testOptionalFields_Bugzilla_55864() throws Exception {
+		XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55864.xlsx");	 
+		try {
+    		String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+
+    				"<PersonInfoRoot>" +
+    				"<PersonData>" +
+    				"<FirstName>Albert</FirstName>" +
+    				"<LastName>Einstein</LastName>" +
+    				"<BirthDate>1879-03-14</BirthDate>" +
+    				"</PersonData>" +
+    				"</PersonInfoRoot>";
+    		
+    		XSSFMap map = wb.getMapInfo().getXSSFMapByName("PersonInfoRoot_Map");
+    		assertNotNull(map);
+    		XSSFImportFromXML importer = new XSSFImportFromXML(map);
+    		 
+    		importer.importFromXML(testXML);
+    		 
+    		XSSFSheet sheet=wb.getSheetAt(0);
+    		 
+    		 XSSFRow rowHeadings = sheet.getRow(0);
+    		 XSSFRow rowData = sheet.getRow(1);
+    		 
+    		 assertEquals("FirstName", rowHeadings.getCell(0).getStringCellValue());
+    		 assertEquals("Albert", rowData.getCell(0).getStringCellValue());
+    		 
+    		 assertEquals("LastName", rowHeadings.getCell(1).getStringCellValue());
+    		 assertEquals("Einstein", rowData.getCell(1).getStringCellValue());
+    		 
+    		 assertEquals("BirthDate", rowHeadings.getCell(2).getStringCellValue());
+    		 assertEquals("1879-03-14", rowData.getCell(2).getStringCellValue());
+    		 
+    		 // Value for OptionalRating is declared optional (minOccurs=0) in 55864.xlsx
+    		 assertEquals("OptionalRating", rowHeadings.getCell(3).getStringCellValue());
+    		 assertNull("", rowData.getCell(3));
+        } finally {
+            wb.close();
+        }
 	}
 }

Added: poi/trunk/test-data/spreadsheet/55864.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/55864.xlsx?rev=1633404&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/55864.xlsx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message