View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-XML: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript-XML.
6   //
7   // MillScript-XML is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-XML is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-XML; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.commons.xml.atttype;
22  
23  import org.millscript.commons.xml.alerts.XmlWellFormednessAlert;
24  import org.millscript.commons.xml.api.AttDef;
25  import org.millscript.commons.xml.api.Name;
26  import org.millscript.commons.xml.tokenizer.AbstractXmlTokenizerImpl;
27  
28  /**
29   * The class provides a abstract default implementation of the
30   * <code>AttDef</code> interface, intended to provide a base for attribute type
31   * specific implementations.
32   */
33  public abstract class AttDefImpl implements AttDef {
34  
35      /**
36       * The <code>Name</code> of the defined attribute.
37       */
38      private final Name attributeName;
39  
40      /**
41       * The default mode for this attriubte. This will be one of "#REQUIRED",
42       * "#IMPLIED" or "#FIXED".
43       */
44      private final String defaultDecl;
45  
46      /**
47       * The default value for this attribute, or <code>null</code> if a default
48       * value is not appropriate.
49       */
50      private final String defaultValue;
51  
52      /**
53       * Constructs a new attribute definition object with the specified
54       * <code>Name</code>, reading the default mode and value from the specified
55       * XML tokenizer.
56       *
57       * @param an    the attributes <code>Name</code>
58       * @param xt    the XML tokenizer to obtain the default mode and value from
59       */
60      public AttDefImpl( final Name an, final AbstractXmlTokenizerImpl xt ) {
61          this.attributeName = an;
62          xt.dropS();
63          if ( xt.tryRead( '#' ) ) {
64              final String modeName = xt.readNmtoken();
65              if ( modeName.equals( "REQUIRED" ) ) {
66                  this.defaultDecl = "#REQUIRED";
67                  this.defaultValue = "";
68              } else if ( modeName.equals( "IMPLIED" ) ) {
69                  this.defaultDecl = "#IMPLIED";
70                  this.defaultValue = "";
71              } else if ( modeName.equals( "FIXED" ) ) {
72                  this.defaultDecl = "#FIXED";
73                  // Must read the default value
74                  this.defaultValue = xt.readAttValue();
75              } else {
76                  throw new XmlWellFormednessAlert(
77                      "Attribute definition expected '#REQUIRED', '#IMPLED', '#FIXED' or a default value"
78                  ).culpritToken( modeName ).mishap();
79              }
80          } else {
81              this.defaultDecl = null;
82              this.defaultValue = xt.readAttValue();
83          }
84      }
85  
86      /**
87       * Constructs a new attribute definition object with the specified
88       * <code>Name</code>, default mode and default value.
89       *
90       * @param an    the attributes <code>Name</code>
91       * @param dm    the default mode for the attribute
92       * @param dv    the default value for the attribute
93       */
94      public AttDefImpl( final Name an, final String dv, final String dm ) {
95          this.attributeName = an;
96          this.defaultDecl = dm;
97          this.defaultValue = dv;
98      }
99  
100     /**
101      * @see org.millscript.commons.xml.api.AttDef#getDefaultDecl()
102      */
103     public String getDefaultDecl() {
104         return this.defaultDecl;
105     }
106 
107     /**
108      * @see org.millscript.commons.xml.api.AttDef#getDefaultValue()
109      */
110     public String getDefaultValue() {
111         return this.defaultValue;
112     }
113 
114     /**
115      * @see org.millscript.commons.xml.api.AttDef#getName()
116      */
117     public Name getName() {
118         return this.attributeName;
119     }
120 
121 }