/** * Throws an exception if the specified URI is absolute. * * @param partUri * The URI to check. * @throws InvalidFormatException * Throws if the specified URI is absolute. */ private static void throwExceptionIfAbsoluteUri(URI partUri) throws InvalidFormatException { if (partUri.isAbsolute()) { throw new InvalidFormatException("Absolute URI forbidden: " + partUri); } }
/** * Throws an exception if the specified part name doesn't start with a * forward slash character '/'. [M1.4] * * @param partUri * The part name to check. * @throws InvalidFormatException * If the specified part name doesn't start with a forward slash * character '/'. */ private static void throwExceptionIfPartNameNotStartsWithForwardSlashChar( URI partUri) throws InvalidFormatException { String uriPath = partUri.getPath(); if (uriPath.length() > 0 && uriPath.charAt(0) != PackagingURIHelper.FORWARD_SLASH_CHAR) { throw new InvalidFormatException( "A part name shall start with a forward slash ('/') character [M1.4]: " + partUri.getPath()); } }
/** * Throws an exception if the specified part name ends with a forwar slash * character '/'. [M1.5] * * @param partUri * The part name to check. * @throws InvalidFormatException * If the specified part name ends with a forwar slash character * '/'. */ private static void throwExceptionIfPartNameEndsWithForwardSlashChar( URI partUri) throws InvalidFormatException { String uriPath = partUri.getPath(); if (uriPath.length() > 0 && uriPath.charAt(uriPath.length() - 1) == PackagingURIHelper.FORWARD_SLASH_CHAR) { throw new InvalidFormatException( "A part name shall not have a forward slash as the last character [M1.5]: " + partUri.getPath()); } }
@Override public boolean load(InputStream ios) throws InvalidFormatException { // Grab the data ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copy(ios, baos); } catch(IOException e) { throw new InvalidFormatException(e.getMessage()); } // Save it data = baos.toByteArray(); // All done return true; }
/** * Throws an exception if the specified URI is empty. [M1.1] * * @param partURI * Part URI to check. * @throws InvalidFormatException * If the specified URI is empty. */ private static void throwExceptionIfEmptyURI(URI partURI) throws InvalidFormatException { if (partURI == null) { throw new IllegalArgumentException("partURI"); } String uriPath = partURI.getPath(); if (uriPath.length() == 0 || ((uriPath.length() == 1) && (uriPath.charAt(0) == PackagingURIHelper.FORWARD_SLASH_CHAR))) { throw new InvalidFormatException( "A part name shall not be empty [M1.1]: " + partURI.getPath()); } }
/** * Get an unused part index based on the namePattern, which doesn't exist yet * and has the lowest positive index * * @param nameTemplate * The template for new part names containing a {@code '#'} for the index, * e.g. "/ppt/slides/slide#.xml" * @return the next available part name index * @throws InvalidFormatException if the nameTemplate is null or doesn't contain * the index char (#) or results in an invalid part name */ public int getUnusedPartIndex(final String nameTemplate) throws InvalidFormatException { if (nameTemplate == null || !nameTemplate.contains("#")) { throw new InvalidFormatException("name template must not be null and contain an index char (#)"); } final Pattern pattern = Pattern.compile(nameTemplate.replace("#", "([0-9]+)")); final ToIntFunction<String> indexFromName = name -> { Matcher m = pattern.matcher(name); return m.matches() ? Integer.parseInt(m.group(1)) : 0; }; return packagePartLookup.keySet().stream() .mapToInt(indexFromName) .collect(BitSet::new, BitSet::set, BitSet::or).nextClearBit(1); } }
void register(final PackagePartCollection partList) throws InvalidFormatException { if (contentType == null) { throw new InvalidFormatException("The part " + partName.getURI().getPath() + " does not have any " + "content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]"); } if (partList.containsKey(partName)) { throw new InvalidFormatException( "A part with the name '"+partName+"' already exist : Packages shall not contain equivalent part names " + "and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]"); } try { partList.put(partName, new ZipPackagePart(ZipPackage.this, zipArchiveEntry, partName, contentType, false)); } catch (InvalidOperationException e) { throw new InvalidFormatException(e.getMessage(), e); } }
throw new InvalidFormatException( "A part name shall not have empty segments [M1.3]: " + partUri.getPath()); throw new InvalidFormatException( "A part name shall not have empty segments [M1.3]: " + partUri.getPath()); throw new InvalidFormatException( "A segment shall not end with a dot ('.') character [M1.9]: " + partUri.getPath()); throw new InvalidFormatException( "A segment shall include at least one non-dot character. [M1.10]: " + partUri.getPath());
throw new InvalidFormatException( "The specified content type '" + contentType
sb.append(", ").append(fStr); throw new InvalidFormatException("Date " + dateStr + " not well formatted, " + "expected format in: "+ sb);
/** * Create an OPC compliant part name. * * @param partName * The part name to validate. * @return The correspondant part name if valid, else <code>null</code>. * @throws InvalidFormatException * Throws if the specified part name is not OPC compliant. * @see #createPartName(URI) */ public static PackagePartName createPartName(String partName) throws InvalidFormatException { URI partNameURI; try { partNameURI = toURI(partName); } catch (URISyntaxException e) { throw new InvalidFormatException(e.getMessage()); } return createPartName(partNameURI); }
throw new InvalidFormatException( "A segment shall not hold any characters other than pchar characters. [M1.6]"); throw new InvalidFormatException("The segment " + segment + " contain invalid encoded character !"); throw new InvalidFormatException( "A segment shall not contain percent-encoded forward slash ('/'), or backward slash ('\') characters. [M1.7]"); throw new InvalidFormatException( "A segment shall not contain percent-encoded unreserved characters. [M1.8]");
/** * Constructor. Parses the content of the specified input stream. * * @param in * If different of <i>null</i> then the content types part is * retrieve and parse. * @throws InvalidFormatException * If the content types part content is not valid. */ public ContentTypeManager(InputStream in, OPCPackage pkg) throws InvalidFormatException { this.container = pkg; this.defaultContentType = new TreeMap<>(); if (in != null) { try { parseContentTypesFile(in); } catch (InvalidFormatException e) { InvalidFormatException ex = new InvalidFormatException("Can't read content types part !"); // here it is useful to add the cause to not loose the original stack-trace ex.initCause(e); throw ex; } } }
throw new InvalidFormatException(e.getMessage());
target = new URI( t.substring(0, t.indexOf('#')) ); } catch(URISyntaxException e) { throw new InvalidFormatException("Invalid target URI: " + target);
/** * Create an OPC compliant part name by resolving it using a base part. * * @param partName * The part name to validate. * @param relativePart * The relative base part. * @return The correspondant part name if valid, else <code>null</code>. * @throws InvalidFormatException * Throws if the specified part name is not OPC compliant. * @see #createPartName(URI) */ public static PackagePartName createPartName(String partName, PackagePart relativePart) throws InvalidFormatException { URI newPartNameURI; try { newPartNameURI = resolvePartUri( relativePart.getPartName().getURI(), new URI(partName)); } catch (URISyntaxException e) { throw new InvalidFormatException(e.getMessage()); } return createPartName(newPartNameURI); }
fCorePropertiesRelationship = true; else throw new InvalidFormatException( "OPC Compliance error [M4.1]: there is more than one core properties relationship in the package !"); throw new InvalidFormatException(e.getMessage());
if (contentTypeEntry != null) { if (this.contentTypeManager != null) { throw new InvalidFormatException("ContentTypeManager can only be created once. This must be a cyclic relation?"); zipArchive.getInputStream(contentTypeEntry), this); } catch (IOException e) { throw new InvalidFormatException(e.getMessage(), e); throw new InvalidFormatException( "Package should contain a content type part [M1.13]");
continue; } catch (InvalidOperationException invoe) { throw new InvalidFormatException(invoe.getMessage(), invoe);
/** * this method write the XSSFWorkbook object data into embedded excel file * * @param workbook * XSSFworkbook object * @throws IOException * @throws InvalidFormatException * @since POI 4.0.0 */ public void saveWorkbook(XSSFWorkbook workbook) throws IOException, InvalidFormatException { PackagePart worksheetPart = getWorksheetPart(); if (worksheetPart == null) { POIXMLRelation chartRelation = getChartRelation(); POIXMLRelation chartWorkbookRelation = getChartWorkbookRelation(); POIXMLFactory chartFactory = getChartFactory(); if (chartRelation != null && chartWorkbookRelation != null && chartFactory != null) { worksheetPart = createWorksheetPart(chartRelation, chartWorkbookRelation, chartFactory); } else { throw new InvalidFormatException("unable to determine chart relations"); } } try (OutputStream xlsOut = worksheetPart.getOutputStream()) { setWorksheetPartCommitted(); workbook.write(xlsOut); } }