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
This commit is contained in:
Javen O'Neal 2016-07-17 09:20:21 +00:00
parent 0ac8c90896
commit c44094c04e
12 changed files with 277 additions and 81 deletions

View File

@ -17,11 +17,18 @@
package org.apache.poi.hssf.usermodel.examples; 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.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. * Demonstrates how to create hyperlinks.
@ -32,6 +39,7 @@ public class Hyperlinks {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFCreationHelper helper = wb.getCreationHelper();
//cell style for hyperlinks //cell style for hyperlinks
//by default hyperlinks are blue and underlined //by default hyperlinks are blue and underlined
@ -47,7 +55,7 @@ public class Hyperlinks {
//URL //URL
cell = sheet.createRow(0).createCell(0); cell = sheet.createRow(0).createCell(0);
cell.setCellValue("URL Link"); cell.setCellValue("URL Link");
HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL); HSSFHyperlink link = helper.createHyperlink(HyperlinkType.URL);
link.setAddress("http://poi.apache.org/"); link.setAddress("http://poi.apache.org/");
cell.setHyperlink(link); cell.setHyperlink(link);
cell.setCellStyle(hlink_style); cell.setCellStyle(hlink_style);
@ -55,7 +63,7 @@ public class Hyperlinks {
//link to a file in the current directory //link to a file in the current directory
cell = sheet.createRow(1).createCell(0); cell = sheet.createRow(1).createCell(0);
cell.setCellValue("File Link"); cell.setCellValue("File Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE); link = helper.createHyperlink(HyperlinkType.FILE);
link.setAddress("link1.xls"); link.setAddress("link1.xls");
cell.setHyperlink(link); cell.setHyperlink(link);
cell.setCellStyle(hlink_style); cell.setCellStyle(hlink_style);
@ -63,7 +71,7 @@ public class Hyperlinks {
//e-mail link //e-mail link
cell = sheet.createRow(2).createCell(0); cell = sheet.createRow(2).createCell(0);
cell.setCellValue("Email Link"); 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 //note, if subject contains white spaces, make sure they are url-encoded
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks"); link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
cell.setHyperlink(link); cell.setHyperlink(link);
@ -77,7 +85,7 @@ public class Hyperlinks {
cell = sheet.createRow(3).createCell(0); cell = sheet.createRow(3).createCell(0);
cell.setCellValue("Worksheet Link"); cell.setCellValue("Worksheet Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT); link = helper.createHyperlink(HyperlinkType.DOCUMENT);
link.setAddress("'Target Sheet'!A1"); link.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link); cell.setHyperlink(link);
cell.setCellStyle(hlink_style); cell.setCellStyle(hlink_style);

View File

@ -22,23 +22,31 @@ package org.apache.poi.common.usermodel;
public interface Hyperlink { public interface Hyperlink {
/** /**
* Link to an existing file or web page * 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 * 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 * 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
* *
* @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(); public int getType();
/**
* Return the type of this hyperlink
*
* @return the type of this hyperlink
* @since POI 3.15 beta 3
*/
public HyperlinkType getTypeEnum();
} }

View File

@ -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;
}
}

View File

@ -17,6 +17,7 @@
package org.apache.poi.hssf.usermodel; 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.hssf.record.common.ExtendedColor;
import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
@ -44,10 +45,19 @@ public class HSSFCreationHelper implements CreationHelper {
return workbook.createDataFormat(); return workbook.createDataFormat();
} }
/**
* {@inheritDoc}
* @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
*/
@Deprecated
@Override @Override
public HSSFHyperlink createHyperlink(int type) { public HSSFHyperlink createHyperlink(int type) {
return new HSSFHyperlink(type); return new HSSFHyperlink(type);
} }
@Override
public HSSFHyperlink createHyperlink(HyperlinkType type) {
return new HSSFHyperlink(type);
}
@Override @Override
public HSSFExtendedColor createExtendedColor() { public HSSFExtendedColor createExtendedColor() {

View File

@ -16,38 +16,16 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.record.HyperlinkRecord; import org.apache.poi.hssf.record.HyperlinkRecord;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.util.Internal;
/** /**
* Represents an Excel hyperlink. * Represents an Excel hyperlink.
*/ */
public class HSSFHyperlink implements 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;
/** /**
* Low-level record object that stores the actual hyperlink data * 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 * If we create a new hyperlink remember its type
*/ */
final protected int link_type; final protected HyperlinkType link_type;
/** /**
* Construct a new hyperlink * 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 * @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; this.link_type = type;
record = new HyperlinkRecord(); record = new HyperlinkRecord();
switch(type){ switch(type){
case LINK_URL: case URL:
case LINK_EMAIL: case EMAIL:
record.newUrlLink(); record.newUrlLink();
break; break;
case LINK_FILE: case FILE:
record.newFileLink(); record.newFileLink();
break; break;
case LINK_DOCUMENT: case DOCUMENT:
record.newDocumentLink(); record.newDocumentLink();
break; break;
default: default:
@ -94,19 +89,19 @@ public class HSSFHyperlink implements Hyperlink {
link_type = getType(record); link_type = getType(record);
} }
private int getType(HyperlinkRecord record) { private static HyperlinkType getType(HyperlinkRecord record) {
int link_type; HyperlinkType link_type;
// Figure out the type // Figure out the type
if (record.isFileLink()) { if (record.isFileLink()) {
link_type = LINK_FILE; link_type = HyperlinkType.FILE;
} else if(record.isDocumentLink()) { } else if(record.isDocumentLink()) {
link_type = LINK_DOCUMENT; link_type = HyperlinkType.DOCUMENT;
} else { } else {
if(record.getAddress() != null && if(record.getAddress() != null &&
record.getAddress().startsWith("mailto:")) { record.getAddress().startsWith("mailto:")) {
link_type = LINK_EMAIL; link_type = HyperlinkType.EMAIL;
} else { } else {
link_type = LINK_URL; link_type = HyperlinkType.URL;
} }
} }
return link_type; return link_type;
@ -119,7 +114,7 @@ public class HSSFHyperlink implements Hyperlink {
link_type = getType(record); link_type = getType(record);
} }
else { else {
link_type = other.getType(); link_type = other.getTypeEnum();
record = new HyperlinkRecord(); record = new HyperlinkRecord();
setFirstRow(other.getFirstRow()); setFirstRow(other.getFirstRow());
setFirstColumn(other.getFirstColumn()); setFirstColumn(other.getFirstColumn());
@ -275,9 +270,20 @@ public class HSSFHyperlink implements Hyperlink {
* Return the type of this hyperlink * Return the type of this hyperlink
* *
* @return the type of this hyperlink * @return the type of this hyperlink
* @see HyperlinkType#forInt
*/ */
@Override @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; return link_type;
} }

View File

@ -16,6 +16,8 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import org.apache.poi.common.usermodel.HyperlinkType;
/** /**
* An object that handles instantiating concrete * An object that handles instantiating concrete
* classes of the various instances one needs for * classes of the various instances one needs for
@ -42,9 +44,16 @@ public interface CreationHelper {
/** /**
* Creates a new Hyperlink, of the given type * Creates a new Hyperlink, of the given type
* @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
*/ */
@Deprecated
Hyperlink createHyperlink(int type); Hyperlink createHyperlink(int type);
/**
* Creates a new Hyperlink, of the given type
*/
Hyperlink createHyperlink(HyperlinkType type);
/** /**
* Creates FormulaEvaluator - an object that evaluates formula cells. * Creates FormulaEvaluator - an object that evaluates formula cells.
* *

View File

@ -18,6 +18,7 @@ package org.apache.poi.xslf.usermodel;
import java.net.URI; import java.net.URI;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
@ -70,12 +71,17 @@ public class XSLFHyperlink implements Hyperlink<XSLFShape,XSLFTextParagraph> {
@Override @Override
public int getType() { public int getType() {
return getTypeEnum().getCode();
}
@Override
public HyperlinkType getTypeEnum() {
String action = _link.getAction(); String action = _link.getAction();
if (action == null) { if (action == null) {
action = ""; action = "";
} }
if (action.equals("ppaction://hlinksldjump") || action.startsWith("ppaction://hlinkshowjump")) { if (action.equals("ppaction://hlinksldjump") || action.startsWith("ppaction://hlinkshowjump")) {
return LINK_DOCUMENT; return HyperlinkType.DOCUMENT;
} }
String address = getAddress(); String address = getAddress();
@ -83,9 +89,9 @@ public class XSLFHyperlink implements Hyperlink<XSLFShape,XSLFTextParagraph> {
address = ""; address = "";
} }
if (address.startsWith("mailto:")) { if (address.startsWith("mailto:")) {
return LINK_EMAIL; return HyperlinkType.EMAIL;
} else { } else {
return LINK_URL; return HyperlinkType.URL;
} }
} }

View File

@ -17,6 +17,7 @@
package org.apache.poi.xssf.streaming; 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.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.DataFormat;
@ -66,11 +67,20 @@ public class SXSSFCreationHelper implements CreationHelper {
public DataFormat createDataFormat() { public DataFormat createDataFormat() {
return helper.createDataFormat(); return helper.createDataFormat();
} }
/**
* {@inheritDoc}
* @deprecated POI 3.15 beta 3. Use {@link #createHyperlink(HyperlinkType)} instead.
*/
@Deprecated
@Override @Override
public Hyperlink createHyperlink(int type) { public Hyperlink createHyperlink(int type) {
return helper.createHyperlink(type); return helper.createHyperlink(type);
} }
@Override @Override
public Hyperlink createHyperlink(HyperlinkType type) {
return helper.createHyperlink(type);
}
@Override
public ExtendedColor createExtendedColor() { public ExtendedColor createExtendedColor() {
return helper.createExtendedColor(); return helper.createExtendedColor();
} }

View File

@ -16,6 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.usermodel; 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.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
@ -53,13 +54,25 @@ public class XSSFCreationHelper implements CreationHelper {
return new XSSFColor(); 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. * 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 Hyperlink}
*/ */
@Override @Override
public XSSFHyperlink createHyperlink(int type) { public XSSFHyperlink createHyperlink(HyperlinkType type) {
return new XSSFHyperlink(type); return new XSSFHyperlink(type);
} }

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ss.usermodel.Hyperlink; 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 * are largely stored as relations of the sheet
*/ */
public class XSSFHyperlink implements Hyperlink { public class XSSFHyperlink implements Hyperlink {
final private int _type; final private HyperlinkType _type;
final private PackageRelationship _externalRel; final private PackageRelationship _externalRel;
final private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink is anchored, getRef() final private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink is anchored, getRef()
private String _location; //what the hyperlink refers to 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} * @param type - the type of hyperlink to create, see {@link Hyperlink}
*/ */
protected XSSFHyperlink(int type) { protected XSSFHyperlink(HyperlinkType type) {
_type = type; _type = type;
_ctHyperlink = CTHyperlink.Factory.newInstance(); _ctHyperlink = CTHyperlink.Factory.newInstance();
_externalRel = null; _externalRel = null;
@ -63,7 +74,7 @@ public class XSSFHyperlink implements Hyperlink {
if (_externalRel == null) { if (_externalRel == null) {
// If it has a location, it's internal // If it has a location, it's internal
if (ctHyperlink.getLocation() != null) { if (ctHyperlink.getLocation() != null) {
_type = Hyperlink.LINK_DOCUMENT; _type = HyperlinkType.DOCUMENT;
_location = ctHyperlink.getLocation(); _location = ctHyperlink.getLocation();
} else if (ctHyperlink.getId() != null) { } else if (ctHyperlink.getId() != null) {
throw new IllegalStateException("The hyperlink for cell " throw new IllegalStateException("The hyperlink for cell "
@ -71,7 +82,7 @@ public class XSSFHyperlink implements Hyperlink {
+ ctHyperlink.getId() + ", but that didn't exist!"); + ctHyperlink.getId() + ", but that didn't exist!");
} else { } else {
// hyperlink is internal and is not related to other parts // hyperlink is internal and is not related to other parts
_type = Hyperlink.LINK_DOCUMENT; _type = HyperlinkType.DOCUMENT;
} }
} else { } else {
URI target = _externalRel.getTargetURI(); URI target = _externalRel.getTargetURI();
@ -84,11 +95,11 @@ public class XSSFHyperlink implements Hyperlink {
// Try to figure out the type // Try to figure out the type
if (_location.startsWith("http://") || _location.startsWith("https://") if (_location.startsWith("http://") || _location.startsWith("https://")
|| _location.startsWith("ftp://")) { || _location.startsWith("ftp://")) {
_type = Hyperlink.LINK_URL; _type = HyperlinkType.URL;
} else if (_location.startsWith("mailto:")) { } else if (_location.startsWith("mailto:")) {
_type = Hyperlink.LINK_EMAIL; _type = HyperlinkType.EMAIL;
} else { } else {
_type = Hyperlink.LINK_FILE; _type = HyperlinkType.FILE;
} }
} }
@ -106,13 +117,13 @@ public class XSSFHyperlink implements Hyperlink {
public XSSFHyperlink(Hyperlink other) { public XSSFHyperlink(Hyperlink other) {
if (other instanceof XSSFHyperlink) { if (other instanceof XSSFHyperlink) {
XSSFHyperlink xlink = (XSSFHyperlink) other; XSSFHyperlink xlink = (XSSFHyperlink) other;
_type = xlink.getType(); _type = xlink.getTypeEnum();
_location = xlink._location; _location = xlink._location;
_externalRel = xlink._externalRel; _externalRel = xlink._externalRel;
_ctHyperlink = (CTHyperlink) xlink._ctHyperlink.copy(); _ctHyperlink = (CTHyperlink) xlink._ctHyperlink.copy();
} }
else { else {
_type = other.getType(); _type = other.getTypeEnum();
_location = other.getAddress(); _location = other.getAddress();
_externalRel = null; _externalRel = null;
_ctHyperlink = CTHyperlink.Factory.newInstance(); _ctHyperlink = CTHyperlink.Factory.newInstance();
@ -132,7 +143,7 @@ public class XSSFHyperlink implements Hyperlink {
* this hyperlink? * this hyperlink?
*/ */
public boolean needsRelationToo() { 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
* *
* @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 @Override
public int getType() { public int getType() {
return _type.getCode();
}
/**
* Return the type of this hyperlink
*
* @return the type of this hyperlink
*/
@Override
public HyperlinkType getTypeEnum() {
return _type; return _type;
} }
@ -230,7 +253,7 @@ public class XSSFHyperlink implements Hyperlink {
_location = address; _location = address;
//we must set location for internal hyperlinks //we must set location for internal hyperlinks
if (_type == Hyperlink.LINK_DOCUMENT) { if (_type == HyperlinkType.DOCUMENT) {
setLocation(address); setLocation(address);
} }
} }
@ -239,21 +262,19 @@ public class XSSFHyperlink implements Hyperlink {
private void validate(String address) { private void validate(String address) {
switch (_type) { switch (_type) {
// email, path to file and url must be valid URIs // email, path to file and url must be valid URIs
case Hyperlink.LINK_EMAIL: case EMAIL:
case Hyperlink.LINK_FILE: case FILE:
case Hyperlink.LINK_URL: case URL:
try { try {
new URI(address); new URI(address);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new IllegalArgumentException("Address of hyperlink must be a valid URI", e); throw new IllegalArgumentException("Address of hyperlink must be a valid URI", e);
} }
break; break;
case Hyperlink.LINK_DOCUMENT: case DOCUMENT:
// currently not evaluating anything. // currently not evaluating anything.
break; break;
default: 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); throw new IllegalStateException("Invalid Hyperlink type: " + _type);
} }
} }
@ -265,6 +286,7 @@ public class XSSFHyperlink implements Hyperlink {
public void setCellReference(String ref) { public void setCellReference(String ref) {
_ctHyperlink.setRef(ref); _ctHyperlink.setRef(ref);
} }
@Internal
protected void setCellReference(CellReference ref) { protected void setCellReference(CellReference ref) {
setCellReference(ref.formatAsString()); setCellReference(ref.formatAsString());
} }

View File

@ -23,7 +23,9 @@ import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFHyperlink; 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.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.ss.usermodel.BaseTestHyperlink; import org.apache.poi.ss.usermodel.BaseTestHyperlink;
@ -278,8 +280,9 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
} }
@Test @Test
public void testCopyHSSFHyperlink() { public void testCopyHSSFHyperlink() throws IOException {
HSSFHyperlink hlink = new HSSFHyperlink(Hyperlink.LINK_URL); HSSFWorkbook hssfworkbook = new HSSFWorkbook();
HSSFHyperlink hlink = hssfworkbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
hlink.setAddress("http://poi.apache.org/"); hlink.setAddress("http://poi.apache.org/");
hlink.setFirstColumn(3); hlink.setFirstColumn(3);
hlink.setFirstRow(2); hlink.setFirstRow(2);
@ -292,6 +295,8 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef())); 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 // 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()); // assertEquals("label", xlink.getTooltip());
hssfworkbook.close();
} }
/* bug 59775: XSSFHyperlink has wrong type if it contains a location (CTHyperlink#getLocation) /* bug 59775: XSSFHyperlink has wrong type if it contains a location (CTHyperlink#getLocation)

View File

@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hslf.record.ExHyperlink; import org.apache.poi.hslf.record.ExHyperlink;
import org.apache.poi.hslf.record.ExHyperlinkAtom; import org.apache.poi.hslf.record.ExHyperlinkAtom;
import org.apache.poi.hslf.record.ExObjList; import org.apache.poi.hslf.record.ExObjList;
@ -127,25 +128,38 @@ public final class HSLFHyperlink implements Hyperlink<HSLFShape,HSLFTextParagrap
* *
* @return the hyperlink URL * @return the hyperlink URL
* @see InteractiveInfoAtom * @see InteractiveInfoAtom
* @deprecated POI 3.15 beta 3. Use {@link #getTypeEnum()}
*/ */
@Override @Override
public int getType() { 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()) { switch (info.getInteractiveInfoAtom().getHyperlinkType()) {
case InteractiveInfoAtom.LINK_Url: 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_NextSlide:
case InteractiveInfoAtom.LINK_PreviousSlide: case InteractiveInfoAtom.LINK_PreviousSlide:
case InteractiveInfoAtom.LINK_FirstSlide: case InteractiveInfoAtom.LINK_FirstSlide:
case InteractiveInfoAtom.LINK_LastSlide: case InteractiveInfoAtom.LINK_LastSlide:
case InteractiveInfoAtom.LINK_SlideNumber: case InteractiveInfoAtom.LINK_SlideNumber:
return LINK_DOCUMENT; return HyperlinkType.DOCUMENT;
case InteractiveInfoAtom.LINK_CustomShow: case InteractiveInfoAtom.LINK_CustomShow:
case InteractiveInfoAtom.LINK_OtherPresentation: case InteractiveInfoAtom.LINK_OtherPresentation:
case InteractiveInfoAtom.LINK_OtherFile: case InteractiveInfoAtom.LINK_OtherFile:
return LINK_FILE; return HyperlinkType.FILE;
default: default:
case InteractiveInfoAtom.LINK_NULL: case InteractiveInfoAtom.LINK_NULL:
return -1; return HyperlinkType.NONE;
} }
} }