/// <summary>
        /// This is used to create a <see cref="TargetDictionary"/> used to store reference link targets
        /// </summary>
        /// <param name="configuration">The configuration element for the target dictionary</param>
        /// <returns>A default <see cref="InMemoryTargetDictionary"/> instance containing the reference link
        /// targets</returns>
        /// <remarks>This can be overridden in derived classes to provide persistent caches with backing stores
        /// other than the default <see cref="Dictionary{TKey, TValue}"/></remarks>.
        public virtual TargetDictionary CreateTargetDictionary(XPathNavigator configuration)
        {
            TargetDictionary d = null;

            try
            {
                d = new InMemoryTargetDictionary(this, configuration);
            }
            catch (Exception ex)
            {
                base.WriteMessage(MessageLevel.Error, BuildComponentUtilities.GetExceptionMessage(ex));
            }

            return(d);
        }
Beispiel #2
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);
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public TransformComponent(BuildAssembler assembler, XPathNavigator configuration) : base(assembler, configuration)
        {
            // load the transforms
            XPathNodeIterator transform_nodes = configuration.Select("transform");

            foreach (XPathNavigator transform_node in transform_nodes)
            {
                // load the transform
                string file = transform_node.GetAttribute("file", String.Empty);
                if (String.IsNullOrEmpty(file))
                {
                    WriteMessage(MessageLevel.Error, "Each transform element must specify a file attribute.");
                }
                file = Environment.ExpandEnvironmentVariables(file);

                Transform transform = null;
                try {
                    transform = new Transform(file);
                } catch (IOException e) {
                    WriteMessage(MessageLevel.Error, String.Format("The transform file '{0}' could not be loaded. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
                } catch (XmlException e) {
                    WriteMessage(MessageLevel.Error, String.Format("The transform file '{0}' is not a valid XML file. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
                } catch (XsltException e) {
                    WriteMessage(MessageLevel.Error, String.Format("The XSL transform '{0}' contains an error. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
                }


                transforms.Add(transform);


                // load any arguments
                XPathNodeIterator argument_nodes = transform_node.Select("argument");
                foreach (XPathNavigator argument_node in argument_nodes)
                {
                    string key = argument_node.GetAttribute("key", String.Empty);
                    if ((key == null) || (key.Length == 0))
                    {
                        WriteMessage(MessageLevel.Error, "When creating a transform argument, you must specify a key using the key attribute");
                    }

                    // set "expand-value" attribute to true to expand environment variables embedded in "value".
                    string expand_attr  = argument_node.GetAttribute("expand-value", String.Empty);
                    bool   expand_value = String.IsNullOrEmpty(expand_attr) ? false : Convert.ToBoolean(expand_attr);

                    string value = argument_node.GetAttribute("value", String.Empty);
                    if ((value != null) && (value.Length > 0))
                    {
                        transform.Arguments.AddParam(key, String.Empty, expand_value ? Environment.ExpandEnvironmentVariables(value) : value);
                    }
                    else
                    {
                        transform.Arguments.AddParam(key, String.Empty, argument_node.Clone());
                    }
                }
            }
        }
Beispiel #4
0
 private void AddTargets(string file, LinkType2 type)
 {
     try {
         XPathDocument document = new XPathDocument(file);
         XmlTargetCollectionUtilities.AddTargets(targets, document.CreateNavigator(), type);
     } catch (XmlSchemaException e) {
         WriteMessage(MessageLevel.Error, String.Format("The reference targets file '{0}' is not valid. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
     } catch (XmlException e) {
         WriteMessage(MessageLevel.Error, String.Format("The reference targets file '{0}' is not well-formed XML. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
     } catch (IOException e) {
         WriteMessage(MessageLevel.Error, String.Format("An access error occured while opening the reference targets file '{0}'. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
     }
 }
        private void LoadContent(string file)
        {
            WriteMessage(MessageLevel.Info, String.Format("Loading shared content file '{0}'.", file));

            try {
                XmlReader reader = XmlReader.Create(file);

                try {
                    reader.MoveToContent();
                    while (!reader.EOF)
                    {
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "item"))
                        {
                            string key   = reader.GetAttribute("id").ToLower();
                            string value = reader.ReadInnerXml();

                            if (content.ContainsKey(key))
                            {
                                WriteMessage(MessageLevel.Info, String.Format("Overriding shared content item '{0}' with value in file '{1}'.", key, file));
                            }
                            content[key] = value;
                            // content.Add(key, value);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }
                } finally {
                    reader.Close();
                }
            } catch (IOException e) {
                WriteMessage(MessageLevel.Error, String.Format("The shared content file '{0}' could not be opened. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
            } catch (XmlException e) {
                WriteMessage(MessageLevel.Error, String.Format("The shared content file '{0}' is not well-formed. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
            } catch (XmlSchemaException e) {
                WriteMessage(MessageLevel.Error, String.Format("The shared content file '{0}' is not valid. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
            }
        }