From d2a4ce944a6be1360d3f6fcd59c8e4950801df55 Mon Sep 17 00:00:00 2001
From: Guillermo Alvarez <guillermo@vaadin.com>
Date: Fri, 1 Jul 2016 11:51:24 +0300
Subject: [PATCH] Improve type detection in XSSFHyperlink with location

---
 .../apache/poi/xssf/usermodel/XSSFHyperlink.java   | 71 +++++++++++-----------
 1 file changed, 36 insertions(+), 35 deletions(-)

diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
index 4b70d50..4e527b0 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
@@ -55,44 +55,45 @@ public class XSSFHyperlink implements Hyperlink {
      * @param hyperlinkRel the relationship in the underlying OPC package which stores the actual link's address
      */
     protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) {
-        _ctHyperlink = ctHyperlink;
-        _externalRel = hyperlinkRel;
-
-        // Figure out the Hyperlink type and distination
-
-        // If it has a location, it's internal
-        if (ctHyperlink.getLocation() != null) {
-            _type = Hyperlink.LINK_DOCUMENT;
-            _location = ctHyperlink.getLocation();
-        } else {
-            // Otherwise it's somehow external, check
-            //  the relation to see how
-            if (_externalRel == null) {
-                if (ctHyperlink.getId() != null) {
-                    throw new IllegalStateException("The hyperlink for cell " + ctHyperlink.getRef() +
-                            " references relation " + ctHyperlink.getId() + ", but that didn't exist!");
-                }
-                // hyperlink is internal and is not related to other parts
-                _type = Hyperlink.LINK_DOCUMENT;
-            } else {
-                URI target = _externalRel.getTargetURI();
-                _location = target.toString();
-
-                // Try to figure out the type
+         _ctHyperlink = ctHyperlink;
+         _externalRel = hyperlinkRel;
+
+         // Figure out the Hyperlink type and distination
+
+         if (_externalRel == null) {
+             // If it has a location, it's internal
+             if (ctHyperlink.getLocation() != null) {
+                 _type = Hyperlink.LINK_DOCUMENT;
+                 _location = ctHyperlink.getLocation();
+             } else if (ctHyperlink.getId() != null) {
+                 throw new IllegalStateException("The hyperlink for cell "
+                         + ctHyperlink.getRef() + " references relation "
+                         + ctHyperlink.getId() + ", but that didn't exist!");
+             } else {
+                 // hyperlink is internal and is not related to other parts
+                 _type = Hyperlink.LINK_DOCUMENT;
+             }
+         } else {
+             URI target = _externalRel.getTargetURI();
+             _location = target.toString();
+             if (ctHyperlink.getLocation() != null) {
+                 // URI fragment
+                 _location += "#" + ctHyperlink.getLocation();
+             }
+
+             // 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;
-                }
-            }
+                     || _location.startsWith("ftp://")) {
+                 _type = Hyperlink.LINK_URL;
+             } else if (_location.startsWith("mailto:")) {
+                 _type = Hyperlink.LINK_EMAIL;
+             } else {
+                 _type = Hyperlink.LINK_FILE;
+             }
+         }
 
+     }
 
-        }
-    }
-    
     /**
      * Create a new XSSFHyperlink. This method is for Internal use only.
      * XSSFHyperlinks can be created by XSSFCreationHelper.
-- 
2.0.4

