use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class PackagePartName method throwExceptionIfPartNameHaveInvalidSegments.
/**
* Throws an exception if the part name has empty segments. [M1.3]
*
* Throws an exception if a segment any characters other than pchar
* characters. [M1.6]
*
* Throws an exception if a segment contain percent-encoded forward slash
* ('/'), or backward slash ('\') characters. [M1.7]
*
* Throws an exception if a segment contain percent-encoded unreserved
* characters. [M1.8]
*
* Throws an exception if the specified part name's segments end with a dot
* ('.') character. [M1.9]
*
* Throws an exception if a segment doesn't include at least one non-dot
* character. [M1.10]
*
* @param partUri
* The part name to check.
* @throws InvalidFormatException
* if the specified URI contain an empty segments or if one the
* segments contained in the part name, ends with a dot ('.')
* character.
*/
private static void throwExceptionIfPartNameHaveInvalidSegments(URI partUri) throws InvalidFormatException {
if (partUri == null) {
throw new IllegalArgumentException("partUri");
}
// Split the URI into several part and analyze each
String[] segments = partUri.toASCIIString().split("/");
if (segments.length <= 1 || !segments[0].equals(""))
throw new InvalidFormatException("A part name shall not have empty segments [M1.3]: " + partUri.getPath());
for (int i = 1; i < segments.length; ++i) {
String seg = segments[i];
if (seg == null || "".equals(seg)) {
throw new InvalidFormatException("A part name shall not have empty segments [M1.3]: " + partUri.getPath());
}
if (seg.endsWith(".")) {
throw new InvalidFormatException("A segment shall not end with a dot ('.') character [M1.9]: " + partUri.getPath());
}
if ("".equals(seg.replaceAll("\\\\.", ""))) {
// implementation rule [M1.9]
throw new InvalidFormatException("A segment shall include at least one non-dot character. [M1.10]: " + partUri.getPath());
}
// Check for rule M1.6, M1.7, M1.8
checkPCharCompliance(seg);
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project tika by apache.
the class AbstractOOXMLExtractor method loadLinkedRelationships.
/**
* This is used by the SAX docx and pptx decorators to load hyperlinks and
* other linked objects
*
* @param bodyPart
* @return
*/
protected Map<String, String> loadLinkedRelationships(PackagePart bodyPart, boolean includeInternal, Metadata metadata) {
Map<String, String> linkedRelationships = new HashMap<>();
try {
PackageRelationshipCollection prc = bodyPart.getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation());
for (int i = 0; i < prc.size(); i++) {
PackageRelationship pr = prc.getRelationship(i);
if (pr == null) {
continue;
}
if (!includeInternal && TargetMode.INTERNAL.equals(pr.getTargetMode())) {
continue;
}
String id = pr.getId();
String url = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
if (id != null && url != null) {
linkedRelationships.put(id, url);
}
}
for (String rel : EMBEDDED_RELATIONSHIPS) {
prc = bodyPart.getRelationshipsByType(rel);
for (int i = 0; i < prc.size(); i++) {
PackageRelationship pr = prc.getRelationship(i);
if (pr == null) {
continue;
}
String id = pr.getId();
String uriString = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
String fileName = uriString;
if (pr.getTargetURI() != null) {
try {
fileName = FileHelper.getFilename(new File(fileName));
} catch (Exception e) {
fileName = uriString;
}
}
if (id != null) {
fileName = (fileName == null) ? "" : fileName;
linkedRelationships.put(id, fileName);
}
}
}
} catch (InvalidFormatException e) {
EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
}
return linkedRelationships;
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project local-data-aragopedia by aragonopendata.
the class GenerateData method readXlsxFile.
private void readXlsxFile(File file, ConfigBean configBean) {
log.debug("Init readXlsxFile");
InputStream inp = null;
Workbook wb = null;
try {
inp = new FileInputStream(file);
wb = WorkbookFactory.create(inp);
} catch (FileNotFoundException e) {
log.error(e.getMessage());
} catch (InvalidFormatException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
Sheet sheet = wb.getSheetAt(0);
Row rowName = sheet.getRow(0);
Row rowNameNormalized = sheet.getRow(1);
Row rowNormalization = sheet.getRow(2);
Row rowDimMesure = sheet.getRow(3);
Row rowType = sheet.getRow(4);
Row rowSkosfile = sheet.getRow(5);
Row rowConstant = sheet.getRow(6);
Row rowConstantValue = sheet.getRow(7);
Row rowRelationKos = sheet.getRow(8);
Row rowKosName = sheet.getRow(9);
boolean cont = true;
int columnReaded = 0;
while (cont) {
Cell cellName = rowName.getCell(columnReaded);
Cell cellNameNormalized = rowNameNormalized.getCell(columnReaded);
Cell cellNormalization = rowNormalization.getCell(columnReaded);
Cell cellDimMesure = rowDimMesure.getCell(columnReaded);
Cell cellType = rowType.getCell(columnReaded);
Cell cellSkosfile = null;
if (rowSkosfile != null)
cellSkosfile = rowSkosfile.getCell(columnReaded);
Cell cellConstant = null;
if (rowConstant != null)
cellConstant = rowConstant.getCell(columnReaded);
Cell cellConstantValue = null;
if (rowConstantValue != null)
cellConstantValue = rowConstantValue.getCell(columnReaded);
Cell cellRelationKos = null;
if (rowRelationKos != null)
cellRelationKos = rowRelationKos.getCell(columnReaded);
Cell cellKosName = null;
if (rowKosName != null)
cellKosName = rowKosName.getCell(columnReaded);
DataBean dataBean = new DataBean();
if (cellName == null) {
if (rowName.getCell((columnReaded + 1)) == null)
cont = false;
else
columnReaded++;
} else {
dataBean.setName(cellName.getStringCellValue());
dataBean.setNameNormalized(cellNameNormalized.getStringCellValue());
dataBean.setNormalizacion(cellNormalization.getStringCellValue());
dataBean.setDimensionMesure(cellDimMesure.getStringCellValue());
dataBean.setIdConfig(configBean.getId());
String type = "";
if (cellType != null) {
type = cellType.getStringCellValue();
} else {
type = "xsd:string";
}
dataBean.setType(type);
if (cellSkosfile != null && !cellSkosfile.getStringCellValue().equals("")) {
HashMap<String, SkosBean> mapSkos = readMappingFile(cellSkosfile.getStringCellValue());
dataBean.setMapSkos(mapSkos);
configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
dataWithSkos.add(dataBean);
} else {
configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
}
if (Prop.addDataConstant && cellConstant != null && cellConstant.getStringCellValue().equals(Constants.constante)) {
if (cellConstantValue != null) {
dataBean.setConstant(cellConstantValue.getStringCellValue() + "");
configBean.getListDataConstant().add(dataBean);
}
}
if (cellRelationKos != null) {
dataBean.setRelationKos(cellRelationKos.getStringCellValue());
dataWithSkosHierarchical.add(dataBean);
}
if (cellKosName != null) {
dataBean.setKosName(cellKosName.getStringCellValue());
} else {
dataBean.setKosName(dataBean.getNameNormalized());
}
columnReaded++;
}
}
log.debug("End readXlsxFile");
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project local-data-aragopedia by aragonopendata.
the class GenerateRDF method readXlsxFile.
private void readXlsxFile(File file, ConfigBean configBean) {
log.debug("Init readXlsxFile");
InputStream inp = null;
Workbook wb = null;
try {
inp = new FileInputStream(file);
wb = WorkbookFactory.create(inp);
} catch (FileNotFoundException e) {
log.error(e.getMessage());
} catch (InvalidFormatException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
Sheet sheet = wb.getSheetAt(0);
Row rowName = sheet.getRow(0);
Row rowNameNormalized = sheet.getRow(1);
Row rowNormalization = sheet.getRow(2);
Row rowDimMesure = sheet.getRow(3);
Row rowType = sheet.getRow(4);
Row rowSkosfile = sheet.getRow(5);
Row rowConstant = sheet.getRow(6);
Row rowConstantValue = sheet.getRow(7);
Row rowRelationKos = sheet.getRow(8);
Row rowKosNameNormalized = sheet.getRow(9);
Row rowKosName = sheet.getRow(10);
boolean cont = true;
int columnReaded = 0;
while (cont) {
Cell cellName = rowName.getCell(columnReaded);
Cell cellNameNormalized = rowNameNormalized.getCell(columnReaded);
Cell cellNormalization = rowNormalization.getCell(columnReaded);
Cell cellDimMesure = rowDimMesure.getCell(columnReaded);
Cell cellType = rowType.getCell(columnReaded);
Cell cellSkosfile = null;
if (rowSkosfile != null)
cellSkosfile = rowSkosfile.getCell(columnReaded);
Cell cellConstant = null;
if (rowConstant != null)
cellConstant = rowConstant.getCell(columnReaded);
Cell cellConstantValue = null;
if (rowConstantValue != null)
cellConstantValue = rowConstantValue.getCell(columnReaded);
Cell cellRelationKos = null;
if (rowRelationKos != null)
cellRelationKos = rowRelationKos.getCell(columnReaded);
Cell cellKosName = null;
if (rowKosName != null)
cellKosName = rowKosName.getCell(columnReaded);
Cell cellKosNameNormalized = null;
if (rowKosNameNormalized != null)
cellKosNameNormalized = rowKosNameNormalized.getCell(columnReaded);
DataBean dataBean = new DataBean();
if (cellName == null) {
if (rowName.getCell((columnReaded + 1)) == null)
cont = false;
else
columnReaded++;
} else {
dataBean.setName(cellName.getStringCellValue());
if (cellNameNormalized != null) {
dataBean.setNameNormalized(cellNameNormalized.getStringCellValue());
} else {
log.error("Error in config " + file.getName() + " in cell name normalized");
}
if (cellNormalization != null) {
dataBean.setNormalizacion(cellNormalization.getStringCellValue());
} else {
log.error("Error in config " + file.getName() + " in cell normalization");
}
if (cellDimMesure != null) {
dataBean.setDimensionMesure(cellDimMesure.getStringCellValue());
} else {
log.error("Error in config " + file.getName() + " in cell dim mesure");
}
dataBean.setIdConfig(configBean.getId());
String type = "";
if (cellType != null) {
type = cellType.getStringCellValue();
} else {
type = "xsd:string";
}
dataBean.setType(type);
if (cellSkosfile != null && !cellSkosfile.getStringCellValue().equals("")) {
HashMap<String, SkosBean> mapSkos = readMappingFile(cellSkosfile.getStringCellValue());
dataBean.setMapSkos(mapSkos);
configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
dataWithSkos.add(dataBean);
} else {
configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
}
if (Prop.addDataConstant && cellConstant != null && cellConstant.getStringCellValue().equals(Constants.constante)) {
if (cellConstantValue != null) {
dataBean.setConstant(cellConstantValue.getStringCellValue() + "");
configBean.getListDataConstant().add(dataBean);
}
}
if (cellRelationKos != null) {
if (Utils.v(cellRelationKos.getStringCellValue())) {
dataBean.setRelationKos(cellRelationKos.getStringCellValue());
dataWithSkosHierarchical.add(dataBean);
}
}
if (Utils.v(cellKosNameNormalized) && Utils.v(cellKosNameNormalized.getStringCellValue())) {
dataBean.setKosNameNormalized(cellKosNameNormalized.getStringCellValue());
} else {
dataBean.setKosNameNormalized(dataBean.getNameNormalized());
}
if (Utils.v(cellKosName) && Utils.v(cellKosName.getStringCellValue())) {
dataBean.setKosName(cellKosName.getStringCellValue());
} else {
dataBean.setKosName(dataBean.getName());
}
columnReaded++;
}
}
log.debug("End readXlsxFile");
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project tika by apache.
the class XSSFBExcelExtractorDecorator method extractHyperLinks.
private void extractHyperLinks(PackagePart sheetPart, XHTMLContentHandler xhtml) throws SAXException {
try {
for (PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
xhtml.startElement("a", "href", rel.getTargetURI().toString());
xhtml.characters(rel.getTargetURI().toString());
xhtml.endElement("a");
}
} catch (InvalidFormatException e) {
//swallow
}
}
Aggregations