Ejemplo n.º 1
0
        /// <summary>
        /// This method deals with the starting tags.
        /// </summary>
        /// <param name="name">the name of the tag</param>
        /// <param name="attributes">the list of attributes</param>
        public void handleStartingTags(String name, Properties attributes)
        {
            //System.err.println("Start: " + name);
            if (ignore || ElementTags.IGNORE.Equals(name))
            {
                ignore = true;
                return;
            }

            // maybe there is some meaningful data that wasn't between tags
            if (currentChunk != null)
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                }
                catch {
                    current = new Paragraph();
                }
                current.Add(currentChunk);
                stack.Push(current);
                currentChunk = null;
            }

            // registerfont
            if (name.Equals("registerfont"))
            {
                FontFactory.register(attributes);
            }

            // header
            if (ElementTags.HEADER.Equals(name))
            {
                stack.Push(new HeaderFooter(attributes));
                return;
            }

            // footer
            if (ElementTags.FOOTER.Equals(name))
            {
                stack.Push(new HeaderFooter(attributes));
                return;
            }

            // before
            if (name.Equals("before"))
            {
                HeaderFooter tmp = (HeaderFooter)stack.Pop();

                tmp.Before = new Phrase(attributes);
                stack.Push(tmp);
                return;
            }

            // after
            if (name.Equals("after"))
            {
                HeaderFooter tmp = (HeaderFooter)stack.Pop();

                tmp.After = new Phrase(attributes);
                stack.Push(tmp);
                return;
            }

            // chunks
            if (Chunk.isTag(name))
            {
                currentChunk = new Chunk(attributes);
                return;
            }

            // symbols
            if (Entities.isTag(name))
            {
                Font f = new Font();
                if (currentChunk != null)
                {
                    handleEndingTags(ElementTags.CHUNK);
                    f = currentChunk.Font;
                }
                currentChunk = Entities.get(attributes[ElementTags.ID], f);
                return;
            }

            // phrases
            if (Phrase.isTag(name))
            {
                stack.Push(new Phrase(attributes));
                return;
            }

            // anchors
            if (Anchor.isTag(name))
            {
                stack.Push(new Anchor(attributes));
                return;
            }

            // paragraphs and titles
            if (Paragraph.isTag(name) || Section.isTitle(name))
            {
                stack.Push(new Paragraph(attributes));
                return;
            }

            // lists
            if (List.isTag(name))
            {
                stack.Push(new List(attributes));
                return;
            }

            // listitems
            if (ListItem.isTag(name))
            {
                stack.Push(new ListItem(attributes));
                return;
            }

            // cells
            if (Cell.isTag(name))
            {
                stack.Push(new Cell(attributes));
                return;
            }

            // tables
            if (Table.isTag(name))
            {
                Table   table  = new Table(attributes);
                float[] widths = table.ProportionalWidths;
                for (int i = 0; i < widths.Length; i++)
                {
                    if (widths[i] == 0)
                    {
                        widths[i] = 100.0f / (float)widths.Length;
                    }
                }
                try {
                    table.Widths = widths;
                }
                catch (BadElementException bee) {
                    // this shouldn't happen
                    throw new Exception("", bee);
                }
                stack.Push(table);
                return;
            }

            // sections
            if (Section.isTag(name))
            {
                IElement previous = (IElement)stack.Pop();
                Section  section;
                try {
                    section = ((Section)previous).addSection(attributes);
                }
                catch (Exception cce) {
                    throw cce;
                }
                stack.Push(previous);
                stack.Push(section);
                return;
            }

            // chapters
            if (Chapter.isTag(name))
            {
                String value;                 // changed after a suggestion by Serge S. Vasiljev
                if ((value = (String)attributes.Remove(ElementTags.NUMBER)) != null)
                {
                    chapters = int.Parse(value);
                }
                else
                {
                    chapters++;
                }
                Chapter chapter = new Chapter(attributes, chapters);
                stack.Push(chapter);
                return;
            }

            // images
            if (Image.isTag(name))
            {
                try {
                    Image  img = Image.getInstance(attributes);
                    Object current;
                    try {
                        // if there is an element on the stack...
                        current = stack.Pop();
                        // ...and it's a Chapter or a Section, the Image can be added directly
                        if (current is Chapter || current is Section || current is Cell)
                        {
                            ((ITextElementArray)current).Add(img);
                            stack.Push(current);
                            return;
                        }
                        // ...if not, the Image is wrapped in a Chunk before it's added
                        else
                        {
                            Stack newStack = new Stack();
                            try {
                                while (!(current is Chapter || current is Section || current is Cell))
                                {
                                    newStack.Push(current);
                                    if (current is Anchor)
                                    {
                                        img.Annotation = new Annotation(0, 0, 0, 0, ((Anchor)current).Reference);
                                    }
                                    current = stack.Pop();
                                }
                                ((ITextElementArray)current).Add(img);
                                stack.Push(current);
                            }
                            catch {
                                document.Add(img);
                            }
                            while (!(newStack.Count == 0))
                            {
                                stack.Push(newStack.Pop());
                            }
                            return;
                        }
                    }
                    catch {
                        // if there is no element on the stack, the Image is added to the document
                        try {
                            document.Add(img);
                        }
                        catch (DocumentException de) {
                            throw new Exception("", de);
                        }
                        return;
                    }
                }
                catch (Exception e) {
                    throw new Exception("", e);
                }
            }

            // annotations
            if (Annotation.isTag(name))
            {
                Annotation        annotation = new Annotation(attributes);
                ITextElementArray current;
                try {
                    try {
                        current = (ITextElementArray)stack.Pop();
                        try {
                            current.Add(annotation);
                        }
                        catch {
                            document.Add(annotation);
                        }
                        stack.Push(current);
                    }
                    catch {
                        document.Add(annotation);
                    }
                    return;
                }
                catch (DocumentException de) {
                    throw new Exception("", de);
                }
            }

            // newlines
            if (isNewline(name))
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                    current.Add(Chunk.NEWLINE);
                    stack.Push(current);
                }
                catch {
                    if (currentChunk == null)
                    {
                        try {
                            document.Add(Chunk.NEWLINE);
                        }
                        catch (DocumentException de) {
                            throw new Exception("", de);
                        }
                    }
                    else
                    {
                        currentChunk.append("\n");
                    }
                }
                return;
            }

            // newpage
            if (isNewpage(name))
            {
                ITextElementArray current;
                try {
                    current = (ITextElementArray)stack.Pop();
                    Chunk newPage = new Chunk("");
                    newPage.setNewPage();
                    current.Add(newPage);
                    stack.Push(current);
                }
                catch {
                    try {
                        document.newPage();
                    }
                    catch (DocumentException de) {
                        throw new Exception("", de);
                    }
                }
                return;
            }

            // newpage
            if (ElementTags.HORIZONTALRULE.Equals(name))
            {
                ITextElementArray current;
                Graphic           hr = new Graphic();
                hr.setHorizontalLine(1.0f, 100.0f);
                try {
                    current = (ITextElementArray)stack.Pop();
                    current.Add(hr);
                    stack.Push(current);
                }
                catch {
                    try {
                        document.Add(hr);
                    }
                    catch (DocumentException de) {
                        throw new Exception("", de);
                    }
                }
                return;
            }

            // documentroot
            if (isDocumentRoot(name))
            {
                String value;
                foreach (string key in attributes.Keys)
                {
                    value = attributes[key];
                    try {
                        document.Add(new Meta(key, value));
                    }
                    catch (DocumentException de) {
                        throw new Exception("", de);
                    }
                }
                if (controlOpenClose)
                {
                    document.Open();
                }
            }
        }