001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.ws.wsdl;
030:
031: import java.util.Collection;
032: import java.util.List;
033:
034: import org.jibx.binding.generator.CustomBase;
035: import org.jibx.binding.model.IClass;
036: import org.jibx.binding.model.IClassItem;
037: import org.jibx.binding.model.IClassLocator;
038: import org.jibx.runtime.IUnmarshallingContext;
039: import org.jibx.runtime.JiBXException;
040: import org.jibx.runtime.impl.UnmarshallingContext;
041: import org.jibx.util.Types;
042:
043: /**
044: * Fault data customization information.
045: */
046: public class FaultCustom extends CustomBase {
047: // fault data customization information
048: private String m_exceptionType;
049: private String m_fieldName;
050: private String m_dataType;
051: private String m_faultName;
052: private String m_elementName;
053: private List m_documentation;
054:
055: /**
056: * Constructor.
057: *
058: * @param parent
059: * @param type fully-qualified exception class name
060: */
061: protected FaultCustom(NestingBase parent, String type) {
062: super (parent);
063: m_exceptionType = type;
064: }
065:
066: /**
067: * Get fully-qualified exception class name.
068: *
069: * @return type
070: */
071: public String getExceptionType() {
072: return m_exceptionType;
073: }
074:
075: /**
076: * Get Fault name. This method should only be used after the {@link
077: * #apply(IClassLocator)} method is called.
078: *
079: * @return parmaterized type
080: */
081: public String getFaultName() {
082: return m_faultName;
083: }
084:
085: /**
086: * Get XML element name for exception data. This method should only be used
087: * after the {@link #apply(IClassLocator)} method is called.
088: *
089: * @return name
090: */
091: public String getElementName() {
092: return m_elementName;
093: }
094:
095: /**
096: * Get fully-qualified name of exception data class.
097: *
098: * @return parmaterized type
099: */
100: public String getDataType() {
101: return m_dataType;
102: }
103:
104: /**
105: * Get value documentation node list. This method should only be used after
106: * the {@link #apply(IClassLocator)} method is called.
107: *
108: * @return list of documentation nodes (<code>null</code> if none)
109: */
110: public List getDocumentation() {
111: return m_documentation;
112: }
113:
114: /**
115: * Apply customizations to fault to fill out members.
116: *
117: * @param icl class locator
118: */
119: public void apply(IClassLocator icl) {
120: String simple = m_exceptionType.substring(m_exceptionType
121: .lastIndexOf('.') + 1);
122: if (simple.endsWith("Exception")) {
123: simple = simple.substring(0, simple.length() - 9);
124: }
125: if (m_elementName == null) {
126: m_elementName = getParent().convertName(simple);
127: }
128: if (m_faultName == null) {
129: m_faultName = m_elementName + "Fault";
130: }
131: IClass clas = icl.getClassInfo(m_exceptionType);
132: if (m_fieldName == null) {
133: IClassItem[] fields = clas.getFields();
134: for (int i = 0; i < fields.length; i++) {
135: IClassItem item = fields[i];
136: String type = item.getTypeName();
137: if (!Types.isSimpleValue(type)) {
138: IClass info = icl.getClassInfo(type);
139: if (info.isModifiable()) {
140: m_fieldName = item.getName();
141: m_dataType = type;
142: break;
143: }
144: }
145: }
146: if (m_fieldName == null) {
147: throw new IllegalStateException(
148: "No data object field found for exception class "
149: + m_exceptionType);
150: }
151: } else {
152: IClassItem field = clas.getField(m_fieldName);
153: if (field == null) {
154: throw new IllegalStateException("Field " + m_fieldName
155: + " not found in exception class "
156: + m_exceptionType);
157: } else {
158: m_dataType = field.getTypeName();
159: }
160: }
161: }
162:
163: /**
164: * Parameter value unmarshalling factory. This gets the containing element
165: * and the name so that the standard constructor can be used.
166: *
167: * @param ictx
168: * @return created instance
169: * @throws JiBXException
170: */
171: private static FaultCustom throwsFactory(IUnmarshallingContext ictx)
172: throws JiBXException {
173: UnmarshallingContext uctx = (UnmarshallingContext) ictx;
174: Object parent = uctx.getStackTop();
175: int depth = 0;
176: if (parent instanceof Collection) {
177: parent = uctx.getStackObject(++depth);
178: }
179: return new FaultCustom((OperationCustom) parent, uctx
180: .attributeText(null, "class"));
181: }
182: }
|