Package org.apache.poi.openxml4j.exceptions

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


        // 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();

                    target = PackagingURIHelper.toURI(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


          ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
        try {
          this.contentTypeManager = new ZipContentTypeManager(
              getZipArchive().getInputStream(entry), this);
        } catch (IOException e) {
          throw new InvalidFormatException(e.getMessage());
        }
        break;
      }
    }

    // At this point, we should have loaded the content type part
    if (this.contentTypeManager == null) {
      throw new InvalidFormatException(
          "Package should contain a content type part [M1.13]");
    }

    // Now create all the relationships
    // (Need to create relationships before other
    //  parts, otherwise we might create a part before
    //  its relationship exists, and then it won't tie up)
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      PackagePartName partName = buildPartName(entry);
      if(partName == null) continue;

      // Only proceed for Relationships at this stage
      String contentType = contentTypeManager.getContentType(partName);
      if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
        try {
          partList.put(partName, new ZipPackagePart(this, entry,
            partName, contentType));
        } catch (InvalidOperationException e) {
          throw new InvalidFormatException(e.getMessage());
        }
      }
    }

    // Then we can go through all the other parts
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      PackagePartName partName = buildPartName(entry);
      if(partName == null) continue;

      String contentType = contentTypeManager
          .getContentType(partName);
      if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
        // Already handled
      }
      else if (contentType != null) {
        try {
          partList.put(partName, new ZipPackagePart(this, entry,
              partName, contentType));
        } catch (InvalidOperationException e) {
          throw new InvalidFormatException(e.getMessage());
        }
      } else {
        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]");
      }
    }
View Full Code Here

      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 {
             logger.log(POILogger.WARN, "OPC Compliance error [M4.1]: " +
                 "there is more than one core properties relationship in the package! " +
                 "POI will use only the first, but other software may reject this file.");
          }
        }

        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-- use first CoreProperties part we come across
            // and ignore any subsequent ones
            if (unmarshallPart instanceof PackagePropertiesPart &&
                hasCorePropertiesPart &&
                needCorePropertiesPart) {
              this.packageProperties = (PackagePropertiesPart) unmarshallPart;
              needCorePropertiesPart = false;
            }
          } 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

                // 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")
                Attr targetModeAttr = element.getAttributeNode(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 = PackagingURIHelper.toURI("http://invalid.uri"); // dummy url
                String value = element.getAttribute(PackageRelationship.TARGET_ATTRIBUTE_NAME);
                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-> dummy-URI used", e);
                }
                addRelationship(target, targetMode, type, id);
            }
        } catch (Exception e) {
            logger.log(POILogger.ERROR, e);
            throw new InvalidFormatException(e.getMessage());
        }
    }
View Full Code Here

          ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
        try {
          this.contentTypeManager = new ZipContentTypeManager(
              getZipArchive().getInputStream(entry), this);
        } catch (IOException e) {
          throw new InvalidFormatException(e.getMessage());
        }
        break;
      }
    }

    // At this point, we should have loaded the content type part
    if (this.contentTypeManager == null) {
      throw new InvalidFormatException(
          "Package should contain a content type part [M1.13]");
    }

    // Now create all the relationships
    // (Need to create relationships before other
    //  parts, otherwise we might create a part before
    //  its relationship exists, and then it won't tie up)
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      PackagePartName partName = buildPartName(entry);
      if(partName == null) continue;

      // Only proceed for Relationships at this stage
      String contentType = contentTypeManager.getContentType(partName);
      if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
        try {
          partList.put(partName, new ZipPackagePart(this, entry,
            partName, contentType));
        } catch (InvalidOperationException e) {
          throw new InvalidFormatException(e.getMessage());
        }
      }
    }

    // Then we can go through all the other parts
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      PackagePartName partName = buildPartName(entry);
      if(partName == null) continue;

      String contentType = contentTypeManager
          .getContentType(partName);
      if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
        // Already handled
      }
      else if (contentType != null) {
        try {
          partList.put(partName, new ZipPackagePart(this, entry,
              partName, contentType));
        } catch (InvalidOperationException e) {
          throw new InvalidFormatException(e.getMessage());
        }
      } else {
        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]");
      }
    }
View Full Code Here

       if(target.getFragment() != null) {
          String t = target.toString();
          try {
             target = new URI( t.substring(0, t.indexOf('#')) );
          } catch(URISyntaxException e) {
             throw new InvalidFormatException("Invalid target URI: " + target);
          }
       }
  
       // Turn that into a name, and fetch
       PackagePartName relName = PackagingURIHelper.createPartName(target);
View Full Code Here

    if (!mMediaType.matches()) {
        // How about with parameters?
        mMediaType = patternTypeSubTypeParams.matcher(contentType);
    }
        if (!mMediaType.matches()) {
      throw new InvalidFormatException(
          "The specified content type '"
          + contentType
          + "' is not compliant with RFC 2616: malformed content type.");
        }
View Full Code Here

      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 {
             logger.log(POILogger.WARN, "OPC Compliance error [M4.1]: " +
                 "there is more than one core properties relationship in the package! " +
                 "POI will use only the first, but other software may reject this file.");
          }
        }

        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-- use first CoreProperties part we come across
            // and ignore any subsequent ones
            if (unmarshallPart instanceof PackagePropertiesPart &&
                hasCorePropertiesPart &&
                needCorePropertiesPart) {
              this.packageProperties = (PackagePropertiesPart) unmarshallPart;
              needCorePropertiesPart = false;
            }
          } 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

    SimpleDateFormat df = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss'Z'");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = df.parse(s, new ParsePosition(0));
    if (d == null) {
      throw new InvalidFormatException("Date not well formated");
    }
    return new Nullable<Date>(d);
  }
View Full Code Here

     // 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();
    
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.