Example #1
0
        /**
         * This method passes encountered text to the pipeline via the
         * {@link Pipeline#content(WorkerContext, Tag, byte[], ProcessObject)}
         * method.
         */
        virtual public void Text(String text)
        {
            if (text.StartsWith("<![CDATA[") && text.EndsWith("]]>"))
            {
                if (IgnoreCdata())
                {
                    return;
                }
                else
                {
                    text = text.Substring(9, text.Length - 12);
                }
            }
            IWorkerContext ctx = GetLocalWC();

            if (null != ctx.GetCurrentTag())
            {
                if (text.Length > 0)
                {
                    IPipeline     wp = rootpPipe;
                    ProcessObject po = new ProcessObject();
                    try {
                        while (null != (wp = wp.Content(ctx, ctx.GetCurrentTag(), text, po)))
                        {
                            ;
                        }
                    } catch (PipelineException e) {
                        throw new RuntimeWorkerException(e);
                    }
                }
            }
        }
Example #2
0
 /*
  * (non-Javadoc)
  *
  * @see
  * com.itextpdf.tool.xml.pipeline.IPipeline#open(com.itextpdf.tool.
  * xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
  */
 public override IPipeline Open(IWorkerContext context, Tag t, ProcessObject po) {
     HtmlPipelineContext hcc = (HtmlPipelineContext)GetLocalContext(context);
     try {
         ITagProcessor tp = hcc.ResolveProcessor(t.Name, t.NameSpace);
         if (tp.IsStackOwner()) {
             hcc.AddFirst(new StackKeeper(t));
         }
         IList<IElement> content = tp.StartElement(context, t);
         if (content.Count > 0) {
             if (tp.IsStackOwner()) {
                 StackKeeper peek;
                 try {
                     peek = hcc.Peek();
                     foreach (IElement elem in content) {
                         peek.Add(elem);
                     }
                 } catch (NoStackException e) {
                     throw new PipelineException(String.Format(LocaleMessages.STACK_404, t.ToString()), e);
                 }
             } else {
                 foreach (IElement elem in content) {
                     hcc.CurrentContent().Add(elem);
                 }
             }
         }
     } catch (NoTagProcessorException e) {
         if (!hcc.AcceptUnknown()) {
             throw e;
         }
     }
     return GetNext();
 }
 /**
  * Called in <code>open</code>, <code>content</code> and <code>close</code> to pass the {@link Writable}s to the handler
  * @param po
  */
 private void Consume(IWorkerContext context, ProcessObject po) {
     if (po.ContainsWritable()) {
         IWritable w = null;
         while ( null != (w =po.Poll())) {
             handler.Add(w);
         }
     }
 }
        public void SetUp() {
            lst = new List<IWritable>();
            IElementHandler elemH = new CustomElementHandler();

            p = new ElementHandlerPipeline(elemH, null);
            po = new ProcessObject();
            writable = new WritableElement(new Chunk("aaaaa"));
            po.Add(writable);
            context = new WorkerContextImpl();
            p.Init(context);
        }
Example #5
0
        /**
         * This method passes encountered text to the pipeline via the
         * {@link Pipeline#content(WorkerContext, Tag, byte[], ProcessObject)}
         * method.
         */
        virtual public void Text(String text)
        {
            IWorkerContext ctx = GetLocalWC();

            if (null != ctx.GetCurrentTag())
            {
                if (text.Length > 0)
                {
                    IPipeline     wp = rootpPipe;
                    ProcessObject po = new ProcessObject();
                    try {
                        while (null != (wp = wp.Content(ctx, ctx.GetCurrentTag(), text, po)))
                        {
                            ;
                        }
                    } catch (PipelineException e) {
                        throw new RuntimeWorkerException(e);
                    }
                }
            }
        }
Example #6
0
        public virtual void StartElement(String tag, IDictionary <String, String> attr, String ns)
        {
            Tag t = CreateTag(tag, attr, ns);
            WorkerContextImpl ctx = (WorkerContextImpl)GetLocalWC();

            if (null != ctx.GetCurrentTag())
            {
                ctx.GetCurrentTag().AddChild(t);
            }
            ctx.SetCurrentTag(t);
            IPipeline     wp = rootpPipe;
            ProcessObject po = new ProcessObject();

            try {
                while (null != (wp = wp.Open(ctx, t, po)))
                {
                    ;
                }
            } catch (PipelineException e) {
                throw new RuntimeWorkerException(e);
            }
        }
Example #7
0
        /**
         * Called when an ending tag is encountered by the {@link SimpleXMLParser}.
         * This method searches for the tags {@link ITagProcessor} in the given
         * {@link TagProcessorFactory}. If none found and acceptUknown is false a
         * {@link NoTagProcessorException} is thrown. If found the TagProcessors
         * endElement is called.<br />
         * The returned IElement by the ITagProcessor is added to the currentContent
         * stack.<br />
         * If any of the parent tags or the given tags
         * {@link ITagProcessor#isStackOwner()} is true. The returned IElement is put
         * on the respective stack.Else it element is added to the document or the
         * elementList.
         *
         */
        public virtual void EndElement(String tag, String ns)
        {
            String thetag = null;

            if (parseHtml)
            {
                thetag = tag.ToLowerInvariant();
            }
            else
            {
                thetag = tag;
            }
            IWorkerContext ctx = GetLocalWC();

            if (null != ctx.GetCurrentTag() && !thetag.Equals(ctx.GetCurrentTag().Name))
            {
                throw new RuntimeWorkerException(String.Format(
                                                     LocaleMessages.GetInstance().GetMessage(LocaleMessages.INVALID_NESTED_TAG), thetag,
                                                     ctx.GetCurrentTag().Name));
            }
            IPipeline     wp = rootpPipe;
            ProcessObject po = new ProcessObject();

            try {
                while (null != (wp = wp.Close(ctx, ctx.GetCurrentTag(), po)))
                {
                    ;
                }
            } catch (PipelineException e) {
                throw new RuntimeWorkerException(e);
            } finally {
                if (null != ctx.GetCurrentTag())
                {
                    ctx.SetCurrentTag(ctx.GetCurrentTag().Parent);
                }
            }
        }
Example #8
0
 public IPipeline Content(IWorkerContext context, Tag t, String content, ProcessObject po) {
     called = true;
     return null;
 }
Example #9
0
 /**
  * This method passes encountered text to the pipeline via the
  * {@link Pipeline#content(WorkerContext, Tag, byte[], ProcessObject)}
  * method.
  */
 virtual public void Text(String text) {
     if (text.StartsWith("<![CDATA[") && text.EndsWith("]]>"))
     {
         if (IgnoreCdata())
             return;
         else
             text = text.Substring(9, text.Length - 12);
     }
     IWorkerContext ctx = GetLocalWC();
     if (null != ctx.GetCurrentTag()) {
         if (text.Length > 0) {
             IPipeline wp = rootpPipe;
             ProcessObject po = new ProcessObject();
             try {
                 while (null != (wp = wp.Content(ctx, ctx.GetCurrentTag(), text, po)));
             } catch (PipelineException e) {
                 throw new RuntimeWorkerException(e);
             }
         }
     }
 }
Example #10
0
 /**
  * This method passes encountered text to the pipeline via the
  * {@link Pipeline#content(WorkerContext, Tag, byte[], ProcessObject)}
  * method.
  */
 virtual public void Text(String text) {
     IWorkerContext ctx = GetLocalWC();
     if (null != ctx.GetCurrentTag()) {
         if (text.Length > 0) {
             IPipeline wp = rootpPipe;
             ProcessObject po = new ProcessObject();
             try {
                 while (null != (wp = wp.Content(ctx, ctx.GetCurrentTag(), text, po)));
             } catch (PipelineException e) {
                 throw new RuntimeWorkerException(e);
             }
         }
     }
 }
Example #11
0
        /*
         * (non-Javadoc)
         *
         * @see
         * com.itextpdf.tool.xml.pipeline.AbstractPipeline#open(com.itextpdf.tool
         * .xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
         */
        public override IPipeline Open(IWorkerContext context, Tag t, ProcessObject po)
        {
            try {
                String tagName = t.Name;
                if (tag.Equals(tagName)) {
                    MapContext cc;
                    cc = (MapContext) context.Get(typeof(PdfWriterPipeline).FullName);
                    Document d = new Document(pagesize);
                    try {
                        Stream os = fm.GetStream();
                        cc[PdfWriterPipeline.DOCUMENT] = d;
                        cc[PdfWriterPipeline.WRITER] = PdfWriter.GetInstance(d, os);
                    } catch (IOException e) {
                        throw new PipelineException(e);
                    } catch (DocumentException e) {
                        throw new PipelineException(e);
                    }

                }
                if (Util.EqualsIgnoreCase(t.Name, opentag)) {
                    MapContext cc;
                    cc = (MapContext) context.Get(typeof(PdfWriterPipeline).FullName);
                    Document d = (Document) cc[PdfWriterPipeline.DOCUMENT];
                    CssUtils cssUtils = CssUtils.GetInstance();
                    float pageWidth = d.PageSize.Width;
                    float marginLeft = 0;
                    float marginRight = 0;
                    float marginTop = 0;
                    float marginBottom = 0;
                    IDictionary<String, String> css = t.CSS;
                    foreach (KeyValuePair<String, String> entry in css) {
                        String key = entry.Key;
                        String value = entry.Value;
                        if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_LEFT)) {
                            marginLeft = cssUtils.ParseValueToPt(value, pageWidth);
                        } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_RIGHT)) {
                            marginRight = cssUtils.ParseValueToPt(value, pageWidth);
                        } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) {
                            marginTop = cssUtils.ParseValueToPt(value, pageWidth);
                        } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) {
                            marginBottom = cssUtils.ParseValueToPt(value, pageWidth);
                        }
                    }
                    d.SetMargins(marginLeft, marginRight, marginTop, marginBottom);
                    d.Open();

                }
            } catch (NoCustomContextException e) {
                throw new PipelineException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.PIPELINE_AUTODOC), e);
            }

            return GetNext();
        }
Example #12
0
 /**
  * @param po
  * @throws PipelineException
  */
 private void Write(IWorkerContext context, ProcessObject po) {
     MapContext mp = (MapContext)GetLocalContext(context);
     if (po.ContainsWritable()) {
         Document doc = (Document) mp[DOCUMENT];
         bool continuousWrite = (bool) mp[CONTINUOUS];
         IWritable writable = null;
         while (null != (writable = po.Poll())) {
             if (writable is WritableElement) {
                 foreach (IElement e in ((WritableElement) writable).Elements()) {
                     try {
                         if (!doc.Add(e)) {
                             LOG.Trace(String.Format(
                                     LocaleMessages.GetInstance().GetMessage(LocaleMessages.ELEMENT_NOT_ADDED),
                                     e.ToString()));
                         }
                     } catch (DocumentException e1) {
                         if (!continuousWrite) {
                             throw new PipelineException(e1);
                         } else {
                             LOG.Error(
                                     LocaleMessages.GetInstance().GetMessage(LocaleMessages.ELEMENT_NOT_ADDED_EXC),
                                     e1);
                         }
                     }
                 }
             }
         }
     }
 }
Example #13
0
 virtual public IPipeline Open(IWorkerContext context, Tag t, ProcessObject po) {
     called = true;
     return null;
 }
Example #14
0
        /*
         * (non-Javadoc)
         *
         * @see
         * com.itextpdf.tool.xml.pipeline.IPipeline#close(com.itextpdf.tool
         * .xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
         */
        public override IPipeline Close(IWorkerContext context, Tag t, ProcessObject po) {
            HtmlPipelineContext hcc = (HtmlPipelineContext)GetLocalContext(context);
            ITagProcessor tp;
            try {
                if (t.LastMarginBottom != null) {
                    hcc.GetMemory()[HtmlPipelineContext.LAST_MARGIN_BOTTOM] = t.LastMarginBottom;
                } else {
                    hcc.GetMemory().Remove(HtmlPipelineContext.LAST_MARGIN_BOTTOM);
                }
                tp = hcc.ResolveProcessor(t.Name, t.NameSpace);
                IList<IElement> elems = null;
                if (tp.IsStackOwner()) {
                    // remove the element from the StackKeeper Queue if end tag is
                    // found
                    StackKeeper tagStack;
                    try {
                        tagStack = hcc.Poll();
                    } catch (NoStackException e) {
                        throw new PipelineException(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.STACK_404), t.ToString()), e);
                    }
                    elems = tp.EndElement(context, t,  tagStack.GetElements());
                } else {
                    elems = tp.EndElement(context, t, hcc.CurrentContent());
                    hcc.CurrentContent().Clear();
                }
                if (elems.Count > 0) {
                    try {
                        StackKeeper stack = hcc.Peek();
                        foreach (IElement elem in elems) {
                            stack.Add(elem);
                        }
                    } catch (NoStackException) {
                        WritableElement writableElement = new WritableElement();
                            po.Add(writableElement);
                            writableElement.AddAll(elems);
                    }

                }
            } catch (NoTagProcessorException e) {
                if (!hcc.AcceptUnknown()) {
                    throw e;
                }
            }
            return GetNext();
        }
Example #15
0
 /*
  * (non-Javadoc)
  *
  * @see com.itextpdf.tool.xml.pipeline.IPipeline#content(com.itextpdf.tool
  * .xml.Tag, java.lang.String, com.itextpdf.tool.xml.pipeline.ProcessObject)
  */
 public override IPipeline Content(IWorkerContext context, Tag t, string text, ProcessObject po) {
     HtmlPipelineContext hcc = (HtmlPipelineContext)GetLocalContext(context);
     ITagProcessor tp;
     try {
         tp = hcc.ResolveProcessor(t.Name, t.NameSpace);
         //String ctn = null;
         //if (null != hcc.CharSet()) {
         //    ctn = hcc.CharSet().GetString(b);
         //} else {
         //    ctn = Encoding.Default.GetString(b);
         //}
         IList<IElement> elems = tp.Content(context, t, text);
         if (elems.Count > 0) {
             StackKeeper peek;
             try {
                 peek = hcc.Peek();
                 foreach (IElement e in elems) {
                     peek.Add(e);
                 }
             } catch (NoStackException) {
                 WritableElement writableElement = new WritableElement();
                 foreach (IElement elem in elems) {
                     writableElement.Add(elem);
                 }
                 po.Add(writableElement);
             }
         }
     } catch (NoTagProcessorException e) {
         if (!hcc.AcceptUnknown()) {
             throw e;
         }
     }
     return GetNext();
 }
Example #16
0
 public IPipeline Close(IWorkerContext context, Tag t, ProcessObject po) {
     called = true;
     return null;
 }
Example #17
0
 /*
  * (non-Javadoc)
  *
  * @see
  * com.itextpdf.tool.xml.pipeline.AbstractPipeline#close(com.itextpdf.tool
  * .xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
  */
 public override IPipeline Close(IWorkerContext context, Tag t, ProcessObject po)
 {
     String tagName = t.Name;
     if (tag.Equals(tagName)) {
         MapContext cc;
         try {
             cc = (MapContext) context.Get(typeof(PdfWriterPipeline).FullName);
             Document d = (Document) cc[PdfWriterPipeline.DOCUMENT];
             d.Close();
         } catch (NoCustomContextException e) {
             throw new PipelineException("AutoDocPipeline depends on PdfWriterPipeline.", e);
         }
         try {
             HtmlPipelineContext hpc = (HtmlPipelineContext) context.Get(typeof(HtmlPipeline).FullName);
             HtmlPipelineContext clone = (HtmlPipelineContext)hpc.Clone();
             clone.SetPageSize(pagesize);
             ((WorkerContextImpl)context).Put(typeof(HtmlPipeline).FullName, clone);
         } catch (NoCustomContextException) {
         }
     }
     return GetNext();
 }
Example #18
0
 /*
  * (non-Javadoc)
  *
  * @see com.itextpdf.tool.xml.pipeline.IPipeline#close(com.itextpdf.tool
  * .xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
  */
 public override IPipeline Close(IWorkerContext context, Tag t, ProcessObject po) {
     Write(context, po);
     return GetNext();
 }
Example #19
0
 /**
  * Called when an ending tag is encountered by the {@link SimpleXMLParser}.
  * This method searches for the tags {@link ITagProcessor} in the given
  * {@link TagProcessorFactory}. If none found and acceptUknown is false a
  * {@link NoTagProcessorException} is thrown. If found the TagProcessors
  * endElement is called.<br />
  * The returned IElement by the ITagProcessor is added to the currentContent
  * stack.<br />
  * If any of the parent tags or the given tags
  * {@link ITagProcessor#isStackOwner()} is true. The returned IElement is put
  * on the respective stack.Else it element is added to the document or the
  * elementList.
  *
  */
 public virtual void EndElement(String tag, String ns) {
     String thetag = null;
     if (parseHtml) {
         thetag = tag.ToLowerInvariant();
     } else {
         thetag = tag;
     }
     IWorkerContext ctx = GetLocalWC();
     if (null != ctx.GetCurrentTag() && !thetag.Equals(ctx.GetCurrentTag().Name)) {
         throw new RuntimeWorkerException(String.Format(
                 LocaleMessages.GetInstance().GetMessage(LocaleMessages.INVALID_NESTED_TAG), thetag,
                 ctx.GetCurrentTag().Name));
     }
     IPipeline wp = rootpPipe;
     ProcessObject po = new ProcessObject();
     try {
         while (null != (wp = wp.Close(ctx, ctx.GetCurrentTag(), po)))
             ;
     } catch (PipelineException e) {
         throw new RuntimeWorkerException(e);
     } finally {
         if (null != ctx.GetCurrentTag())
             ctx.SetCurrentTag(ctx.GetCurrentTag().Parent);
     }
 }
 /* (non-Javadoc)
  * @see com.itextpdf.tool.xml.pipeline.AbstractPipeline#content(com.itextpdf.tool.xml.Tag, java.lang.String, com.itextpdf.tool.xml.pipeline.ProcessObject)
  */
 public override IPipeline Content(IWorkerContext context, Tag t, string text, ProcessObject po) {
     Consume(context, po);
     return GetNext();
 }
Example #21
0
        public virtual void StartElement(String tag, IDictionary<String, String> attr, String ns) {
            Tag t = CreateTag(tag, attr, ns);
            WorkerContextImpl ctx = (WorkerContextImpl)GetLocalWC();
            if (null != ctx.GetCurrentTag()) {
                ctx.GetCurrentTag().AddChild(t);
            }
            ctx.SetCurrentTag(t);
            IPipeline wp = rootpPipe;
            ProcessObject po = new ProcessObject();
            try {
                while (null != (wp = wp.Open(ctx, t, po)))
                    ;
            } catch (PipelineException e) {
                throw new RuntimeWorkerException(e);
            }

        }
Example #22
0
 /*
  * (non-Javadoc)
  *
  * @see
  * com.itextpdf.tool.xml.pipeline.IPipeline#open(com.itextpdf.tool.
  * xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
  */
 public override IPipeline Open(IWorkerContext context, Tag t, ProcessObject po) {
     HtmlPipelineContext hcc = (HtmlPipelineContext)GetLocalContext(context);
     try {
         Object lastMarginBottom = null;
         if (hcc.GetMemory().TryGetValue(HtmlPipelineContext.LAST_MARGIN_BOTTOM, out lastMarginBottom)) {
             t.LastMarginBottom = lastMarginBottom;
             hcc.GetMemory().Remove(HtmlPipelineContext.LAST_MARGIN_BOTTOM);
         }
         ITagProcessor tp = hcc.ResolveProcessor(t.Name, t.NameSpace);
         AddStackKeeper(t, hcc, tp);
         IList<IElement> content = tp.StartElement(context, t);
         if (content.Count > 0) {
             if (tp.IsStackOwner()) {
                 StackKeeper peek = hcc.Peek();
                 if (peek == null)
                     throw new PipelineException(String.Format(LocaleMessages.STACK_404, t.ToString()));
                 foreach (IElement elem in content) {
                     peek.Add(elem);
                 }
             } else {
                 foreach (IElement elem in content) {
                     hcc.CurrentContent().Add(elem);
                     if (elem.Type == Element.BODY)
                     {
                         WritableElement writableElement = new WritableElement();
                         writableElement.Add(elem);
                         po.Add(writableElement);
                         hcc.CurrentContent().Remove(elem);
                     }
                 }
             }
         }
     } catch (NoTagProcessorException e) {
         if (!hcc.AcceptUnknown()) {
             throw e;
         }
     }
     return GetNext();
 }