Example #1
0
        /// <summary>
        ///  main transform which needs the orginal File
        /// </summary>
        /// <param name="directionXSL">transform direction</param>
        /// <param name="resourceResolver">xsl location</param>
        /// <param name="originalFile">original File</param>
        /// <param name="inputFile">File after pretreatment</param>
        /// <param name="outputFile">output file</param>
        protected void MainTransform(string directionXSL, XmlUrlResolver resourceResolver, string originalFile, string inputFile, string outputFile)
        {
            XPathDocument     xslDoc;
            XmlReaderSettings xrs            = new XmlReaderSettings();
            XmlReader         source         = null;
            XmlWriter         writer         = null;
            OoxZipResolver    zipResolver    = null;
            string            zipXMLFileName = "input.xml";

            try
            {
                //xrs.ProhibitDtd = true;

                xslDoc          = new XPathDocument(((ResourceResolver)resourceResolver).GetInnerStream(directionXSL));
                xrs.XmlResolver = resourceResolver;
                string    sr      = ZipXMLFile(inputFile);
                ZipReader archive = ZipFactory.OpenArchive(sr);
                source = XmlReader.Create(archive.GetEntry(zipXMLFileName));

                XslCompiledTransform xslt     = new XslCompiledTransform();
                XsltSettings         settings = new XsltSettings(true, false);
                xslt.Load(xslDoc, settings, resourceResolver);

                if (!originalFile.Equals(string.Empty))
                {
                    zipResolver = new OoxZipResolver(originalFile, resourceResolver);
                }
                XsltArgumentList parameters = new XsltArgumentList();
                parameters.XsltMessageEncountered += new XsltMessageEncounteredEventHandler(MessageCallBack);

                // zip format
                parameters.AddParam("outputFile", "", outputFile);
                // writer = new OoxZipWriter(inputFile);
                writer = new UofZipWriter(outputFile);

                if (zipResolver != null)
                {
                    xslt.Transform(source, parameters, writer, zipResolver);
                }
                else
                {
                    xslt.Transform(source, parameters, writer);
                }
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (source != null)
                {
                    source.Close();
                }
            }
        }
Example #2
0
        public XmlContainer(BinaryReader _reader, uint size, uint typeCode, uint version, uint instance)
            : base(_reader, size, typeCode, version, instance)
        {
            // Note: XmlContainers contain the data of a partial "unfinished"
            // OOXML file (.zip based) as their body.
            //
            // I really don't like writing the data to a temp file just to
            // be able to open it via ZipUtils.
            //
            // Possible alternatives:
            // 1) Using System.IO.Compression -- supports inflation, but can't parse Zip header data
            // 2) Modifying zlib + minizlib + ZipLib so I can pass in bytes, possible, but not worth the effort

            string tempPath = Path.GetTempFileName();

            try
            {
                using (FileStream fs = new FileStream(tempPath, FileMode.Create))
                {
                    using (BinaryWriter tempStream = new BinaryWriter(fs))
                    {
                        int    count = (int)this.Reader.BaseStream.Length;
                        byte[] bytes = this.Reader.ReadBytes(count);

                        tempStream.Write(bytes);

                        tempStream.Flush();
                        fs.Flush();

                        tempStream.Close();
                        fs.Close();
                    }
                }

                using (ZipReader zipReader = ZipFactory.OpenArchive(tempPath))
                {
                    this.XmlDocumentElement = ExtractDocumentElement(zipReader, GetRelations(zipReader, ""));
                }
            }
            finally
            {
                try
                {
                    File.Delete(tempPath);
                }
                catch (IOException) { /* OK */ }
            }
        }
Example #3
0
        public XmlContainer(BinaryReader _reader, uint size, uint typeCode, uint version, uint instance)
            : base(_reader, size, typeCode, version, instance)
        {
            // Note: XmlContainers contain the data of a partial "unfinished" OOXML file (.zip based) as their body.
            //
            // I really don't like writing the data to a temp file just to be able to open it via ZipUtils.
            //
            // Possible alternatives:
            // 1) Using System.IO.Compression -- supports inflation, but can't parse Zip header data
            // 2) Modifying zlib + minizlib + ZipLib so I can pass in bytes, possible, but not worth the effort

            // KH - I've left the original comment above, but I've ported this to use option (1) as the IZipLib result can't read headers anyway - it can only open entries.

            using (var zipReader = ZipFactory.OpenArchive(this.Reader.BaseStream))
                this.XmlDocumentElement = ExtractDocumentElement(zipReader, GetRelations(zipReader, ""));
        }
Example #4
0
        private void DoUofToOoxTransform(string inputFile, string outputFile, string resourceDir)
        {
            guid = Guid.NewGuid();
            if (!IsUof(inputFile))
            {
                throw new NotAnUofDocumentException(inputFile + " 不是UOF文件");
            }

            LinkedList <IProcessor> .Enumerator enumer = uof2ooxPreProcessors.GetEnumerator();
            string outputPath = Path.GetTempPath().ToString() + guid.ToString();
            string output     = outputPath + Path.DirectorySeparatorChar + "tmpDoc";

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            //  string input = EIPicturePre(inputFile, outputPath);

            ZipReader archive = ZipFactory.OpenArchive(inputFile);

            archive.ExtractUOFDocument(inputFile, outputPath);

            string input = inputFile;

            int i = 0;

            while (enumer.MoveNext())
            {
                i++;
                enumer.Current.OriginalFilename = inputFile;
                enumer.Current.InputFilename    = input;
                enumer.Current.OutputFilename   = output + i + ".xml";
                enumer.Current.transform();
                input = enumer.Current.OutputFilename;
            }

            DoUofToOoxMainTransform(enumer.Current.OutputFilename, outputFile, resourceDir);
            //  Directory.Delete(outputPath, true);
        }
Example #5
0
        public override bool transform()
        {
            FileStream    fs          = null;
            XmlTextReader txtReader   = null;
            string        extractPath = Path.GetDirectoryName(outputFile) + "\\";
            ZipReader     archive     = ZipFactory.OpenArchive(inputFile);

            archive.ExtractOfficeDocument(inputFile, extractPath);
            string   prefix = this.GetType().Namespace + "." + TranslatorConstants.RESOURCE_LOCATION + ".Powerpoint.oox2uof";//路径
            Assembly asm    = Assembly.GetExecutingAssembly();

            foreach (string name in asm.GetManifestResourceNames())
            {
                if (name.StartsWith(prefix))
                {
                    string     filename   = name.Substring(prefix.Length + 1);
                    FileStream writer     = new FileStream(extractPath + filename, FileMode.Create);
                    Stream     baseStream = asm.GetManifestResourceStream(name);
                    int        Length     = 10240;
                    Byte[]     buffer     = new Byte[Length];
                    int        bytesRead  = baseStream.Read(buffer, 0, Length);
                    while (bytesRead > 0)
                    {
                        writer.Write(buffer, 0, bytesRead);
                        bytesRead = baseStream.Read(buffer, 0, Length);
                    }
                    baseStream.Close();
                    writer.Close();
                }
            }
            bool isSuccess = true;

            try
            {
                // add theme rels
                AddThemeRels(extractPath);

                File.Copy(extractPath + "pre1.xsl", extractPath + @"\ppt\pre1.xsl", true);
                XPathDocument        doc       = new XPathDocument(extractPath + @"\ppt\presentation.xml");
                XslCompiledTransform transFrom = new XslCompiledTransform();
                XsltSettings         setting   = new XsltSettings(true, false);
                XmlUrlResolver       xur       = new XmlUrlResolver();
                transFrom.Load(extractPath + @"\ppt\pre1.xsl", setting, xur);
                XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
                fs = new FileStream(extractPath + "pre1tmp.xml", FileMode.Create);
                transFrom.Transform(nav, null, fs);
                fs.Close();
                doc = new XPathDocument(extractPath + "pre1tmp.xml");
                nav = ((IXPathNavigable)doc).CreateNavigator();
                fs  = new FileStream(extractPath + "pre2tmp.xml", FileMode.Create);
                Assembly ass = Assembly.Load("ppt_oox2uof");//使用预编译后的xslt
                Type     t   = ass.GetType("pre2");
                transFrom.Load(t);

                transFrom.Transform(nav, null, fs);
                fs.Close();
                xmlDoc    = new XmlDocument();
                txtReader = new XmlTextReader(extractPath + "pre2tmp.xml");
                xmlDoc.Load(txtReader);
                txtReader.Close();
                setNSManager();            //
                string currentPath = "//p:sp";
                purifyMethod(currentPath); //

                //2011-01-12罗文甜:阴影预处理
                XmlNodeList shapeShadeList = xmlDoc.SelectNodes("//a:outerShdw", nm);
                if (shapeShadeList != null)
                {
                    foreach (XmlNode shapeShade in shapeShadeList)
                    {
                        double dist, dir;
                        if (((XmlElement)shapeShade).HasAttribute("dist"))
                        {
                            dist = Convert.ToDouble(shapeShade.Attributes.GetNamedItem("dist").Value) / 12700;
                        }
                        else
                        {
                            dist = 0.0;
                        }
                        if (((XmlElement)shapeShade).HasAttribute("dir"))
                        {
                            dir = (Convert.ToDouble(shapeShade.Attributes.GetNamedItem("dir").Value) * 3.1415) / (180 * 60000);
                        }
                        else
                        {
                            dir = 0.0;
                        }
                        double     xValue = dist * Math.Cos(dir);
                        double     yValue = dist * Math.Sin(dir);
                        XmlElement x      = xmlDoc.CreateElement("x");
                        x.InnerText = Convert.ToString(xValue);
                        shapeShade.AppendChild(x);
                        XmlElement y = xmlDoc.CreateElement("y");
                        y.InnerText = Convert.ToString(yValue);
                        shapeShade.AppendChild(y);
                    }
                }
                resultWriter = new XmlTextWriter(outputFile, System.Text.Encoding.UTF8);
                xmlDoc.Save(resultWriter);
                resultWriter.Close();

                string tmpFile = extractPath + "tmpID.xml";
                ChangeIDVal(outputFile, tmpFile);//
            }
            catch (Exception e)
            {
                logger.Error("Fail in OoxToUofPreProcessorOnePresentation: " + e.Message);
                logger.Error(e.StackTrace);
                isSuccess = false;
                throw new Exception("Fail in OoxToUofPreProcessorOnePresentation");
            }
            finally
            {
                if (resultWriter != null)
                {
                    resultWriter.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (txtReader != null)
                {
                    txtReader.Close();
                }
            }

            return(isSuccess);
        }
Example #6
0
        ///// <summary>
        ///// Initialize according to the input file type
        ///// </summary>
        ///// <param name="inputFileType">DocType of input file</param>
        ///// <returns>return IUOFTranslator</returns>
        //public static IUOFTranslator CheckFileType(DocType inputFileType)
        //{
        //    switch (inputFileType)
        //    {
        //        case DocType.Word: return new WordTranslator();
        //        case DocType.Powerpoint: return new PresentationTranslator();
        //        case DocType.Excel: return new SpreadsheetTranslator();
        //        default: throw new Exception("not an office 2010 file");
        //    }
        //}

        /// <summary>
        /// check Microsoft file type
        /// </summary>
        /// <param name="srcFileName">source file name</param>
        /// <returns>document type</returns>
        public static MSDocType CheckMSFileType(string srcFileName)
        {
            FileInfo fi = new FileInfo(srcFileName);

            if (File.Exists(srcFileName))
            {
                XmlReader source  = null;
                ZipReader archive = ZipFactory.OpenArchive(srcFileName);

                // get the main entry xml file
                string entry = string.Empty;
                switch (fi.Extension.ToLower())
                {
                case ".docx": entry = TranslatorMgrConstants.WordDocument_xml; break;

                case ".pptx": entry = TranslatorMgrConstants.PresentationDocument_xml; break;

                case ".xlsx": entry = TranslatorMgrConstants.SpreadsheetDocument_xml; break;
                }
                source = XmlReader.Create(archive.GetEntry(entry));

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(source);

                // namespace of strict document
                XmlNamespaceManager nm = new XmlNamespaceManager(xdoc.NameTable);
                nm.AddNamespace("w", TranslatorMgrConstants.XMLNS_W);
                nm.AddNamespace("p", TranslatorMgrConstants.XMLNS_P);
                nm.AddNamespace("ws", TranslatorMgrConstants.XMLNS_WS);

                switch (fi.Extension.ToLower())
                {
                case ".docx":
                {
                    // @w:conformance
                    if (xdoc.SelectSingleNode("w:document", nm) != null)
                    {
                        return(MSDocType.StrictWord);
                    }
                    else
                    {
                        return(MSDocType.TransitionalWord);
                    }
                }

                case ".pptx":
                {
                    if (xdoc.SelectSingleNode("p:presentation", nm) != null)
                    {
                        return(MSDocType.StrictPowerpoint);
                    }
                    else
                    {
                        return(MSDocType.TransitionalPowerpoint);
                    }
                }

                case ".xlsx":
                {
                    if (xdoc.SelectSingleNode("ws:workbook", nm) != null)
                    {
                        return(MSDocType.StrictExcel);
                    }
                    else
                    {
                        return(MSDocType.TransitionalExcel);
                    }
                }

                default: return(MSDocType.Unknown);
                }
            }
            else
            {
                return(MSDocType.Unknown);
            }
        }
Example #7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="filename">Absolute path to the Zip package</param>
 public ZipResolver(String filename)
 {
     archive = ZipFactory.OpenArchive(filename);
     // initialize hash table of odf resource file names
     entries = new Hashtable();
 }
Example #8
0
        protected override void DoOoxToUofMainTransform(string originalFile, string inputFile, string outputFile, string resourceDir)
        {
            string wordPrePath   = Path.GetDirectoryName(inputFile) + Path.DirectorySeparatorChar;
            string grpTmp        = Path.GetDirectoryName(inputFile) + "\\" + "grpTmp.xml";//把grpTmp.xml放在temp下的缓存文件夹下
            string drawingRelTmp = Path.GetDirectoryName(inputFile) + "\\" + "drawingRelTmp.xml";
            string picture_xml   = "ppt\\media";
            string mediaPath     = wordPrePath + picture_xml;

            XmlDocument xdoc = new XmlDocument();

            xdoc.Load(inputFile);//tempdoc1.xml
            XmlNamespaceManager nm = new XmlNamespaceManager(xdoc.NameTable);

            nm.AddNamespace("p", TranslatorConstants.XMLNS_P);
            nm.AddNamespace("a", TranslatorConstants.XMLNS_A);
            nm.AddNamespace("w", TranslatorConstants.XMLNS_W);
            nm.AddNamespace("dsp", TranslatorConstants.XMLNS_DSP);
            FileStream fs = new FileStream(grpTmp, FileMode.Create);

            try
            {
                GrpShPre(xdoc, nm, "p", fs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }

            // chart转换成图片
            string charts_xml = wordPrePath + "ppt\\charts";

            if (Directory.Exists(charts_xml))
            {
                string[] charts = Directory.GetFiles(charts_xml, "chart*");
                if (charts.Length > 0)
                {
                    if (!Directory.Exists(mediaPath))
                    {
                        Directory.CreateDirectory(mediaPath);
                    }
                }
                foreach (string chartXMLFile in charts)
                {
                    SaveChartAsPic(chartXMLFile, mediaPath + Path.AltDirectorySeparatorChar + Path.GetFileName(chartXMLFile) + ".jpg", System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Jpeg);
                }
            }

            //ole对象处理lyy
            string tmpPic = wordPrePath + "tmpOle.xml";

            if (Directory.Exists(wordPrePath + "ppt\\embeddings"))
            {
                xdoc.Load(grpTmp);
                xdoc = OlePretreatment(xdoc, "p:presentation", wordPrePath, nm);
                xdoc.Save(tmpPic);
                grpTmp = tmpPic;
            }
            // 图片预处理

            tmpPic = wordPrePath + "tmpPic.xml";
            if (Directory.Exists(mediaPath))
            {
                xdoc.Load(grpTmp);
                xdoc = PicPretreatment(xdoc, "p:presentation", mediaPath, nm);
                xdoc.Save(tmpPic);
                grpTmp = tmpPic;
            }
            //ole对象处理
            AddDrawingRel(xdoc, nm, drawingRelTmp);

            XmlReaderSettings xrs    = new XmlReaderSettings();
            XmlReader         source = null;

            string toMainSheet    = ZipXMLFile(drawingRelTmp);
            string zipXMLFileName = "input.xml";

            //string sr = ZipXMLFile(inputFile);
            ZipReader archive = ZipFactory.OpenArchive(toMainSheet);

            source = XmlReader.Create(archive.GetEntry(zipXMLFileName));

            //xdoc.Load(drawingRelTmp);

            XslCompiledTransform transFrom = new XslCompiledTransform();
            XsltSettings         setting   = new XsltSettings(true, false);
            XmlUrlResolver       xur       = new XmlUrlResolver();
            XPathNavigator       nav       = ((IXPathNavigable)xdoc).CreateNavigator();//root

            fs = null;
            string mainOutputFile = Path.GetDirectoryName(inputFile) + "\\" + "mainOutputFile.xml";//缓存文件中保存mainOutputFile.xml


            fs = new FileStream(mainOutputFile, FileMode.Create);
            string mainSheet = Path.GetDirectoryName(inputFile).ToString() + "\\" + TranslatorConstants.OOXToUOF_XSL;//从缓存中调用oox2uof.xsl

            transFrom.Load(mainSheet, setting, xur);
            //Assembly ass = Assembly.Load("ppt_oox2uof");//调用ppt_oox2uof.dll程序集
            //Type t = ass.GetType("oox2uof");//name=oox2uof.xslt
            //transFrom.Load(t);
            //transFrom.Transform(nav, null, fs);

            transFrom.Transform(source, null, fs);
            fs.Close();
            mainOutput = mainOutputFile;


            //XmlUrlResolver resourceResolver = null;

            //try
            //{

            //    resourceResolver = new ResourceResolver(Assembly.GetExecutingAssembly(),
            //        this.GetType().Namespace + "." + TranslatorConstants.RESOURCE_LOCATION + "." + "Powerpoint.oox2uof");
            // MainTransform(TranslatorConstants.OOXToUOF_XSL, resourceResolver, originalFile, grpTmp, outputFile);

            ////}
            //catch (Exception ex)
            //{
            //    logger.Warn(ex.Message);
            //}
        }
Example #9
0
 public OoxZipResolver(String filename, XmlUrlResolver resourceResolver)
 {
     archive = ZipFactory.OpenArchive(filename);
     entries = new Hashtable();
     this.resourceResolver = resourceResolver;
 }
Example #10
0
        /// <summary>
        /// Check the validity of an Office Open XML file.
        /// </summary>
        /// <param name="fileName">The path of the docx file.</param>
        public void validate(String fileName)
        {
            // 0. The file must exist and be a valid Zip archive

            try
            {
                reader = ZipFactory.OpenArchive(fileName);
            }
            catch (ZipException e)
            {
                throw new NotAnOoxDocumentException("Problem opening the pptx file : " + e.Message);
            }
            catch (Exception e)
            {
                throw new PptxValidatorException("Problem opening the pptx file : " + e.Message);
            }

            // 1. [Content_Types].xml must be present and valid
            Stream contentTypes = null;

            try
            {
                contentTypes = reader.GetEntry(OOX_CONTENT_TYPE_FILE);
            }
            catch (Exception)
            {
                //modified by lohith - to display user friendly message
                throw new NotAnOoxDocumentException("The pptx package must have a \"/[Content_Types].xml\" file");
                //throw new PptxValidatorException("The pptx package must have a \"/[Content_Types].xml\" file");
            }
            this.validateXml(contentTypes);

            // Code to verify whether a file is valid pptx file or not
            //bug no. 1698280
            //Code chcanges 1 of 2
            if (!this.validatePptx(reader))
            {
                throw new NotAnOoxDocumentException("The pptx package must have a Presentation.xml file");
            }

            // End



            // 2. _rels/.rels must be present and valid
            Stream relationShips = null;

            try
            {
                relationShips = reader.GetEntry(OOX_RELATIONSHIP_FILE);
            }
            catch (Exception)
            {
                //modified by lohith - to display user friendly message
                throw new NotAnOoxDocumentException("The pptx package must have a a \"/_rels/.rels\" file");

                //throw new PptxValidatorException("The pptx package must have a \"/_rels/.rels\" file");
            }
            this.validateXml(relationShips);

            // 3. _rel/.rels must contain a relationship of type openDocument
            relationShips = reader.GetEntry(OOX_RELATIONSHIP_FILE);
            XmlReader r         = XmlReader.Create(relationShips);
            String    docTarget = null;

            while (r.Read() && docTarget == null)
            {
                if (r.NodeType == XmlNodeType.Element && r.GetAttribute("Type") == OOX_DOCUMENT_RELATIONSHIP_TYPE)
                {
                    docTarget = r.GetAttribute("Target");
                }
            }
            if (docTarget == null)
            {
                //modified by lohith - to display user friendly message
                throw new NotAnOoxDocumentException("openDocument relation not found in \"/_rels/.rels\"");

                //throw new PptxValidatorException("openDocument relation not found in \"/_rels/.rels\"");
            }


            // 4. For each item in _rels/.rels
            relationShips = reader.GetEntry(OOX_RELATIONSHIP_FILE);
            r             = XmlReader.Create(relationShips);
            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element && r.LocalName == "Relationship")
                {
                    String target = r.GetAttribute("Target");

                    // 4.1. The target item must exist in the package
                    Stream item = null;
                    try
                    {
                        item = reader.GetEntry(target);
                    }
                    catch (Exception)
                    {
                        //modified by lohith - to display user friendly message
                        throw new NotAnOoxDocumentException("The file \"" + target + "\" is described in the \"/_rels/.rels\" file but does not exist in the package.");

                        //throw new PptxValidatorException("The file \"" + target + "\" is described in the \"/_rels/.rels\" file but does not exist in the package.");
                    }

                    // 4.2. A content type can be found in [Content_Types].xml file
                    String ct = this.findContentType(reader, "/" + target);

                    // 4.3. If it's an xml file, it has to be valid
                    if (ct.EndsWith("+xml"))
                    {
                        this.validateXml(item);
                    }
                }
            }

            Stream partRel       = null;
            String partDir       = docTarget.Substring(0, docTarget.IndexOf("/"));
            String partRelPath   = partDir + "/_rels/" + docTarget.Substring(docTarget.IndexOf("/") + 1) + ".rels";
            bool   partRelExists = true;

            try
            {
                partRel = reader.GetEntry(partRelPath);
            }
            catch (Exception)
            {
                partRelExists = false;
            }

            if (partRelExists)
            {
                this.validateXml(partRel);

                // 5. For each item in /ppt/_rels/presentation.xml.rels
                partRel = reader.GetEntry(partRelPath);
                r       = XmlReader.Create(partRel);
                ValidateRels(partDir, r);

                //retrieve all ids referenced in the document
                r.Close();
                Stream doc = reader.GetEntry(docTarget);
                r = XmlReader.Create(doc);
                ArrayList ids = new ArrayList();
                while (r.Read())
                {
                    if (r.LocalName == "custShow")
                    {
                        r.Skip();
                    }
                    else
                    {
                        if (r.NodeType == XmlNodeType.Element && r.GetAttribute("id", OOX_DOC_REL_NS) != null)
                        {
                            ids.Add(r.GetAttribute("id", OOX_DOC_REL_NS));
                        }
                    }
                }

                // check if each id exists in the partRel file
                if (ids.Count != 0)
                {
                    if (!partRelExists)
                    {
                        //modified by lohith - to display user friendly message
                        throw new NotAnOoxDocumentException("Referenced id exist but no part relationship file found");

                        //throw new PptxValidatorException("Referenced id exist but no part relationship file found");
                    }
                    relationShips = reader.GetEntry(partRelPath);
                    r             = XmlReader.Create(relationShips);
                    while (r.Read())
                    {
                        if (r.NodeType == XmlNodeType.Element && r.LocalName == "Relationship")
                        {
                            if (ids.Contains(r.GetAttribute("Id")))
                            {
                                ids.Remove(r.GetAttribute("Id"));
                            }
                        }
                    }
                    if (ids.Count != 0)
                    {
                        //modified by lohith - to display user friendly message
                        throw new NotAnOoxDocumentException("One or more relationship id have not been found in the partRelationship file : " + ids[0]);

                        //throw new PptxValidatorException("One or more relationship id have not been found in the partRelationship file : " + ids[0]);
                    }
                }
            }

            //6. For each item in /ppt/slideMasters/_rels/slideMasters.xml.rels
            docTarget = null;
            partRel   = reader.GetEntry(partRelPath);
            r         = XmlReader.Create(partRel);
            while (r.Read() && docTarget == null)
            {
                if (r.NodeType == XmlNodeType.Element && r.GetAttribute("Type") == OOX_SIDEMASTER_RELATIONSHIP_TYPE)
                {
                    docTarget   = r.GetAttribute("Target");
                    partDir     = OOX_SIDEMASTER_RELATIONSHIP_FILE;
                    partRelPath = partDir + "/_rels/" + docTarget.Substring(docTarget.IndexOf("/") + 1) + ".rels";

                    partRel = reader.GetEntry(partRelPath);
                    r       = XmlReader.Create(partRel);
                    ValidateRels(partDir, r);
                }
            }

            //7. Validation for SlideLayouts if required will be added here.

            //8. For each slide in a presentation, validation needs to be added here

            //9. During developement required validations for pptx feature e.g notes master, numbering etc. will be added.

            //10. are all referenced relationships exist in the part relationship file
        }
Example #11
0
        public override bool transform()
        {
            //string Document_xml = "word/document.xml";//word下的doucument.xml文档
            string picture_xml = "word\\media";
            // string tmpFile = Path.GetTempPath() + "tmpDoc.xml";
            // string tmpFile2 = Path.GetTempPath() + "sndtmpDoc.xml";
            // Guid gPath = Guid.NewGuid();
            // string wordPrePath = Path.GetTempPath() + gPath.ToString() + "\\";
            //   if (!Directory.Exists(wordPrePath))
            //     Directory.CreateDirectory(wordPrePath);
            string wordPrePath = Path.GetDirectoryName(outputFile) + "\\";//输出文档路径
            string mediaPath   = wordPrePath + picture_xml;

            string tblVtcl           = wordPrePath + "tblVertical.xml";
            string tmpFile           = wordPrePath + "tmpDoc.xml";
            string tmpFile2          = wordPrePath + "sndtmpDoc.xml"; //预处理中的中间文档
            string tmpFile3          = wordPrePath + "thrtmpDoc.xml"; //预处理中的中间文档
            string preOutputFileName = wordPrePath + "tempdoc.xml";   //预处理出来的中间文档

            XmlReader source  = null;
            XmlWriter writer  = null;
            ZipReader archive = ZipFactory.OpenArchive(inputFile);

            archive.ExtractOfficeDocument(inputFile, wordPrePath);
            //archive.ExtractUOFDocument(inputFile, wordPrePath);
            bool isSuccess = true;

            try
            {
                OoxToUofTableProcessing tableProcessing2 = new OoxToUofTableProcessing(wordPrePath + "word" + Path.AltDirectorySeparatorChar + "document.xml");
                tableProcessing2.Processing(tblVtcl);

                //找到预处理第一步的式样单pretreatmentStep1.xsl
                XPathDocument        xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.OOXToUOF_PRETREAT_STEP1_XSL, TranslatorConstants.OOXToUOF_WORD_LOCATION);
                XslCompiledTransform xslt  = new XslCompiledTransform();
                xslt.Load(xpdoc);

                // source = XmlReader.Create(archive.GetEntry(Document_xml));//解压,得到document.xml文档
                source = XmlReader.Create(tblVtcl);
                writer = new XmlTextWriter(tmpFile, Encoding.UTF8); //tmpDoc.xml
                xslt.Transform(source, writer);                     //document.xml--经过预处理.xsl--tmpDoc.xml
                if (writer != null)
                {
                    writer.Close();
                }
                if (source != null)
                {
                    source.Close();
                }

                //第二步预处理
                xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.OOXToUOF_PRETREAT_STEP2_XSL, TranslatorConstants.OOXToUOF_WORD_LOCATION);
                xslt  = new XslCompiledTransform();
                xslt.Load(xpdoc);
                writer = new XmlTextWriter(tmpFile2, Encoding.UTF8); //sndtemDoc.xml
                xslt.Transform(tmpFile, writer);                     //tmpDoc--经过预处理2.xsl--sndtemDoc.xml
                if (writer != null)
                {
                    writer.Close();
                }

                //第三步预处理
                xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.OOXToUOF_PRETREAT_STEP3_XSL, TranslatorConstants.OOXToUOF_WORD_LOCATION);
                xslt  = new XslCompiledTransform();
                xslt.Load(xpdoc);
                // xslt.Transform(tmpFile2, outputFile);
                xslt.Transform(tmpFile2, tmpFile3);//经过预处理3--tempdoc.xml

                //2011/6/17zhaobj:阴影预处理
                XmlNamespaceManager nms = null;
                XmlDocument         xmlDoc;
                XmlTextWriter       resultWriter = null;
                xmlDoc = new XmlDocument();
                xmlDoc.Load(tmpFile3);

                nms = new XmlNamespaceManager(xmlDoc.NameTable);
                nms.AddNamespace("a", TranslatorConstants.XMLNS_A);
                nms.AddNamespace("w", TranslatorConstants.XMLNS_W);

                XmlNodeList shapeShadeList = xmlDoc.SelectNodes("//a:outerShdw", nms);
                if (shapeShadeList != null)
                {
                    foreach (XmlNode shapeShade in shapeShadeList)
                    {
                        double dist, dir;
                        if (((XmlElement)shapeShade).HasAttribute("dist"))
                        {
                            dist = Convert.ToDouble(shapeShade.Attributes.GetNamedItem("dist").Value) / 12700;
                        }
                        else
                        {
                            dist = 0.0;
                        }
                        if (((XmlElement)shapeShade).HasAttribute("dir"))
                        {
                            dir = (Convert.ToDouble(shapeShade.Attributes.GetNamedItem("dir").Value) * 3.1415) / (180 * 60000);
                        }
                        else
                        {
                            dir = 0.0;
                        }
                        double     xValue = dist * Math.Cos(dir);
                        double     yValue = dist * Math.Sin(dir);
                        XmlElement x      = xmlDoc.CreateElement("x");
                        x.InnerText = Convert.ToString(xValue);
                        shapeShade.AppendChild(x);
                        XmlElement y = xmlDoc.CreateElement("y");
                        y.InnerText = Convert.ToString(yValue);
                        shapeShade.AppendChild(y);
                    }
                }


                resultWriter = new XmlTextWriter(preOutputFileName, System.Text.Encoding.UTF8);
                xmlDoc.Save(resultWriter);
                resultWriter.Close();

                /*
                 *
                 *
                 *
                 */

                OutputFilename = preOutputFileName;

                //图片预处理
                //if (Directory.Exists(wordPrePath + picture_xml))
                //{
                //    xmlDoc.Load(preOutputFileName);
                //    DirectoryInfo mediaInfo = new DirectoryInfo(wordPrePath + picture_xml);
                //    FileInfo[] medias = mediaInfo.GetFiles();
                //    XmlNode root = xmlDoc.SelectSingleNode("w:document", nms);
                //    XmlElement mediaNode=xmlDoc.CreateElement("w","media",TranslatorConstants.XMLNS_W);
                //    foreach (FileInfo media in medias)
                //    {
                //        XmlElement mediaFileNode = xmlDoc.CreateElement("u2opic", "picture", "urn:u2opic:xmlns:post-processings:special");
                //        mediaFileNode.SetAttribute("target", "urn:u2opic:xmlns:post-processings:special", media.FullName);
                //        mediaNode.AppendChild(mediaFileNode);
                //    }
                //    root.AppendChild(mediaNode);
                //    xmlDoc.Save(tmpPic);
                //    OutputFilename = tmpPic;
                //}
            }
            catch (Exception e)
            {
                logger.Error("Fail in OoxToUofPreProcessorOneWord: " + e.Message);
                logger.Error(e.StackTrace);
                isSuccess = false;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (source != null)
                {
                    source.Close();
                }
                if (archive != null)
                {
                    archive.Close();
                }
                if (File.Exists(tmpFile2))
                {
                    File.Delete((tmpFile2));
                }
            }
            return(isSuccess);
        }
Example #12
0
        /// <summary>
        /// Check the validity of an Office Open XML file.
        /// </summary>
        /// <param name="fileName">The path of the docx file.</param>
        public void validate(string fileName)
        {
            // 0. The file must exist and be a valid Zip archive
            ZipReader reader = null;

            try
            {
                reader = ZipFactory.OpenArchive(fileName);
            }
            catch (Exception e)
            {
                throw new ValidationException("Problem opening the docx file : " + e.Message);
            }

            // 1. [Content_Types].xml must be present and valid
            Stream contentTypes = null;

            try
            {
                contentTypes = reader.GetEntry(OOX_CONTENT_TYPE_FILE);
            }
            catch (Exception)
            {
                throw new ValidationException("The docx package must have a \"/[Content_Types].xml\" file");
            }
            this.validateXml(contentTypes);

            // 2. _rels/.rels must be present and valid
            Stream relationShips = null;

            try
            {
                relationShips = reader.GetEntry(OOX_RELATIONSHIP_FILE);
            }
            catch (Exception)
            {
                throw new ValidationException("The docx package must have a \"/_rels/.rels\" file");
            }
            this.validateXml(relationShips);

            // 3. _rel/.rels must contain a relationship of type openDocument
            relationShips = reader.GetEntry(OOX_RELATIONSHIP_FILE);
            XmlReader r         = XmlReader.Create(relationShips);
            String    docTarget = null;

            while (r.Read() && docTarget == null)
            {
                if (r.NodeType == XmlNodeType.Element && r.GetAttribute("Type") == OOX_DOCUMENT_RELATIONSHIP_TYPE)
                {
                    docTarget = r.GetAttribute("Target");
                }
            }
            if (docTarget == null)
            {
                throw new ValidationException("openDocument relation not found in \"/_rels/.rels\"");
            }

            // 4. For each item in _rels/.rels
            relationShips = reader.GetEntry(OOX_RELATIONSHIP_FILE);
            r             = XmlReader.Create(relationShips);
            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element && r.LocalName == "Relationship")
                {
                    String target = r.GetAttribute("Target");



                    // 4.1. The target item must exist in the package
                    Stream item = null;
                    try
                    {
                        item = reader.GetEntry(target);
                    }
                    catch (Exception)
                    {
                        throw new ValidationException("The file \"" + target + "\" is described in the \"/_rels/.rels\" file but does not exist in the package.");
                    }

                    // 4.2. A content type can be found in [Content_Types].xml file
                    String ct = this.findContentType(reader, "/" + target);

                    // 4.3. If it's an xml file, it has to be valid
                    if (ct.EndsWith("+xml"))
                    {
                        this.validateXml(item);
                    }
                }
            }

            // Does a part relationship exist ?
            Stream partRel       = null;
            String partDir       = docTarget.Substring(0, docTarget.IndexOf("/"));
            String partRelPath   = partDir + "/_rels/" + docTarget.Substring(docTarget.IndexOf("/") + 1) + ".rels";
            bool   partRelExists = true;

            try
            {
                partRel = reader.GetEntry(partRelPath);
            }
            catch (Exception)
            {
                partRelExists = false;
            }

            if (partRelExists)
            {
                this.validateXml(partRel);

                // 5. For each item in /word/_rels/document.xml.rels
                partRel = reader.GetEntry(partRelPath);
                r       = XmlReader.Create(partRel);
                while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element && r.LocalName == "Relationship")
                    {
                        String target = partDir + "/" + r.GetAttribute("Target");

                        // Is the target item exist in the package ?
                        Stream item       = null;
                        bool   fileExists = true;
                        try
                        {
                            item = reader.GetEntry(target);
                        }
                        catch (Exception)
                        {
                            //throw new OoxValidatorException("The file \"" + target + "\" is described in the \"/word/_rels/document.xml.rels\" file but does not exist in the package.");
                            fileExists = false;
                        }

                        if (fileExists)
                        {
                            // 5.1. A content type can be found in [Content_Types].xml file
                            String ct = this.findContentType(reader, "/" + target);

                            // 5.2. If it's an xml file, it has to be valid
                            if (ct.EndsWith("+xml"))
                            {
                                this.validateXml(item);
                            }
                        }
                    }
                }
            }

            // 6. do all referenced relationships exist in the part relationship file

            // retrieve all ids referenced in the document

            Stream doc = reader.GetEntry(docTarget);

            r = XmlReader.Create(doc);
            ArrayList ids = new ArrayList();

            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element && r.GetAttribute("id", OOX_DOC_REL_NS) != null)
                {
                    ids.Add(r.GetAttribute("id", OOX_DOC_REL_NS));
                }
            }

            // check if each id exists in the partRel file

            if (ids.Count != 0)
            {
                if (!partRelExists)
                {
                    throw new ValidationException("Referenced id exist but no part relationship file found");
                }
                relationShips = reader.GetEntry(partRelPath);
                r             = XmlReader.Create(relationShips);
                while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element && r.LocalName == "Relationship")
                    {
                        if (ids.Contains(r.GetAttribute("Id")))
                        {
                            ids.Remove(r.GetAttribute("Id"));
                        }
                    }
                }
                if (ids.Count != 0)
                {
                    throw new ValidationException("One or more relationship id have not been found in the partRelationship file : " + ids[0]);
                }
            }
        }