001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.html;
014
015import org.apache.juneau.internal.*;
016
017/**
018 * @deprecated Use {@link BasicHtmlDocTemplate}
019 */
020@Deprecated
021public class HtmlDocTemplateBasic implements HtmlDocTemplate {
022   @Override /* HtmlDocTemplate */
023   public void writeTo(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
024      w.sTag("html").nl(0);
025      w.sTag(1, "head").nl(1);
026      head(session, w, o);
027      w.eTag(1, "head").nl(1);
028      w.sTag(1, "body").nl(1);
029      body(session, w, o);
030      w.eTag(1, "body").nl(1);
031      w.eTag("html").nl(0);
032   }
033
034   /**
035    * Renders the contents of the <code><xt>&lt;head&gt;</xt></code> element.
036    *
037    * @param session The current serializer session.
038    * @param w The writer being written to.
039    * @param o The object being serialized.
040    * @throws Exception Any exception can be thrown.
041    */
042   // TODO - Make protected in 8.0.
043   @Override
044   public void head(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
045
046      String[] head = session.getHead();
047      for (int i = 0; i < head.length; i++)
048         w.sIf(i > 0).appendln(2, head[i]);
049
050      if (hasStyle(session)) {
051         w.sTag(2, "style").nl(2);
052         style(session, w, o);
053         w.ie(2).eTag("style").nl(2);
054      }
055      if (hasScript(session)) {
056         w.sTag(2, "script").nl(2);
057         script(session, w, o);
058         w.ie(2).eTag("script").nl(2);
059      }
060   }
061
062   /**
063    * Renders the contents of the <code><xt>&lt;head&gt;</xt>/<xt>&lt;style&gt;</xt></code> element.
064    *
065    * @param session The current serializer session.
066    * @param w The writer being written to.
067    * @param o The object being serialized.
068    * @throws Exception Any exception can be thrown.
069    */
070   // TODO - Make protected in 8.0.
071   @Override
072   public void style(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
073
074      int i = 0;
075      for (String s : session.getStylesheet())
076         w.sIf(i++ > 0).append(3, "@import ").q().append(session.resolveUri(s)).q().appendln(";");
077
078      if (session.isNowrap())
079         w.appendln(3, "div.data * {white-space:nowrap;} ");
080
081      for (String s : session.getStyle())
082         w.sIf(i > 0).appendln(3, s);
083   }
084
085   /**
086    * Renders the contents of the <code><xt>&lt;head&gt;</xt>/<xt>&lt;script&gt;</xt></code> element.
087    *
088    * @param session The current serializer session.
089    * @param w The writer being written to.
090    * @param o The object being serialized.
091    * @throws Exception Any exception can be thrown.
092    */
093   // TODO - Make protected in 8.0.
094   @Override
095   public void script(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
096      int i = 0;
097      for (String s : session.getScript())
098         w.sIf(i++ > 0).append(3, s).append('\n'); // Must always append a newline even if whitespace disabled!
099   }
100
101   /**
102    * Renders the contents of the <code><xt>&lt;body&gt;</xt></code> element.
103    *
104    * @param session The current serializer session.
105    * @param w The writer being written to.
106    * @param o The object being serialized.
107    * @throws Exception Any exception can be thrown.
108    */
109   // TODO - Make protected in 8.0.
110   @Override
111   public void body(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
112
113      if (hasHeader(session)) {
114         w.sTag(2, "header").nl(2);
115         header(session, w, o);
116         w.ie(2).eTag("header").nl(2);
117      }
118
119      if (hasNav(session)) {
120         w.sTag(2, "nav").nl(2);
121         nav(session, w, o);
122         w.ie(2).eTag("nav").nl(2);
123      }
124
125      w.sTag(2, "section").nl(2);
126
127      w.sTag(3, "article").nl(3);
128      article(session, w, o);
129      w.ie(3).eTag("article").nl(3);
130
131      if (hasAside(session)) {
132         w.sTag(3, "aside").nl(3);
133         aside(session, w, o);
134         w.ie(3).eTag("aside").nl(3);
135      }
136
137      w.ie(2).eTag("section").nl(2);
138
139      if (hasFooter(session)) {
140         w.sTag(2, "footer").nl(2);
141         footer(session, w, o);
142         w.ie(2).eTag("footer").nl(2);
143      }
144   }
145
146   /**
147    * Renders the contents of the <code><xt>&lt;body&gt;</xt>/<xt>&lt;header&gt;</xt></code> element.
148    *
149    * @param session The current serializer session.
150    * @param w The writer being written to.
151    * @param o The object being serialized.
152    * @throws Exception Any exception can be thrown.
153    */
154   // TODO - Make protected in 8.0.
155   @Override
156   public void header(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
157      // Write the title of the page.
158      String[] header = session.getHeader();
159      for (int i = 0; i < header.length; i++)
160         w.sIf(i > 0).appendln(3, header[i]);
161   }
162
163   /**
164    * Renders the contents of the <code><xt>&lt;body&gt;</xt>/<xt>&lt;nav&gt;</xt></code> element.
165    *
166    * @param session The current serializer session.
167    * @param w The writer being written to.
168    * @param o The object being serialized.
169    * @throws Exception Any exception can be thrown.
170    */
171   // TODO - Make protected in 8.0.
172   @Override
173   public void nav(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
174      String[] links = session.getNavLinks();
175      if (links.length > 0 && ! ArrayUtils.contains("NONE", links)) {
176         w.sTag(3, "ol").nl(3);
177         for (String l : links) {
178            w.sTag(4, "li");
179            if (l.matches("(?s)\\S+\\:.*")) {
180               int i = l.indexOf(':');
181               String key = l.substring(0, i);
182               String val = l.substring(i+1).trim();
183               if (val.startsWith("<"))
184                  w.nl(4).appendln(5, val);
185               else
186                  w.oTag("a").attr("href", session.resolveUri(val), true).cTag().text(key, true).eTag("a");
187               w.eTag("li").nl(4);
188            } else {
189               w.nl(4).appendln(5, l);
190               w.eTag(4, "li").nl(4);
191            }
192         }
193         w.eTag(3, "ol").nl(3);
194      }
195      String[] nav = session.getNav();
196      if (nav.length > 0) {
197         for (int i = 0; i < nav.length; i++)
198            w.sIf(i > 0).appendln(3, nav[i]);
199      }
200   }
201
202   /**
203    * Renders the contents of the <code><xt>&lt;body&gt;</xt>/<xt>&lt;aside&gt;</xt></code> element.
204    *
205    * @param session The current serializer session.
206    * @param w The writer being written to.
207    * @param o The object being serialized.
208    * @throws Exception Any exception can be thrown.
209    */
210   // TODO - Make protected in 8.0.
211   @Override
212   public void aside(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
213      String[] aside = session.getAside();
214      for (int i = 0; i < aside.length; i++)
215         w.sIf(i > 0).appendln(4, aside[i]);
216   }
217
218   /**
219    * Renders the contents of the <code><xt>&lt;body&gt;</xt>/<xt>&lt;article&gt;</xt></code> element.
220    *
221    * @param session The current serializer session.
222    * @param w The writer being written to.
223    * @param o The object being serialized.
224    * @throws Exception Any exception can be thrown.
225    */
226   // TODO - Make protected in 8.0.
227   @Override
228   public void article(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
229      // To allow for page formatting using CSS, we encapsulate the data inside two div tags:
230      // <div class='outerdata'><div class='data' id='data'>...</div></div>
231      w.oTag(4, "div").attr("class","outerdata").append('>').nl(4);
232      w.oTag(5, "div").attr("class","data").attr("id", "data").append('>').nl(5);
233
234      if (o == null) {
235         w.append(6, "<null/>").nl(6);
236      } else if (ObjectUtils.isEmpty(o)){
237         String m = session.getNoResultsMessage();
238         if (exists(m))
239            w.append(6, m).nl(6);
240      } else {
241         session.indent = 6;
242         w.flush();
243         session.parentSerialize(w, o);
244      }
245
246      w.ie(5).eTag("div").nl(5);
247      w.ie(4).eTag("div").nl(4);
248   }
249
250   /**
251    * Renders the contents of the <code><xt>&lt;body&gt;</xt>/<xt>&lt;footer&gt;</xt></code> element.
252    *
253    * @param session The current serializer session.
254    * @param w The writer being written to.
255    * @param o The object being serialized.
256    * @throws Exception Any exception can be thrown.
257    */
258   // TODO - Make protected in 8.0.
259   @Override
260   public void footer(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception {
261      String[] footer = session.getFooter();
262      for (int i = 0; i < footer.length; i++)
263         w.sIf(i > 0).appendln(3, footer[i]);
264   }
265
266   /**
267    * Returns <jk>true</jk> if this page should render a <code><xt>&lt;head&gt;</xt>/<xt>&lt;style&gt;</xt></code> element.
268    *
269    * @param session The current serializer session.
270    * @return A boolean flag.
271    */
272   // TODO - Make protected in 8.0.
273   @Override
274   public boolean hasStyle(HtmlDocSerializerSession session) {
275      return true;
276   }
277
278   /**
279    * Returns <jk>true</jk> if this page should render a <code><xt>&lt;head&gt;</xt>/<xt>&lt;script&gt;</xt></code> element.
280    *
281    * @param session The current serializer session.
282    * @return A boolean flag.
283    */
284   // TODO - Make protected in 8.0.
285   @Override
286   public boolean hasScript(HtmlDocSerializerSession session) {
287      return true;
288   }
289
290   /**
291    * Returns <jk>true</jk> if this page should render a <code><xt>&lt;body&gt;</xt>/<xt>&lt;header&gt;</xt></code>
292    * element.
293    *
294    * @param session The current serializer session.
295    * @return A boolean flag.
296    */
297   // TODO - Make protected in 8.0.
298   @Override
299   public boolean hasHeader(HtmlDocSerializerSession session) {
300      return session.getHeader().length > 0;
301   }
302
303   /**
304    * Returns <jk>true</jk> if this page should render a <code><xt>&lt;body&gt;</xt>/<xt>&lt;nav&gt;</xt></code>
305    * element.
306    *
307    * @param session The current serializer session.
308    * @return A boolean flag.
309    */
310   // TODO - Make protected in 8.0.
311   @Override
312   public boolean hasNav(HtmlDocSerializerSession session) {
313      return session.getNav().length > 0 || session.getNavLinks().length > 0;
314   }
315
316   /**
317    * Returns <jk>true</jk> if this page should render a <code><xt>&lt;body&gt;</xt>/<xt>&lt;aside&gt;</xt></code>
318    * element.
319    *
320    * @param session The current serializer session.
321    * @return A boolean flag.
322    */
323   // TODO - Make protected in 8.0.
324   @Override
325   public boolean hasAside(HtmlDocSerializerSession session) {
326      return session.getAside().length > 0;
327   }
328
329   /**
330    * Returns <jk>true</jk> if this page should render a <code><xt>&lt;body&gt;</xt>/<xt>&lt;footer&gt;</xt></code>
331    * element.
332    *
333    * @param session The current serializer session.
334    * @return A boolean flag.
335    */
336   // TODO - Make protected in 8.0.
337   @Override
338   public boolean hasFooter(HtmlDocSerializerSession session) {
339      return session.getFooter().length > 0;
340   }
341
342   private static boolean exists(String s) {
343      return s != null && ! "NONE".equals(s);
344   }
345}