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: package org.netbeans.modules.xslt.project.anttasks;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.logging.Logger;
032: import javax.xml.namespace.QName;
033: import org.apache.tools.ant.BuildException;
034: import org.netbeans.modules.xslt.tmap.model.xsltmap.TransformationDescType;
035: import org.netbeans.modules.xslt.tmap.model.xsltmap.XmlUtil;
036: import org.netbeans.modules.xslt.tmap.model.xsltmap.XsltMapConst;
037: import org.netbeans.modules.xslt.project.XsltproConstants;
038: import org.netbeans.modules.xslt.project.anttasks.jbi.TMapServiceEntry;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.NamedNodeMap;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043:
044: /**
045: *
046: * @author Vitaly Bychkov
047: * @version 1.0
048: */
049: public class OldProjectTransformer {
050: /**
051: * Map of namespace to its prefix
052: */
053: private Map<String, String> mNameSpacePrefix = new HashMap<String, String>();
054:
055: // Member variable representing source directory
056: /**
057: * Source directory
058: */
059: private String mSourceDirectory = null;
060: // Member variable representing build directory
061: /**
062: * Build directory
063: */
064: private String mBuildDirectory = null;
065: // Member variable representing project classpath
066:
067: private List<TMapServiceEntry> mProviders = new ArrayList<TMapServiceEntry>();
068:
069: private List<TMapServiceEntry> mConsumers = new ArrayList<TMapServiceEntry>();
070:
071: /**
072: * Logger instance
073: */
074: private Logger logger = Logger
075: .getLogger(OldProjectTransformer.class.getName());
076:
077: public static final String VERSION_ATTR_NAME = "version"; // NOI18N
078: public static final String VERSION_ATTR_VALUE = "1.0"; // NOI18N
079: public static final String NS_ATTR_NAME = "xmlns"; // NOI18N
080: public static final String NS_ATTR_VALUE = "http://java.sun.com/xml/ns/jbi"; // NOI18N
081: public static final String NS_XSI_ATTR_NAME = "xmlns:xsi"; // NOI18N
082: public static final String NS_XSI_ATTR_VALUE = "http://www.w3.org/2001/XMLSchema-instance"; // NOI18N
083: public static final String XSI_ATTR_NAME = "xsi:schemaLocation"; // NOI18N
084: public static final String XSI_ATTR_VALUE = "http://java.sun.com/xml/ns/jbi jbi.xsd"; // NOI18N
085:
086: public static final String NAMESPACE_PREFIX = "ns"; // NOI18N
087: public static final String COLON_SEPARATOR = ":"; // NOI18N
088:
089: public OldProjectTransformer(String srcDir, String buildDir) {
090: mSourceDirectory = srcDir;
091: mBuildDirectory = buildDir;
092: }
093:
094: /**
095: * Set the build directory
096: * @param buildDir build directory
097: */
098: public void setBuildDirectory(String buildDir) {
099: mBuildDirectory = buildDir;
100: }
101:
102: /**
103: * Get the build directory
104: * @return String value of the build directory
105: */
106: public String getBuildDirectory() {
107: return mBuildDirectory;
108: }
109:
110: /**
111: * Set the source directory
112: * @param srcDir source directory
113: */
114: public void setSourceDirectory(String srcDir) {
115: this .mSourceDirectory = srcDir;
116: }
117:
118: /**
119: * Get the source directory
120: * @return String value of the source directory
121: */
122: public String getSourceDirectory() {
123: return this .mSourceDirectory;
124: }
125:
126: public void execute() throws BuildException {
127: File xsltMapFile = getXsltMapFile();
128:
129: Document document = null;
130: if (xsltMapFile != null && xsltMapFile.exists()) {
131: document = XmlUtil.getDocument(xsltMapFile);
132: }
133:
134: if (document != null) {
135: NodeList inputNodeList = document
136: .getElementsByTagName(TransformationDescType.INPUT
137: .getTagName());
138: if (inputNodeList != null && inputNodeList.getLength() > 0) {
139: populateProviderServices(xsltMapFile.getParentFile(),
140: inputNodeList);
141: }
142: NodeList outputNodeList = document
143: .getElementsByTagName(TransformationDescType.OUTPUT
144: .getTagName());
145: if (outputNodeList != null
146: && outputNodeList.getLength() > 0) {
147: populateConsumerServices(xsltMapFile.getParentFile(),
148: outputNodeList);
149: }
150: }
151: try {
152:
153: generateTransformMap();
154: } catch (IOException ex) {
155: throw new BuildException(ex);
156: }
157: }
158:
159: private void generateTransformMap() throws IOException {
160: FileOutputStream fos = null;
161: try {
162:
163: StringBuffer sb = new StringBuffer();
164: sb.append("<transformmap \n");
165: sb
166: .append(" xmlns=\"http://xml.netbeans.org/schema/transformmap\"\n");
167:
168: int nss = mNameSpacePrefix.size();
169: int i = 0;
170: Set<String> nsUris = mNameSpacePrefix.keySet();
171: for (String nsUri : nsUris) {
172: sb.append(" xmlns:"
173: + mNameSpacePrefix.get(nsUri) + "=\"" + nsUri
174: + "\"");
175: i++;
176: if (i < nss - 1) {
177: sb.append("\n");
178: }
179: }
180:
181: sb.append(">\n");
182:
183: if (mProviders != null) {
184:
185: List<TMapServiceEntry> uniqueServices = getUniqueServices(mProviders);
186: for (TMapServiceEntry uniqueService : uniqueServices) {
187: sb.append(" <service partnerLinkType=\"")
188: .append(
189: getColonedQName(uniqueService
190: .getPartnerLinkNameQname(),
191: mNameSpacePrefix)).append(
192: "\"");
193: sb.append(" roleName=\"").append(
194: uniqueService.getRoleName()).append(
195: "\" >\n");
196:
197: sb.append(getServiceOperations(mProviders,
198: uniqueService));
199:
200: sb.append(" </service>\n");
201:
202: }
203: }
204:
205: sb.append("</transformmap>\n");
206: String content = sb.toString();
207: fos = new FileOutputStream(getTransformMapFile());
208: store(content.getBytes("UTF-8"), fos);
209: } finally {
210: if (fos != null) {
211: fos.close();
212: }
213: }
214: }
215:
216: private List<TMapServiceEntry> getUniqueServices(
217: List<TMapServiceEntry> allServices) {
218: List<TMapServiceEntry> uniqueServices = new ArrayList<TMapServiceEntry>();
219: if (allServices == null) {
220: return uniqueServices;
221: }
222:
223: for (int j = 0; j < allServices.size(); j++) {
224: TMapServiceEntry tmpService = allServices.get(j);
225: boolean isUnique = true;
226: for (TMapServiceEntry uniqueServiceEntry : uniqueServices) {
227: QName servicePltQname = tmpService
228: .getPartnerLinkNameQname();
229: String serviceRoleName = tmpService.getRoleName();
230: if (servicePltQname != null
231: && servicePltQname.equals(uniqueServiceEntry
232: .getPartnerLinkNameQname())
233: && serviceRoleName != null
234: && serviceRoleName.equals(uniqueServiceEntry
235: .getRoleName())) {
236: isUnique = false;
237: }
238: }
239:
240: if (isUnique) {
241: uniqueServices.add(tmpService);
242: }
243: }
244:
245: return uniqueServices;
246: }
247:
248: private String getServiceOperations(
249: List<TMapServiceEntry> allServices,
250: TMapServiceEntry uniqueService) {
251: assert uniqueService != null && allServices != null;
252: assert uniqueService.getPartnerLinkNameQname() != null
253: && uniqueService.getRoleName() != null;
254: StringBuffer serviceOperations = new StringBuffer("");
255:
256: for (TMapServiceEntry service : allServices) {
257:
258: if (uniqueService.getRoleName().equals(
259: service.getRoleName())
260: && uniqueService.getPartnerLinkNameQname().equals(
261: service.getPartnerLinkNameQname())) {
262: TMapServiceEntry invoke = getInvoke(service);
263:
264: StringBuffer invokeSb = new StringBuffer();
265: if (invoke != null) {
266: invokeSb.append(
267: " <invoke partnerLinkType=\"")
268: .append(
269: getColonedQName(invoke
270: .getPartnerLinkNameQname(),
271: mNameSpacePrefix));
272: invokeSb.append(" opName=\"").append(
273: invoke.getOperation()).append("\" ");
274: invokeSb.append(" roleName=\"").append(
275: invoke.getRoleName()).append("\" ");
276: invokeSb.append(" file=\"")
277: .append(invoke.getFile()).append("\" ");
278: invokeSb.append(" transformJBI=\"").append(
279: invoke.getTransformJBI()).append("\" ");
280: } else {
281: invokeSb.append("/>\n");
282: }
283:
284: StringBuffer sbOperation = new StringBuffer();
285: sbOperation.append(" <operation");
286: sbOperation.append(" opName=\"").append(
287: service.getOperation()).append("\" ");
288: // sbOperation.append(" file=\"").
289: // append(service.getFile()).
290: // append("\" ");
291: // sbOperation.append(" transformJBI=\"").
292: // append(service.getTransformJBI()).
293: // append("\" ");
294: sbOperation.append(invokeSb);
295:
296: serviceOperations.append(sbOperation);
297: }
298: }
299:
300: return serviceOperations.toString();
301: }
302:
303: private TMapServiceEntry getInvoke(TMapServiceEntry input) {
304: if (mConsumers == null || mConsumers.size() == 0
305: || input == null || input.getNode() == null) {
306: return null;
307: }
308:
309: TMapServiceEntry invoke = null;
310:
311: for (TMapServiceEntry consumer : mConsumers) {
312: Node tmpNode = consumer.getNode();
313: tmpNode = tmpNode == null ? null : tmpNode.getParentNode();
314: if (tmpNode == null
315: || !TransformationDescType.INPUT.getTagName()
316: .equals(tmpNode.getLocalName())) {
317: continue;
318: }
319:
320: if (tmpNode.equals(input.getNode().getParentNode())) {
321: invoke = consumer;
322: break;
323: }
324: }
325:
326: return invoke;
327: }
328:
329: private File getXsltMapFile() throws BuildException {
330: String srcDir = getSourceDirectory();
331: if (srcDir == null || "".equals(srcDir)) {
332: throw new BuildException(
333: "source directory shouldn't be null or empty");
334: }
335:
336: File xsltMapFile = new File(srcDir + "/"
337: + XsltproConstants.XSLTMAP_XML);
338: return xsltMapFile;
339: }
340:
341: private File getTransformMapFile() throws BuildException {
342: String srcDir = getSourceDirectory();
343: if (srcDir == null || "".equals(srcDir)) {
344: throw new BuildException(
345: "source directory shouldn't be null or empty");
346: }
347:
348: File transformMapFile = new File(srcDir + "/"
349: + XsltproConstants.TRANSFORMMAP_XML);
350: return transformMapFile;
351: }
352:
353: /**
354: * Collect the namespaces and generate Prefix
355: * @param namespaceURI
356: * @return namespace prefix
357: */
358: private String populateNamespace(String namespaceURI) {
359: if (namespaceURI == null || "".equals(namespaceURI)) {
360: return null;
361: }
362:
363: String namespacePrefix = null;
364: namespacePrefix = (String) mNameSpacePrefix.get(namespaceURI);
365:
366: if (namespacePrefix == null) {
367: namespacePrefix = NAMESPACE_PREFIX
368: + mNameSpacePrefix.size();
369: mNameSpacePrefix.put(namespaceURI, namespacePrefix);
370: }
371: return namespacePrefix;
372: }
373:
374: private void populateServices(File projectSourceRoot,
375: NodeList nodeList, List<TMapServiceEntry> services) {
376:
377: if (services == null) {
378: return;
379: }
380:
381: TMapServiceEntry service = null;
382:
383: assert nodeList != null;
384:
385: for (int i = 0; i < nodeList.getLength(); i++) {
386: Node tmpNode = nodeList.item(i);
387: NamedNodeMap namedNodeMap = tmpNode.getAttributes();
388:
389: String partnerLink = XmlUtil.getAttrValue(namedNodeMap,
390: XsltMapConst.PARTNER_LINK);
391: QName partnerLinkQname = getQName(partnerLink);
392: if (partnerLinkQname == null) {
393: continue;
394: }
395: populateNamespace(partnerLinkQname.getNamespaceURI());
396:
397: String roleName = XmlUtil.getAttrValue(namedNodeMap,
398: XsltMapConst.ROLE_NAME);
399: String operation = XmlUtil.getAttrValue(namedNodeMap,
400: XsltMapConst.OPERATION);
401: String file = XmlUtil.getAttrValue(namedNodeMap,
402: XsltMapConst.FILE);
403: String transformJBI = XmlUtil.getAttrValue(namedNodeMap,
404: XsltMapConst.TRANSFORM_JBI);
405:
406: services.add(new TMapServiceEntry(partnerLinkQname,
407: roleName, operation, file, transformJBI, tmpNode));
408:
409: }
410: }
411:
412: private void populateProviderServices(File projectSourceRoot,
413: NodeList nodeList) {
414: if (mProviders == null) {
415: mProviders = new ArrayList<TMapServiceEntry>();
416: }
417: populateServices(projectSourceRoot, nodeList, mProviders);
418: }
419:
420: private void populateConsumerServices(File projectSourceRoot,
421: NodeList nodeList) {
422: if (mConsumers == null) {
423: mConsumers = new ArrayList<TMapServiceEntry>();
424: }
425: populateServices(projectSourceRoot, nodeList, mConsumers);
426: }
427:
428: private static String getColonedQName(QName qn, Map nsTable) {
429: String ns = qn.getNamespaceURI();
430: String prefix = (String) nsTable.get(ns);
431: if (prefix == null)
432: return qn.getLocalPart();
433: else
434: return prefix + COLON_SEPARATOR + qn.getLocalPart();
435: }
436:
437: private static QName getQName(String qname) {
438: return QName.valueOf(qname);
439: }
440:
441: private static void store(byte input[], OutputStream output)
442: throws IOException {
443:
444: ByteArrayInputStream in = new ByteArrayInputStream(input);
445: byte buf[] = new byte[4096];
446: for (int n = 0; (n = in.read(buf)) != -1;) {
447: output.write(buf, 0, n);
448: }
449:
450: output.flush();
451: }
452: }
|