2008-04-04 13:59:43 -04:00
|
|
|
/* ====================================================================
|
|
|
|
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.xssf.usermodel;
|
|
|
|
|
2008-04-05 17:43:53 -04:00
|
|
|
import java.net.URI;
|
|
|
|
|
2008-04-04 13:59:43 -04:00
|
|
|
import org.apache.poi.ss.usermodel.Hyperlink;
|
2008-04-05 17:43:53 -04:00
|
|
|
import org.apache.poi.ss.util.CellReference;
|
2008-04-04 13:59:43 -04:00
|
|
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
|
|
|
|
|
|
|
|
import org.openxml4j.opc.PackagePart;
|
2008-04-05 17:43:53 -04:00
|
|
|
import org.openxml4j.opc.PackageRelationship;
|
|
|
|
|
2008-04-04 13:59:43 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* XSSF Implementation of a Hyperlink.
|
|
|
|
* Note - unlike with HSSF, many kinds of hyperlink
|
|
|
|
* are largely stored as relations of the sheet
|
|
|
|
*/
|
|
|
|
public class XSSFHyperlink implements Hyperlink {
|
|
|
|
private int type;
|
2008-04-05 17:43:53 -04:00
|
|
|
private PackageRelationship externalRel;
|
2008-04-04 13:59:43 -04:00
|
|
|
private CTHyperlink ctHyperlink;
|
2008-04-05 17:43:53 -04:00
|
|
|
private String location;
|
2008-04-04 13:59:43 -04:00
|
|
|
|
2008-04-05 17:43:53 -04:00
|
|
|
protected XSSFHyperlink(int type) {
|
2008-04-04 13:59:43 -04:00
|
|
|
this.type = type;
|
|
|
|
this.ctHyperlink = CTHyperlink.Factory.newInstance();
|
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) {
|
2008-04-04 13:59:43 -04:00
|
|
|
this.ctHyperlink = ctHyperlink;
|
2008-04-05 17:43:53 -04:00
|
|
|
this.externalRel = hyperlinkRel;
|
2008-04-04 13:59:43 -04:00
|
|
|
|
2008-04-07 06:31:55 -04:00
|
|
|
// Figure out the Hyperlink type and distination
|
2008-04-05 17:43:53 -04:00
|
|
|
|
|
|
|
// If it has a location, it's internal
|
|
|
|
if(ctHyperlink.getLocation() != null) {
|
|
|
|
type = Hyperlink.LINK_DOCUMENT;
|
|
|
|
location = ctHyperlink.getLocation();
|
|
|
|
} else {
|
|
|
|
// Otherwise it's somehow external, check
|
|
|
|
// the relation to see how
|
|
|
|
if(externalRel == null) {
|
|
|
|
if(ctHyperlink.getId() != null) {
|
|
|
|
throw new IllegalStateException("The hyperlink for cell " + ctHyperlink.getRef() + " references relation " + ctHyperlink.getId() + ", but that didn't exist!");
|
|
|
|
} else {
|
|
|
|
throw new IllegalStateException("A sheet hyperlink must either have a location, or a relationship. Found:\n" + ctHyperlink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 06:31:55 -04:00
|
|
|
URI target = externalRel.getTargetURI();
|
|
|
|
location = target.toString();
|
|
|
|
|
|
|
|
// Try to figure out the type
|
|
|
|
if(location.startsWith("http://") || location.startsWith("https://")
|
|
|
|
|| location.startsWith("ftp://")) {
|
|
|
|
type = Hyperlink.LINK_URL;
|
|
|
|
} else if(location.startsWith("mailto:")) {
|
|
|
|
type = Hyperlink.LINK_EMAIL;
|
|
|
|
} else {
|
|
|
|
type = Hyperlink.LINK_FILE;
|
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
}
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the underlying hyperlink object
|
|
|
|
*/
|
|
|
|
protected CTHyperlink getCTHyperlink() {
|
|
|
|
return ctHyperlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we need to a relation too, to represent
|
|
|
|
* this hyperlink?
|
|
|
|
*/
|
|
|
|
public boolean needsRelationToo() {
|
2008-04-05 17:43:53 -04:00
|
|
|
return (type != Hyperlink.LINK_DOCUMENT);
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the relation if required
|
|
|
|
*/
|
2008-04-05 17:43:53 -04:00
|
|
|
protected void generateRelationIfNeeded(PackagePart sheetPart) {
|
|
|
|
if(needsRelationToo()) {
|
2008-04-07 06:31:55 -04:00
|
|
|
// Generate the relation
|
|
|
|
PackageRelationship rel =
|
2008-08-11 15:14:03 -04:00
|
|
|
sheetPart.addExternalRelationship(location, XSSFRelation.SHEET_HYPERLINKS.getRelation());
|
2008-04-07 06:31:55 -04:00
|
|
|
|
|
|
|
// Update the r:id
|
|
|
|
ctHyperlink.setId(rel.getId());
|
2008-04-05 17:43:53 -04:00
|
|
|
}
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2008-04-05 17:43:53 -04:00
|
|
|
/**
|
|
|
|
* Get the reference of the cell this applies to,
|
|
|
|
* eg A55
|
|
|
|
*/
|
|
|
|
public String getCellRef() {
|
|
|
|
return ctHyperlink.getRef();
|
|
|
|
}
|
|
|
|
|
2008-04-04 13:59:43 -04:00
|
|
|
public String getAddress() {
|
2008-04-05 17:43:53 -04:00
|
|
|
return location;
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
public String getLabel() {
|
2008-04-05 17:43:53 -04:00
|
|
|
return ctHyperlink.getDisplay();
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
2008-10-17 13:44:53 -04:00
|
|
|
public String getLocation() {
|
|
|
|
return ctHyperlink.getLocation();
|
|
|
|
}
|
2008-04-04 13:59:43 -04:00
|
|
|
|
|
|
|
public void setLabel(String label) {
|
2008-04-05 17:43:53 -04:00
|
|
|
ctHyperlink.setDisplay(label);
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
2008-10-17 13:44:53 -04:00
|
|
|
|
|
|
|
public void setLocation(String location){
|
|
|
|
ctHyperlink.setLocation(location);
|
|
|
|
}
|
|
|
|
|
2008-04-04 13:59:43 -04:00
|
|
|
public void setAddress(String address) {
|
2008-04-05 17:43:53 -04:00
|
|
|
location = address;
|
2008-10-17 13:44:53 -04:00
|
|
|
//we must set location for internal hyperlinks
|
|
|
|
if(type == Hyperlink.LINK_DOCUMENT){
|
|
|
|
setLocation(address);
|
|
|
|
}
|
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
|
2008-04-07 06:31:55 -04:00
|
|
|
/**
|
|
|
|
* Assigns this hyperlink to the given cell reference
|
|
|
|
*/
|
|
|
|
protected void setCellReference(String ref) {
|
|
|
|
ctHyperlink.setRef(ref);
|
|
|
|
}
|
|
|
|
|
2008-04-05 17:43:53 -04:00
|
|
|
private CellReference buildCellReference() {
|
|
|
|
return new CellReference(ctHyperlink.getRef());
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
|
2008-04-05 17:43:53 -04:00
|
|
|
public int getFirstColumn() {
|
|
|
|
return buildCellReference().getCol();
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
public int getLastColumn() {
|
|
|
|
return buildCellReference().getCol();
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
|
|
|
|
public int getFirstRow() {
|
|
|
|
return buildCellReference().getRow();
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
public int getLastRow() {
|
2008-04-05 17:43:53 -04:00
|
|
|
return buildCellReference().getRow();
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
|
2008-04-05 17:43:53 -04:00
|
|
|
public void setFirstColumn(int col) {
|
|
|
|
ctHyperlink.setRef(
|
|
|
|
new CellReference(
|
|
|
|
getFirstRow(), col
|
|
|
|
).formatAsString()
|
|
|
|
);
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
public void setLastColumn(int col) {
|
|
|
|
setFirstColumn(col);
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
2008-04-05 17:43:53 -04:00
|
|
|
public void setFirstRow(int row) {
|
|
|
|
ctHyperlink.setRef(
|
|
|
|
new CellReference(
|
|
|
|
row, getFirstColumn()
|
|
|
|
).formatAsString()
|
|
|
|
);
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
public void setLastRow(int row) {
|
2008-04-05 17:43:53 -04:00
|
|
|
setFirstRow(row);
|
2008-04-04 13:59:43 -04:00
|
|
|
}
|
|
|
|
}
|