poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r645446 - in /poi/branches/ooxml: ./ src/ooxml/java/org/apache/poi/xssf/usermodel/ src/ooxml/testcases/org/apache/poi/xssf/usermodel/
Date Mon, 07 Apr 2008 10:31:56 GMT
Author: nick
Date: Mon Apr  7 03:31:55 2008
New Revision: 645446

URL: http://svn.apache.org/viewvc?rev=645446&view=rev
Log:
Finish off XSSFHyperlink support

Modified:
    poi/branches/ooxml/build.xml
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java

Modified: poi/branches/ooxml/build.xml
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/build.xml?rev=645446&r1=645445&r2=645446&view=diff
==============================================================================
--- poi/branches/ooxml/build.xml (original)
+++ poi/branches/ooxml/build.xml Mon Apr  7 03:31:55 2008
@@ -151,8 +151,8 @@
   <property name="ooxml.jar5.dir" location="${ooxml.lib}/jsr173_1.0_api.jar"/>
   <property name="ooxml.jar5.url" value="${repository}/xmlbeans/jars/jsr173_1.0_api.jar"/>
   <!-- No official release of openxml4j yet -->
-  <property name="ooxml.jar6.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080403.jar"/>
-  <property name="ooxml.jar6.url" value="http://people.apache.org/~nick/openxml4j-bin-alpha-080403.jar"/>
+  <property name="ooxml.jar6.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080407.jar"/>
+  <property name="ooxml.jar6.url" value="http://people.apache.org/~nick/openxml4j-bin-alpha-080407.jar"/>
 
   <!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm -->
   <!-- "Copy these file(s), free of charge" -->

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=645446&r1=645445&r2=645446&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Mon Apr
 7 03:31:55 2008
@@ -423,14 +423,20 @@
     /**
      * Creates an XSSFRichTextString for you.
      */
-    public RichTextString createRichTextString(String text) {
-        return new XSSFRichTextString(text);
-    }
-
-    public Hyperlink getHyperlink() {
-        return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
-    }
-    public void setHyperlink(Hyperlink hyperlink) {
-        row.getSheet().setCellHyperlink((XSSFHyperlink)hyperlink);
-    }
+	public RichTextString createRichTextString(String text) {
+		return new XSSFRichTextString(text);
+	}
+	
+	public Hyperlink getHyperlink() {
+		return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
+	}
+	public void setHyperlink(Hyperlink hyperlink) {
+		XSSFHyperlink link = (XSSFHyperlink)hyperlink;
+		
+		// Assign to us
+		link.setCellReference( new CellReference(row.getRowNum(), cellNum).formatAsString() );
+		
+		// Add to the lists
+		row.getSheet().setCellHyperlink(link);
+	}
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java?rev=645446&r1=645445&r2=645446&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java Mon
Apr  7 03:31:55 2008
@@ -22,7 +22,6 @@
 import org.apache.poi.ss.util.CellReference;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
 
-import org.openxml4j.opc.Package;
 import org.openxml4j.opc.PackagePart;
 import org.openxml4j.opc.PackageRelationship;
 
@@ -46,7 +45,7 @@
 		this.ctHyperlink = ctHyperlink;
 		this.externalRel = hyperlinkRel;
 		
-		// Figure out the Hyperlink type
+		// Figure out the Hyperlink type and distination
 		
 		// If it has a location, it's internal
 		if(ctHyperlink.getLocation() != null) {
@@ -62,10 +61,19 @@
 					throw new IllegalStateException("A sheet hyperlink must either have a location, or a
relationship. Found:\n" + ctHyperlink);
 				}
 			}
-			// TODO
 			
-			//URI target = externalRel.getTargetURI();
-			//location = target.toString();
+			URI target = externalRel.getTargetURI();
+			location = target.toString();
+			
+			// Try to figure out the type
+			if(location.startsWith("http://") || location.startsWith("https://")
+					|| location.startsWith("ftp://")) {
+				type = Hyperlink.LINK_URL;
+			} else if(location.startsWith("mailto:")) {
+				type = Hyperlink.LINK_EMAIL;
+			} else {
+				type = Hyperlink.LINK_FILE;
+			}
 		}
 	}
 
@@ -89,7 +97,12 @@
 	 */
 	protected void generateRelationIfNeeded(PackagePart sheetPart) {
 		if(needsRelationToo()) {
-			// TODO
+			// Generate the relation
+			PackageRelationship rel =
+				sheetPart.addExternalRelationship(location, XSSFWorkbook.SHEET_HYPERLINKS.getRelation());
+			
+			// Update the r:id
+			ctHyperlink.setId(rel.getId());
 		}
 	}
 	
@@ -119,6 +132,13 @@
 		location = address;
 	}
 
+	/**
+	 * Assigns this hyperlink to the given cell reference
+	 */
+	protected void setCellReference(String ref) {
+		ctHyperlink.setRef(ref);
+	}
+	
 	private CellReference buildCellReference() {
 		return new CellReference(ctHyperlink.getRef());
 	}

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=645446&r1=645445&r2=645446&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Mon Apr
 7 03:31:55 2008
@@ -180,10 +180,9 @@
     			hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
     		}
     		
-    		// TODO: fix openxml4j
-//    		hyperlinks.add(
-//    				new XSSFHyperlink(hyperlink, hyperRel)
-//    		);
+    		hyperlinks.add(
+    				new XSSFHyperlink(hyperlink, hyperRel)
+    		);
     	}
     }
     

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java?rev=645446&r1=645445&r2=645446&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java
(original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java
Mon Apr  7 03:31:55 2008
@@ -23,6 +23,10 @@
 
 import junit.framework.TestCase;
 
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.ss.usermodel.Row;
 import org.openxml4j.opc.Package;
 
 public class TestXSSFHyperlink extends TestCase {
@@ -52,8 +56,9 @@
 		
 		XSSFSheet sheet = (XSSFSheet)workbook.getSheetAt(0);
 
-		// TODO - check hyperlinks
-		//assertEquals(4, sheet.getNumHyperlinks());
+		// Check the hyperlinks
+		assertEquals(4, sheet.getNumHyperlinks());
+		doTestHyperlinkContents(sheet);
 	}
 
     public void testLoadSave() throws Exception {
@@ -64,9 +69,14 @@
 		assertTrue(xml.exists());
     	
 		XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+		CreationHelper createHelper = workbook.getCreationHelper();
 		assertEquals(3, workbook.getNumberOfSheets());
+		XSSFSheet sheet = (XSSFSheet)workbook.getSheetAt(0);
 
-		// TODO - check hyperlinks
+		// Check hyperlinks
+		assertEquals(4, sheet.getNumHyperlinks());
+		doTestHyperlinkContents(sheet);
+		
 		
 		// Write out, and check
 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -79,8 +89,99 @@
 		assertNotNull(wb2.getSheetAt(0));
 		assertNotNull(wb2.getSheetAt(1));
 		assertNotNull(wb2.getSheetAt(2));
+		
+		sheet = (XSSFSheet)wb2.getSheetAt(0);
 
-		// TODO
+		
+		// Check hyperlinks again
+		assertEquals(4, sheet.getNumHyperlinks());
+		doTestHyperlinkContents(sheet);
+		
+		
+		// Add one more, and re-check
+		Row r17 = sheet.createRow(17);
+		Cell r17c = r17.createCell(2);
+		
+		Hyperlink hyperlink = createHelper.createHyperlink(Hyperlink.LINK_URL);
+		hyperlink.setAddress("http://poi.apache.org/spreadsheet/");
+		hyperlink.setLabel("POI SS Link");
+		r17c.setHyperlink(hyperlink);
+		
+		assertEquals(5, sheet.getNumHyperlinks());
+		doTestHyperlinkContents(sheet);
+		
+		assertEquals(Hyperlink.LINK_URL, 
+				sheet.getRow(17).getCell(2).getHyperlink().getType());
+		assertEquals("POI SS Link", 
+				sheet.getRow(17).getCell(2).getHyperlink().getLabel());
+		assertEquals("http://poi.apache.org/spreadsheet/", 
+				sheet.getRow(17).getCell(2).getHyperlink().getAddress());
+		
+		
+		// Save and re-load once more
+		baos = new ByteArrayOutputStream();
+		wb2.write(baos);
+		bais = new ByteArrayInputStream(baos.toByteArray());
+		
+		
+		XSSFWorkbook wb3 = new XSSFWorkbook(Package.open(bais));
+		assertEquals(3, wb3.getNumberOfSheets());
+		assertNotNull(wb3.getSheetAt(0));
+		assertNotNull(wb3.getSheetAt(1));
+		assertNotNull(wb3.getSheetAt(2));
+		
+		sheet = (XSSFSheet)wb3.getSheetAt(0);
+		
+		assertEquals(5, sheet.getNumHyperlinks());
+		doTestHyperlinkContents(sheet);
+		
+		assertEquals(Hyperlink.LINK_URL, 
+				sheet.getRow(17).getCell(2).getHyperlink().getType());
+		assertEquals("POI SS Link", 
+				sheet.getRow(17).getCell(2).getHyperlink().getLabel());
+		assertEquals("http://poi.apache.org/spreadsheet/", 
+				sheet.getRow(17).getCell(2).getHyperlink().getAddress());
+    }
+
+    /**
+     * Only for WithMoreVariousData.xlsx !
+     */
+    private void doTestHyperlinkContents(XSSFSheet sheet) {
+		assertNotNull(sheet.getRow(3).getCell(2).getHyperlink());
+		assertNotNull(sheet.getRow(14).getCell(2).getHyperlink());
+		assertNotNull(sheet.getRow(15).getCell(2).getHyperlink());
+		assertNotNull(sheet.getRow(16).getCell(2).getHyperlink());
+		
+		// First is a link to poi
+		assertEquals(Hyperlink.LINK_URL, 
+				sheet.getRow(3).getCell(2).getHyperlink().getType());
+		assertEquals(null, 
+				sheet.getRow(3).getCell(2).getHyperlink().getLabel());
+		assertEquals("http://poi.apache.org/", 
+				sheet.getRow(3).getCell(2).getHyperlink().getAddress());
+		
+		// Next is an internal doc link
+		assertEquals(Hyperlink.LINK_DOCUMENT, 
+				sheet.getRow(14).getCell(2).getHyperlink().getType());
+		assertEquals("Internal hyperlink to A2", 
+				sheet.getRow(14).getCell(2).getHyperlink().getLabel());
+		assertEquals("Sheet1!A2", 
+				sheet.getRow(14).getCell(2).getHyperlink().getAddress());
+		
+		// Next is a file
+		assertEquals(Hyperlink.LINK_FILE, 
+				sheet.getRow(15).getCell(2).getHyperlink().getType());
+		assertEquals(null, 
+				sheet.getRow(15).getCell(2).getHyperlink().getLabel());
+		assertEquals("WithVariousData.xlsx", 
+				sheet.getRow(15).getCell(2).getHyperlink().getAddress());
+		
+		// Last is a mailto
+		assertEquals(Hyperlink.LINK_EMAIL, 
+				sheet.getRow(16).getCell(2).getHyperlink().getType());
+		assertEquals(null, 
+				sheet.getRow(16).getCell(2).getHyperlink().getLabel());
+		assertEquals("mailto:dev@poi.apache.org?subject=XSSF%20Hyperlinks", 
+				sheet.getRow(16).getCell(2).getHyperlink().getAddress());
     }
-    
 }



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


Mime
View raw message