/** * * C O P Y R I G H T N O T I C E * Copyright (c) 2001 by: * * The MicroArray Gene Expression Database group (MGED) * * Rosetta Inpharmatics * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * * @author $Author: rhubley $ * @version $Revision: 1.2 $ * */ package org.biomage.tools.helpers; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.TreeSet; import java.util.Set; import java.util.Vector; import org.w3c.dom.*; /** * Description: * Class that provides static helper metheds for outputting strings. * */ public class StringOutputHelpers { /** * Description: * The environment specific new line. */ final static public String NEWLINE = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator")); /** * Description: * Size of indentation. */ static public String oneIndent = " "; static protected String javaBodyIndent = " * "; static protected String cppBodyIndent = javaBodyIndent; /** * Description: * The verboseness level. */ static protected int verbose = 0; /** * Description: * Looks for verboseness argument to set the level of output. * *

* @param args: list of command line arguments. *

*/ static public void setVerbose( int level ) { verbose = level; } /** * Description: * The verboseness level. */ static public void writeOutput( String mess, int level ) { if (level <= verbose) { System.out.println(mess); } } /** * Description: * Takes the string and returns a new string based on the input but with * the first letter capitalized. * *

* @param string: string to copy and return the copy with the first letter capitalized. *

* *

* @return a string with the first letter capitalized. *

* */ public static String initialCap( String string ) { return string.substring(0,1).toUpperCase() + string.substring(1); } /** * Description: * Takes the string and returns a new string based on the input but with * the first letter in lower case. * *

* @param string: string to copy and return the copy with the first letter in lower case. *

* *

* @return a string with the first letter in lower case. *

* */ public static String initialLower( String string ) throws Exception { return string.substring(0,1).toLowerCase() + string.substring(1); } /** * Description: * Takes the comment, formats it for JavaDoc and outputs it. * *

* @param writer: used to write the comment to the file. * @param comment: main string to write to the document. * @param indentLevel: how far to indent. * @param keyWord: JavaDoc keyword to prepend to the first line. * @param startComment: Add lines to begin the comment. * @param endComment: end the comment. *

* */ public static void writeJavaComment( FileWriter writer, String comment, int indentLevel, String keyWord, boolean startComment, boolean endComment ) throws Exception { String delimIndent = javaBodyIndent; String indent = ""; int keyWordRoom = (null == keyWord? 0 : keyWord.length()); for (int i = 0; i < indentLevel; i++) { indent = oneIndent + indent; if ( 1 != i) { delimIndent = oneIndent + delimIndent; } } if (startComment) { writer.write(indent+ "/**" + NEWLINE); } if (null == comment || 0 == comment.trim().length()) { writer.write(delimIndent + NEWLINE); }else { Vector commentLines = wrapString(comment.trim(),72 - indent.length(),keyWordRoom + 3); // Write out the comment writeCommentBody(writer, commentLines, keyWord, delimIndent); } if (endComment) { writer.write(delimIndent + NEWLINE); writer.write(indent + " */" + NEWLINE); } } /** * Description: * Takes the comment, formats it for C++ and outputs it. * *

* @param writer: used to write the comment to the file. * @param comment: main string to write to the document. * @param indentLevel: how far to indent. * @param keyWord: JavaDoc keyword to prepend to the first line. * @param startComment: Add lines to begin the comment. * @param endComment: end the commetn. *

* */ static protected void writeCPPComment( FileWriter writer, String comment, int indentLevel, String keyWord, boolean startComment, boolean endComment ) throws Exception { if (null == comment || 0 == comment.trim().length()) { return; } String delimIndent = cppBodyIndent; String indent = ""; int keyWordRoom = (null == keyWord? 0 : keyWord.length()); for (int i = 0; i < indentLevel; i++) { indent = oneIndent + indent; } if (startComment) { writer.write(indent+ "/**" + "" + NEWLINE + ""); } Vector commentLines = wrapString(comment.trim(),72 - indent.length(),keyWordRoom + 3); // Write out the comment if ( 0 < commentLines.size() ) { if ( null != keyWord ) { String firstLine = keyWord + " " + (String) commentLines.elementAt(0); commentLines.setElementAt(firstLine, 0); } for (int i = 0; i < commentLines.size(); i++) { writer.write(indent + " * " + (String) commentLines.elementAt(i) + NEWLINE); } } if (endComment) { writer.write(indent + " */" + NEWLINE); } } /** * Description: * Takes the comment, formats it for JavaDoc and outputs it. * *

* @param writer: used to write the comment to the file. * @param comment: main string to write to the document. * @param keyWord: keyword to prepend to the first line. * @param startComment: Add lines to begin the comment. * @param endComment: end the comment. *

* */ public static void writeDTDComment( FileWriter writer, String comment, String keyWord, boolean startComment, boolean endComment ) throws Exception { if (null == comment || 0 == (comment = comment.trim()).length()) { return; } int keyWordRoom = (null == keyWord? 0 : keyWord.length()); if (startComment) { writer.write("" + NEWLINE); } } /** * Description: * Takes the comment, formats it for Doc and outputs it. * *

* @param writer: used to write the comment to the file. * @param comment: main string to write to the document. *

* */ public static void writeDocComment( FileWriter writer, int lineSize, String comment, String indent ) throws Exception { if (null == comment || 0 == (comment = comment.trim()).length()) { return; } Vector commentLines = wrapString(comment.trim(),lineSize - oneIndent.length(),0); // Write out the comment writeCommentBody(writer, commentLines, null, indent); } /** * Description: * Writes out the body of a comment. * *

* @param writer: used to write the comment to the filethe string to wrap. * @param commentLines: the comment broken into lines. * @param keyWord: prependeed to first line. * @param delimIndent: how far and format of the indent. *

* *

* @return a Vector that has the original String broken into * appropriate sized pieces. *

* */ protected static void writeCommentBody( FileWriter writer, Vector commentLines, String keyWord, String delimIndent ) throws Exception { if ( 0 < commentLines.size() ) { if ( null != keyWord ) { String firstLine = keyWord + " " + (String) commentLines.elementAt(0); commentLines.setElementAt(firstLine, 0); } for (int i = 0; i < commentLines.size(); i++) { String line = (String) commentLines.elementAt(i); writer.write(delimIndent + line + NEWLINE); } } } /** * Description: * Takes the input string and breaks it into appropriate sized * pieces. * *

* @param string: the string to wrap. * @param lineLength: length to use as basis of wrapping. * @param firstLineOffset: how much less room for the first line. *

* *

* @return a Vector that has the original String broken into * appropriate sized pieces. *

* */ protected static Vector wrapString( String string, int lineLength, int firstLineOffset ) throws Exception { Vector strings = new Vector(); int index = -1; // treat first line special int nextChunk = lineLength - firstLineOffset; while (string.length() > nextChunk) { // offset to skip if newline found int nextStart = 0; if ( -1 == (index = string.indexOf(NEWLINE)) || index > nextChunk) { int tabIndex = string.lastIndexOf('\t',nextChunk); int spaceIndex = string.lastIndexOf(' ',nextChunk); if (-1 == tabIndex && -1 == spaceIndex ) { // no spaces so just chop it index = lineLength; } else { index = (spaceIndex > tabIndex? spaceIndex : tabIndex) + 1; } } else { nextStart = NEWLINE.length(); } String nextLine = string.substring(0,index); strings.addElement(nextLine); string = string.substring(index + nextStart); nextChunk = lineLength; } if (0 < string.length()) { while ( -1 != (index = string.indexOf(NEWLINE)) ) { String nextLine = string.substring(0,index); strings.addElement(nextLine); string = string.substring(index + NEWLINE.length()); nextChunk = lineLength; } if (0 < string.length()) { strings.addElement(string); } } return strings; } /** * Description: * Writes the header information out to the file. * *

* @param writer: FileWriter to used to write to the file. * @param header: File for the header information. *

* */ static public void writeHeader( FileWriter writer, File header ) throws Exception { FileReader reader = new FileReader(header); int curChar = -1; while(-1 != (curChar = reader.read())) { if ('%' == curChar) { // Get rid of the marker while ('%' != reader.read()) { // EMPTY } SimpleDateFormat format = new SimpleDateFormat("EEE, MMM dd, yyyy hh:mm:ss a"); String date = format.format(new Date()); writer.write(date, 0, date.length()); } else { writer.write(curChar); } } reader.close(); } /** * Description: * Creates the directory for the package. * *

* @param fullOutputDir: the full path for the project. * @param packageDirName: the directory that needs to exist. *

* *

* @return the modified fullOutputDir. *

* */ static public String createPackageDir( String fullOutputDir, String packageDirName ) throws Exception { if (fullOutputDir.length() - 1 != fullOutputDir.lastIndexOf(File.separatorChar)) { fullOutputDir = fullOutputDir + File.separatorChar; } fullOutputDir = fullOutputDir + packageDirName; File packageDir = new File(fullOutputDir); if (!packageDir.exists() && !packageDir.mkdirs()) { throw new Exception("printJavaClassFile(): Could not create the packageDir path " + fullOutputDir); } return fullOutputDir; } }