Ejemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="templateParsingState"></param>
        /// <param name="me"></param>
        /// <returns></returns>
        protected IFileContainer GetFileContainer(ITemplateParsingState templateParsingState, System.Xml.XmlNode me)
        {
            string currentWorkingDirectory = templateParsingState.GetCWD(me);
            XmlAttribute filenameAttribute = me.Attributes["filename"];

            if (null == filenameAttribute)
            {
                me.ParentNode.ParentNode.InsertBefore(
                    templateParsingState.GenerateWarningNode("filename not specified: " + me.OuterXml),
                    me.ParentNode);

                return null;
            }

            string filename = templateParsingState.WebConnection.WebServer.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath(
                currentWorkingDirectory,
                filenameAttribute.Value);

            try
            {
                return templateParsingState.WebConnection.WebServer.FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(filename);
            }
            catch (FileNotFoundException fnfe)
            {
                log.Warn("Attempted to get permission for a non-existant file: " + filenameAttribute.Value, fnfe);

                me.ParentNode.ParentNode.InsertBefore(
                    templateParsingState.GenerateWarningNode("File doesn't exist: " + me.OuterXml),
                    me.ParentNode);

                return null;
            }
        }
        private void HandleConditional(
            ITemplateParsingState templateParsingState,
            IDictionary<string, object> getParameters,
            XmlNode conditionalNode)
        {
            Dictionary<string, ITemplateConditionHandler> templateConditionHandlers = templateParsingState.TemplateHandlerLocator.TemplateConditionHandlers;

            XmlNode current = conditionalNode.FirstChild;
            bool conditionMet = false;

            // Note:  Looping continues after the condition is met to ensure that all nodes are in the correct namespace
            while (null != current)
            {
                // Make sure the namespace is proper
                if (current.NamespaceURI != templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace)
                    conditionalNode.ParentNode.InsertBefore(
                        templateParsingState.GenerateWarningNode("All nodes within an <if> must be of " + templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace + " namespace: " + current.OuterXml),
                        conditionalNode);

                else
                {
                    // Make sure the node is supported
                    ITemplateConditionHandler templateConditionHandler;
                    if (!templateConditionHandlers.TryGetValue(current.LocalName, out templateConditionHandler))
                        conditionalNode.ParentNode.InsertBefore(
                            templateParsingState.GenerateWarningNode("There is no condition handler for " + current.LocalName + ": " + current.OuterXml),
                            conditionalNode);

                    if (!conditionMet)
                        try
                        {
                            if (templateConditionHandler.IsConditionMet(templateParsingState, current))
                            {
                                conditionMet = true;

                                foreach (XmlNode xmlNode in Enumerable<XmlNode>.FastCopy(Enumerable<XmlNode>.Filter(current.ChildNodes)))
                                    conditionalNode.ParentNode.InsertBefore(xmlNode, conditionalNode);
                            }
                        }
                        catch (Exception e)
                        {
                            log.Error("Unhandled error for condition tag " + current.LocalName + ": " + current.OuterXml, e);

                            conditionalNode.ParentNode.InsertBefore(
                                templateParsingState.GenerateWarningNode("An unhandled error occured processing: " + current.OuterXml),
                                conditionalNode);
                        }
                }

                current = current.NextSibling;
            }

            conditionalNode.ParentNode.RemoveChild(conditionalNode);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="templateParsingState"></param>
        /// <param name="me"></param>
        /// <returns></returns>
        public bool IsConditionMet(ITemplateParsingState templateParsingState, XmlNode me)
        {
            IFileContainer fileContainer = GetFileContainer(templateParsingState, me);

            if (null == fileContainer)
                return false;

            XmlAttribute namedPermissionAttribute = me.Attributes["namedpermission"];

            if (null == namedPermissionAttribute)
            {
                me.ParentNode.ParentNode.InsertBefore(
                    templateParsingState.GenerateWarningNode("namedpermission not specified: " + me.OuterXml),
                    me.ParentNode);

                return false;
            }

            return DetermineCondition(templateParsingState, fileContainer, namedPermissionAttribute.Value);
        }
Ejemplo n.º 4
0
        IEnumerable<XmlNode> DoParseTag(ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element)
        {
            // Generate the xml
            StringBuilder xml = new StringBuilder(
                string.Format("<html xmlns=\"{0}\"><head></head><body>", templateParsingState.TemplateDocument.FirstChild.NamespaceURI),
                element.OuterXml.Length);

            foreach (XmlNode xmlNode in element.ChildNodes)
                if (xmlNode is XmlText)
                    xml.Append(((XmlText)xmlNode).InnerText);
                else
                    xml.Append(xmlNode.OuterXml);

            xml.Append("</body></html>");

            XmlDocument xmlDocument;
            try
            {
                xmlDocument = templateParsingState.LoadXmlDocument(
                    xml.ToString(),
                    templateParsingState.GetXmlParseMode(element),
                    element.OuterXml);
            }
            catch (WebResultsOverrideException wroe)
            {
                templateParsingState.ReplaceNodes(
                    element,
                    templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString));

                return new XmlNode[0];
            }

            // import the new tags
            IEnumerable<XmlNode> importedNodes = Enumerable<XmlNode>.FastCopy(Enumerable<XmlNode>.Cast(
                xmlDocument.FirstChild.ChildNodes[1].ChildNodes));

            templateParsingState.ReplaceNodes(element, importedNodes);

            return importedNodes;
        }
        private void LoadSnipit(
            ITemplateParsingState templateParsingState,
            IDictionary<string, object> getParameters,
            XmlElement element)
        {
            XmlAttribute srcAttribute = (XmlAttribute)element.Attributes.GetNamedItem("src", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace);

            if (null == srcAttribute)
                templateParsingState.ReplaceNodes(
                    element,
                    templateParsingState.GenerateWarningNode("oc:src must be specified: " + element.OuterXml));

            else
            {
                string fileName = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath(
                    templateParsingState.GetCWD(element),
                    srcAttribute.Value);

                IFileContainer fileContainer = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(fileName);
                templateParsingState.WebConnection.TouchedFiles.Add(fileContainer);

                // If the user doesn't have permission for the component and the component has something to use instead, use it
                if (null == fileContainer.LoadPermission(templateParsingState.WebConnection.Session.User.Id) && element.HasChildNodes)
                {
                    XmlNode replacement = element.OwnerDocument.CreateElement("div");

                    foreach (XmlNode errorNode in element.ChildNodes)
                        replacement.AppendChild(errorNode);

                    element.ParentNode.InsertAfter(replacement, element);
                    element.ParentNode.RemoveChild(element);
                }
                else
                {
                    // If the user has permission, resolve the component and deal with default errors

                    XmlDocument snipitDocument = new XmlDocument();

                    // TODO:  GET Parameters

                    try
                    {
                        snipitDocument.LoadXml(string.Format(
                            "<div xmlns=\"{0}\" xmlns:oc=\"{1}\">{2}</div>",
                            templateParsingState.TemplateDocument.FirstChild.NamespaceURI,
                            templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace,
                            fileContainer.CastFileHandler<ITextHandler>().ReadAll()));

                        XmlNodeList replacementNodes = snipitDocument.FirstChild.ChildNodes;
                        templateParsingState.SetCWD(replacementNodes, fileContainer.ParentDirectoryHandler.FileContainer.FullPath);
                        templateParsingState.ReplaceNodes(element, replacementNodes);
                    }
                    catch (WebResultsOverrideException wroe)
                    {
                        templateParsingState.ReplaceNodes(
                            element,
                            templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString));
                    }
                    catch (Exception e)
                    {
                        log.Error("An error occured when loading a component", e);

                        templateParsingState.ReplaceNodes(
                            element,
                            templateParsingState.GenerateWarningNode("An unhandled error occured.  See the system logs for more information"));
                    }
                }
            }
        }
        private void LoadComponent(
            ITemplateParsingState templateParsingState,
            IDictionary<string, object> getParameters,
            XmlElement element)
        {
            XmlAttribute srcAttribute = (XmlAttribute)element.Attributes.GetNamedItem("src", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace);
            XmlAttribute urlAttribute = (XmlAttribute)element.Attributes.GetNamedItem("url", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace);

            // handle GET parameters
            // First, handle oc:getpassthrough
            IDictionary<string, object> myGetParameters;
            XmlAttribute getpassthroughAttribute = (XmlAttribute)element.Attributes.GetNamedItem("getpassthough", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace);
            if (null == getpassthroughAttribute)
            {
                myGetParameters = DictionaryFunctions.Create<string, object>(getParameters);
                myGetParameters.Remove("Method");
                myGetParameters.Remove("Action");
            }
            else
            {
                if ("false" == getpassthroughAttribute.Value)
                    myGetParameters = new Dictionary<string, object>();
                else
                {
                    myGetParameters = DictionaryFunctions.Create<string, object>(getParameters);
                    myGetParameters.Remove("Method");
                    myGetParameters.Remove("Action");
                }
            }

            // Next, pull out get parameters from the tag
            foreach (XmlAttribute attribute in element.Attributes)
                if ("" == attribute.NamespaceURI)
                    myGetParameters[attribute.LocalName] = attribute.Value;

            if ((null == srcAttribute) && (null == urlAttribute))
                // Remove empty components
                element.ParentNode.RemoveChild(element);

            else if ((null != srcAttribute) && (null != urlAttribute))
                templateParsingState.ReplaceNodes(
                    element,
                    templateParsingState.GenerateWarningNode("Either oc:src or oc:url can be specified; you can not choose both: " + element.OuterXml));

            else if (null != srcAttribute)
            {
                string fileName = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath(
                    templateParsingState.GetCWD(element),
                    srcAttribute.Value);

                IFileContainer fileContainer;
                try
                {
                    fileContainer = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(fileName);
                }
                catch (FileDoesNotExist fdne)
                {
                    log.Warn("An attempt was made to use a non-existant file in a template", fdne);

                    templateParsingState.ReplaceNodes(
                        element,
                        templateParsingState.GenerateWarningNode(fileName + " not found"));

                    return;
                }

                templateParsingState.WebConnection.TouchedFiles.Add(fileContainer);

                // If the user doesn't have permission for the component and the component has something to use instead, use it
                if (null == fileContainer.LoadPermission(templateParsingState.WebConnection.Session.User.Id) && element.HasChildNodes)
                {
                    XmlNode replacement = element.OwnerDocument.CreateElement("div");

                    foreach (XmlNode errorNode in element.ChildNodes)
                        replacement.AppendChild(errorNode);

                    element.ParentNode.InsertAfter(replacement, element);
                    element.ParentNode.RemoveChild(element);
                }
                else
                {
                    // If the user has permission, resolve the component and deal with default errors

                    XmlDocument componentDocument;

                    // TODO:  GET Parameters

                    try
                    {
                        componentDocument = templateParsingState.LoadXmlDocumentAndReplaceGetParameters(
                            myGetParameters,
                            fileContainer,
                            templateParsingState.GetXmlParseMode(element));

                        XmlNode firstChild = componentDocument.FirstChild;
                        XmlNodeList replacementNodes;
                        if ((firstChild.LocalName == "componentdef") && (firstChild.NamespaceURI == templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace))
                            replacementNodes = firstChild.ChildNodes;
                        else
                            replacementNodes = componentDocument.ChildNodes;

                        templateParsingState.SetCWD(replacementNodes, fileContainer.ParentDirectoryHandler.FileContainer.FullPath);
                        templateParsingState.ReplaceNodes(element, replacementNodes);
                    }
                    catch (WebResultsOverrideException wroe)
                    {
                        templateParsingState.ReplaceNodes(
                            element,
                            templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString));
                    }
                    catch (Exception e)
                    {
                        log.Error("An error occured when loading a component", e);

                        templateParsingState.ReplaceNodes(
                            element,
                            templateParsingState.GenerateWarningNode("An unhandled error occured.  See the system logs for more information"));
                    }
                }
            }

            else if (null != urlAttribute)
            {
                XmlNode resultNode;
                string url = urlAttribute.Value;

                foreach (KeyValuePair<string, object> getParameter in myGetParameters)
                    url = HTTPStringFunctions.AppendGetParameter(url, getParameter.Key, getParameter.Value.ToString());

                try
                {
                    if (url.StartsWith("https://"))
                        resultNode = templateParsingState.GenerateWarningNode("https component nodes aren't supported due to certificate complexities");
                    else if (url.StartsWith("http://"))
                    {
                        HttpResponseHandler httpResponse = templateParsingState.WebConnection.Session.HttpWebClient.Get(url);

                        if ("text/xml" == httpResponse.ContentType)
                            resultNode =
                                templateParsingState.LoadXmlDocument("<html xmlns=\"" + templateParsingState.TemplateHandlerLocator.TemplatingConstants.HtmlNamespace + "\"><head></head><body>" + httpResponse.AsString() + "</body></html>", XmlParseMode.Xml, templateParsingState.GetCWD(element)).FirstChild.ChildNodes[1];
                        else
                            resultNode = element.OwnerDocument.CreateTextNode(httpResponse.AsString());
                    }
                    else
                        try
                        {
                            ShellWebConnection shellWebConnection = new BlockingShellWebConnection(
                                url,
                                templateParsingState.WebConnection,
                                null,
                                templateParsingState.WebConnection.CookiesFromBrowser);

                            IWebResults shellResults = shellWebConnection.GenerateResultsForClient();

                            if ("text/xml" == shellResults.ContentType)
                                resultNode =
                                    templateParsingState.LoadXmlDocument("<html xmlns=\"" + templateParsingState.TemplateHandlerLocator.TemplatingConstants.HtmlNamespace + "\"><head></head><body>" + shellResults.ResultsAsString + "</body></html>", XmlParseMode.Xml, templateParsingState.GetCWD(
                                    element)).FirstChild.ChildNodes[1];
                            else
                                resultNode = element.OwnerDocument.CreateTextNode(shellResults.ResultsAsString);
                        }
                        catch (WebResultsOverrideException wroe)
                        {
                            resultNode = templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString);
                        }
                }
                catch (Exception e)
                {
                    log.Error("An error occured when loading a component", e);
                    resultNode = templateParsingState.GenerateWarningNode("An unhandled error occured.  See the system logs for more information");
                }

                templateParsingState.ReplaceNodes(element, resultNode);
            }
        }