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