]> source.dussan.org Git - poi.git/commitdiff
bug 59873: replace Hyperlink.LINK_* int constants with HyperlinkType enum
authorJaven O'Neal <onealj@apache.org>
Sun, 17 Jul 2016 09:20:21 +0000 (09:20 +0000)
committerJaven O'Neal <onealj@apache.org>
Sun, 17 Jul 2016 09:20:21 +0000 (09:20 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753035 13f79535-47bb-0310-9956-ffa450edef68

12 files changed:
src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java
src/java/org/apache/poi/common/usermodel/Hyperlink.java
src/java/org/apache/poi/common/usermodel/HyperlinkType.java [new file with mode: 0644]
src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java
src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java
src/java/org/apache/poi/ss/usermodel/CreationHelper.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCreationHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java

index 0872749d0248da430a0ce11480769e83370e258b..c1ccad91008b30c9b64806aa4d26465fd4e18ea0 100644 (file)
 
 package org.apache.poi.hssf.usermodel.examples;
 
-import org.apache.poi.hssf.usermodel.*;
-import org.apache.poi.hssf.util.HSSFColor;
-
-import java.io.IOException;
 import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.common.usermodel.HyperlinkType;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.hssf.usermodel.HSSFCreationHelper;
+import org.apache.poi.hssf.usermodel.HSSFFont;
+import org.apache.poi.hssf.usermodel.HSSFHyperlink;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.util.HSSFColor;
 
 /**
  * Demonstrates how to create hyperlinks.
@@ -32,6 +39,7 @@ public class Hyperlinks {
 
     public static void main(String[] args) throws IOException  {
         HSSFWorkbook wb = new HSSFWorkbook();
+        HSSFCreationHelper helper = wb.getCreationHelper();
 
         //cell style for hyperlinks
         //by default hyperlinks are blue and underlined
@@ -47,7 +55,7 @@ public class Hyperlinks {
         //URL
         cell = sheet.createRow(0).createCell(0);
         cell.setCellValue("URL Link");
-        HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
+        HSSFHyperlink link = helper.createHyperlink(HyperlinkType.URL);
         link.setAddress("http://poi.apache.org/");
         cell.setHyperlink(link);
         cell.setCellStyle(hlink_style);
@@ -55,7 +63,7 @@ public class Hyperlinks {
         //link to a file in the current directory
         cell = sheet.createRow(1).createCell(0);
         cell.setCellValue("File Link");
-        link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
+        link = helper.createHyperlink(HyperlinkType.FILE);
         link.setAddress("link1.xls");
         cell.setHyperlink(link);
         cell.setCellStyle(hlink_style);
@@ -63,7 +71,7 @@ public class Hyperlinks {
         //e-mail link
         cell = sheet.createRow(2).createCell(0);
         cell.setCellValue("Email Link");
-        link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
+        link = helper.createHyperlink(HyperlinkType.EMAIL);
         //note, if subject contains white spaces, make sure they are url-encoded
         link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
         cell.setHyperlink(link);
@@ -77,7 +85,7 @@ public class Hyperlinks {
 
         cell = sheet.createRow(3).createCell(0);
         cell.setCellValue("Worksheet Link");
-        link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
+        link = helper.createHyperlink(HyperlinkType.DOCUMENT);
         link.setAddress("'Target Sheet'!A1");
         cell.setHyperlink(link);
         cell.setCellStyle(hlink_style);
index f75c89020433ffbf95e20612ee064ba613770c8f..132a3eb4c30a904a5d007732a06c78337206465b 100644 (file)
@@ -22,23 +22,31 @@ package org.apache.poi.common.usermodel;
 public interface Hyperlink {
     /**
      * Link to an existing file or web page
+     * 
+     * @deprecated POI 3.15 beta 3. Use {@link HyperlinkType#URL} instead.
      */
-    public static final int LINK_URL = 1;
+    public static final int LINK_URL = 1; // HyperlinkType.URL.getCode()
 
     /**
      * Link to a place in this document
+     * 
+     * @deprecated POI 3.15 beta 3. Use {@link HyperlinkType#DOCUMENT} instead.
      */
-    public static final int LINK_DOCUMENT = 2;
+    public static final int LINK_DOCUMENT = 2; // HyperlinkType.DOCUMENT.getCode()
 
     /**
      * Link to an E-mail address
+     * 
+     * @deprecated POI 3.15 beta 3. Use {@link HyperlinkType#EMAIL} instead.
      */
-    public static final int LINK_EMAIL = 3;
+    public static final int LINK_EMAIL = 3; // HyperlinkType.EMAIL.getCode()
 
     /**
-     * Link to a file
+     * Link to an file
+     * 
+     * @deprecated POI 3.15 beta 3. Use {@link HyperlinkType#FILE} instead.
      */
-    public static final int LINK_FILE = 4;
+    public static final int LINK_FILE = 4; // HyperlinkType.FILE.getCode()
 
     
     /**
@@ -73,6 +81,16 @@ public interface Hyperlink {
      * Return the type of this hyperlink
      *
      * @return the type of this hyperlink
+     * @see HyperlinkType#forInt(int)
+     * @deprecated POI 3.15 beta 3. Use {@link #getTypeEnum()}
      */
     public int getType();
+    
+    /**
+     * Return the type of this hyperlink
+     *
+     * @return the type of this hyperlink
+     * @since POI 3.15 beta 3
+     */
+    public HyperlinkType getTypeEnum();
 }
diff --git a/src/java/org/apache/poi/common/usermodel/HyperlinkType.java b/src/java/org/apache/poi/common/usermodel/HyperlinkType.java
new file mode 100644 (file)
index 0000000..0d8e139
--- /dev/null
@@ -0,0 +1,75 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.common.usermodel;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.poi.util.Internal;
+
+/**
+ * @since POI 3.15 beta 3
+ */
+public enum HyperlinkType {
+    /** Not a hyperlink */
+    @Internal
+    NONE(-1),
+    
+    /**
+     * Link to an existing file or web page
+     */
+    URL(1),
+
+    /**
+     * Link to a place in this document
+     */
+    DOCUMENT(2),
+
+    /**
+     * Link to an E-mail address
+     */
+    EMAIL(3),
+
+    /**
+     * Link to a file
+     */
+    FILE(4);
+    
+    private final int code;
+    private HyperlinkType(int code) {
+        this.code = code;
+    }
+    
+    private static final Map<Integer, HyperlinkType> map = new HashMap<Integer, HyperlinkType>();
+    static {
+        for (HyperlinkType type : values()) {
+            map.put(type.getCode(), type);
+        }
+    }
+    
+    public int getCode() {
+        return code;
+    }
+    
+    public static HyperlinkType forInt(int code) {
+        HyperlinkType type = map.get(code);
+        if (type == null) {
+            throw new IllegalArgumentException("Invalid type: " + code);
+        }
+        return type;
+    }
+}
index 337255fd3b2fa19a5baed85ec752eb1429752397..7ce7ef1be8a97fd631c3715164b8ba813341c7de 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.hssf.record.common.ExtendedColor;
 import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.util.Internal;
@@ -44,10 +45,19 @@ public class HSSFCreationHelper implements CreationHelper {
         return workbook.createDataFormat();
     }
 
+    /**
+     * {@inheritDoc}
+     * @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
+     */
+    @Deprecated
     @Override
     public HSSFHyperlink createHyperlink(int type) {
         return new HSSFHyperlink(type);
     }
+    @Override
+    public HSSFHyperlink createHyperlink(HyperlinkType type) {
+        return new HSSFHyperlink(type);
+    }
 
     @Override
     public HSSFExtendedColor createExtendedColor() {
index 1f3175121c927c86dad567814ad9714f096c35de..d91498679cab7cf79d4c58c5d14f71c06237fe4e 100644 (file)
 ==================================================================== */
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.hssf.record.HyperlinkRecord;
 import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.util.Internal;
 
 /**
  * Represents an Excel hyperlink.
  */
-public class HSSFHyperlink implements Hyperlink {\r
-\r
-    /**\r
-     * Link to an existing file or web page
-     * May be deprecated in the future. Consider using {@link Hyperlink#LINK_URL} instead.\r
-     */\r
-    public static final int LINK_URL = Hyperlink.LINK_URL;\r
-\r
-    /**
-     * Link to a place in this document
-     * May be deprecated in the future. Consider using {@link Hyperlink#LINK_DOCUMENT} instead.
-     */
-    public static final int LINK_DOCUMENT = Hyperlink.LINK_DOCUMENT;
-
-    /**
-     * Link to an E-mail address
-     * May be deprecated in the future. Consider using {@link Hyperlink#LINK_EMAIL} instead.
-     */
-    public static final int LINK_EMAIL = Hyperlink.LINK_EMAIL;
-
-    /**
-     * Link to a file
-     * May be deprecated in the future. Consider using {@link Hyperlink#LINK_FILE} instead.
-     */
-    public static final int LINK_FILE = Hyperlink.LINK_FILE;
+public class HSSFHyperlink implements Hyperlink {
 
     /**
      * Low-level record object that stores the actual hyperlink data
@@ -56,26 +34,43 @@ public class HSSFHyperlink implements Hyperlink {
     /**\r
      * If we create a new hyperlink remember its type\r
      */\r
-    final protected int link_type;\r
-\r
+    final protected HyperlinkType link_type;\r
+
     /**
      * Construct a new hyperlink
+     * 
+     * This method is internal to be used only by {@link HSSFCreationHelper#createHyperlink(int)}
      *
      * @param type the type of hyperlink to create
+     * @deprecated POI 3.15 beta 3
      */
-    public HSSFHyperlink( int type )
+    @Internal(since="3.15 beta 3")
+    protected HSSFHyperlink( int type )
+    {
+        this(HyperlinkType.forInt(type));
+    }
+    \r
+    /**
+     * Construct a new hyperlink
+     * 
+     * This method is internal to be used only by {@link HSSFCreationHelper#createHyperlink(int)}
+     *
+     * @param type the type of hyperlink to create
+     */
+    @Internal(since="3.15 beta 3")
+    protected HSSFHyperlink( HyperlinkType type )
     {
         this.link_type = type;
         record = new HyperlinkRecord();
         switch(type){
-            case LINK_URL:
-            case LINK_EMAIL:
+            case URL:
+            case EMAIL:
                 record.newUrlLink();
                 break;
-            case LINK_FILE:
+            case FILE:
                 record.newFileLink();
                 break;
-            case LINK_DOCUMENT:
+            case DOCUMENT:
                 record.newDocumentLink();
                 break;
             default:
@@ -94,19 +89,19 @@ public class HSSFHyperlink implements Hyperlink {
         link_type = getType(record);
     }
     
-    private int getType(HyperlinkRecord record) {
-        int link_type;
+    private static HyperlinkType getType(HyperlinkRecord record) {
+        HyperlinkType link_type;
         // Figure out the type
         if (record.isFileLink()) {
-            link_type = LINK_FILE;
+            link_type = HyperlinkType.FILE;
         } else if(record.isDocumentLink()) {
-            link_type = LINK_DOCUMENT;
+            link_type = HyperlinkType.DOCUMENT;
         } else {
             if(record.getAddress() != null &&
                     record.getAddress().startsWith("mailto:")) {
-                link_type = LINK_EMAIL;
+                link_type = HyperlinkType.EMAIL;
             } else {
-                link_type = LINK_URL;
+                link_type = HyperlinkType.URL;
             }
         }
         return link_type;
@@ -119,7 +114,7 @@ public class HSSFHyperlink implements Hyperlink {
             link_type = getType(record);
         }
         else {
-            link_type = other.getType();
+            link_type = other.getTypeEnum();
             record = new HyperlinkRecord();
             setFirstRow(other.getFirstRow());
             setFirstColumn(other.getFirstColumn());
@@ -271,13 +266,24 @@ public class HSSFHyperlink implements Hyperlink {
         record.setLabel(label);
     }
 
+    /**
+     * Return the type of this hyperlink
+     *
+     * @return the type of this hyperlink
+     * @see HyperlinkType#forInt
+     */
+    @Override
+    public int getType() {
+        return link_type.getCode();
+    }
+    
     /**
      * Return the type of this hyperlink
      *
      * @return the type of this hyperlink
      */
     @Override
-    public int getType(){
+    public HyperlinkType getTypeEnum() {
         return link_type;
     }
     
index 749cacf0b7afcc163c623a0e09e69172bd37aaf4..25e1cdaae8476e4e979d02b5d71a4ad4f0ca8e4a 100644 (file)
@@ -16,6 +16,8 @@
 ==================================================================== */
 package org.apache.poi.ss.usermodel;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
+
 /**
  * An object that handles instantiating concrete
  *  classes of the various instances one needs for
@@ -42,8 +44,15 @@ public interface CreationHelper {
 
     /**
      * Creates a new Hyperlink, of the given type
+     * @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
      */
+    @Deprecated
     Hyperlink createHyperlink(int type);
+    
+    /**
+     * Creates a new Hyperlink, of the given type
+     */
+    Hyperlink createHyperlink(HyperlinkType type);
 
     /**
      * Creates FormulaEvaluator - an object that evaluates formula cells.
index fd66d1f7e5692d54b26b461ce7c13ad5161a86d3..09b883c00f8cf6624c65de8dd90ea38adbba4692 100644 (file)
@@ -18,6 +18,7 @@ package org.apache.poi.xslf.usermodel;
 \r
 import java.net.URI;\r
 \r
+import org.apache.poi.common.usermodel.HyperlinkType;\r
 import org.apache.poi.openxml4j.opc.PackagePart;\r
 import org.apache.poi.openxml4j.opc.PackagePartName;\r
 import org.apache.poi.openxml4j.opc.PackageRelationship;\r
@@ -70,12 +71,17 @@ public class XSLFHyperlink implements Hyperlink<XSLFShape,XSLFTextParagraph> {
 \r
     @Override\r
     public int getType() {\r
+        return getTypeEnum().getCode();\r
+    }\r
+    \r
+    @Override\r
+    public HyperlinkType getTypeEnum() {\r
         String action = _link.getAction();\r
         if (action == null) {\r
             action = "";\r
         }\r
         if (action.equals("ppaction://hlinksldjump") || action.startsWith("ppaction://hlinkshowjump")) {\r
-            return LINK_DOCUMENT;\r
+            return HyperlinkType.DOCUMENT;\r
         }\r
         \r
         String address = getAddress();\r
@@ -83,9 +89,9 @@ public class XSLFHyperlink implements Hyperlink<XSLFShape,XSLFTextParagraph> {
             address = "";\r
         }\r
         if (address.startsWith("mailto:")) {\r
-            return LINK_EMAIL;\r
+            return HyperlinkType.EMAIL;\r
         } else {\r
-            return LINK_URL;\r
+            return HyperlinkType.URL;\r
         }\r
     }\r
 \r
index 24d68666579d8f2cd28e73d96b6962abb9b5a02d..201879702fbfcc7a0c2e5797acf7276c7bf06ac9 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.xssf.streaming;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.ss.usermodel.ClientAnchor;
 import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.DataFormat;
@@ -66,11 +67,20 @@ public class SXSSFCreationHelper implements CreationHelper {
     public DataFormat createDataFormat() {
         return helper.createDataFormat();
     }
+    /**
+     * {@inheritDoc}
+     * @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
+     */
+    @Deprecated
     @Override
     public Hyperlink createHyperlink(int type) {
         return helper.createHyperlink(type);
     }
     @Override
+    public Hyperlink createHyperlink(HyperlinkType type) {
+        return helper.createHyperlink(type);
+    }
+    @Override
     public ExtendedColor createExtendedColor() {
         return helper.createExtendedColor();
     }
index 4ea560d33aa703ffc93c66eb5e25bab16ea0ad1a..63dc78069992069a893538be34f7c71b6064d340 100644 (file)
@@ -16,6 +16,7 @@
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.Hyperlink;
 import org.apache.poi.util.Internal;
@@ -56,12 +57,24 @@ public class XSSFCreationHelper implements CreationHelper {
     /**
      * Create a new XSSFHyperlink.
      *
-     * @param type - the type of hyperlink to create, see {@link Hyperlink}
+     * @param type - the type of hyperlink to create, see {@link HyperlinkType}
+     * @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
      */
+    @Deprecated
     @Override
     public XSSFHyperlink createHyperlink(int type) {
         return new XSSFHyperlink(type);
     }
+    
+    /**
+     * Create a new XSSFHyperlink.
+     *
+     * @param type - the type of hyperlink to create, see {@link Hyperlink}
+     */
+    @Override
+    public XSSFHyperlink createHyperlink(HyperlinkType type) {
+        return new XSSFHyperlink(type);
+    }
 
     /**
      * Creates a XSSFFormulaEvaluator, the object that evaluates formula cells.
index 2d1127246431d6be8b484175c5e43f6839d0f882..54bd9d280aa269a21604b4897604e72f2e2243df 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
 import java.net.URI;
 import java.net.URISyntaxException;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.ss.usermodel.Hyperlink;
@@ -32,17 +33,27 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
  * are largely stored as relations of the sheet
  */
 public class XSSFHyperlink implements Hyperlink {
-    final private int _type;
+    final private HyperlinkType _type;
     final private PackageRelationship _externalRel;
     final private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink is anchored, getRef()
     private String _location; //what the hyperlink refers to
 
     /**
-     * Create a new XSSFHyperlink. This method is protected to be used only by XSSFCreationHelper
+     * Create a new XSSFHyperlink. This method is protected to be used only by {@link XSSFCreationHelper#createHyperlink(int)}
      *
      * @param type - the type of hyperlink to create, see {@link Hyperlink}
+     * @deprecated POI 3.15 beta 3. Use {@link XSSFHyperlink(Hyperlink)} instead.
      */
     protected XSSFHyperlink(int type) {
+        this(HyperlinkType.forInt(type));
+    }
+    
+    /**
+     * Create a new XSSFHyperlink. This method is protected to be used only by {@link XSSFCreationHelper#createHyperlink(int)}
+     *
+     * @param type - the type of hyperlink to create, see {@link Hyperlink}
+     */
+    protected XSSFHyperlink(HyperlinkType type) {
         _type = type;
         _ctHyperlink = CTHyperlink.Factory.newInstance();
         _externalRel = null;
@@ -63,7 +74,7 @@ public class XSSFHyperlink implements Hyperlink {
         if (_externalRel == null) {
             // If it has a location, it's internal
             if (ctHyperlink.getLocation() != null) {
-                _type = Hyperlink.LINK_DOCUMENT;
+                _type = HyperlinkType.DOCUMENT;
                 _location = ctHyperlink.getLocation();
             } else if (ctHyperlink.getId() != null) {
                 throw new IllegalStateException("The hyperlink for cell "
@@ -71,7 +82,7 @@ public class XSSFHyperlink implements Hyperlink {
                         + ctHyperlink.getId() + ", but that didn't exist!");
             } else {
                 // hyperlink is internal and is not related to other parts
-                _type = Hyperlink.LINK_DOCUMENT;
+                _type = HyperlinkType.DOCUMENT;
             }
         } else {
             URI target = _externalRel.getTargetURI();
@@ -84,11 +95,11 @@ public class XSSFHyperlink implements Hyperlink {
             // Try to figure out the type
                if (_location.startsWith("http://") || _location.startsWith("https://")
                     || _location.startsWith("ftp://")) {
-                _type = Hyperlink.LINK_URL;
+                _type = HyperlinkType.URL;
             } else if (_location.startsWith("mailto:")) {
-                _type = Hyperlink.LINK_EMAIL;
+                _type = HyperlinkType.EMAIL;
             } else {
-                _type = Hyperlink.LINK_FILE;
+                _type = HyperlinkType.FILE;
             }
         }
 
@@ -106,13 +117,13 @@ public class XSSFHyperlink implements Hyperlink {
     public XSSFHyperlink(Hyperlink other) {
         if (other instanceof XSSFHyperlink) {
             XSSFHyperlink xlink = (XSSFHyperlink) other;
-            _type = xlink.getType();
+            _type = xlink.getTypeEnum();
             _location = xlink._location;
             _externalRel = xlink._externalRel;
             _ctHyperlink = (CTHyperlink) xlink._ctHyperlink.copy();
         }
         else {
-            _type = other.getType();
+            _type = other.getTypeEnum();
             _location = other.getAddress();
             _externalRel = null;
             _ctHyperlink = CTHyperlink.Factory.newInstance();
@@ -132,7 +143,7 @@ public class XSSFHyperlink implements Hyperlink {
      * this hyperlink?
      */
     public boolean needsRelationToo() {
-        return (_type != Hyperlink.LINK_DOCUMENT);
+        return (_type != HyperlinkType.DOCUMENT);
     }
 
     /**
@@ -153,9 +164,21 @@ public class XSSFHyperlink implements Hyperlink {
      * Return the type of this hyperlink
      *
      * @return the type of this hyperlink
+     * @see HyperlinkType#forInt
+     * @deprecated POI 3.15 beta 3. Use {@link #getTypeEnum()} instead.
      */
     @Override
     public int getType() {
+        return _type.getCode();
+    }
+    
+    /**
+     * Return the type of this hyperlink
+     *
+     * @return the type of this hyperlink
+     */
+    @Override
+    public HyperlinkType getTypeEnum() {
         return _type;
     }
 
@@ -230,7 +253,7 @@ public class XSSFHyperlink implements Hyperlink {
 
        _location = address;
         //we must set location for internal hyperlinks
-        if (_type == Hyperlink.LINK_DOCUMENT) {
+        if (_type == HyperlinkType.DOCUMENT) {
             setLocation(address);
         }
     }
@@ -239,21 +262,19 @@ public class XSSFHyperlink implements Hyperlink {
     private void validate(String address) {
         switch (_type) {
             // email, path to file and url must be valid URIs
-            case Hyperlink.LINK_EMAIL:
-            case Hyperlink.LINK_FILE:
-            case Hyperlink.LINK_URL:
+            case EMAIL:
+            case FILE:
+            case URL:
                 try {
                     new URI(address);
                 } catch (URISyntaxException e) {
                     throw new IllegalArgumentException("Address of hyperlink must be a valid URI", e);
                 }
                 break;
-            case Hyperlink.LINK_DOCUMENT:
+            case DOCUMENT:
                 // currently not evaluating anything.
                 break;
             default:
-                // this check wouldn't need to be done if _type was checked when object was set
-                // since _type is final, this check would only need to be done once
                 throw new IllegalStateException("Invalid Hyperlink type: " + _type);
         }
     }
@@ -265,6 +286,7 @@ public class XSSFHyperlink implements Hyperlink {
     public void setCellReference(String ref) {
         _ctHyperlink.setRef(ref);
     }
+    @Internal
     protected void setCellReference(CellReference ref) {
         setCellReference(ref.formatAsString());
     }
index ec68569b73c5fe67721235ed2bcf6c522d0be0d2..7d0f3725caabd7c3d87bb655faaeee41c89201b9 100644 (file)
@@ -23,7 +23,9 @@ import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.hssf.usermodel.HSSFHyperlink;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
 import org.apache.poi.ss.usermodel.BaseTestHyperlink;
@@ -278,8 +280,9 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
     }
     
     @Test
-    public void testCopyHSSFHyperlink() {
-        HSSFHyperlink hlink = new HSSFHyperlink(Hyperlink.LINK_URL);
+    public void testCopyHSSFHyperlink() throws IOException {
+        HSSFWorkbook hssfworkbook = new HSSFWorkbook();
+        HSSFHyperlink hlink = hssfworkbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
         hlink.setAddress("http://poi.apache.org/");
         hlink.setFirstColumn(3);
         hlink.setFirstRow(2);
@@ -292,6 +295,8 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
         assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef()));
         // Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
         // assertEquals("label", xlink.getTooltip());
+        
+        hssfworkbook.close();
     }
     
     /* bug 59775: XSSFHyperlink has wrong type if it contains a location (CTHyperlink#getLocation)
index 949a1230dd873a97cd8e5debf99dcc80c205f350..033f3bbebb351bff85221e7a6e781fc7000dd723 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.ListIterator;
 
+import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.hslf.record.ExHyperlink;
 import org.apache.poi.hslf.record.ExHyperlinkAtom;
 import org.apache.poi.hslf.record.ExObjList;
@@ -127,25 +128,38 @@ public final class HSLFHyperlink implements Hyperlink<HSLFShape,HSLFTextParagrap
      *
      * @return the hyperlink URL
      * @see InteractiveInfoAtom
+     * @deprecated POI 3.15 beta 3. Use {@link #getTypeEnum()}
      */
     @Override
     public int getType() {
+        return getTypeEnum().getCode();
+    }
+    
+    /**
+     * Gets the type of the hyperlink action.
+     * Must be a <code>LINK_*</code>  constant</code>
+     *
+     * @return the hyperlink URL
+     * @see InteractiveInfoAtom
+     */
+    @Override
+    public HyperlinkType getTypeEnum() {
         switch (info.getInteractiveInfoAtom().getHyperlinkType()) {
         case InteractiveInfoAtom.LINK_Url:
-            return (exHyper.getLinkURL().startsWith("mailto:")) ? LINK_EMAIL : LINK_URL;
+            return (exHyper.getLinkURL().startsWith("mailto:")) ? HyperlinkType.EMAIL : HyperlinkType.URL;
         case InteractiveInfoAtom.LINK_NextSlide:
         case InteractiveInfoAtom.LINK_PreviousSlide:
         case InteractiveInfoAtom.LINK_FirstSlide:
         case InteractiveInfoAtom.LINK_LastSlide:
         case InteractiveInfoAtom.LINK_SlideNumber:
-            return LINK_DOCUMENT;
+            return HyperlinkType.DOCUMENT;
         case InteractiveInfoAtom.LINK_CustomShow:
         case InteractiveInfoAtom.LINK_OtherPresentation:
         case InteractiveInfoAtom.LINK_OtherFile:
-            return LINK_FILE;
+            return HyperlinkType.FILE;
         default:
         case InteractiveInfoAtom.LINK_NULL:
-            return -1;
+            return HyperlinkType.NONE;
         }
     }