001: /* -*- mode: Java; c-basic-offset: 2; -*- */
002:
003: /**
004: * LZX Attributes
005: */package org.openlaszlo.compiler;
006:
007: import org.openlaszlo.xml.internal.Schema.Type;
008: import org.openlaszlo.xml.internal.XMLUtils;
009: import org.jdom.Element;
010:
011: /** Contains information about an attribute of a laszlo viewsystem class.
012: */
013: class AttributeSpec {
014: /** The source Element from which this attribute was parsed */
015: Element source = null;
016: /** The attribute name */
017: String name;
018: /** The default value */
019: String defaultValue = null;
020: /** The setter function */
021: String setter;
022: /** The type of the attribute value*/
023: Type type;
024: /** Is this attribute required to instantiate an instance of this class? */
025: boolean required = false;
026: /** When does the initial value for this attribute get evaluated? */
027: String when = NodeModel.WHEN_IMMEDIATELY;
028:
029: /** If this is a method, the arglist */
030: String arglist = null;
031:
032: /** Can this attribute be overridden without a warning? value is null, 'true' or 'false' */
033: String override = null;
034:
035: /** Is this attribute equivalent to element content of a given type? */
036: int contentType = NO_CONTENT;
037:
038: /** Element content types: */
039: static final int NO_CONTENT = 0;
040: static final int TEXT_CONTENT = 1;
041: static final int HTML_CONTENT = 2;
042:
043: private String typeToLZX() {
044: switch (contentType) {
045: case TEXT_CONTENT:
046: return "text";
047: case HTML_CONTENT:
048: return "html";
049: default:
050: return type.toString();
051: }
052: }
053:
054: public String toLZX(String indent, ClassModel super class) {
055: AttributeSpec super Spec = super class.getAttribute(name);
056: if (ViewSchema.METHOD_TYPE.equals(type)) {
057: return indent
058: + " <method name='"
059: + name
060: + "'"
061: + (("".equals(arglist)) ? ""
062: : (" args='" + arglist + "'")) + " />";
063: }
064:
065: if (super Spec == null) {
066: if (ViewSchema.EVENT_HANDLER_TYPE.equals(type)) {
067: return indent + "<event name='" + name + "' />";
068: }
069: return indent
070: + "<attribute name='"
071: + name
072: + "'"
073: + ((defaultValue != null) ? (" value='"
074: + defaultValue + "'") : "")
075: + ((type != null) ? (" type='" + typeToLZX() + "'")
076: : "")
077: + ((when != NodeModel.WHEN_IMMEDIATELY) ? (" when='"
078: + when + "'")
079: : "")
080: + (required ? (" required='true'") : "") + " />";
081: } else if (!ViewSchema.EVENT_HANDLER_TYPE.equals(type)) {
082: String attrs = "";
083: if (defaultValue != null
084: && (!defaultValue.equals(super Spec.defaultValue))) {
085: attrs += " value='" + defaultValue + "'";
086: }
087: if (type != null
088: && (!type.equals(super class.getAttributeType(name)))) {
089: attrs += " type='" + typeToLZX() + "'";
090: }
091: if (when != null && (!when.equals(super Spec.when))) {
092: attrs += " when='" + when + "'";
093: }
094: if (required != super Spec.required) {
095: attrs += " required='" + required + "'";
096: }
097: if (attrs.length() > 0) {
098: return indent + "<attribute name='" + name + "'"
099: + attrs + " />";
100: }
101: }
102: return null;
103: }
104:
105: public String toString() {
106: if (ViewSchema.METHOD_TYPE.equals(type)) {
107: return "[AttributeSpec: method name='"
108: + name
109: + "'"
110: + (("".equals(arglist)) ? ""
111: : (" args='" + arglist + "'"))
112: + " override="
113: + (override == null ? "null"
114: : ("'" + override + "'")) + "]";
115: }
116: if (ViewSchema.EVENT_HANDLER_TYPE.equals(type)) {
117: return "[AttributeSpec: event name='" + name + "' ]";
118: }
119: return "[AttributeSpec: attribute name='"
120: + name
121: + "'"
122: + ((defaultValue != null) ? (" value='" + defaultValue + "'")
123: : "")
124: + ((type != null) ? (" type='" + typeToLZX() + "'")
125: : "")
126: + ((when != NodeModel.WHEN_IMMEDIATELY) ? (" when='"
127: + when + "'") : "")
128: + (required ? (" required='true'") : "") + " ";
129: }
130:
131: AttributeSpec(String name, Type type, String defaultValue,
132: String setter, Element source) {
133: this .source = source;
134: this .name = name;
135: this .type = type;
136: this .defaultValue = defaultValue;
137: this .setter = setter;
138: this .when = XMLUtils.getAttributeValue(source, "when",
139: NodeModel.WHEN_IMMEDIATELY);
140: }
141:
142: AttributeSpec(String name, Type type, String defaultValue,
143: String setter, boolean required, Element source) {
144: this .source = source;
145: this .name = name;
146: this .type = type;
147: this .defaultValue = defaultValue;
148: this .setter = setter;
149: this .required = required;
150: this .when = XMLUtils.getAttributeValue(source, "when",
151: NodeModel.WHEN_IMMEDIATELY);
152: }
153:
154: AttributeSpec(String name, Type type, String defaultValue,
155: String setter) {
156: this .name = name;
157: this .type = type;
158: this .defaultValue = defaultValue;
159: this .setter = setter;
160: }
161:
162: AttributeSpec(String name, Type type, String defaultValue,
163: String setter, boolean required) {
164: this .name = name;
165: this .type = type;
166: this .defaultValue = defaultValue;
167: this .setter = setter;
168: this .required = required;
169: }
170: }
171:
172: /**
173: * @copyright Copyright 2001-2007 Laszlo Systems, Inc. All Rights
174: * Reserved. Use is subject to license terms.
175: */
|