public PackingList()

in src/main/java/com/netflix/imflibrary/st0429_8/PackingList.java [123:228]


    public PackingList(ResourceByteRangeProvider resourceByteRangeProvider)throws IOException {

        JAXBElement<PackingListType> packingListTypeJAXBElement = null;
        imfErrorLogger = new IMFErrorLoggerImpl();

        String packingListNamespaceURI = getPackingListSchemaURI(resourceByteRangeProvider, imfErrorLogger);
        PKLSchema pklSchema = supportedPKLSchemas.get(packingListNamespaceURI);
        if(pklSchema == null){
            String message = String.format("Please check the PKL document, currently we only support the " +
                    "following schema URIs %s", serializePKLSchemasToString());
            imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR, IMFErrorLogger.IMFErrors
                    .ErrorLevels.FATAL, message);
            throw new IMFException(message, imfErrorLogger);
        }

        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            try (InputStream inputStream = resourceByteRangeProvider.getByteRangeAsStream(0, resourceByteRangeProvider.getResourceSize() - 1);
                 InputStream xmldsig_core_is = contextClassLoader.getResourceAsStream(PackingList.xmldsig_core_schema_path);
                 InputStream pkl_is = contextClassLoader.getResourceAsStream(pklSchema.getPKLSchemaPath());
            ) {
                StreamSource[] streamSources = new StreamSource[2];
                streamSources[0] = new StreamSource(xmldsig_core_is);
                streamSources[1] = new StreamSource(pkl_is);

                SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = schemaFactory.newSchema(streamSources);

                ValidationEventHandlerImpl validationEventHandlerImpl = new ValidationEventHandlerImpl(true);
                JAXBContext jaxbContext = JAXBContext.newInstance(pklSchema.getPKLContext());
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                unmarshaller.setEventHandler(validationEventHandlerImpl);
                unmarshaller.setSchema(schema);

                packingListTypeJAXBElement = (JAXBElement) unmarshaller.unmarshal(inputStream);

                if (validationEventHandlerImpl.hasErrors()) {
                    List<ValidationEventHandlerImpl.ValidationErrorObject> errors = validationEventHandlerImpl.getErrors();
                    for (ValidationEventHandlerImpl.ValidationErrorObject error : errors) {
                        String errorMessage = "Line Number : " + error.getLineNumber().toString() + " - " + error.getErrorMessage();
                        imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR, error.getValidationEventSeverity(), errorMessage);
                    }
                    throw new IMFException(validationEventHandlerImpl.toString(), imfErrorLogger);
                }
            }
        }
        catch(SAXException | JAXBException e){
            imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_AM_ERROR, IMFErrorLogger.IMFErrors.ErrorLevels.FATAL,
                    e.getMessage());
        }

        if(imfErrorLogger.hasFatalErrors())
        {
            throw new IMFException("PackingList parsing failed", imfErrorLogger);
        }

        this.pklSchema = pklSchema;
        this.packingListTypeJAXBElement = packingListTypeJAXBElement;

        switch(this.pklSchema.getPKLContext())
        {
            case "org.smpte_ra.schemas._429_8._2007.pkl":
                //this.packingListType = PackingList.checkConformance(packingListTypeJAXBElement.getValue());
                org.smpte_ra.schemas._429_8._2007.pkl.PackingListType packingListType_st0429_8_2007_PKL = (org.smpte_ra.schemas._429_8._2007.pkl.PackingListType) this.packingListTypeJAXBElement.getValue();
                this.uuid = UUIDHelper.fromUUIDAsURNStringToUUID(packingListType_st0429_8_2007_PKL.getId());

                for (org.smpte_ra.schemas._429_8._2007.pkl.AssetType assetType : packingListType_st0429_8_2007_PKL.getAssetList().getAsset())
                {
                    Asset asset = new Asset(assetType.getId(), Arrays.copyOf(assetType.getHash(), assetType.getHash().length),
					    assetType.getSize().longValue(), assetType.getType(),
					    assetType.getOriginalFileName() != null ? assetType.getOriginalFileName().getValue() : null);
                    this.assetList.add(asset);
                }
                break;
            case "org.smpte_ra.schemas._2067_2._2016.pkl":
                org.smpte_ra.schemas._2067_2._2016.pkl.PackingListType packingListType_st2067_2_2016_PKL = (org.smpte_ra.schemas._2067_2._2016.pkl.PackingListType) this.packingListTypeJAXBElement.getValue();
                this.uuid = UUIDHelper.fromUUIDAsURNStringToUUID(packingListType_st2067_2_2016_PKL.getId());

                for (org.smpte_ra.schemas._2067_2._2016.pkl.AssetType assetType : packingListType_st2067_2_2016_PKL.getAssetList().getAsset())
                {
                    Asset asset = new Asset(assetType.getId(), Arrays.copyOf(assetType.getHash(), assetType.getHash().length),
					    assetType.getSize().longValue(), assetType.getType(),
					    assetType.getOriginalFileName() != null ? assetType.getOriginalFileName().getValue() : null,
					    assetType.getHashAlgorithm().getAlgorithm());
                    this.assetList.add(asset);
                }
                break;
            default:
                String message = String.format("Please check the PKL document, currently we only support the " +
                        "following schema URIs %s", serializePKLSchemasToString());
                imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR, IMFErrorLogger.IMFErrors
                        .ErrorLevels.FATAL, message);
                throw new IMFException(message, imfErrorLogger);
        }

        Set<UUID> assetUUIDs = new HashSet<>();
        for(Asset asset : this.assetList){
            if(assetUUIDs.contains(asset.getUUID())){
                imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR, IMFErrorLogger.IMFErrors.ErrorLevels.FATAL,
                        String.format("More than one PackingList Asset seems to use AssetUUID %s this is invalid.", asset.getUUID().toString()));
            }
            else{
                assetUUIDs.add(asset.getUUID());
            }
        }
    }