Ejemplo n.º 1
0
        /// <summary>Collects CSS declarationss.</summary>
        /// <param name="rootNode">the root node</param>
        /// <param name="resourceResolver">the resource resolver</param>
        /// <param name="cssContext">the CSS context</param>
        private void CollectCssDeclarations(INode rootNode, ResourceResolver resourceResolver, CssContext cssContext
                                            )
        {
            cssStyleSheet = new CssStyleSheet();
            LinkedList <INode> q = new LinkedList <INode>();

            q.Add(rootNode);
            while (!q.IsEmpty())
            {
                INode currentNode = q.JGetFirst();
                q.RemoveFirst();
                if (currentNode is IElementNode)
                {
                    IElementNode headChildElement = (IElementNode)currentNode;
                    if (TagConstants.STYLE.Equals(headChildElement.Name()))
                    {
                        if (currentNode.ChildNodes().Count > 0 && currentNode.ChildNodes()[0] is IDataNode)
                        {
                            String        styleData  = ((IDataNode)currentNode.ChildNodes()[0]).GetWholeData();
                            CssStyleSheet styleSheet = CssStyleSheetParser.Parse(styleData);
                            styleSheet = WrapStyleSheetInMediaQueryIfNecessary(headChildElement, styleSheet);
                            cssStyleSheet.AppendCssStyleSheet(styleSheet);
                        }
                    }
                    else
                    {
                        if (CssUtils.IsStyleSheetLink(headChildElement))
                        {
                            String styleSheetUri = headChildElement.GetAttribute(AttributeConstants.HREF);
                            try {
                                using (Stream stream = resourceResolver.RetrieveResourceAsInputStream(styleSheetUri)) {
                                    if (stream != null)
                                    {
                                        CssStyleSheet styleSheet = CssStyleSheetParser.Parse(stream, resourceResolver.ResolveAgainstBaseUri(styleSheetUri
                                                                                                                                            ).ToExternalForm());
                                        styleSheet = WrapStyleSheetInMediaQueryIfNecessary(headChildElement, styleSheet);
                                        cssStyleSheet.AppendCssStyleSheet(styleSheet);
                                    }
                                }
                            }
                            catch (Exception exc) {
                                ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Css.Resolve.DefaultCssResolver));
                                logger.Error(iText.Html2pdf.LogMessageConstant.UNABLE_TO_PROCESS_EXTERNAL_CSS_FILE, exc);
                            }
                        }
                    }
                }
                foreach (INode child in currentNode.ChildNodes())
                {
                    if (child is IElementNode)
                    {
                        q.Add(child);
                    }
                }
            }
            CheckIfPagesCounterMentioned(cssStyleSheet, cssContext);
        }
Ejemplo n.º 2
0
        private void CollectCssDeclarations(INode rootNode, ResourceResolver resourceResolver)
        {
            this.css = new CssStyleSheet();
            LinkedList <INode> q = new LinkedList <INode>();

            if (rootNode != null)
            {
                q.Add(rootNode);
            }
            while (!q.IsEmpty())
            {
                INode currentNode = q.JRemoveFirst();
                if (currentNode is IElementNode)
                {
                    IElementNode headChildElement = (IElementNode)currentNode;
                    if (SvgConstants.Tags.STYLE.Equals(headChildElement.Name()))
                    {
                        //XML parser will parse style tag contents as text nodes
                        if (!currentNode.ChildNodes().IsEmpty() && (currentNode.ChildNodes()[0] is IDataNode || currentNode.ChildNodes
                                                                        ()[0] is ITextNode))
                        {
                            String styleData;
                            if (currentNode.ChildNodes()[0] is IDataNode)
                            {
                                styleData = ((IDataNode)currentNode.ChildNodes()[0]).GetWholeData();
                            }
                            else
                            {
                                styleData = ((ITextNode)currentNode.ChildNodes()[0]).WholeText();
                            }
                            CssStyleSheet styleSheet = CssStyleSheetParser.Parse(styleData);
                            //TODO (DEVSIX-2263): media query wrap
                            //styleSheet = wrapStyleSheetInMediaQueryIfNecessary(headChildElement, styleSheet);
                            this.css.AppendCssStyleSheet(styleSheet);
                        }
                    }
                    else
                    {
                        if (SvgCssUtils.IsStyleSheetLink(headChildElement))
                        {
                            String styleSheetUri = headChildElement.GetAttribute(SvgConstants.Attributes.HREF);
                            try {
                                Stream        stream     = resourceResolver.RetrieveStyleSheet(styleSheetUri);
                                byte[]        bytes      = StreamUtil.InputStreamToArray(stream);
                                CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new MemoryStream(bytes), resourceResolver.ResolveAgainstBaseUri
                                                                                         (styleSheetUri).ToExternalForm());
                                this.css.AppendCssStyleSheet(styleSheet);
                            }
                            catch (System.IO.IOException exc) {
                                ILog logger = LogManager.GetLogger(typeof(iText.Svg.Css.Impl.SvgStyleResolver));
                                logger.Error(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_PROCESS_EXTERNAL_CSS_FILE, exc);
                            }
                        }
                    }
                }
                foreach (INode child in currentNode.ChildNodes())
                {
                    if (child is IElementNode)
                    {
                        q.Add(child);
                    }
                }
            }
        }