Exemple #1
0
        private void FileCreatedHandler(Object o, EventArgs e)
        {
            FileCreatedEventArgs fe = e as FileCreatedEventArgs;

            if (fe == null)
            {
                return;
            }

            string path = Path.Combine(fe.HxfPath, outputValue).ToLower();

            XmlWriter tempWriter;

            if (!writers.TryGetValue(path, out tempWriter))
            {
                if (writer != null)
                {
                    writer.WriteEndDocument();
                    writer.Close();
                }
                WriteFile(path);
            }

            WriteFileElement(fe.FilePath);
        }
        //=====================================================================

        /// <summary>
        /// This is used to create the HxF files as needed and write the filenames to them
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void FileCreatedHandler(object sender, EventArgs e)
        {
            XmlWriter writer;

            FileCreatedEventArgs fe = e as FileCreatedEventArgs;

            // Ignore if not our event or if it's not a content file
            if (fe == null || !fe.IsContentFile)
            {
                return;
            }

            // The HxF files should go in the parent folder of the content files
            string basePath = Path.GetDirectoryName(Path.GetDirectoryName(fe.FilePath));

            string path = Path.Combine(basePath, outputFilename);

            // This allows for multiple concurrent output files in different locations
            if (!writers.TryGetValue(path, out writer))
            {
                writer = this.WriteFile(path);
                writers.Add(path, writer);
            }

            if (basePath.Length > 1)
            {
                WriteFileElement(writer, fe.FilePath.Substring(basePath.Length + 1));
            }
            else
            {
                WriteFileElement(writer, fe.FilePath);
            }
        }
Exemple #3
0
        public override void Apply(XmlDocument document, string key)
        {
            // set the evaluation context
            context["key"] = key;

            XPathExpression path_xpath = path_expression.Clone();

            path_xpath.SetContext(context);

            // evaluate the path
            string path = document.CreateNavigator().Evaluate(path_xpath).ToString();
            string file = Path.GetFileName(path);

            string fileLinkPath = Path.Combine(linkPath, file);

            if (basePath != null)
            {
                path = Path.Combine(basePath, path);
            }


            // *SECURIY* The path name may be derived from user entered meta data in Doc Studio and as such
            // it is not trustworthy. To pass, the path must be inside the directory tree base_directory
            // (which is the current directory if a path is not specified).

            // This test is causing problems

            /*
             * string absoluteBasePath = (basePath == null) ? Directory.GetCurrentDirectory() : Path.GetFullPath(basePath);
             * string targetPath = Path.GetDirectoryName(path);
             * if (!targetPath.StartsWith(absoluteBasePath, StringComparison.CurrentCultureIgnoreCase)) {
             *  WriteMessage(MessageLevel.Error, string.Format("Cannot save document outside of base directory: {0}", targetPath));
             *  return;
             * }
             */
            string targetDirectory = Path.GetDirectoryName(path);

            if (!Directory.Exists(targetDirectory))
            {
                Directory.CreateDirectory(targetDirectory);
            }

            // save the document
            // select_expression determines which nodes get saved. If there is no select_expression
            // we simply save the root node as before. If there is a select_expression, we evaluate the
            // xpath expression and save the resulting node set. The select expression also enables the
            // "literal-text" processing instruction, which outputs its content as unescaped text.
            if (select_expression == null)
            {
                XmlNode doctype = document.DocumentType;
                try {
                    //Console.WriteLine("path = '{0}'", path);
                    //document.Save(path);

                    using (XmlWriter writer = XmlWriter.Create(path, settings)) {
                        document.Save(writer);
                    }
                } catch (IOException e) {
                    WriteMessage(MessageLevel.Error, String.Format("An access error occured while attempting to save to the file '{0}'. The error message is: {1}", path, BuildComponentUtilities.GetExceptionMessage(e)));
                } catch (XmlException e) {
                    WriteMessage(MessageLevel.Error, String.Format("Invalid XML was written to the output file '{0}'. The error message is: '{1}'", path, BuildComponentUtilities.GetExceptionMessage(e)));
                }

                // Get the relative html path for HXF generation.
                int    index    = fileLinkPath.IndexOf('/');
                string htmlPath = fileLinkPath.Substring(index + 1, fileLinkPath.Length - (index + 1));

                FileCreatedEventArgs fe = new FileCreatedEventArgs(htmlPath, Path.GetDirectoryName(targetDirectory));
                OnComponentEvent(fe);
            }
            else
            {
                // IMPLEMENTATION NOTE: The separate StreamWriter is used to maintain XML indenting.
                // Without it the XmlWriter won't honor our indent settings after plain text nodes have been
                // written.
                settings.ConformanceLevel = ConformanceLevel.Auto;
                using (StreamWriter output = File.CreateText(path)) {
                    using (XmlWriter writer = XmlWriter.Create(output, settings)) {
                        XPathExpression select_xpath = select_expression.Clone();
                        select_xpath.SetContext(context);
                        XPathNodeIterator ni = document.CreateNavigator().Select(select_expression);
                        while (ni.MoveNext())
                        {
                            if (ni.Current.NodeType == XPathNodeType.ProcessingInstruction && ni.Current.Name.Equals("literal-text"))
                            {
                                writer.Flush();
                                output.Write(ni.Current.Value);
                            }
                            else
                            {
                                ni.Current.WriteSubtree(writer);
                            }
                        }
                    }
                }
            }
        }
		public override void Apply (XmlDocument document, string id) {

			XPathNodeIterator artLinkIterator = document.CreateNavigator().Select(artLinkExpression);
			XPathNavigator[] artLinks = BuildComponentUtilities.ConvertNodeIteratorToArray(artLinkIterator);
			foreach (XPathNavigator artLink in artLinks) {

				string name = artLink.GetAttribute("target", String.Empty).ToLower();

				if (targets.ContainsKey(name)) {
					ArtTarget target = targets[name];

                    // evaluate the path
                    string path = document.CreateNavigator().Evaluate(target.OutputXPath).ToString();
                                        
                    if (target.baseOutputPath != null) path = Path.Combine(target.baseOutputPath, path);
                    string outputPath = Path.Combine(path, target.Name);

                    string targetDirectory = Path.GetDirectoryName(outputPath);
                    
                    if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory);
                                        				
					if (File.Exists(target.InputPath)) {

						if (File.Exists(outputPath)) {
							File.SetAttributes(outputPath, FileAttributes.Normal);
						}

						File.Copy(target.InputPath, outputPath, true);
					} else {
						WriteMessage(MessageLevel.Warn, String.Format("The file '{0}' for the art target '{1}' was not found.", target.InputPath, name));
					}

                    // Get the relative art path for HXF generation.
                    int index = target.LinkPath.IndexOf('/');
                    string artPath = target.LinkPath.Substring(index+1, target.LinkPath.Length - (index+1));
                    
                    FileCreatedEventArgs fe = new FileCreatedEventArgs(artPath, Path.GetDirectoryName(path));
                    OnComponentEvent(fe);

					XmlWriter writer = artLink.InsertAfter();

					writer.WriteStartElement("img");
					if (!String.IsNullOrEmpty(target.Text)) writer.WriteAttributeString("alt", target.Text);

                    if (target.FormatXPath == null) {
                        writer.WriteAttributeString("src", target.LinkPath);
                    }
                    else {
                        // WebDocs way, which uses the 'format' xpath expression to calculate the target path
                        // and then makes it relative to the current page if the 'relative-to' attribute is
                        // used.
                        string src = BuildComponentUtilities.EvalXPathExpr(document, target.FormatXPath, "key", Path.GetFileName(outputPath));
                        if (target.RelativeToXPath != null)
                            src = BuildComponentUtilities.GetRelativePath(src, BuildComponentUtilities.EvalXPathExpr(document, target.RelativeToXPath, "key", id));
                        writer.WriteAttributeString("src", src);
                    }

					writer.WriteEndElement();

					writer.Close();

					artLink.DeleteSelf();

				} else {
					WriteMessage(MessageLevel.Warn, String.Format("Unknown art target '{0}'", name));
				}

			}				

		}
		public override void Apply (XmlDocument document, string key) {

			// set the evaluation context
			context["key"] = key;

			XPathExpression path_xpath = path_expression.Clone();
			path_xpath.SetContext(context);

			// evaluate the path
			string path = document.CreateNavigator().Evaluate(path_xpath).ToString();
            string file = Path.GetFileName(path);
            
            string fileLinkPath = Path.Combine(linkPath, file);
            if (basePath != null) path = Path.Combine(basePath, path);
                       

            // *SECURIY* The path name may be derived from user entered meta data in Doc Studio and as such 
            // it is not trustworthy. To pass, the path must be inside the directory tree base_directory 
            // (which is the current directory if a path is not specified).

            // This test is causing problems
            /*
            string absoluteBasePath = (basePath == null) ? Directory.GetCurrentDirectory() : Path.GetFullPath(basePath);
            string targetPath = Path.GetDirectoryName(path);
            if (!targetPath.StartsWith(absoluteBasePath, StringComparison.CurrentCultureIgnoreCase)) {
                WriteMessage(MessageLevel.Error, string.Format("Cannot save document outside of base directory: {0}", targetPath));
                return;
            }
            */
            string targetDirectory = Path.GetDirectoryName(path);
            if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory);

            // save the document
            // select_expression determines which nodes get saved. If there is no select_expression
            // we simply save the root node as before. If there is a select_expression, we evaluate the
            // xpath expression and save the resulting node set. The select expression also enables the 
            // "literal-text" processing instruction, which outputs its content as unescaped text.
            if (select_expression == null) {
                XmlNode doctype = document.DocumentType;
                try {
                    //Console.WriteLine("path = '{0}'", path);
                    //document.Save(path);

                    using (XmlWriter writer = XmlWriter.Create(path, settings)) {
                        document.Save(writer);
                    }

                } catch (IOException e) {
                    WriteMessage(MessageLevel.Error, String.Format("An access error occured while attempting to save to the file '{0}'. The error message is: {1}", path, BuildComponentUtilities.GetExceptionMessage(e)));
                } catch (XmlException e) {
                    WriteMessage(MessageLevel.Error, String.Format("Invalid XML was written to the output file '{0}'. The error message is: '{1}'", path, BuildComponentUtilities.GetExceptionMessage(e)));
                }

                // Get the relative html path for HXF generation.
                int index = fileLinkPath.IndexOf('/');
                string htmlPath = fileLinkPath.Substring(index + 1, fileLinkPath.Length - (index + 1));
                
                FileCreatedEventArgs fe = new FileCreatedEventArgs(htmlPath, Path.GetDirectoryName(targetDirectory));
                OnComponentEvent(fe);
            }
            else {
                // IMPLEMENTATION NOTE: The separate StreamWriter is used to maintain XML indenting.
                // Without it the XmlWriter won't honor our indent settings after plain text nodes have been
                // written.
                settings.ConformanceLevel = ConformanceLevel.Auto;
                using (StreamWriter output = File.CreateText(path)) {
                    using (XmlWriter writer = XmlWriter.Create(output, settings)) {
                        XPathExpression select_xpath = select_expression.Clone();
                        select_xpath.SetContext(context);
                        XPathNodeIterator ni = document.CreateNavigator().Select(select_expression);
                        while (ni.MoveNext()) {
                            if (ni.Current.NodeType == XPathNodeType.ProcessingInstruction && ni.Current.Name.Equals("literal-text")) {
                                writer.Flush();
                                output.Write(ni.Current.Value);
                            }
                            else
                                ni.Current.WriteSubtree(writer);
                        }
                    }
                }
            }
		}
        public override void Apply(XmlDocument document, string id)
        {
            XPathNodeIterator artLinkIterator = document.CreateNavigator().Select(artLinkExpression);

            XPathNavigator[] artLinks = BuildComponentUtilities.ConvertNodeIteratorToArray(artLinkIterator);
            foreach (XPathNavigator artLink in artLinks)
            {
                string name = artLink.GetAttribute("target", String.Empty).ToLower();

                if (targets.ContainsKey(name))
                {
                    ArtTarget target = targets[name];

                    // evaluate the path
                    string path = document.CreateNavigator().Evaluate(target.OutputXPath).ToString();

                    if (target.baseOutputPath != null)
                    {
                        path = Path.Combine(target.baseOutputPath, path);
                    }
                    string outputPath = Path.Combine(path, target.Name);

                    string targetDirectory = Path.GetDirectoryName(outputPath);

                    if (!Directory.Exists(targetDirectory))
                    {
                        Directory.CreateDirectory(targetDirectory);
                    }

                    if (File.Exists(target.InputPath))
                    {
                        if (File.Exists(outputPath))
                        {
                            File.SetAttributes(outputPath, FileAttributes.Normal);
                        }

                        File.Copy(target.InputPath, outputPath, true);
                    }
                    else
                    {
                        WriteMessage(MessageLevel.Warn, String.Format("The file '{0}' for the art target '{1}' was not found.", target.InputPath, name));
                    }

                    // Get the relative art path for HXF generation.
                    int    index   = target.LinkPath.IndexOf('/');
                    string artPath = target.LinkPath.Substring(index + 1, target.LinkPath.Length - (index + 1));

                    FileCreatedEventArgs fe = new FileCreatedEventArgs(artPath, Path.GetDirectoryName(path));
                    OnComponentEvent(fe);

                    XmlWriter writer = artLink.InsertAfter();

                    writer.WriteStartElement("img");
                    if (!String.IsNullOrEmpty(target.Text))
                    {
                        writer.WriteAttributeString("alt", target.Text);
                    }

                    if (target.FormatXPath == null)
                    {
                        writer.WriteAttributeString("src", target.LinkPath);
                    }
                    else
                    {
                        // WebDocs way, which uses the 'format' xpath expression to calculate the target path
                        // and then makes it relative to the current page if the 'relative-to' attribute is
                        // used.
                        string src = BuildComponentUtilities.EvalXPathExpr(document, target.FormatXPath, "key", Path.GetFileName(outputPath));
                        if (target.RelativeToXPath != null)
                        {
                            src = BuildComponentUtilities.GetRelativePath(src, BuildComponentUtilities.EvalXPathExpr(document, target.RelativeToXPath, "key", id));
                        }
                        writer.WriteAttributeString("src", src);
                    }

                    writer.WriteEndElement();

                    writer.Close();

                    artLink.DeleteSelf();
                }
                else
                {
                    WriteMessage(MessageLevel.Warn, String.Format("Unknown art target '{0}'", name));
                }
            }
        }