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-2007 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: package org.netbeans.modules.etl.project.anttasks;
042:
043: import java.io.FileOutputStream;
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.Map;
047:
048: import javax.xml.namespace.QName;
049:
050: import org.apache.tools.ant.BuildException;
051:
052: /**
053: *
054: */
055:
056: public class JBIFileWriter {
057:
058: private String mJbiDescriptorFile;
059:
060: private String etlMapFile;
061:
062: // keyed by prefix
063: private Map<String, String> prefixTable = new HashMap<String, String>();
064:
065: // keyed by name space
066: private Map<String, String> nsTable = new HashMap<String, String>();
067:
068: private FileOutputStream fos = null;
069:
070: public JBIFileWriter(String mJbiDescriptorFile, String etlMapFile) {
071: this .mJbiDescriptorFile = mJbiDescriptorFile;
072: this .etlMapFile = etlMapFile;
073: }
074:
075: public void writeJBI() {
076:
077: List<ETLMapEntry> etlEntryList = null;
078: try {
079: etlEntryList = ETLMapReader.parse(etlMapFile);
080: populatePrefixAndNamespaceTable(etlEntryList);
081: generateJBI(etlEntryList);
082: } catch (Exception e1) {
083: e1.printStackTrace();
084: }
085:
086: }
087:
088: /**
089: * @param etlEntryList
090: * @throws BuildException
091: */
092: private void populatePrefixAndNamespaceTable(
093: List<ETLMapEntry> etlEntryList) throws BuildException {
094: try {
095:
096: // Populate prefixTable
097: int nsIndex = 1;
098: for (int i = 0, I = etlEntryList.size(); i < I; i++) {
099: ETLMapEntry entry = etlEntryList.get(i);
100: String ns = entry.getPartnerLink().getNamespaceURI();
101: if (ns != null && !ns.trim().equals("")
102: && !nsTable.containsKey(ns)) {
103: nsTable.put(ns, "ns" + nsIndex);
104: prefixTable.put("ns" + nsIndex, ns);
105: nsIndex++;
106: }
107:
108: ns = entry.getPortType().getNamespaceURI();
109: if (ns != null && !ns.trim().equals("")
110: && !nsTable.containsKey(ns)) {
111: nsTable.put(ns, "ns" + nsIndex);
112: prefixTable.put("ns" + nsIndex, ns);
113: nsIndex++;
114: }
115:
116: if (entry.getType() != ETLMapEntry.REQUEST_REPLY_SERVICE) {
117: ns = entry.getPartnerLink().getNamespaceURI();
118: if (ns != null && !ns.trim().equals("")
119: && !nsTable.containsKey(ns)) {
120: nsTable.put(ns, "ns" + nsIndex);
121: prefixTable.put("ns" + nsIndex, ns);
122: nsIndex++;
123: }
124:
125: ns = entry.getPortType().getNamespaceURI();
126: if (ns != null && !ns.trim().equals("")
127: && !nsTable.containsKey(ns)) {
128: nsTable.put(ns, "ns" + nsIndex);
129: prefixTable.put("ns" + nsIndex, ns);
130: nsIndex++;
131: }
132: }
133: }
134:
135: } catch (Exception e) {
136: throw new BuildException(e.getMessage());
137: }
138: }
139:
140: /**
141: * @param etlEntryList
142: * @throws Exception
143: */
144: private void generateJBI(List<ETLMapEntry> etlEntryList)
145: throws Exception {
146: // Generate jbi.xml
147: // <?xml version='1.0'?>
148: // <jbi version="1.0"
149: // xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
150: // xmlns="http://java.sun.com/xml/ns/jbi"
151: // xsi:schemaLocation="http://java.sun.com/xml/ns/jbi jbi.xsd"
152: // xmlns:ns0=${ns1} ... xmlns:nsN=${nsN} >
153: // <services binding-component="false">
154: // <provides interface-name=port-type service-name=partner-link
155: // endpoint-name=role-name/>
156: // <consumes interface-name=port-type service-name=partner-link
157: // endpoint-name=role-name link-type="standard"/>
158: // </services>
159: // </jbi>
160: try {
161: StringBuffer sb = new StringBuffer();
162: sb.append("<!--start of generated code -->\n");
163: sb.append("<jbi version=\"1.0\"\n");
164: sb
165: .append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
166: sb
167: .append(" xmlns=\"http://java.sun.com/xml/ns/jbi\"\n");
168: sb
169: .append(" xsi:schemaLocation=\"http://java.sun.com/xml/ns/jbi jbi.xsd\"\n");
170: for (int i = 0, I = nsTable.size(); i < I; i++) {
171: String ns = "ns" + (i + 1);
172: sb.append(" xmlns:" + ns + "=\""
173: + prefixTable.get(ns) + "\"");
174: if (i < I - 1) {
175: sb.append("\n");
176: }
177: }
178: sb.append(">\n");
179: sb.append(" <services binding-component=\"false\">\n");
180: // Generate all <provides> first
181: for (int i = 0, I = etlEntryList.size(); i < I; i++) {
182: ETLMapEntry xme = (ETLMapEntry) etlEntryList.get(i);
183: sb.append(" <provides interface-name=\""
184: + getDottedQName(xme.getPortType(), nsTable));
185: sb
186: .append("\" service-name=\""
187: + getDottedQName(xme.getPartnerLink(),
188: nsTable));
189: sb.append("\" endpoint-name=\"" + xme.getRoleName());
190: sb.append("\"/>\n");
191: }
192: // Generate all <consumes> second
193: for (int i = 0, I = etlEntryList.size(); i < I; i++) {
194: ETLMapEntry xme = (ETLMapEntry) etlEntryList.get(i);
195: if (!xme.getType().equals(
196: ETLMapEntry.REQUEST_REPLY_SERVICE)) {
197: sb
198: .append(" <consumes interface-name=\""
199: + getDottedQName(xme.getPortType(),
200: nsTable));
201: sb.append("\" service-name=\""
202: + getDottedQName(xme.getPartnerLink(),
203: nsTable));
204: sb
205: .append("\" endpoint-name=\""
206: + xme.getRoleName());
207: sb.append("\" link-type=\"standard\"/>\n");
208: }
209: }
210: sb.append(" </services>\n");
211: sb.append(" </jbi>\n");
212: sb.append("<!--end of generated code -->\n");
213:
214: String content = sb.toString();
215: fos = new FileOutputStream(mJbiDescriptorFile);
216: FileUtil.copy(content.getBytes("UTF-8"), fos);
217: } catch (Exception e) {
218: throw e;
219: } finally {
220: if (fos != null) {
221: try {
222: fos.close();
223: } catch (Exception e1) {
224: e1.printStackTrace();
225: }
226: }
227: }
228: }
229:
230: private static String getDottedQName(QName qn, Map nsTable) {
231: String ns = qn.getNamespaceURI();
232: String prefix = (String) nsTable.get(ns);
233: if (prefix == null) {
234: return qn.getLocalPart();
235: }
236: return prefix + ":" + qn.getLocalPart();
237: }
238:
239: public static void main(String[] args) {
240: JBIFileWriter fw = new JBIFileWriter("test/jbi.xml",
241: "test/etlmap.xml");
242: fw.writeJBI();
243:
244: }
245:
246: }
|