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: /*
021: * Sun Public License Notice
022: *
023: * The contents of this file are subject to the Sun Public License
024: * Version 1.0 (the "License"). You may not use this file except in
025: * compliance with the License. A copy of the License is available at
026: * http://www.sun.com/
027: *
028: * The Original Code is NetBeans. The Initial Developer of the Original
029: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: */
032:
033: package org.netbeans.modules.sql.project.wsdl;
034:
035: import org.netbeans.api.db.explorer.DatabaseConnection;
036: import org.w3c.dom.Document;
037:
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.Transformer;
040: import javax.xml.transform.OutputKeys;
041: import javax.xml.transform.stream.StreamResult;
042: import javax.xml.transform.dom.DOMSource;
043:
044: import java.util.Map;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.logging.Logger;
048: import java.util.logging.Level;
049: import java.io.Writer;
050: import java.io.PrintWriter;
051: import java.io.FileOutputStream;
052:
053: /**
054: * Generates an xml file containing list of sql files and their associated
055: * connection parameters. This file should ideally be generated at design time
056: * as the user creates new sql files and associates connections to the sql file.
057: */
058: public class SQLEngineFileGenerator {
059: private Map sqlDefinitionMap = new HashMap();
060: private String engineFileName = null;
061: private String projectName = null;
062:
063: private static Logger logger = Logger
064: .getLogger(SQLEngineFileGenerator.class.getName());
065:
066: public SQLEngineFileGenerator(String engineFileName,
067: String projectName) {
068: this .engineFileName = engineFileName;
069: this .projectName = projectName;
070: }
071:
072: public void addSQLDefinition(String sqlFileName,
073: DatabaseConnection dbConn) {
074: sqlDefinitionMap.put(sqlFileName, dbConn);
075: }
076:
077: public void persistEngineFile(String jndi_name,
078: String transactionRequired) {
079: StringBuffer sb = new StringBuffer();
080: sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
081: sb.append("<sqlengine name=\"" + projectName + "\">");
082: Iterator iter = sqlDefinitionMap.keySet().iterator();
083: while (iter.hasNext()) {
084: String sqlFileName = (String) iter.next();
085: DatabaseConnection dbConn = (DatabaseConnection) sqlDefinitionMap
086: .get(sqlFileName);
087: sb.append("<sqldef>" + "\n");
088: sb.append("<sqlfile name=\"" + sqlFileName + "\"/>" + "\n");
089: sb.append("<connectiondef name=\"" + dbConn.getName()
090: + "\"" + "\t");
091: sb.append("driverClass=\"" + dbConn.getDriverClass() + "\""
092: + "\t");
093: sb.append("dbURL=\"" + dbConn.getDatabaseURL() + "\""
094: + "\t");
095: sb.append("databaseName=\"" + dbConn.getSchema() + "\""
096: + "\t");
097: sb.append("user=\"" + dbConn.getUser() + "\"" + "\t");
098: //TODO HOT FIX sb.append("password=\"" + org.netbeans.modules.sql.project.security.Base64Impl.getInstance().encode(dbConn.getPassword()) + "\"" + "\t");
099: sb.append("jndi_name=\"" + jndi_name + "\"" + "\t");
100: sb.append("transactionRequired=\"" + transactionRequired
101: + "\"" + "\t");
102: sb.append(">\n</connectiondef>");
103: sb.append("\n</sqldef>");
104: }
105: sb.append("\n</sqlengine>");
106: try {
107: //Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")));
108: //Writer fileWriter = new FileWriter(engineFileName);
109: //indentWSDLFile(fileWriter, doc);
110: FileOutputStream fos = new FileOutputStream(engineFileName);
111: FileUtil.copy(sb.toString().getBytes("UTF-8"), fos);
112:
113: } catch (Exception e) {
114: }
115: }
116:
117: private void indentWSDLFile(Writer writer, Document doc) {
118: try {
119: // Use a Transformer for output
120: TransformerFactory tFactory = TransformerFactory
121: .newInstance();
122: Transformer transformer = tFactory.newTransformer();
123: DOMSource source = new DOMSource(doc);
124: PrintWriter pw = new PrintWriter(writer); //USE PRINTWRITER
125: StreamResult result = new StreamResult(pw);
126: transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
127: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
128: transformer.setOutputProperty(OutputKeys.MEDIA_TYPE,
129: "text/xml"); // NOI18N
130: // indent the output to make it more legible...
131: try {
132: transformer.setOutputProperty(
133: "{http://xml.apache.org/xslt}indent-amount",
134: "4"); // NOI18N
135: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
136: } catch (Exception e) {
137: ; // the JAXP implementation doesn't support indentation, no big deal
138: }
139: transformer.transform(source, result);
140: } catch (Exception e) {
141: }
142: }
143: }
|