Package org.apache.poi.openxml4j.exceptions

Examples of org.apache.poi.openxml4j.exceptions.InvalidFormatException


      PackagePart[] parts = this.getPartsImpl();
      this.partList = new PackagePartCollection();
      for (PackagePart part : parts) {
        if (partList.containsKey(part._partName))
          throw new InvalidFormatException(
              "A part with the name '"
                  + part._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]");

        // Check OPC compliance rule M4.1
        if (part.getContentType().equals(
            ContentTypes.CORE_PROPERTIES_PART)) {
          if (!hasCorePropertiesPart)
            hasCorePropertiesPart = true;
          else
            throw new InvalidFormatException(
                "OPC Compliance error [M4.1]: there is more than one core properties relationship in the package !");
        }

        PartUnmarshaller partUnmarshaller = partUnmarshallers
            .get(part._contentType);

        if (partUnmarshaller != null) {
          UnmarshallContext context = new UnmarshallContext(this,
              part._partName);
          try {
            PackagePart unmarshallPart = partUnmarshaller
                .unmarshall(context, part.getInputStream());
            partList.put(unmarshallPart._partName, unmarshallPart);

            // Core properties case
            if (unmarshallPart instanceof PackagePropertiesPart)
              this.packageProperties = (PackagePropertiesPart) unmarshallPart;
          } catch (IOException ioe) {
            logger.log(POILogger.WARN, "Unmarshall operation : IOException for "
                + part._partName);
            continue;
          } catch (InvalidOperationException invoe) {
            throw new InvalidFormatException(invoe.getMessage());
          }
        } else {
          try {
            partList.put(part._partName, part);
          } catch (InvalidOperationException e) {
            throw new InvalidFormatException(e.getMessage());
          }
        }
      }
    }
    return new ArrayList<PackagePart>(partList.values());
View Full Code Here


      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());
  }
View Full Code Here

    }

    // 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("\\\\.", ""))) {
        // Normally will never been invoked with the previous
        // 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
View Full Code Here

      if (errorFlag && c == '%') {
        // We certainly found an encoded character, check for length
        // now ( '%' HEXDIGIT HEXDIGIT)
        if (((segment.length() - i) < 2)) {
          throw new InvalidFormatException("The segment " + segment
              + " contain invalid encoded character !");
        }

        // If not percent encoded character error occur then reset the
        // flag -> the character is valid
        errorFlag = false;

        // Decode the encoded character
        char decodedChar = (char) Integer.parseInt(segment.substring(
            i + 1, i + 3), 16);
        i += 2;

        /* Check rule M1.7 */
        if (decodedChar == '/' || decodedChar == '\\')
          throw new InvalidFormatException(
              "A segment shall not contain percent-encoded forward slash ('/'), or backward slash ('\') characters. [M1.7]");

        /* Check rule M1.8 */

        // Check for unreserved character like define in RFC3986
        if ((decodedChar >= 'A' && decodedChar <= 'Z')
            || (decodedChar >= 'a' && decodedChar <= 'z')
            || (decodedChar >= '0' && decodedChar <= '9'))
          errorFlag = true;

        // Check for unreserved character "-", ".", "_", "~"
        for (int j = 0; !errorFlag
            && j < RFC3986_PCHAR_UNRESERVED_SUP.length; ++j) {
          if (c == RFC3986_PCHAR_UNRESERVED_SUP[j].charAt(0)) {
            errorFlag = true;
            break;
          }
        }
        if (errorFlag)
          throw new InvalidFormatException(
              "A segment shall not contain percent-encoded unreserved characters. [M1.8]");
      }

      if (errorFlag)
        throw new InvalidFormatException(
            "A segment shall not hold any characters other than pchar characters. [M1.6]");
    }
  }
View Full Code Here

  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());
  }
View Full Code Here

  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());
  }
View Full Code Here

   *             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);
  }
View Full Code Here

      throws InvalidFormatException {
    URI partNameURI;
    try {
      partNameURI = new URI(partName);
    } catch (URISyntaxException e) {
      throw new InvalidFormatException(e.getMessage());
    }
    return createPartName(partNameURI);
  }
View Full Code Here

    URI newPartNameURI;
    try {
      newPartNameURI = resolvePartUri(
          relativePart.getPartName().getURI(), new URI(partName));
    } catch (URISyntaxException e) {
      throw new InvalidFormatException(e.getMessage());
    }
    return createPartName(newPartNameURI);
  }
View Full Code Here

        // Check Rule M4.1
        if (type.equals(PackageRelationshipTypes.CORE_PROPERTIES))
          if (!fCorePropertiesRelationship)
            fCorePropertiesRelationship = true;
          else
            throw new InvalidFormatException(
                "OPC Compliance error [M4.1]: there is more than one core properties relationship in the package !");

        /* End OPC Compliance */

        // TargetMode (default value "Internal")
        Attribute targetModeAttr = element
            .attribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME);
        TargetMode targetMode = TargetMode.INTERNAL;
        if (targetModeAttr != null) {
          targetMode = targetModeAttr.getValue().toLowerCase()
              .equals("internal") ? TargetMode.INTERNAL
              : TargetMode.EXTERNAL;
        }

        // Target converted in URI
        URI target;
        String value = "";
        try {
          value = element.attribute(
              PackageRelationship.TARGET_ATTRIBUTE_NAME)
              .getValue();

          if (value.indexOf("\\") != -1) {
            logger
                .log(POILogger.INFO, "target contains \\ therefore not a valid URI"
                    + value + " replaced by /");
            value = value.replaceAll("\\\\", "/");
            // word can save external relationship with a \ instead
            // of /
          }

          target = new URI(value);
        } catch (URISyntaxException e) {
          logger.log(POILogger.ERROR, "Cannot convert " + value
              + " in a valid relationship URI-> ignored", e);
          continue;
        }
        addRelationship(target, targetMode, type, id);
      }
    } catch (Exception e) {
      logger.log(POILogger.ERROR, e);
      throw new InvalidFormatException(e.getMessage());
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.poi.openxml4j.exceptions.InvalidFormatException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.