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