list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

Html Generation

The first example below illustrates the generation template capability of Umple. It is an html generation library. The second example is a program that uses the first example.

Example

  1. // Umple library for generating html  
  2. // Currently it supports h1, h2 ... as well as p, and table tags.  
  3. // This will be extended to support more of html later  
  4. // It serves as an illustration of Umple's generation templates  
  5.   
  6. strictness allow 75; // We are going to allow custom constructors  
  7.   
  8. // Class representing either a top level html node or a collection of subnodes  
  9. class HtmlNode {  
  10.   String getContent() {return "";};  // should be abstract  
  11. }  
  12.   
  13. // Simple text to be output between tags - these are leaves  
  14. class HtmlTextNode {  
  15.   isA HtmlNode;  
  16.   String content;  
  17. }  
  18.   
  19. // Non-leaf nodes in the html tree  
  20. class HtmlRegularNode {  
  21.   isA HtmlNode;  
  22.   const String Xmltagstart = "<";  
  23.   const String Xmltagend = ">";  
  24.   
  25.   // Arguments for the constructor  
  26.   String tag; // e.g. p, h1, a  
  27.   String arguments; // e.g. href  
  28.     
  29.   0..1 -> * HtmlNode subnodes; // whatever to emit between tags  
  30.     
  31.   // The following template will render any html node  
  32.   rendering <<!<<=Xmltagstart>><<=getTag()>> <<=getArguments()>><<=Xmltagend>>  
  33.   <<#  
  34.   for(HtmlNode n : getSubnodes()) {#>><<=n.getContent()>>  
  35.   <<#}#>><<=Xmltagstart>>/<<=getTag()>><<=Xmltagend>>!>>  
  36.     
  37.   emit getContent()(rendering);  
  38.     
  39.   HtmlTable table() {  
  40.     HtmlTable t = new HtmlTable();  
  41.     addSubnode(t);  
  42.     return t;  
  43.   }  
  44.     
  45.   HtmlTextNode text(String s) {  
  46.     HtmlTextNode t = new HtmlTextNode(s);  
  47.     addSubnode(t);  
  48.     return(t);  
  49.   }  
  50.   
  51.   HtmlRegularNode taggedText(String tag, String arguments, String s) {  
  52.     HtmlRegularNode r = new HtmlRegularNode(tag, arguments);  
  53.     HtmlTextNode t = new HtmlTextNode(s);  
  54.     r.addSubnode(t);  
  55.     addSubnode(r);  
  56.     return(r);  
  57.   }  
  58. }  
  59.   
  60. class HtmlTable {  
  61.   isA HtmlRegularNode;  
  62.   
  63.   HtmlTable() {  
  64.     super("table","");  
  65.   }  
  66.   
  67.   HtmlRow tr() {  
  68.     HtmlRow r = new HtmlRow();  
  69.     addSubnode(r);  
  70.     return r;  
  71.   }    
  72. }  
  73.   
  74. class HtmlRow {  
  75.   isA HtmlRegularNode;  
  76.   
  77.   HtmlRow() {  
  78.     super("tr","");  
  79.   }  
  80.   
  81.   // Add a cell that contains text  
  82.   HtmlCell td(String s) {  
  83.     HtmlCell c = td();  
  84.     c.text(s);  
  85.     return(c);  
  86.   }  
  87.   
  88.   // Add a cell that can contain anything  
  89.   HtmlCell td() {  
  90.     HtmlCell c = new HtmlCell();  
  91.     addSubnode(c);  
  92.     return c;  
  93.   }  
  94. }  
  95.   
  96. class HtmlCell {  
  97.   isA HtmlRegularNode;  
  98.   
  99.   HtmlCell() {  
  100.     super("td","");  
  101.   }  
  102. }  
  103.   
  104. class HtmlGen {  
  105.   
  106.   lazy HtmlNode firstNode;  
  107.   
  108.   // Subtrees for the header and body  
  109.   0..1 -> * HtmlNode headerNodes;  
  110.   0..1 -> * HtmlNode bodyNodes;  
  111.    
  112.   after constructor {  
  113.     firstNode = new HtmlTextNode(filehead());  
  114.   }  
  115.   
  116.   filehead <<!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">!>>  
  117.   emit filehead()(filehead);  
  118.   
  119.   xmlns <<!xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"!>>  
  120.   emit xmlns()(xmlns);  
  121.     
  122.   wholefile <<!<<=getFirstNode().getContent()>>  
  123. <html <<=xmlns()>> >  
  124.   <head>  
  125.   <<#for(HtmlNode h: getHeaderNodes()) {#>>  
  126.     <<=h.getContent()>>  
  127.   <<#}#>>  
  128.   </head>  
  129.   <body>  
  130.   <<#for(HtmlNode h: getBodyNodes()) {#>>  
  131.     <<=h.getContent()>>  
  132.   <<#}#>>  
  133.   </body>  
  134. </html>  
  135. !>>  
  136.       
  137.   emit wholefile()(wholefile);  
  138.   
  139.   // Creates a new simple node; but does not add it anywhere  
  140.   HtmlRegularNode simpleNode(String tag, String content) {  
  141.     HtmlTextNode t = new HtmlTextNode(content);  
  142.     HtmlRegularNode r = new HtmlRegularNode(tag,"");  
  143.     r.addSubnode(t);  
  144.     return r;  
  145.   }  
  146.   
  147.   // Add a top level node of any type that contains a string  
  148.   HtmlNode addNode(HtmlNode n) {  
  149.     addBodyNode(n);  
  150.     return n;  
  151.   }   
  152.   
  153.   // Add a node of any type that contains a string  
  154.   HtmlRegularNode addSimpleNode(String tag, String text) {  
  155.     HtmlRegularNode t = simpleNode(tag,text);  
  156.     addBodyNode(t);  
  157.     return t;  
  158.   }   
  159.   
  160.   // Adds a top level header at a certain level with plain text  
  161.   // Subsequent calls can be made to add additional elements  
  162.   // to the result  
  163.   HtmlRegularNode h(int level, String text) {  
  164.     return addSimpleNode("h"+level,text);  
  165.   }   
  166.   
  167.   // Adds a top level p with plain text  
  168.   // Subsequent calls can add additional elements to the p  
  169.   // beyond just the text  
  170.   HtmlRegularNode p(String text) {  
  171.     return addSimpleNode("p",text);  
  172.   }   
  173.   
  174.       
  175.   // Adds a top level table  
  176.   HtmlTable table() {  
  177.     HtmlTable t = new HtmlTable();  
  178.     addBodyNode(t);  
  179.     return t;  
  180.   }  
  181. }  
  182.   
  183.   
  184.         

Load the above code into UmpleOnline

 

Another Example

  1. // Umple program demonstrating generation  
  2. // of the html generation library for Umple  
  3.   
  4. use ../HtmlGeneration.ump;  
  5.   
  6. class TimesTable {  
  7.   public static void main(String[] argv) {  
  8.     int times = 10; // default  
  9.     HtmlGen h = new HtmlGen();  
  10.     HtmlRegularNode p;  
  11.   
  12.     if(argv.length > 0) {  
  13.       times=Integer.parseInt(argv[0]);  
  14.     }  
  15.       
  16.     // Specify the table main header  
  17.     h.h(1, "Multiplication Table: "+times);  
  18.   
  19.     // Generate an explanatory paragraph  
  20.     p = h.p("The following is a ");  
  21.     p.taggedText("i","","simple");  
  22.     p.text(" multiplication table");  
  23.       
  24.     // Generate another paragraph with some subnodes  
  25.     // showing a slightly different use of the API  
  26.     p = new HtmlRegularNode("p","");      
  27.     h.addNode(p);  
  28.     p.text("Nodes in ");  
  29.     p.taggedText("font""color=\"red\"""red");  
  30.     p.text(" are powers of 3");  
  31.   
  32.     h.h(2, "The table");  
  33.   
  34.     // Create a table at the top level  
  35.     HtmlTable t=h.table();  
  36.   
  37.     // Add the top row of the table  
  38.     HtmlRow r;  
  39.     r=t.tr();  
  40.     r.td("*");  
  41.     for(int i=1; i<= times; i++) {  
  42.       r.td().taggedText("b","",""+i);;  
  43.     }  
  44.   
  45.     // Add the interior rows of the table  
  46.     for(int i=1; i<= times; i++) {  
  47.       r=t.tr();  
  48.       r.td().taggedText("b","",""+i);  
  49.       for(int j=1; j<= times; j++) {  
  50.         String output=""+i*j;  
  51.         if((i*j)%3==0) {  
  52.           HtmlCell c = r.td();  
  53.           c.taggedText("font""color=\"red\"",output);  
  54.         }  
  55.         else {  
  56.           r.td(output);  
  57.         }  
  58.       }  
  59.     }  
  60.       
  61.     System.out.println(h.wholefile());   
  62.   }  
  63. }  
  64.   
  65.         

Load the above code into UmpleOnline