001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * ConfigurationWriter.java
044: *
045: * Created on July 11, 2005, 10:08 AM
046: *
047: */
048: package org.netbeans.modules.mobility.end2end.client.config;
049:
050: import java.io.IOException;
051: import java.io.OutputStream;
052: import java.util.Iterator;
053: import java.util.List;
054: import java.util.Properties;
055: import java.util.Set;
056:
057: import org.netbeans.modules.mobility.end2end.classdata.AbstractService;
058: import org.netbeans.modules.mobility.end2end.classdata.ClassData;
059: import org.netbeans.modules.mobility.end2end.classdata.MethodData;
060: import org.netbeans.modules.mobility.end2end.classdata.OperationData;
061: import org.netbeans.modules.mobility.end2end.classdata.PortData;
062: import org.netbeans.modules.mobility.end2end.classdata.TypeData;
063: import org.netbeans.modules.mobility.end2end.classdata.WSDLService;
064: import org.openide.ErrorManager;
065: import org.openide.xml.XMLUtil;
066: import org.w3c.dom.Document;
067: import org.w3c.dom.Element;
068: import org.w3c.dom.Node;
069:
070: /**
071: *
072: * @author Michal Skvor
073: */
074: public class ConfigurationWriter {
075:
076: private final static String TYPE = "type";
077: private final static String NAME = "name";
078:
079: private ConfigurationWriter() {
080: //to avoid instantiation
081: }
082:
083: public synchronized static void write(final OutputStream os,
084: final Configuration configuration) throws Exception {
085: final Document doc = XMLUtil.createDocument("wsclientconfig",
086: null, null, null); //NOI18N
087:
088: // Root Element
089: ///Element root = doc.createElement( "wsclientconfig" );
090: final Element root = doc.getDocumentElement();
091: root.setAttribute("version", "1.0"); //NOI18N
092: final String serviceType = configuration.getServiceType();
093: root.setAttribute("serviceType", serviceType); //NOI18N
094:
095: // Client config
096: root.appendChild(serializeClient(doc, configuration
097: .getClientConfiguration()));
098:
099: // Server config
100: if (!Configuration.JSR172_TYPE.equals(configuration
101: .getServiceType())) {
102: root.appendChild(serializeServer(doc, configuration
103: .getServerConfigutation()));
104: }
105:
106: // services
107: root.appendChild(serializeServices(doc, configuration
108: .getServices(), serviceType));
109:
110: try {
111: XMLUtil.write(doc, os, "UTF-8"); //NOI18N
112: } catch (IOException ioEx) {
113: ErrorManager.getDefault().notify(ioEx);
114: }
115: }
116:
117: private static Node serializeClient(final Document doc,
118: final ClientConfiguration clientConfiguration) {
119: final Element cc = doc.createElement("client"); // NOI18N
120: final Element project = doc.createElement("project"); // NOI18N
121: final Node name = doc.createTextNode(clientConfiguration
122: .getProjectName());
123: project.appendChild(name);
124: cc.appendChild(project);
125:
126: final Element clazz = doc.createElement("class"); // NOI18N
127: final ClassDescriptor cd = clientConfiguration
128: .getClassDescriptor();
129: clazz.setAttribute(TYPE, cd.getType()); // NOI18N
130: clazz.setAttribute("location", cd.getLocation()); // NOI18N
131: cc.appendChild(clazz);
132:
133: final Properties properties = clientConfiguration
134: .getProperties();
135: final Set keys = properties.keySet();
136: for (final Iterator<String> i = keys.iterator(); i.hasNext();) {
137: final String key = i.next();
138: final String value = properties.getProperty(key);
139: final Element property = doc.createElement("property"); // NOI18N
140: property.setAttribute(NAME, key); // NOI18N
141: property.setAttribute("value", value); // NOI18N
142:
143: cc.appendChild(property);
144: }
145:
146: return cc;
147: }
148:
149: private static Node serializeServer(final Document doc,
150: final ServerConfiguration serverConfiguration) {
151: final Element cc = doc.createElement("server"); // NOI18N
152:
153: final Element project = doc.createElement("project"); // NOI18N
154: project.setAttribute("path", serverConfiguration
155: .getProjectPath()); // NOI18N
156: final Node name = doc.createTextNode(serverConfiguration
157: .getProjectName());
158: project.appendChild(name);
159: cc.appendChild(project);
160:
161: final Element clazz = doc.createElement("class"); // NOI18N
162: final ClassDescriptor cd = serverConfiguration
163: .getClassDescriptor();
164: clazz.setAttribute(TYPE, cd.getType()); // NOI18N
165: clazz.setAttribute("location", cd.getLocation()); // NOI18N
166: clazz.setAttribute("mapping", cd.getMapping()); // NOI18N
167: cc.appendChild(clazz);
168:
169: final Properties properties = serverConfiguration
170: .getProperties();
171: final Set keys = properties.keySet();
172: for (final Iterator<String> i = keys.iterator(); i.hasNext();) {
173: final String key = i.next();
174: final String value = properties.getProperty(key);
175: final Element property = doc.createElement("property"); // NOI18N
176: property.setAttribute(NAME, key); // NOI18N
177: property.setAttribute("value", value); // NOI18N
178:
179: cc.appendChild(property);
180: }
181:
182: return cc;
183: }
184:
185: private static Node serializeServices(final Document doc,
186: final List<AbstractService> services,
187: final String serviceType) {
188: final Element ss = doc.createElement("services"); // NOI18N
189:
190: for (int i = 0; i < services.size(); i++) {
191: final Element s = doc.createElement("service"); // NOI18N
192: if (Configuration.CLASS_TYPE.equals(serviceType)) {
193: final AbstractService service = services.get(i);
194: final List<ClassData> classes = service.getData();
195: for (final ClassData classData : classes) {
196: final Element clazz = doc.createElement("class"); // NOI18N
197: clazz.setAttribute(TYPE, classData.getType()); // NOI18N
198: final List<OperationData> methods = classData
199: .getOperations();
200: for (final MethodData method : methods) {
201: final Element m = doc.createElement("method"); // NOI18N
202: m.setAttribute(NAME, method.getName()); // NOI18N
203:
204: final Element returnType = doc
205: .createElement("return"); // NOI18N
206: returnType.setAttribute(TYPE, method
207: .getReturnType()); // NOI18N
208: m.appendChild(returnType);
209:
210: final List<TypeData> returnTypes = method
211: .getParameterTypes();
212: for (final TypeData td : returnTypes) {
213: final Element param = doc
214: .createElement("param"); // NOI18N
215: param.setAttribute(NAME, td.getName()); // NOI18N
216: param.setAttribute(TYPE, td.getType()); // NOI18N
217: m.appendChild(param);
218: }
219: clazz.appendChild(m);
220: }
221: s.appendChild(clazz);
222: }
223: ss.appendChild(s);
224:
225: } else if (Configuration.WSDLCLASS_TYPE.equals(serviceType)) {
226: final WSDLService service = (WSDLService) services
227: .get(i);
228: s.setAttribute("url", service.getUrl()); // NOI18N
229: s.setAttribute("file", service.getFile()); // NOI18N
230: s.setAttribute(NAME, service.getName()); // NOI18N
231: s.setAttribute(TYPE, service.getType()); // NOI18N
232:
233: final List<ClassData> ports = service.getData();
234: for (final ClassData clData : ports) {
235: final PortData portData = (PortData) clData;
236: final Element port = doc.createElement("port"); // NOI18N
237: port.setAttribute(TYPE, portData.getType()); // NOI18N
238: port.setAttribute(NAME, portData.getName()); // NOI18N
239: final List<OperationData> operations = portData
240: .getOperations();
241: for (final OperationData operation : operations) {
242: final Element m = doc
243: .createElement("operation"); // NOI18N
244: m.setAttribute("method", operation
245: .getMethodName()); // NOI18N
246: m.setAttribute(NAME, operation.getName()); // NOI18N
247: final Element returnType = doc
248: .createElement("return"); // NOI18N
249: returnType.setAttribute(TYPE, operation
250: .getReturnType()); // NOI18N
251: m.appendChild(returnType);
252:
253: final List<TypeData> returnTypes = operation
254: .getParameterTypes();
255: for (final TypeData td : returnTypes) {
256: final Element param = doc
257: .createElement("param"); // NOI18N
258: param.setAttribute(NAME, td.getName()); // NOI18N
259: param.setAttribute(TYPE, td.getType()); // NOI18N
260: m.appendChild(param);
261: }
262: port.appendChild(m);
263: }
264: s.appendChild(port);
265: }
266: ss.appendChild(s);
267: } else if (Configuration.JSR172_TYPE.equals(serviceType)) {
268: final WSDLService service = (WSDLService) services
269: .get(i);
270: s.setAttribute("url", service.getUrl()); // NOI18N
271: s.setAttribute("file", service.getFile()); // NOI18N
272: ss.appendChild(s);
273: }
274: }
275:
276: return ss;
277: }
278: }
|