001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)DocumentImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl2.impl;
030:
031: import java.util.Map;
032: import java.util.List;
033: import java.util.ArrayList;
034:
035: import org.apache.xmlbeans.XmlException;
036: import org.apache.xmlbeans.XmlOptions;
037:
038: import org.w3.ns.wsdl.DocumentationType;
039:
040: import org.w3c.dom.DocumentFragment;
041: import org.w3c.dom.Element;
042:
043: /**
044: * Implementation of Documentation for a WSDL, component.
045: *
046: * @author Sun Microsystems, Inc.
047: */
048: final class DocumentImpl extends Document {
049: /** Documentation Xml bean. */
050: private DocumentationType mBean;
051:
052: /** The component which is documented by this component. */
053: private ExtensibleDocumentedComponent mParent;
054:
055: /** Container for this component */
056: private DescriptionImpl mContainer;
057:
058: /**
059: * Fetch the Xml bean for this component.
060: *
061: * @return The XML Bean for this documentation component.
062: */
063: DocumentationType getBean() {
064: return this .mBean;
065: }
066:
067: /**
068: * Construct a Document component from the given XmlBean.
069: *
070: * @param bean The XmlBean for this document component.
071: * @param parent The parent for which this component serves as
072: * documentation.
073: * @param defs The container for this component.
074: */
075: private DocumentImpl(DocumentationType bean,
076: ExtensibleDocumentedComponent parent, DescriptionImpl defs) {
077: this .mBean = bean;
078: this .mParent = parent;
079: this .mContainer = defs;
080: }
081:
082: /**
083: * Get DOM document fragment containing documentation.
084: *
085: * @return DOM document fragment containing documentation
086: */
087: public DocumentFragment getDocumentation() {
088: XmlOptions opts = new XmlOptions().setSaveOuter()
089: .setSaveAggresiveNamespaces();
090: DocumentFragment result = (DocumentFragment) getBean()
091: .newDomNode(opts);
092:
093: return result;
094: }
095:
096: /**
097: * Set DOM element containing documentation.
098: *
099: * @param theDocumentFragment DOM element containing documentation
100: */
101: public void setDocumentation(DocumentFragment theDocumentFragment) {
102: if (theDocumentFragment != null) {
103: DocumentationType newBean = null;
104:
105: try {
106: newBean = DocumentationType.Factory
107: .parse(theDocumentFragment);
108: } catch (XmlException ex) {
109: ; // Keep checkstyle happy.
110:
111: // Retry after wrapping the fragment in a documentation element
112: // (below).
113: }
114:
115: if (newBean == null) {
116: // Try wrapping in a wsdl documentation element.
117: org.w3c.dom.Document doc = theDocumentFragment
118: .getOwnerDocument();
119: Element elm = doc.createElementNS(
120: Constants.WSDL_NAMESPACE_NAME, "documentation");
121: elm.appendChild(theDocumentFragment);
122:
123: try {
124: newBean = DocumentationType.Factory.parse(elm);
125: } catch (XmlException ex) {
126: System.err
127: .println("Error parsing documentation fragment");
128: System.err.println(ex.getMessage());
129: System.err.println(ex.getError().toString());
130: ex.printStackTrace(System.err);
131: }
132:
133: }
134:
135: if (newBean != null) {
136: DocumentationType oldBean = getBean();
137: Map map = mContainer.getDocumentMap();
138:
139: synchronized (map) {
140: map.put(newBean, map.remove(oldBean));
141:
142: DocumentationType[] docTypeArray = mParent
143: .getExBean().getDocumentationArray();
144: ArrayList theList = new ArrayList();
145: if (docTypeArray != null) {
146: for (int i = 0; i < docTypeArray.length; ++i) {
147: theList.add(i, docTypeArray[i]);
148: }
149: }
150:
151: int theIndex = theList.indexOf(oldBean);
152: if (theIndex != -1)
153: theList.set(theIndex, newBean);
154: else
155: theList.add(newBean);
156:
157: mParent.getExBean().setDocumentationArray(
158: (DocumentationType[]) theList.toArray());
159: }
160: }
161: }
162: return; // $$TODO -- need to verify insertion point of theDocumentFragment
163: }
164:
165: /**
166: * A factory class for creating / finding components for given XML beans.
167: * <p>
168: * This factory guarantees that there will only be one component for each
169: * XML bean instance.
170: */
171: static class Factory {
172: /**
173: * Find the WSDL documentation component associated with the given XML
174: * bean, creating a new component if necessary.
175: * <p>
176: * This is thread-safe.<p>
177: *
178: * @param bean The XML bean to find the component for.
179: * @param parent The parent for which this component serves as
180: * documentation.
181: * @param defs The container for the component.
182: * @return The WSDL documentation component for the given <code>bean</code>
183: * (null if the <code>bean</code> is null).
184: */
185: static DocumentImpl getInstance(DocumentationType bean,
186: ExtensibleDocumentedComponent parent,
187: DescriptionImpl defs) {
188: DocumentImpl result;
189:
190: if (bean != null) {
191: Map map = defs.getDocumentMap();
192:
193: synchronized (map) {
194: result = (DocumentImpl) map.get(bean);
195:
196: if (result == null) {
197: result = new DocumentImpl(bean, parent, defs);
198: map.put(bean, result);
199: }
200: }
201: } else {
202: result = null;
203: }
204:
205: return result;
206: }
207: }
208: }
209:
210: // End-of-file: DocumentImpl.java
|