// TODO Load element with XMLBeans or dynamic table // TODO Check every element/namespace for compliance public PackagePart Unmarshall(UnmarshallContext context, Stream in1) { PackagePropertiesPart coreProps = new PackagePropertiesPart(context .Package, context.PartName); // If the input stream is null then we try to get it from the // package. if (in1 == null) { if (context.ZipEntry != null) { in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(context.ZipEntry); } else if (context.Package != null) { // Try to retrieve the part inputstream from the URI ZipEntry zipEntry; try { zipEntry = ZipHelper .GetCorePropertiesZipEntry((ZipPackage)context .Package); } catch (OpenXml4NetException) { throw new IOException( "Error while trying to get the part input stream."); } in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(zipEntry); } else { throw new IOException( "Error while trying to get the part input stream."); } } XmlDocument xmlDoc = null; try { xmlDoc = DocumentHelper.LoadDocument(in1); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", namespaceCP); nsmgr.AddNamespace("dc", namespaceDC); nsmgr.AddNamespace("dcterms", namespaceDcTerms); nsmgr.AddNamespace("xsi", namespaceXSI); nsmgr.AddNamespace("cml", PackageNamespaces.MARKUP_COMPATIBILITY); nsmgr.AddNamespace("dcmitype", PackageNamespaces.DCMITYPE); //xmlDoc.ReadNode(reader); //try { //xmlDoc = xmlReader.read(in1); /* Check OPC compliance */ // Rule M4.2, M4.3, M4.4 and M4.5/ CheckElementForOPCCompliance(xmlDoc.DocumentElement); /* End OPC compliance */ //} catch (DocumentException e) { // throw new IOException(e.getMessage()); //} } catch (XmlException ex) { throw new IOException(ex.Message, ex); } if (xmlDoc != null && xmlDoc.DocumentElement != null) { coreProps.SetCategoryProperty(LoadCategory(xmlDoc)); coreProps.SetContentStatusProperty(LoadContentStatus(xmlDoc)); coreProps.SetContentTypeProperty(LoadContentType(xmlDoc)); coreProps.SetCreatedProperty(LoadCreated(xmlDoc)); coreProps.SetCreatorProperty(LoadCreator(xmlDoc)); coreProps.SetDescriptionProperty(LoadDescription(xmlDoc)); coreProps.SetIdentifierProperty(LoadIdentifier(xmlDoc)); coreProps.SetKeywordsProperty(LoadKeywords(xmlDoc)); coreProps.SetLanguageProperty(LoadLanguage(xmlDoc)); coreProps.SetLastModifiedByProperty(LoadLastModifiedBy(xmlDoc)); coreProps.SetLastPrintedProperty(LoadLastPrinted(xmlDoc)); coreProps.SetModifiedProperty(LoadModified(xmlDoc)); coreProps.SetRevisionProperty(LoadRevision(xmlDoc)); coreProps.SetSubjectProperty(LoadSubject(xmlDoc)); coreProps.SetTitleProperty(LoadTitle(xmlDoc)); coreProps.SetVersionProperty(LoadVersion(xmlDoc)); } return(coreProps); }
// TODO Load element with XMLBeans or dynamic table // TODO Check every element/namespace for compliance public PackagePart Unmarshall(UnmarshallContext context, Stream in1) { PackagePropertiesPart coreProps = new PackagePropertiesPart(context .Package, context.PartName); // If the input stream is null then we try to get it from the // package. if (in1 == null) { if (context.ZipEntry != null) { in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(context.ZipEntry); } else if (context.Package != null) { // Try to retrieve the part inputstream from the URI ZipEntry zipEntry; try { zipEntry = ZipHelper .GetCorePropertiesZipEntry((ZipPackage)context .Package); } catch (OpenXml4NetException) { throw new IOException( "Error while trying to get the part input stream."); } in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(zipEntry); } else throw new IOException( "Error while trying to get the part input stream."); } XmlDocument xmlDoc = null; try { xmlDoc = DocumentHelper.LoadDocument(in1); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", namespaceCP); nsmgr.AddNamespace("dc", namespaceDC); nsmgr.AddNamespace("dcterms", namespaceDcTerms); nsmgr.AddNamespace("xsi", namespaceXSI); nsmgr.AddNamespace("cml", PackageNamespaces.MARKUP_COMPATIBILITY); nsmgr.AddNamespace("dcmitype", PackageNamespaces.DCMITYPE); //xmlDoc.ReadNode(reader); //try { //xmlDoc = xmlReader.read(in1); /* Check OPC compliance */ // Rule M4.2, M4.3, M4.4 and M4.5/ CheckElementForOPCCompliance(xmlDoc.DocumentElement); /* End OPC compliance */ //} catch (DocumentException e) { // throw new IOException(e.getMessage()); //} } catch (XmlException ex) { if (ex.Message.Contains("MaxCharactersFromEntities")) { // } else throw ex; } if (xmlDoc!=null && xmlDoc.DocumentElement != null) { coreProps.SetCategoryProperty(LoadCategory(xmlDoc)); coreProps.SetContentStatusProperty(LoadContentStatus(xmlDoc)); coreProps.SetContentTypeProperty(LoadContentType(xmlDoc)); coreProps.SetCreatedProperty(LoadCreated(xmlDoc)); coreProps.SetCreatorProperty(LoadCreator(xmlDoc)); coreProps.SetDescriptionProperty(LoadDescription(xmlDoc)); coreProps.SetIdentifierProperty(LoadIdentifier(xmlDoc)); coreProps.SetKeywordsProperty(LoadKeywords(xmlDoc)); coreProps.SetLanguageProperty(LoadLanguage(xmlDoc)); coreProps.SetLastModifiedByProperty(LoadLastModifiedBy(xmlDoc)); coreProps.SetLastPrintedProperty(LoadLastPrinted(xmlDoc)); coreProps.SetModifiedProperty(LoadModified(xmlDoc)); coreProps.SetRevisionProperty(LoadRevision(xmlDoc)); coreProps.SetSubjectProperty(LoadSubject(xmlDoc)); coreProps.SetTitleProperty(LoadTitle(xmlDoc)); coreProps.SetVersionProperty(LoadVersion(xmlDoc)); } return coreProps; }
/** * Load the parts of the archive if it has not been done yet. The * relationships of each part are not loaded. * Note - Rule M4.1 states that there may only ever be one Core * Properties Part, but Office produced files will sometimes * have multiple! As Office ignores all but the first, we relax * Compliance with Rule M4.1, and ignore all others silently too. * @return All this package's parts. */ public List<PackagePart> GetParts() { ThrowExceptionIfWriteOnly(); // If the part list is null, we parse the package to retrieve all parts. if (partList == null) { /* Variables use to validate OPC Compliance */ // Check rule M4.1 -> A format consumer shall consider more than // one core properties relationship for a package to be an error // (We just log it and move on, as real files break this!) bool hasCorePropertiesPart = false; bool needCorePropertiesPart = true; PackagePart[] parts = this.GetPartsImpl(); this.partList = new PackagePartCollection(); foreach (PackagePart part in parts) { bool pnFound = false; foreach (PackagePartName pn in partList.Keys) { if (part.PartName.Name.StartsWith(pn.Name)) { pnFound = true; break; } } if (pnFound) 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.ContentType.Equals( ContentTypes.CORE_PROPERTIES_PART)) { if (!hasCorePropertiesPart) hasCorePropertiesPart = true; else Console.WriteLine( "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."); } if (partUnmarshallers.ContainsKey(part.contentType)) { PartUnmarshaller partUnmarshaller = partUnmarshallers[part.contentType]; UnmarshallContext context = new UnmarshallContext(this, part.PartName); try { PackagePart unmarshallPart = partUnmarshaller .Unmarshall(context, part.GetInputStream()); partList[unmarshallPart.PartName] = unmarshallPart; // Core properties case-- use first CoreProperties part we come across // and ignore any subsequent ones if (unmarshallPart is PackagePropertiesPart && hasCorePropertiesPart && needCorePropertiesPart) { this.packageProperties = (PackagePropertiesPart)unmarshallPart; needCorePropertiesPart = false; } } catch (IOException) { logger.Log(POILogger.WARN, "Unmarshall operation : IOException for " + part.PartName); continue; } catch (InvalidOperationException invoe) { throw new InvalidFormatException(invoe.Message); } } else { try { partList[part.PartName] = part; } catch (InvalidOperationException e) { throw new InvalidFormatException(e.Message); } } } } return new List<PackagePart>(partList.Values); }