AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / xgen / src / main / java / org / onap / aaf / misc / xgen / html / HTMLGen.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.xgen.html;
23
24 import java.io.Writer;
25
26 import org.onap.aaf.misc.xgen.Mark;
27 import org.onap.aaf.misc.xgen.XGen;
28
29 public abstract class HTMLGen extends XGen<HTMLGen> {
30         public static final String A = "a";
31         public static final String P = "p";
32         public static final String LI = "li";
33         public static final String OL = "ol";
34         public static final String UL = "ul";
35         
36         
37         public static final String TABLE = "table";
38         public static final String THEAD = "thead";
39         public static final String TBODY = "tbody";
40         public static final String TR = "tr";
41         public static final String TH = "th";
42         public static final String TD = "td";
43         
44         public static final String TITLE = "title";
45         public static final String H1 = "h1";
46         public static final String H2 = "h2";
47         public static final String H3 = "h3";
48         public static final String H4 = "h4";
49         public static final String H5 = "h5";
50         
51         
52         
53         // --------------------------- HTML Version Specific -----------------------
54         public abstract HTMLGen html(String ... attributes);
55         public abstract HTMLGen charset(String charset);
56         public abstract Mark head();
57         public abstract Mark body(String ... attribs);
58
59         
60         // HTML 5 has simplified sectioning
61         public abstract Mark header(String ... attribs);
62         public abstract Mark footer(String ... attribs);
63         public abstract Mark section(String ... attribs);
64         public abstract Mark article(String ... attribs);
65         public abstract Mark aside(String ... attribs);
66         public abstract Mark nav(String ... attribs);
67
68         // --------------------------- HTML Version Specific -----------------------
69
70         public HTMLGen imports(Imports imports) {
71                 //this.imports=imports;
72                 for(String str : imports.css) {
73                         forward.print("<link rel=\"stylesheet\" href=\"");
74                         forward.print(imports.themePath(null));
75                         forward.print(str);
76                         forward.println("\">");
77                 }
78
79                 for(String str : imports.js) {
80                         forward.print("<script type=\"text/javascript\" src=\"");
81                         forward.print(imports.themePath(null));
82                         forward.print(str);
83                         forward.println("\"></script>");
84                 }
85                 return this;
86         }
87         
88         public HTMLGen jsVars(String ... attrs) {
89                 forward.println("<script type=text/javascript>");
90                 if(attrs!=null) {
91                         for(int i=0; i<attrs.length;++i) {
92                                 forward.append(' ');
93                                 String[] split = attrs[i].split("=",2);
94                                 switch(split.length) {
95                                         case 2:
96                                                 forward.print("  var ");
97                                                 forward.append(split[0]);
98                                                 forward.append("='");
99                                                 forward.append(split[1]);
100                                                 forward.println("';");
101                                                 break;
102                                 }
103                         }
104                 }
105                 forward.println("</script>");
106                 return this;
107         }
108
109         public HTMLGen(Writer w) {
110                 super(w);
111         }
112
113         /**
114          * Use "directive" to handle non-ended HTML tags like <meta ... >  and <link ...>
115          * @param tag
116          * @param attrs
117          * @return
118          */
119         public HTMLGen directive(String tag, String ... attrs) {
120                 forward.append('<');
121                 forward.append(tag);
122                 addAttrs(attrs);
123                 forward.append('>');
124                 if(pretty) {
125                         forward.println();
126                 }
127                 return this;
128         }
129
130         public Mark divID(String ... attrs) {
131                 Mark div;
132                 if(attrs.length>0) {
133                         div = new Mark(attrs[0]);
134                         attrs[0]="id="+attrs[0];
135                 } else {
136                         div = new Mark();
137                 }
138                 incr(div, "div", attrs);
139                 return div;
140         }
141
142         public HTMLGen img(String ... attrs) {
143                 return tagOnly("img", attrs);
144         }
145         
146         /**
147          * Input Cheesecake... creates a Label and Field in the form of Table Rows.
148          * Make sure you create a table first, ie.  incr(HTMLGen.TABLE);
149          * 
150          * Setting Required to "true" will add required Attribute to both Label and Field.  In HTML5, "required" in the input will
151          * validate there is data in the fields before submitting.  "required" does nothing for label, but allows for
152          * easy CSS coding... "label[required] { ... }", so that colors can be changed
153          * 
154          * @param id
155          * @param label
156          * @param required
157          * @param attrs
158          * @return
159          */
160         public HTMLGen input(String id, String label, boolean required, String ... attrs) {
161                 Mark mtr = new Mark(TR);
162                 Mark mtd = new Mark(TD);
163                 incr(mtr);
164                 incr(mtd);
165                 incr("label",true, "for="+id,required?"required":null).text(label).end();
166                 end(mtd);
167                 String nattrs[] = new String[attrs.length+(required?3:2)];
168                 nattrs[0]="id="+id;
169                 nattrs[1]="name="+id;
170                 System.arraycopy(attrs, 0, nattrs, 2, attrs.length);
171                 if(required) {
172                         nattrs[nattrs.length-1]="required";
173                 }
174                 incr(mtd);
175                 tagOnly("input",nattrs);
176                 end(mtr);
177                 return this;
178         }
179         
180         //  Common tags that do not have standard endings.  These are here to help people who don't know to pick directive  
181         public HTMLGen br() {
182                 forward.append("<br>");
183                 if(pretty) {
184                         forward.println();
185                 }
186                 return this;
187         }
188
189         public HTMLGen p(String ... text) {
190                 forward.append("<p>");
191                 for(String s : text) {
192                         forward.append(s);
193                 }
194                 if(pretty) {
195                         forward.println();
196                 }
197                 return this;
198         }
199
200         public HTMLGen hr() {
201                 forward.append("<hr>");
202                 if(pretty) {
203                         forward.println();
204                 }
205                 return this;
206         }
207
208         public JSGen js(Mark mark) {
209                 return new JSGen(mark, this);
210         }
211
212         public JSGen js() {
213                 return js(null);
214         }
215 //
216 //      protected void cssInline(String filename) {
217 //              File file = new File(imports.webDir,filename);
218 //              try {
219 //                      String line;
220 //                      BufferedReader br = new BufferedReader(new FileReader(file));
221 //                      try {
222 //                              forward.print("<style>");
223 //                              prettyln(forward);
224 //                              while((line=br.readLine())!=null) {
225 //                                      forward.print((pretty?line:line.trim()));
226 //                                      prettyln(forward);
227 //                              }                       
228 //                      }finally {
229 //                              forward.print("</style>");
230 //                              prettyln(forward);
231 //                              br.close();
232 //                      }
233 //              } catch (IOException e) {
234 //                      e.printStackTrace();
235 //                      // Can't read, suffice to import normally?
236 //                      // for now, just skip
237 //              }
238 //      }
239         
240 }