001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis.type;
019:
020: import java.util.Set;
021:
022: import javax.xml.namespace.QName;
023:
024: import org.apache.cxf.aegis.Context;
025: import org.apache.cxf.aegis.DatabindingException;
026: import org.apache.cxf.aegis.xml.MessageReader;
027: import org.apache.cxf.aegis.xml.MessageWriter;
028: import org.jdom.Element;
029:
030: /**
031: * A Type reads and writes XML fragments to create and write objects.
032: *
033: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
034: */
035: public abstract class Type {
036:
037: protected Class typeClass;
038:
039: private QName schemaType;
040:
041: private TypeMapping typeMapping;
042:
043: private boolean abstrct = true;
044:
045: private boolean nillable = true;
046:
047: private boolean writeOuter = true;
048:
049: public Type() {
050: }
051:
052: /**
053: * Read in the XML fragment and create an object.
054: *
055: * @param reader
056: * @param context
057: * @return
058: * @throws DatabindingException
059: */
060: public abstract Object readObject(MessageReader reader,
061: Context context) throws DatabindingException;
062:
063: /**
064: * Writes the object to the MessageWriter.
065: *
066: * @param object
067: * @param writer
068: * @param context
069: * @throws DatabindingException
070: */
071: public abstract void writeObject(Object object,
072: MessageWriter writer, Context context)
073: throws DatabindingException;
074:
075: public void writeSchema(Element root) {
076: }
077:
078: /**
079: * @return Returns the typeMapping.
080: */
081: public TypeMapping getTypeMapping() {
082: return typeMapping;
083: }
084:
085: /**
086: * @param typeMapping The typeMapping to set.
087: */
088: public void setTypeMapping(TypeMapping typeMapping) {
089: this .typeMapping = typeMapping;
090: }
091:
092: /**
093: * @return Returns the typeClass.
094: */
095: public Class getTypeClass() {
096: return typeClass;
097: }
098:
099: /**
100: * @param typeClass The typeClass to set.
101: */
102: public void setTypeClass(Class typeClass) {
103: this .typeClass = typeClass;
104:
105: if (typeClass.isPrimitive()) {
106: setNillable(false);
107: }
108: }
109:
110: /**
111: * @return True if a complex type schema must be written.
112: */
113: public boolean isComplex() {
114: return false;
115: }
116:
117: public boolean isAbstract() {
118: return abstrct;
119: }
120:
121: public void setAbstract(boolean ab) {
122: this .abstrct = ab;
123: }
124:
125: public boolean isNillable() {
126: return nillable;
127: }
128:
129: public void setNillable(boolean nillable) {
130: this .nillable = nillable;
131: }
132:
133: /**
134: * Return a set of Type dependencies. Returns null if this type has no
135: * dependencies.
136: *
137: * @return Set of <code>Type</code> dependencies
138: */
139: public Set<Type> getDependencies() {
140: return null;
141: }
142:
143: /**
144: * @see java.lang.Object#equals(java.lang.Object)
145: */
146: @Override
147: public boolean equals(Object obj) {
148: if (obj == this ) {
149: return true;
150: }
151:
152: if (obj instanceof Type) {
153: Type type = (Type) obj;
154:
155: if (type.getSchemaType().equals(getSchemaType())
156: && type.getTypeClass().equals(getTypeClass())) {
157: return true;
158: }
159: }
160:
161: return false;
162: }
163:
164: @Override
165: public int hashCode() {
166: int hashcode = 0;
167:
168: if (getTypeClass() != null) {
169: hashcode ^= getTypeClass().hashCode();
170: }
171:
172: if (getSchemaType() != null) {
173: hashcode ^= getSchemaType().hashCode();
174: }
175:
176: return hashcode;
177: }
178:
179: /**
180: * @return Get the schema type.
181: */
182: public QName getSchemaType() {
183: return schemaType;
184: }
185:
186: /**
187: * @param name The qName to set.
188: */
189: public void setSchemaType(QName name) {
190: schemaType = name;
191: }
192:
193: public boolean isWriteOuter() {
194: return writeOuter;
195: }
196:
197: public void setWriteOuter(boolean writeOuter) {
198: this .writeOuter = writeOuter;
199: }
200:
201: @Override
202: public String toString() {
203: StringBuffer sb = new StringBuffer(getClass().getName());
204: sb.append("[class=");
205: Class c = getTypeClass();
206: sb.append((c == null) ? "<null>" : c.getName());
207: sb.append(",\nQName=");
208: QName q = getSchemaType();
209: sb.append((q == null) ? "<null>" : q.toString());
210: sb.append("]");
211: return sb.toString();
212: }
213: }
|