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.
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
//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);
//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);
//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);
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);
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()
/**
* 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();
}
--- /dev/null
+/* ====================================================================
+ 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;
+ }
+}
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;
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() {
==================================================================== */
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
/**\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:
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;
link_type = getType(record);
}
else {
- link_type = other.getType();
+ link_type = other.getTypeEnum();
record = new HyperlinkRecord();
setFirstRow(other.getFirstRow());
setFirstColumn(other.getFirstColumn());
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;
}
==================================================================== */
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
/**
* 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.
\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
\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
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
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;
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();
}
==================================================================== */
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;
/**
* 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.
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;
* 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;
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 "
+ 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();
// 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;
}
}
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();
* this hyperlink?
*/
public boolean needsRelationToo() {
- return (_type != Hyperlink.LINK_DOCUMENT);
+ return (_type != HyperlinkType.DOCUMENT);
}
/**
* 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;
}
_location = address;
//we must set location for internal hyperlinks
- if (_type == Hyperlink.LINK_DOCUMENT) {
+ if (_type == HyperlinkType.DOCUMENT) {
setLocation(address);
}
}
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);
}
}
public void setCellReference(String ref) {
_ctHyperlink.setRef(ref);
}
+ @Internal
protected void setCellReference(CellReference ref) {
setCellReference(ref.formatAsString());
}
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;
}
@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);
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)
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;
*
* @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;
}
}