use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class ContentTypeManager method parseContentTypesFile.
/**
* Parse the content types part.
*
* @throws InvalidFormatException
* Throws if the content type doesn't exist or the XML format is
* invalid.
*/
private void parseContentTypesFile(InputStream in) throws InvalidFormatException {
try {
Document xmlContentTypetDoc = DocumentHelper.readDocument(in);
// Default content types
NodeList defaultTypes = xmlContentTypetDoc.getDocumentElement().getElementsByTagNameNS(TYPES_NAMESPACE_URI, DEFAULT_TAG_NAME);
int defaultTypeCount = defaultTypes.getLength();
for (int i = 0; i < defaultTypeCount; i++) {
Element element = (Element) defaultTypes.item(i);
String extension = element.getAttribute(EXTENSION_ATTRIBUTE_NAME);
String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
addDefaultContentType(extension, contentType);
}
// Overriden content types
NodeList overrideTypes = xmlContentTypetDoc.getDocumentElement().getElementsByTagNameNS(TYPES_NAMESPACE_URI, OVERRIDE_TAG_NAME);
int overrideTypeCount = overrideTypes.getLength();
for (int i = 0; i < overrideTypeCount; i++) {
Element element = (Element) overrideTypes.item(i);
URI uri = new URI(element.getAttribute(PART_NAME_ATTRIBUTE_NAME));
PackagePartName partName = PackagingURIHelper.createPartName(uri);
String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
addOverrideContentType(partName, contentType);
}
} catch (URISyntaxException urie) {
throw new InvalidFormatException(urie.getMessage());
} catch (SAXException e) {
throw new InvalidFormatException(e.getMessage());
} catch (IOException e) {
throw new InvalidFormatException(e.getMessage());
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class XSLFSlideShow method getSlideComments.
/**
* Returns all the comments for the given slide
*/
@Internal
public CTCommentList getSlideComments(CTSlideIdListEntry slide) throws IOException, XmlException {
PackageRelationshipCollection commentRels;
PackagePart slidePart = getSlidePart(slide);
try {
commentRels = slidePart.getRelationshipsByType(XSLFRelation.COMMENTS.getRelation());
} catch (InvalidFormatException e) {
throw new IllegalStateException(e);
}
if (commentRels.size() == 0) {
// No comments for this slide
return null;
}
if (commentRels.size() > 1) {
throw new IllegalStateException("Expecting 0 or 1 comments for a slide, but found " + commentRels.size());
}
try {
PackagePart cPart = slidePart.getRelatedPart(commentRels.getRelationship(0));
CmLstDocument commDoc = CmLstDocument.Factory.parse(cPart.getInputStream(), DEFAULT_XML_OPTIONS);
return commDoc.getCmLst();
} catch (InvalidFormatException e) {
throw new IllegalStateException(e);
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class XSLFSlideShow method getNodesPart.
/**
* Gets the PackagePart of the notes for the
* given slide, or null if there isn't one.
*/
public PackagePart getNodesPart(CTSlideIdListEntry parentSlide) throws IOException, XmlException {
PackageRelationshipCollection notes;
PackagePart slidePart = getSlidePart(parentSlide);
try {
notes = slidePart.getRelationshipsByType(XSLFRelation.NOTES.getRelation());
} catch (InvalidFormatException e) {
throw new IllegalStateException(e);
}
if (notes.size() == 0) {
// No notes for this slide
return null;
}
if (notes.size() > 1) {
throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
}
try {
return slidePart.getRelatedPart(notes.getRelationship(0));
} catch (InvalidFormatException e) {
throw new IllegalStateException(e);
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class XSSFSheet method initHyperlinks.
/**
* Read hyperlink relations, link them with CTHyperlink beans in this worksheet
* and initialize the internal array of XSSFHyperlink objects
*/
private void initHyperlinks() {
hyperlinks = new ArrayList<XSSFHyperlink>();
if (!worksheet.isSetHyperlinks()) {
return;
}
try {
PackageRelationshipCollection hyperRels = getPackagePart().getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
// Turn each one into a XSSFHyperlink
for (CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
PackageRelationship hyperRel = null;
if (hyperlink.getId() != null) {
hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
}
hyperlinks.add(new XSSFHyperlink(hyperlink, hyperRel));
}
} catch (InvalidFormatException e) {
throw new POIXMLException(e);
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class XSSFWorkbook method setVBAProject.
/**
* Adds a vbaProject.bin file to the workbook. This will change the workbook
* type if necessary.
*
* @throws IOException
*/
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
if (!isMacroEnabled()) {
setWorkbookType(XSSFWorkbookType.XLSM);
}
PackagePartName ppName;
try {
ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
} catch (InvalidFormatException e) {
throw new POIXMLException(e);
}
OPCPackage opc = getPackage();
OutputStream outputStream;
if (!opc.containPart(ppName)) {
POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
outputStream = relationship.getPackagePart().getOutputStream();
} else {
PackagePart part = opc.getPart(ppName);
outputStream = part.getOutputStream();
}
try {
IOUtils.copy(vbaProjectStream, outputStream);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
Aggregations