View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2005 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.loaders;
23  
24  import org.millscript.commons.xml.api.Name;
25  import org.millscript.commons.xml.api.token.StartTagToken;
26  import org.millscript.commons.xml.tokenizer.NoNamespacesName;
27  import org.millscript.millscript.alert.Alerts;
28  
29  import java.util.HashSet;
30  
31  /**
32   * This class is a starting point for implementing an XHTML template loader. It
33   * allows specific sub-classes to customize the loading process for custom tags
34   * and attributes.
35   */
36  public abstract class BasicXHTMLTemplateLoader extends BasicTemplateLoader {
37  
38      /**
39       * A set of supported element names.
40       */
41      private static final HashSet< Name > SUPPORTED_ELEMENTS = new HashSet< Name >();
42  
43      /**
44       * Array of all the supported XHTML element names.
45       */
46      private static final String[] xhtmlTagNames = {
47          "a", "abbr", "address", "applet", "area", "b", "base",
48          "basefont", "bdo", "bgsound", "big", "blink", "blockquote",
49          "body", "br", "button", "caption", "center", "cite", "code",
50          "col", "colgroup", "dd", "dfn", "dir", "div", "dl", "dt",
51          "em", "embed", "fieldset", "font", "form", "frame",
52          "frameset", "head", "h1", "h2", "h3", "h4", "h5", "h6", "hr",
53          "html", "i", "iframe", "ilayer", "img", "input", "isindex",
54          "kbd", "keygen", "label", "layer", "legend", "li", "link",
55          "map", "marquee", "menu", "meta", "nobr", "noembed",
56          "noframes", "nolayer", "noscript", "object", "ol", "optgroup",
57          "option", "p", "param", "pre", "q", "s", "samp", "script",
58          "select", "server", "small", "spacer", "span", "strike",
59          "strong", "style", "sub", "sup", "table", "tbody", "td",
60          "textarea", "tfoot", "th", "thead", "title", "tr", "tt", "u",
61          "ul", "var", "wbr"
62      };
63  
64      //  Initialize the set supported elements
65      static {
66          for ( int i = 0; i < xhtmlTagNames.length; i++ ) {
67              SUPPORTED_ELEMENTS.add(
68                  new NoNamespacesName( xhtmlTagNames[ i ] )
69              );
70          }
71      }
72  
73      /**
74       * Constructs a new basic XHTML template loader with optional XML namespace
75       * support.
76       *
77       * @param ns    indicates if the XML tokenizer should support namespaces
78       */
79      BasicXHTMLTemplateLoader( final boolean ns ) {
80          super( ns );
81      }
82  
83      /**
84       * @see org.millscript.millscript.loaders.BasicTemplateLoader#compileStartTag(org.millscript.commons.xml.api.token.StartTagToken)
85       */
86      @Override
87      public void compileStartTag( final StartTagToken token ) {
88          // TODO - Convert this to MillScript-Util
89          // An XHTML template only supports a limited set of elements.
90          if ( SUPPORTED_ELEMENTS.contains( token.getName() ) ) {
91              // The element is supported, so handle it normally
92              super.compileStartTag( token );
93          } else {
94              // Unsupported element
95              throw(
96                  Alerts.template(
97                      "Unrecognized template element (spelling error?)",
98                      "All XHTML element names must be pre-registered"
99                  ).decorate( token ).mishap()
100             );
101         }
102     }
103 
104 }