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.anttasks;
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: StringBuffer sb = new StringBuffer();
079: sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
080: sb.append("<sqlengine name=\"" + projectName + "\">");
081: Iterator iter = sqlDefinitionMap.keySet().iterator();
082: while (iter.hasNext()) {
083: String sqlFileName = (String) iter.next();
084: DatabaseConnection dbConn = (DatabaseConnection) sqlDefinitionMap
085: .get(sqlFileName);
086: sb.append("<sqldef>" + "\n");
087: sb.append("<sqlfile name=\"" + sqlFileName + "\"/>" + "\n");
088: sb.append("<connectiondef name=\"" + dbConn.getName()
089: + "\"" + "\t");
090: sb.append("driverClass=\"" + dbConn.getDriverClass() + "\""
091: + "\t");
092: sb.append("dbURL=\"" + dbConn.getDatabaseURL() + "\""
093: + "\t");
094: sb.append("databaseName=\"" + dbConn.getSchema() + "\""
095: + "\t");
096: sb.append("user=\"" + dbConn.getUser() + "\"" + "\t");
097: sb.append("password=\"" + dbConn.getPassword() + "\""
098: + "\t");
099: sb.append("jndi_name=\"" + jndi_name + "\"" + "\t");
100: sb.append(">\n</connectiondef>");
101: sb.append("\n</sqldef>");
102: }
103: sb.append("\n</sqlengine>");
104: try {
105: //Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")));
106: //Writer fileWriter = new FileWriter(engineFileName);
107: //indentWSDLFile(fileWriter, doc);
108: FileOutputStream fos = new FileOutputStream(engineFileName);
109: FileUtil.copy(sb.toString().getBytes("UTF-8"), fos);
110:
111: } catch (Exception e) {
112: }
113: }
114:
115: private void indentWSDLFile(Writer writer, Document doc) {
116: try {
117: // Use a Transformer for output
118: TransformerFactory tFactory = TransformerFactory
119: .newInstance();
120: Transformer transformer = tFactory.newTransformer();
121: DOMSource source = new DOMSource(doc);
122: PrintWriter pw = new PrintWriter(writer); //USE PRINTWRITER
123: StreamResult result = new StreamResult(pw);
124: transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
125: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
126: transformer.setOutputProperty(OutputKeys.MEDIA_TYPE,
127: "text/xml"); // NOI18N
128: // indent the output to make it more legible...
129: try {
130: transformer.setOutputProperty(
131: "{http://xml.apache.org/xslt}indent-amount",
132: "4"); // NOI18N
133: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
134: } catch (Exception e) {
135: ; // the JAXP implementation doesn't support indentation, no big deal
136: }
137: transformer.transform(source, result);
138: } catch (Exception e) {
139: }
140: }
141: }
|