From c44094c04e145dd3068e660f744095afa97e23f4 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sun, 17 Jul 2016 09:20:21 +0000 Subject: [PATCH] bug 59873: replace Hyperlink.LINK_* int constants with HyperlinkType enum git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753035 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/usermodel/examples/Hyperlinks.java | 24 ++++-- .../poi/common/usermodel/Hyperlink.java | 28 ++++-- .../poi/common/usermodel/HyperlinkType.java | 75 ++++++++++++++++ .../hssf/usermodel/HSSFCreationHelper.java | 10 +++ .../poi/hssf/usermodel/HSSFHyperlink.java | 86 ++++++++++--------- .../poi/ss/usermodel/CreationHelper.java | 9 ++ .../poi/xslf/usermodel/XSLFHyperlink.java | 12 ++- .../xssf/streaming/SXSSFCreationHelper.java | 10 +++ .../xssf/usermodel/XSSFCreationHelper.java | 15 +++- .../poi/xssf/usermodel/XSSFHyperlink.java | 58 +++++++++---- .../poi/xssf/usermodel/TestXSSFHyperlink.java | 9 +- .../poi/hslf/usermodel/HSLFHyperlink.java | 22 ++++- 12 files changed, 277 insertions(+), 81 deletions(-) create mode 100644 src/java/org/apache/poi/common/usermodel/HyperlinkType.java diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java index 0872749d0..c1ccad910 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java @@ -17,11 +17,18 @@ 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); diff --git a/src/java/org/apache/poi/common/usermodel/Hyperlink.java b/src/java/org/apache/poi/common/usermodel/Hyperlink.java index f75c89020..132a3eb4c 100644 --- a/src/java/org/apache/poi/common/usermodel/Hyperlink.java +++ b/src/java/org/apache/poi/common/usermodel/Hyperlink.java @@ -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 index 000000000..0d8e139c8 --- /dev/null +++ b/src/java/org/apache/poi/common/usermodel/HyperlinkType.java @@ -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 map = new HashMap(); + 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; + } +} diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java index 337255fd3..7ce7ef1be 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java @@ -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() { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java b/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java index 1f3175121..d91498679 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java @@ -16,37 +16,15 @@ ==================================================================== */ 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 { - - /** - * Link to an existing file or web page - * May be deprecated in the future. Consider using {@link Hyperlink#LINK_URL} instead. - */ - public static final int LINK_URL = Hyperlink.LINK_URL; - - /** - * 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 { /** * If we create a new hyperlink remember its type */ - final protected int link_type; - + final protected HyperlinkType link_type; + /** * 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 + */ + @Internal(since="3.15 beta 3") + protected HSSFHyperlink( int type ) + { + this(HyperlinkType.forInt(type)); + } + + /** + * 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 */ - public HSSFHyperlink( int type ) + @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()); @@ -275,9 +270,20 @@ public class HSSFHyperlink implements Hyperlink { * Return the type of this hyperlink * * @return the type of this hyperlink + * @see HyperlinkType#forInt */ @Override - public int getType(){ + public int getType() { + return link_type.getCode(); + } + + /** + * Return the type of this hyperlink + * + * @return the type of this hyperlink + */ + @Override + public HyperlinkType getTypeEnum() { return link_type; } diff --git a/src/java/org/apache/poi/ss/usermodel/CreationHelper.java b/src/java/org/apache/poi/ss/usermodel/CreationHelper.java index 749cacf0b..25e1cdaae 100644 --- a/src/java/org/apache/poi/ss/usermodel/CreationHelper.java +++ b/src/java/org/apache/poi/ss/usermodel/CreationHelper.java @@ -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. diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java index fd66d1f7e..09b883c00 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java @@ -18,6 +18,7 @@ package org.apache.poi.xslf.usermodel; import java.net.URI; +import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackageRelationship; @@ -70,12 +71,17 @@ public class XSLFHyperlink implements Hyperlink { @Override public int getType() { + return getTypeEnum().getCode(); + } + + @Override + public HyperlinkType getTypeEnum() { String action = _link.getAction(); if (action == null) { action = ""; } if (action.equals("ppaction://hlinksldjump") || action.startsWith("ppaction://hlinkshowjump")) { - return LINK_DOCUMENT; + return HyperlinkType.DOCUMENT; } String address = getAddress(); @@ -83,9 +89,9 @@ public class XSLFHyperlink implements Hyperlink { address = ""; } if (address.startsWith("mailto:")) { - return LINK_EMAIL; + return HyperlinkType.EMAIL; } else { - return LINK_URL; + return HyperlinkType.URL; } } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCreationHelper.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCreationHelper.java index 24d686665..201879702 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCreationHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCreationHelper.java @@ -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(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java index 4ea560d33..63dc78069 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java @@ -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; @@ -53,13 +54,25 @@ public class XSSFCreationHelper implements CreationHelper { return new XSSFColor(); } + /** + * Create a new XSSFHyperlink. + * + * @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(int type) { + public XSSFHyperlink createHyperlink(HyperlinkType type) { return new XSSFHyperlink(type); } 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 2d1127246..54bd9d280 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java @@ -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(int type) { + 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()); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java index ec68569b7..7d0f3725c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java @@ -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) diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java index 949a1230d..033f3bbeb 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java @@ -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 HyperlinkLINK_* constant + * + * @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; } }