Bug 53282 - Hyperlink with a non-breaking space throws java.lang.IllegalStateException

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1563540 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2014-02-02 01:56:02 +00:00
parent 3fb8fc3830
commit 54d462a3f6
4 changed files with 607 additions and 580 deletions

View File

@ -22,14 +22,14 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.POILogFactory;
/**
* Represents a collection of PackageRelationship elements that are owned by a
@ -320,10 +320,11 @@ public final class PackageRelationshipCollection implements
// Check OPC compliance M4.1 rule
boolean fCorePropertiesRelationship = false;
for (Iterator i = root
.elementIterator(PackageRelationship.RELATIONSHIP_TAG_NAME); i
.hasNext();) {
Element element = (Element) i.next();
@SuppressWarnings("unchecked")
Iterator<Element> iter = (Iterator<Element>)
root.elementIterator(PackageRelationship.RELATIONSHIP_TAG_NAME);
while (iter.hasNext()) {
Element element = iter.next();
// Relationship ID
String id = element.attribute(
PackageRelationship.ID_ATTRIBUTE_NAME).getValue();
@ -353,19 +354,19 @@ public final class PackageRelationshipCollection implements
}
// Target converted in URI
URI target;
String value = "";
try {
value = element.attribute(
URI target = PackagingURIHelper.toURI("http://invalid.uri"); // dummy url
String value = element.attribute(
PackageRelationship.TARGET_ATTRIBUTE_NAME)
.getValue();
try {
// when parsing of the given uri fails, we can either
// ignore this relationship, which leads to IllegalStateException
// later on, or use a dummy value and thus enable processing of the
// package
target = PackagingURIHelper.toURI(value);
} catch (URISyntaxException e) {
logger.log(POILogger.ERROR, "Cannot convert " + value
+ " in a valid relationship URI-> ignored", e);
continue;
+ " in a valid relationship URI-> dummy-URI used", e);
}
addRelationship(target, targetMode, type, id);
}

View File

@ -30,6 +30,7 @@ import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@ -62,64 +63,73 @@ public final class XSSFRelation extends POIXMLRelation {
);
public static final XSSFRelation MACROS_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.sheet.macroEnabled.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation TEMPLATE_WORKBOOK = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation MACRO_TEMPLATE_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.template.macroEnabled.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation MACRO_ADDIN_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.addin.macroEnabled.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation WORKSHEET = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
"/xl/worksheets/sheet#.xml",
XSSFSheet.class
);
public static final XSSFRelation CHARTSHEET = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet",
"/xl/chartsheets/sheet#.xml",
XSSFChartSheet.class
);
public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
"/xl/sharedStrings.xml",
SharedStringsTable.class
);
public static final XSSFRelation STYLES = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
PackageRelationshipTypes.STYLE_PART,
"/xl/styles.xml",
StylesTable.class
);
public static final XSSFRelation DRAWINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.drawing+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
"/xl/drawings/drawing#.xml",
XSSFDrawing.class
);
public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.vmlDrawing",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
"/xl/drawings/vmlDrawing#.vml",
XSSFVMLDrawing.class
);
public static final XSSFRelation CHART = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart",
@ -150,75 +160,84 @@ public final class XSSFRelation extends POIXMLRelation {
public static final XSSFRelation IMAGES = new XSSFRelation(
null,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
null,
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_EMF = new XSSFRelation(
"image/x-emf",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.emf",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_WMF = new XSSFRelation(
"image/x-wmf",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.wmf",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_PICT = new XSSFRelation(
"image/pict",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.pict",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_JPEG = new XSSFRelation(
"image/jpeg",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.jpeg",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_PNG = new XSSFRelation(
"image/png",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.png",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_DIB = new XSSFRelation(
"image/dib",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.dib",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_GIF = new XSSFRelation(
"image/gif",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.gif",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_TIFF = new XSSFRelation(
"image/tiff",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.tiff",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_EPS = new XSSFRelation(
"image/x-eps",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.eps",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_BMP = new XSSFRelation(
"image/x-ms-bmp",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.bmp",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_WPG = new XSSFRelation(
"image/x-wpg",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.wpg",
XSSFPictureData.class
);
@ -229,18 +248,21 @@ public final class XSSFRelation extends POIXMLRelation {
"/xl/comments#.xml",
CommentsTable.class
);
public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation(
null,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
PackageRelationshipTypes.HYPERLINK_PART,
null,
null
);
public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
null,
POIXMLDocument.OLE_OBJECT_REL_TYPE,
null,
null
);
public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
null,
POIXMLDocument.PACK_OBJECT_REL_TYPE,
@ -254,30 +276,35 @@ public final class XSSFRelation extends POIXMLRelation {
"/xl/vbaProject.bin",
null
);
public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
"application/vnd.ms-office.activeX+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
"/xl/activeX/activeX#.xml",
null
);
public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
"application/vnd.ms-office.activeX",
"http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
"/xl/activeX/activeX#.bin",
null
);
public static final XSSFRelation THEME = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.theme+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
"/xl/theme/theme#.xml",
ThemesTable.class
);
public static final XSSFRelation CALC_CHAIN = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain",
"/xl/calcChain.xml",
CalculationChain.class
);
public static final XSSFRelation PRINTER_SETTINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings",
@ -311,7 +338,6 @@ public final class XSSFRelation extends POIXMLRelation {
return null;
}
/**
* Get POIXMLRelation by relation type
*

View File

@ -26,7 +26,6 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
@ -1414,30 +1413,31 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
* error message when called via WorkbookFactory.
* (You need to supply a password explicitly for them)
*/
@Test
public void bug55692() throws Exception {
InputStream inpA = POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx");
InputStream inpB = POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx");
InputStream inpC = POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx");
@Test(expected=EncryptedDocumentException.class)
public void bug55692_stream() throws Exception {
// Directly on a Stream
try {
WorkbookFactory.create(inpA);
fail("Should've raised a EncryptedDocumentException error");
} catch (EncryptedDocumentException e) {}
WorkbookFactory.create(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
}
@Test(expected=EncryptedDocumentException.class)
public void bug55692_poifs() throws Exception {
// Via a POIFSFileSystem
POIFSFileSystem fsP = new POIFSFileSystem(inpB);
try {
POIFSFileSystem fsP = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
WorkbookFactory.create(fsP);
fail("Should've raised a EncryptedDocumentException error");
} catch (EncryptedDocumentException e) {}
}
@Test(expected=EncryptedDocumentException.class)
public void bug55692_npoifs() throws Exception {
// Via a NPOIFSFileSystem
NPOIFSFileSystem fsNP = new NPOIFSFileSystem(inpC);
try {
NPOIFSFileSystem fsNP = new NPOIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
WorkbookFactory.create(fsNP);
fail("Should've raised a EncryptedDocumentException error");
} catch (EncryptedDocumentException e) {}
}
@Test
public void bug53282() {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53282b.xlsx");
Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
assertEquals("#@_#", c.getStringCellValue());
assertEquals("http://invalid.uri", c.getHyperlink().getAddress());
}
}

Binary file not shown.