001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.sql.project.wsdl;
021:
022: import java.io.FileOutputStream;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.xml.namespace.QName;
028:
029: public class JBIFileWriter {
030:
031: private String mJbiDescriptorFile;
032:
033: private String sqlMapFile;
034:
035: private String mbuildDir;
036: // keyed by prefix
037: private Map prefixTable = new HashMap();
038:
039: // keyed by name space
040: private Map nsTable = new HashMap();
041:
042: private FileOutputStream fos = null;
043:
044: public JBIFileWriter(String mJbiDescriptorFile, String sqlMapFile,
045: String mBuildDir) {
046: this .mJbiDescriptorFile = mJbiDescriptorFile;
047: this .sqlMapFile = sqlMapFile;
048: this .mbuildDir = mBuildDir;
049: }
050:
051: public JBIFileWriter(String mJbiDescriptorFile, String sqlMapFile) {
052: this .mJbiDescriptorFile = mJbiDescriptorFile;
053: this .sqlMapFile = sqlMapFile;
054: }
055:
056: public void writeJBI() {
057:
058: List sqlEntryList = null;
059: try {
060: sqlEntryList = SQLMapReader.parse("sqlmap.xml", mbuildDir);
061: populatePrefixAndNamespaceTable(sqlEntryList);
062: generateJBI(sqlEntryList);
063: } catch (Exception e1) {
064: e1.printStackTrace();
065: }
066:
067: }
068:
069: /**
070: * @param sqlEntryList
071: * @throws BuildException
072: */
073: private void populatePrefixAndNamespaceTable(List sqlEntryList)
074: throws Exception {
075: try {
076:
077: // Populate prefixTable
078: int nsIndex = 1;
079: for (int i = 0, I = sqlEntryList.size(); i < I; i++) {
080: SQLMapEntry entry = (SQLMapEntry) sqlEntryList.get(i);
081: String ns = entry.getPartnerLink().getNamespaceURI();
082: if (ns != null && !ns.trim().equals("")
083: && !nsTable.containsKey(ns)) {
084: nsTable.put(ns, "ns" + nsIndex);
085: prefixTable.put("ns" + nsIndex, ns);
086: nsIndex++;
087: }
088:
089: ns = entry.getPortType().getNamespaceURI();
090: if (ns != null && !ns.trim().equals("")
091: && !nsTable.containsKey(ns)) {
092: nsTable.put(ns, "ns" + nsIndex);
093: prefixTable.put("ns" + nsIndex, ns);
094: nsIndex++;
095: }
096:
097: if (entry.getType() != SQLMapEntry.REQUEST_REPLY_SERVICE) {
098: ns = entry.getPartnerLink().getNamespaceURI();
099: if (ns != null && !ns.trim().equals("")
100: && !nsTable.containsKey(ns)) {
101: nsTable.put(ns, "ns" + nsIndex);
102: prefixTable.put("ns" + nsIndex, ns);
103: nsIndex++;
104: }
105:
106: ns = entry.getPortType().getNamespaceURI();
107: if (ns != null && !ns.trim().equals("")
108: && !nsTable.containsKey(ns)) {
109: nsTable.put(ns, "ns" + nsIndex);
110: prefixTable.put("ns" + nsIndex, ns);
111: nsIndex++;
112: }
113: }
114: }
115:
116: } catch (Exception e) {
117: throw new Exception(e.getMessage());
118: }
119: }
120:
121: /**
122: * @param sqlEntryList
123: * @throws Exception
124: */
125: private void generateJBI(List sqlEntryList) throws Exception {
126: // Generate jbi.xml
127: // <?xml version='1.0'?>
128: // <jbi version="1.0"
129: // xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
130: // xmlns="http://java.sun.com/xml/ns/jbi"
131: // xsi:schemaLocation="http://java.sun.com/xml/ns/jbi jbi.xsd"
132: // xmlns:ns0=${ns1} ... xmlns:nsN=${nsN} >
133: // <services binding-component="false">
134: // <provides interface-name=port-type service-name=partner-link
135: // endpoint-name=role-name/>
136: // <consumes interface-name=port-type service-name=partner-link
137: // endpoint-name=role-name link-type="standard"/>
138: // </services>
139: // </jbi>
140: try {
141: StringBuffer sb = new StringBuffer();
142: sb.append("<!--start of generated code -->\n");
143: sb.append("<jbi version=\"1.0\"\n");
144: sb
145: .append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
146: sb
147: .append(" xmlns=\"http://java.sun.com/xml/ns/jbi\"\n");
148: sb
149: .append(" xsi:schemaLocation=\"http://java.sun.com/xml/ns/jbi jbi.xsd\"\n");
150: for (int i = 0, I = nsTable.size(); i < I; i++) {
151: String ns = "ns" + (i + 1);
152: sb.append(" xmlns:" + ns + "=\""
153: + prefixTable.get(ns) + "\"");
154: if (i < I - 1) {
155: sb.append("\n");
156: }
157: }
158: sb.append(">\n");
159: sb.append(" <services binding-component=\"false\">\n");
160: // Generate all <provides> first
161: //for (int i = 0, I = sqlEntryList.size(); i < I; i++) {
162: SQLMapEntry xme = (SQLMapEntry) sqlEntryList.get(0);
163: sb.append(" <provides interface-name=\""
164: + getDottedQName(xme.getPortType(), nsTable));
165: sb.append("\" service-name=\""
166: + getDottedQName(xme.getPartnerLink(), nsTable));
167: sb.append("\" endpoint-name=\"" + xme.getRoleName());
168: sb.append("\"/>\n");
169: //}
170: // Generate all <consumes> second
171: //for (int i = 0, I = sqlEntryList.size(); i < I; i++) {
172: // SQLMapEntry xme = (SQLMapEntry) sqlEntryList.get(0);
173: if (!xme.getType()
174: .equals(SQLMapEntry.REQUEST_REPLY_SERVICE)) {
175: sb.append(" <consumes interface-name=\""
176: + getDottedQName(xme.getPortType(), nsTable));
177: sb
178: .append("\" service-name=\""
179: + getDottedQName(xme.getPartnerLink(),
180: nsTable));
181: sb.append("\" endpoint-name=\"" + xme.getRoleName());
182: sb.append("\" link-type=\"standard\"/>\n");
183: }
184: //}
185: sb.append(" </services>\n");
186: sb.append(" </jbi>\n");
187: sb.append("<!--end of generated code -->\n");
188:
189: String content = sb.toString();
190: fos = new FileOutputStream(mJbiDescriptorFile);
191: FileUtil.copy(content.getBytes("UTF-8"), fos);
192: } catch (Exception e) {
193: throw e;
194: } finally {
195: if (fos != null) {
196: try {
197: fos.close();
198: } catch (Exception e1) {
199: e1.printStackTrace();
200: }
201: }
202: }
203: }
204:
205: private static String getDottedQName(QName qn, Map nsTable) {
206: String ns = qn.getNamespaceURI();
207: String prefix = (String) nsTable.get(ns);
208: if (prefix == null) {
209: return qn.getLocalPart();
210: }
211: return prefix + ":" + qn.getLocalPart();
212: }
213:
214: public static void main(String[] args) {
215: JBIFileWriter fw = new JBIFileWriter("test/jbi.xml",
216: "test/sqlmap.xml", "");
217: fw.writeJBI();
218:
219: }
220:
221: }
|