Background Color:
 
Background Pattern:
Reset
Search
Home Recent Changes Show All Pages

An Introduction to the XML Parser

Not Rated Yet

The Scryber XML Parser is a dynamic parser. This means that it has no knowledge of the components it instantiates and populates, it only understands declarative namespaces, elements and attributes.

If we inspect the code for the PDFPanel class we see the following declaration...

    [PDFParsableComponent("Panel")]
    public class PDFPanel : PDFVisualComponent, IPDFViewPortComponent
    {
        private PDFVisualComponentList _content;

        /// 
        /// Gets the content collection of Components in this panel
        /// 
        [PDFArray(typeof(PDFVisualComponent))]
        [PDFElement("Content")]
        public virtual PDFVisualComponentList Contents
        {
            get
            {
                if (null == _content)
                {
                    _content = new PDFVisualComponentList(this.InnerContent);
                }
                return _content;
            }
        }
       
        //Rest of file content

    }

We can see the PDFParsableComponent attribute on the class definition with a name "Panel" and an inner property called "Contents" that is of type PDFVisualComponentList.

Parsing XML file content

If we have a look at a simple document a below, we can see the panel within the page content, and this also has a label within it.
<?xml version="1.0" encoding="utf-8" ?>
<pdf:Document xmlns:pdf="Scryber.Components, Scryber.Components, Version=0.8.0.0, Culture=neutral, PublicKeyToken=872cbeb81db952fe"
              xmlns:styles="Scryber.Styles, Scryber.Styles, Version=0.8.0.0, Culture=neutral, PublicKeyToken=872cbeb81db952fe" >
  <Pages>
    <pdf:Page id="FirstPage" name="UniqueName" 
              outline-title="Outline" visible="true" 
              styles:class="page document" >
      <Content>
        <pdf:Panel >
          <Content>
            <pdf:Label text="This is my label" styles:font-family="Times" />
          </Content>
        </pdf:Panel>
      </Content>
    </pdf:Page>
  </Pages>
</pdf:Document>

When the parser reads the XML file and meets an element <pdf:Panel > it knows that it belongs to the pdf xmlns - which points the parser to the Scryber.Components namespace, in the the Scryber.Components assembly, and it is looking for a class decorated with a PDFParsableComponentAttribute of name "Panel"
Our Scryber.Components.PDFPanel matches, so it creates an instance of this type.
Once created it continues on from there and reads more nodes.

If the parser then meets an element called 'Content' it knows this is a match for our PDFElement attribute, and it also knows this contains a collection of PDFVisualComponents, based on the PDFArrayAttribute, so the parser will then continue on to the child contents parsing each in turn and adding them to the panels collection. In our case a single label

It is an explicit inclusion, rather than an implicit inclusion model.
Classes must be decorated with a PDFParsableAttribute in order to be read and parsed. If not, then the class will not be found, and an error raised by default.

Parsing attributes

On the label component are 2 attributes text and styles:font-family.
Taking a look at the class PDFLabel we can see the Text property declared

    [PDFParsableComponent("Label")]
    public class PDFLabel: PDFTextBase
    {
        
        [PDFAttribute("text")]
        [PDFElement("")]
        public string Text
        {
            get { return base.BaseText; }
            set { base.BaseText = value; }
        }

     //Rest of the class
    }

The property is decorated with both a PDFAttributeAttribute which allows the content of the label to be included in a text attribute on the XML file, but also an empty PDFElementAttribute, this means the textual content can also be included within the start and end tags.

Inherited properties

On the XML Label element there is also a font-family attribute within the styles namespace.
There is no attribute on the label with this decoration, but if we go up the class hierarchy we can see the PDFVisualComponent does have exactly this property

        /// 
        /// Gets or sets the Font Family of this component
        /// 
        [PDFAttribute("font-family", Const.PDFStylesNamespace)]
        public string FontFamily
        {
            get
            {
                PDFFontStyle b;
                if (this.HasStyle && this.Style.TryGetFont(out b))
                    return b.FontFamily;
                else
                    return string.Empty;
            }
            set
            {
                this.Style.Font.FontFamily = value;
            }
        }

The namespace overload allows explicit namespaces to be attributed to the parsed node, but if it is not defined then it is assumed to belong to the same namespace.

This means the Scryber XML parser is completely extensible. With a namespace declaration that refers to a .NET Namespace and Assembly the parser can derive the classes that can be parsed, and build an object graph of the content that can then be rendered

The next step is Writing your own components



  Rating
Rate This Page: Poor Great   |  Rate Content |
Average rating:  No Ratings Yet   
Number of Ratings : 0
  Comments
Add Comment
No Comments Yet