1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }