private void check(Cell c)
 {
     bool hasText = false;
     foreach (Piece p in c.pieces)
         if (!String.IsNullOrEmpty(p.getText()))
             hasText = true;
     check(hasText, "Title cells must have text");
 }
        private XElement renderCell(XElement tr, Cell c, String name, String icon, List<Boolean> indents, bool hasChildren, String anchor)
        {
            var tc = new XElement(XmlNs.XHTMLNS + name, new XAttribute("class", "heirarchy"));
            tr.Add(tc);

            if (indents != null)
            {
                var spacerImg = new XElement(XmlNs.XHTMLNS + "img",
                   new XAttribute("src", srcFor("tbl_spacer.png")),
                   new XAttribute("class", "heirarchy"),
                   new XAttribute("alt", "."));
                tc.Add(spacerImg);

                tc.Add(new XAttribute("style", "vertical-align: top; text-align : left; padding:0px 4px 0px 4px; white-space: nowrap; background-image: url("
                          + checkExists(indents, hasChildren) + ")"));

                for (int i = 0; i < indents.Count - 1; i++)
                {
                    if (indents[i])
                    {
                        var blankImg = new XElement(XmlNs.XHTMLNS + "img",
                            new XAttribute("src", srcFor("tbl_blank.png")),
                            new XAttribute("class", "heirarchy"),
                            new XAttribute("alt", "."));
                        tc.Add(blankImg);
                    }
                    else
                    {
                        var vlineImage = new XElement(XmlNs.XHTMLNS + "img",
                            new XAttribute("src", srcFor("tbl_vline.png")),
                            new XAttribute("class", "heirarchy"),
                            new XAttribute("alt", "."));
                        tc.Add(vlineImage);
                    }
                }

                if (indents.Any())
                {
                    if (indents[indents.Count - 1])
                    {
                        var vjoinEndImage = new XElement(XmlNs.XHTMLNS + "img",
                            new XAttribute("src", srcFor("tbl_vjoin_end.png")),
                            new XAttribute("class", "heirarchy"),
                            new XAttribute("alt", "."));
                        tc.Add(vjoinEndImage);
                    }
                    else
                    {
                        var vjoinImage = new XElement(XmlNs.XHTMLNS + "img",
                            new XAttribute("src", srcFor("tbl_vjoin.png")),
                            new XAttribute("class", "heirarchy"),
                            new XAttribute("alt", "."));
                        tc.Add(vjoinImage);
                    }
                }
            }
            else
            {
                string style = "vertical-align:top; text-align:left; padding:0px 4px 0px 4px";
                
                if (c is Title)
                {
                    var width = ((Title)c).width;

                    if (width != 0) style += "; width:" + width.ToString() + "px";
                }

                tc.Add(new XAttribute("style", style));
            }

            if (!String.IsNullOrEmpty(icon))
            {
                tc.Add(new XElement(XmlNs.XHTMLNS + "img",
                    new XAttribute("src", srcFor(icon)),
                    new XAttribute("class", "heirarchy"),
                    new XAttribute("style", "background-color: white;"),
                    new XAttribute("alt", ".")));
                //tc.addText(" ");
            }

            foreach (Piece p in c.pieces)
            {
                if (!String.IsNullOrEmpty(p.getTag()))
                {
                    var newTag = new XElement(XmlNs.XHTMLNS + p.getTag());
                    tc.Add(newTag);
                    addStyle(newTag, p);
                }
                else if (!String.IsNullOrEmpty(p.getReference()))
                {
                    var newTag = new XElement(XmlNs.XHTMLNS + "a");
                    tc.Add(newTag);
                    XElement a = addStyle(newTag, p);
                    a.Add(new XAttribute("href", p.getReference()));

                    if (!String.IsNullOrEmpty(p.getHint()))
                        a.Add(new XAttribute("title", p.getHint()));

                    a.Add(new XText(p.getText()));
                }
                else
                {
                    if (!String.IsNullOrEmpty(p.getHint()))
                    {
                        var span = new XElement(XmlNs.XHTMLNS + "span");
                        tc.Add(span);

                        XElement s = addStyle(span, p);
                        s.Add(new XAttribute("title", p.getHint()));
                        s.Add(new XText(p.getText()));
                    }
                    else if (p.getStyle() != null)
                    {
                        var span = new XElement(XmlNs.XHTMLNS + "span");
                        tc.Add(span);
                        XElement s = addStyle(span, p);
                        var text = p.getText() ?? "(empty?)";
                        s.Add(new XText(text));
                    }
                    else
                        tc.Add(new XText(p.getText()));
                }
            }
            if (!String.IsNullOrEmpty(anchor))
            {
                var a = new XElement(XmlNs.XHTMLNS + "a",
                            new XAttribute("name", nmTokenize(anchor)));
                // .addText(" ")
                tc.Add(a);
            }

            return tc;
        }
        private void genElement(HierarchicalTableGenerator gen, List<Row> rows, ElementNavigator nav, 
                    Profile profile, bool showMissing)
        {
            var element = nav.Current;

            if(onlyInformationIsMapping(nav.Structure.Element, element)) return;  // we don't even show it in this case

            Row row = new Row();
            row.setAnchor(element.Path);
            String s = element.GetNameFromPath();

            bool hasDef = element.Definition != null;
            bool ext = false;
    
            if (s == "extension" || s == "modifierExtension")
            { 
                row.setIcon("icon_extension_simple.png");
                ext = true;
            }
            else if (!hasDef || element.Definition.Type == null || element.Definition.Type.Count == 0)
            {
                row.setIcon("icon_element.gif");
            }
            else if (hasDef && element.Definition.Type.Count > 1)
            {
                if (allTypesAre(element.Definition.Type, "ResourceReference"))
                    row.setIcon("icon_reference.png");
                else
                    row.setIcon("icon_choice.gif");
            }
            else if (hasDef && element.Definition.Type[0].Code.StartsWith("@"))
            {
                //TODO: That's not a legal code, will this ever appear?
                //I am pretty sure this depends on ElementDefn.NameReference
                row.setIcon("icon_reuse.png");
            }
            else if (hasDef && _pkp.isPrimitive(element.Definition.Type[0].Code))
                row.setIcon("icon_primitive.png");
            else if (hasDef && _pkp.isReference(element.Definition.Type[0].Code))
                row.setIcon("icon_reference.png");
            else if (hasDef && _pkp.isDataType(element.Definition.Type[0].Code))
                row.setIcon("icon_datatype.gif");
            else
                row.setIcon("icon_resource.png");


            var reference = _pkp.GetLinkForElementDefinition(nav.Structure, profile, element);
            //String reference = defPath == null ? null : defPath + makePathLink(element);
            UnusedTracker used = new UnusedTracker();
            used.used = true;
            
            Cell left = new Cell(null, reference, s, !hasDef ? null : element.Definition.Formal, null);
            row.getCells().Add(left);
    
            if (ext)
            {
                // If this element (row) in the table is an extension...
                if (element.Definition != null && element.Definition.Type.Count == 1 && element.Definition.Type[0].Profile != null) 
                {
                    Profile.ProfileExtensionDefnComponent extDefn = _pkp.getExtensionDefinition(profile, element.Definition.Type[0].Profile);
        
                    if (extDefn == null) 
                    {
                        row.getCells().Add(new Cell(null, null, !hasDef ? null : describeCardinality(element.Definition, null, used), null, null));
                        row.getCells().Add(new Cell(null, null, "?? "+element.Definition.Type[0].Profile, null, null));
                        generateDescription(gen, row, element, null, used.used, element.Definition.Type[0].Profile, profile);
                    }
                    else 
                    {
                        row.getCells().Add(new Cell(null, null, !hasDef ? null : describeCardinality(element.Definition, extDefn.Definition, used), null, null));
                        genTypes(gen, row, extDefn.Definition, profile);
                        generateDescription(gen, row, element, extDefn.Definition, used.used, element.Definition.Type[0].Profile, profile);
                    } 
                }
                else if (element.Definition != null) 
                {
                    row.getCells().Add(new Cell(null, null, !hasDef ? null : describeCardinality(element.Definition, null, used), null, null));
                    genTypes(gen, row, element.Definition, profile);
                    generateDescription(gen, row, element, null, used.used, null, profile);
                } 
                else 
                {
                    row.getCells().Add(new Cell(null, null, !hasDef ? null : describeCardinality(element.Definition, null, used), null, null));
                    row.getCells().Add(new Cell());
                    generateDescription(gen, row, element, null, used.used, null, profile);
                }
            } 
            else 
            {
                row.getCells().Add(new Cell(null, null, !hasDef ? null : describeCardinality(element.Definition, null, used), null, null));
                
                if (element.Definition != null)
                    genTypes(gen, row, element.Definition, profile);
                else
                    row.getCells().Add(new Cell());
        
                generateDescription(gen, row, element, null, used.used, null, profile);
            }
      
            if (element.Slicing != null) 
            {
                row.setIcon("icon_slice.png");
                row.getCells()[2].getPieces().Clear();
        
                foreach (Cell cell in row.getCells())
                    foreach (Piece p in cell.getPieces())
                    {
                        p.addStyle("font-style: italic");
                    }        
            }

            if (used.used || showMissing)
                rows.Add(row);
      
            if (!used.used) 
            {
                foreach (Cell cell in row.getCells())
                foreach (Piece p in cell.getPieces()) 
                {
                    p.setStyle("text-decoration:line-through");
                    p.setReference(null);
                }
            } 
            else
            {
                if (nav.MoveToFirstChild())
                {
                    do
                    {
                        genElement(gen, row.getSubRows(), nav, profile, showMissing);
                    } while (nav.MoveToNext());

                   nav.MoveToParent();
                }
            }
        }
        private Cell generateDescription(HierarchicalTableGenerator gen, Row row, Profile.ElementComponent element, Profile.ElementDefinitionComponent fallback, 
            bool used, String extensionUrl, Profile profile)
        {
            Cell c = new Cell();
            row.getCells().Add(c);

            if (used)
            {
                if (element.Definition != null && element.Definition.Short != null)
                {
                    if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                    c.addPiece(new Piece(null, element.Definition.Short, null));
                }
                else if (fallback != null && fallback.Short != null)
                {
                    if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                    c.addPiece(new Piece(null, fallback.Short, null));
                }

                if (extensionUrl != null)
                {
                    if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                    String fullUrl = extensionUrl;      
                    String reference = _pkp.GetLinkForExtensionDefinition(profile, extensionUrl);
                    c.getPieces().Add(new Piece(null, "URL: ", null).addStyle("font-weight:bold"));
                    c.getPieces().Add(new Piece(reference, fullUrl, null));
                }

                if (element.Slicing != null)
                {
                    if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                    c.getPieces().Add(new Piece(null, "Slice: ", null).addStyle("font-weight:bold"));
                    c.getPieces().Add(new Piece(null, describeSlice(element.Slicing), null));
                }

                if (element.Definition != null)
                {
                    if (element.Definition.Binding != null)
                    {
                        if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                        String reference = _pkp.GetLinkForBinding(element.Definition.Binding);
                        c.getPieces().Add(new Piece(null, "Binding: ", null).addStyle("font-weight:bold"));
                        c.getPieces().Add(new Piece(reference, element.Definition.Binding.Name, null));
                    }

                    if (element.Definition.Constraint != null)
                    {
                        foreach (Profile.ElementDefinitionConstraintComponent inv in element.Definition.Constraint)
                        {
                            if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                            c.getPieces().Add(new Piece(null, "Inv-" + inv.Key + ": ", null).addStyle("font-weight:bold"));
                            c.getPieces().Add(new Piece(null, inv.Human, null));
                        }
                    }

                    if (element.Definition.Value != null)
                    {
                        if (c.getPieces().Any()) c.addPiece(new Piece("br"));
                        c.getPieces().Add(new Piece(null, "Fixed Value: ", null).addStyle("font-weight:bold"));
                        c.getPieces().Add(new Piece(null, element.Definition.Value.ForDisplay(), null));
                    }

                    // ?? example from definition    
                }
            }

            return c;
        }
        private void genTypes(HierarchicalTableGenerator gen, Row r, Profile.ElementDefinitionComponent elementDefn, Profile profile)
        {
            Cell c = new Cell();
            r.getCells().Add(c);

            if (elementDefn.Type == null) return;

            bool first = true;
            foreach (Profile.TypeRefComponent t in elementDefn.Type)
            {
                if (first)
                    first = false;
                else
                    c.addPiece(new Piece(null, ", ", null));

                if (t.Code == "ResourceReference" || (t.Code == "Resource" && t.Profile != null))
                {
                    var reference = _pkp.GetLinkForProfileReference(profile, t.Profile);
                    var label = _pkp.GetLabelForProfileReference(profile, t.Profile);

                    if (t.Profile.StartsWith("http://hl7.org/fhir/Profile/"))
                    {
                        String rn = t.Profile.Substring(28);
                        c.addPiece(new Piece(_pkp.GetLinkForTypeDocu(rn), rn, null));
                    }
                    else if (t.Profile.StartsWith("#"))
                        c.addPiece(new Piece(_pkp.GetLinkForLocalStructure(profile, t.Profile.Substring(1)), t.Profile, null));
                    else
                        c.addPiece(new Piece(t.Profile, t.Profile, null));
                }
                else if (t.Profile != null)
                { // a profiled type
                    var reference = _pkp.GetLinkForProfileReference(profile, t.Profile);
                    var label = _pkp.GetLabelForProfileReference(profile, t.Profile);

                    if (reference != null)
                    {
                        String[] parts = reference.Split('|');      //TODO: Not too sure, was: String[] parts = ref.split("\\|"); in Java
                        c.addPiece(new Piece(reference,label,t.Code));
                    }
                    else
                        c.addPiece(new Piece(reference, t.Code, null));
                }
                else if (_pkp.HasLinkForTypeDocu(t.Code))
                {
                    c.addPiece(new Piece(_pkp.GetLinkForTypeDocu(t.Code), t.Code, null));
                }
                else
                    c.addPiece(new Piece(null, t.Code, null));
            }
        }
        private void genTypes(HierarchicalTableGenerator gen, Row r, Profile.ElementDefinitionComponent elementDefn, String profileBaseFileName, Profile profile)
        {
            Cell c = new Cell();
            r.getCells().Add(c);

            if (elementDefn.Type == null) return;

            bool first = true;
            foreach (Profile.TypeRefComponent t in elementDefn.Type)
            {
                if (first)
                    first = false;
                else
                    c.addPiece(new Piece(null, ", ", null));

                if (t.Code == "ResourceReference" || (t.Code == "Resource" && t.Profile != null))
                {
                    if (t.Profile.StartsWith("http://hl7.org/fhir/Profile/"))
                    {
                        String rn = t.Profile.Substring(28);
                        c.addPiece(new Piece(_pkp.getLinkFor(rn), rn, null));
                    }
                    else if (t.Profile.StartsWith("#"))
                        c.addPiece(new Piece(profileBaseFileName + "." + t.Profile.Substring(1).ToLower() + ".html", t.Profile, null));
                    else
                        c.addPiece(new Piece(t.Profile, t.Profile, null));
                }
                else if (t.Profile != null)
                { // a profiled type
                    String reference = _pkp.getLinkForProfile(profile, t.Profile);
                    if (reference != null)
                    {
                        String[] parts = reference.Split('|');      //TODO: Not too sure, was: String[] parts = ref.split("\\|"); in Java
                        c.addPiece(new Piece(parts[0], parts[1], t.Code));
                    }
                    else
                        c.addPiece(new Piece(reference, t.Code, null));
                }
                else if (_pkp.hasLinkFor(t.Code))
                {
                    c.addPiece(new Piece(_pkp.getLinkFor(t.Code), t.Code, null));
                }
                else
                    c.addPiece(new Piece(null, t.Code, null));
            }
        }