001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.drapgen.ast;
017:
018: import nu.xom.Attribute;
019:
020: import static org.directwebremoting.drapgen.ast.SerializationStrings.*;
021:
022: /**
023: * We are assuming that there is no point to non-public fields for Drapgen
024: * @see java.lang.reflect.Field
025: * @author Joe Walker [joe at getahead dot ltd dot uk]
026: */
027: public class Field extends Element {
028: /**
029: * All {@link Field}s need a parent {@link Type}
030: * @param parent the type of which we are a part
031: */
032: protected Field(Type parent) {
033: this .parent = parent;
034: }
035:
036: /**
037: * @return the name
038: */
039: public String getName() {
040: return name;
041: }
042:
043: /**
044: * @param name the name to set
045: */
046: public void setName(String name) {
047: this .name = name;
048: }
049:
050: /**
051: * @return the type
052: */
053: public Type getType() {
054: return type;
055: }
056:
057: /**
058: * @param type the type to set
059: */
060: public void setType(Type type) {
061: this .type = type;
062: }
063:
064: /**
065: * @return the value
066: */
067: public String getValue() {
068: return value;
069: }
070:
071: /**
072: * @param value the value to set
073: */
074: public void setValue(String value) {
075: this .value = value;
076: }
077:
078: /**
079: * Create a XOM Element from this
080: * @return a Element representing this Type
081: */
082: protected nu.xom.Element toXomElement() {
083: nu.xom.Element field = new nu.xom.Element(CONSTANT);
084: writeDocumentation(field);
085: field.addAttribute(new Attribute(NAME, name));
086: field.addAttribute(new Attribute(TYPE, type.getFullName()));
087: if (value == null) {
088: field.addAttribute(new Attribute(VALUE, "null"));
089: } else {
090: field.addAttribute(new Attribute(VALUE, value));
091: }
092:
093: return field;
094: }
095:
096: /**
097: * Load this type with data from the given document
098: * @param element The element to load from
099: */
100: protected void fromXomDocument(nu.xom.Element element) {
101: readDocumentation(element);
102: name = element.getAttributeValue(NAME);
103: type = parent.getProject().getType(
104: element.getAttributeValue(TYPE));
105: value = element.getAttributeValue(VALUE);
106: }
107:
108: /* (non-Javadoc)
109: * @see java.lang.Object#hashCode()
110: */
111: @Override
112: public int hashCode() {
113: return parent.hashCode() + name.hashCode();
114: }
115:
116: /* (non-Javadoc)
117: * @see java.lang.Object#equals(java.lang.Object)
118: */
119: @Override
120: public boolean equals(Object obj) {
121: if (obj == null) {
122: return false;
123: }
124:
125: if (obj == this ) {
126: return true;
127: }
128:
129: if (!this .getClass().equals(obj.getClass())) {
130: return false;
131: }
132:
133: Field that = (Field) obj;
134:
135: if (!this .parent.equals(that.parent)) {
136: return false;
137: }
138:
139: if (!this .name.equals(that.name)) {
140: return false;
141: }
142:
143: return true;
144: }
145:
146: /* (non-Javadoc)
147: * @see java.lang.Object#toString()
148: */
149: @Override
150: public String toString() {
151: return parent.toString() + "." + name;
152: }
153:
154: private Type parent;
155:
156: private String name;
157:
158: private Type type;
159:
160: private String value;
161: }
|