/** * * 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.generate_doc; import org.biomage.tools.generate_classes.CreateFile; import org.biomage.tools.generate_classes.CreateClassFile; import org.biomage.tools.generate_classes.XMIParseHelpers; import org.biomage.tools.helpers.StringOutputHelpers; import org.biomage.tools.generate_dtd.WriteDTDFile; import java.io.File; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.List; import java.util.TreeSet; import java.util.Set; import java.util.Vector; import org.w3c.dom.*; public class CreateDocFile { static protected final String NEWLINE = StringOutputHelpers.NEWLINE; static protected final String indent = " "; protected boolean doWrap = false; /** * Description: * Constructor for the Doc file generator. * *
* @param classFiles: information on the classes to obtain the documentation. * @param docFileName: what to name the output file. * @param outputDir: where to write the file. *
*/ public CreateDocFile( Vector classFiles, String docFileName, String outputDir ) throws Exception { writeDoc(classFiles,docFileName,outputDir,doWrap); } /** * Description: * Constructor for the Doc file generator. * *
* @param classFiles: information on the classes to obtain the documentation. * @param docFileName: what to name the output file. * @param outputDir: where to write the file. *
*/ public CreateDocFile( Vector classFiles, String docFileName, String outputDir, boolean doWrap ) throws Exception { writeDoc(classFiles,docFileName,outputDir,doWrap); } /** * Description: * Constructor for the Doc file generator. * *
* @param classFiles: information on the classes to obtain the documentation. * @param docFileName: what to name the output file. * @param outputDir: where to write the file. *
*/ private void writeDoc( Vector classFiles, String docFileName, String outputDir, boolean doWrap ) throws Exception { this.doWrap = doWrap; System.out.println("wrap = " + doWrap); // Order the class files by inheritence/package classFiles = WriteDTDFile.sortWriteFiles(classFiles); // Create the writer String path = ""; if (null != outputDir) { path = outputDir + File.separatorChar; } FileWriter writer = new FileWriter( path + docFileName); for (int i = 0; i < classFiles.size(); i++) { writeFileDocs(writer, (CreateFile) classFiles.get(i)); } writer.close(); } /** * Description: * Work horse method that takes a CreateFile and writes out the documentation * for it. * *
* @param writer: used to write the comments to the file. * @param createFile: represents the class whose comments are to be written. *
*/ public void writeFileDocs( FileWriter writer, CreateFile createFile ) throws Exception { if (CreateFile.JAVA_INTERFACE == createFile.getFileType() || CreateFile.UML_MODEL == createFile.getFileType()) { return; } String doc = createFile.getClassDoc(); if (!doWrap) { // Need to check for new lines and make sure each paragraph is set off properly int index = -1; int last = 0; while (-1 != (index = doc.indexOf(NEWLINE, last)) && index != doc.length() - 1 ) { doc = doc.substring(0, index + NEWLINE.length()) + indent + indent + indent + doc.substring(index + NEWLINE.length()); last = index + NEWLINE.length(); } } if (CreateFile.UML_PACKAGE == createFile.getFileType()) { if (doWrap) { writer.write(createFile.getClassFileName().replace('_', ' ') + NEWLINE); StringOutputHelpers.writeDocComment(writer,45,doc,indent + indent + indent); } else { writer.write(createFile.getClassFileName().replace('_', ' ') + NEWLINE + indent + indent + indent + doc + NEWLINE); } return; } if (doWrap) { writer.write(indent + createFile.getClassFileName() + NEWLINE); StringOutputHelpers.writeDocComment(writer,45,doc,indent + indent + indent); } else { writer.write(indent + createFile.getClassFileName() + NEWLINE + indent + indent + indent + doc + NEWLINE); } // process the class if (null != createFile.getBaseClassFileName()) { writer.write(indent + indent + "Derived from " + createFile.getBaseClassFileName() + NEWLINE); } writeMethods(writer, createFile.getMethodInfo()); writeAttrs(writer, createFile.getAssociationInfo(), "Associations"); writeAttrs(writer, createFile.getAttrInfo(), "Attributes"); } /** * Description: * Constructor for the Doc file generator. * *
* @param writer: used to write the comments to the file. * @param methods: the methods to write out the documentation. *
*/ public void writeMethods( FileWriter writer, Vector methods ) throws Exception { if (null != methods && 0 != methods.size()) { writer.write(indent + indent + "Methods" + ":" + NEWLINE); for (int i = 0; i < methods.size(); i++) { CreateClassFile.MethodInformation methodInfo = (CreateClassFile.MethodInformation) methods.get(i); writer.write(indent + indent + indent + "\\b" + methodInfo.getName() + "()\\b : " + methodInfo.getDatatype() + NEWLINE); writer.write(indent + indent + indent + methodInfo.getComment() + NEWLINE); Vector paramInfos = methodInfo.getParamInfos(); if (null != paramInfos && 0 != paramInfos.size()) { writer.write(indent + indent + indent + indent + "Parameters:" + NEWLINE); for (int j = 0; j < paramInfos.size(); j++) { CreateClassFile.MethodInformation.ParamInfo paramInfo = (CreateClassFile.MethodInformation.ParamInfo) paramInfos.get(j); writer.write(indent + indent + indent + indent + "\\b" + paramInfo.getName() + "\\b : " + paramInfo.getDatatype() + NEWLINE); if ( null != paramInfo.getComment() ) { String doc = paramInfo.getComment(); // Need to check for new lines and make sure each paragraph is set off properly int index = -1; int last = 0; while (-1 != (index = doc.indexOf(NEWLINE, last)) && index != doc.length() - 1 ) { doc = doc.substring(0, index + NEWLINE.length()) + indent + indent + indent + indent + doc.substring(index + NEWLINE.length()); last = index + NEWLINE.length(); } writer.write(indent + indent + indent + indent + doc + NEWLINE); } } } } } } /** * Description: * Constructor for the Doc file generator. * *
* @param writer: used to write the comments to the file. * @param attrs: the attributes (or subclass) to write out the documentation. * @param type: the type of attribute (Attribute, Association, or Operations) *
*/ public void writeAttrs( FileWriter writer, Vector attrs, String type ) throws Exception { if (null != attrs && 0 != attrs.size()) { writer.write(indent + indent + type + ":" + NEWLINE); for (int i = 0; i < attrs.size(); i++) { CreateFile.AttrInformation attrInfo = (CreateFile.AttrInformation) attrs.get(i); writer.write(indent + indent + indent + "\\b" + attrInfo.getName() + "\\b : "); if (attrInfo instanceof CreateFile.AssociationAttrInformation) { writeHeader(writer, (CreateFile.AssociationAttrInformation) attrInfo); } else { writeHeader(writer, (CreateClassFile.DataTypeAttrInformation) attrInfo); } if (null != attrInfo.getComment()) { String doc = attrInfo.getComment(); if (!doWrap) { // Need to check for new lines and make sure each paragraph is set off properly int index = -1; int last = 0; while (-1 != (index = doc.indexOf(NEWLINE, last)) && index != doc.length() - 1 ) { doc = doc.substring(0, index + NEWLINE.length()) + indent + indent + indent + indent + doc.substring(index + NEWLINE.length()); last = index + NEWLINE.length(); } } if (doWrap) { StringOutputHelpers.writeDocComment(writer,45,doc,indent + indent + indent + indent); } else { writer.write(indent + indent + indent + indent + doc + NEWLINE); } } else { StringOutputHelpers.writeOutput("\t\tWARNING: atttribute " + attrInfo.getName() + " has no comment", 1); } } } } /** * Description: * Simple method to distinguish between true attribute from the other * subclasses. * *
* @param writer: used to write the comments to the file. * @param attr: the attribute to write out the header information. *
*/ public void writeHeader( FileWriter writer, CreateClassFile.DataTypeAttrInformation attr ) throws Exception { writer.write(attr.getDatatype()); String value = attr.getInitialValue().trim(); if (null == value || 0 == value.length()) { value = (attr.isRequired()? "required" : "optional"); } else { value = "default: \\i" + value + "\\i"; } writer.write(" (" + value + ")" + NEWLINE); if ( attr.isEnum()) { writer.write(indent + indent + indent + indent + "enumeration "); Vector enums = attr.getEnumValues(); writer.write("{\\i" + enums.get(0) + "\\i"); for (int i = 1; i < enums.size(); i++) { writer.write(" | \\i" + enums.get(i) + "\\i"); } writer.write("}" + NEWLINE); } } /** * Description: * Simple method to distinguish between true attribute from the other * subclasses. * *
* @param writer: used to write the comments to the file. * @param attr: the attribute to write out the header information. *
*/ public void writeHeader( FileWriter writer, CreateFile.AssociationAttrInformation attr ) throws Exception { writer.write(attr.getOriginalType() + " (" + attr.getMinCard() + ".." + (-1 == attr.getMaxCard()? "n" : new Integer(attr.getMaxCard()).toString()) + ")" + NEWLINE); } static public void writeMinimal( Vector classFiles, String docFileName, String outputDir ) throws Exception { // Order the class files by inheritence/package classFiles = WriteDTDFile.sortWriteFiles(classFiles); // Create the writer String path = ""; if (null != outputDir) { path = outputDir + File.separatorChar; } FileWriter writer = new FileWriter( path + docFileName); // Make a map from class name to its createFile. This is used to get the base // class information for each class Map name2createFile = new HashMap(); for (int i = 0; i < classFiles.size(); i++) { name2createFile.put(((CreateFile) classFiles.get(i)).getClassFileName(), (CreateFile) classFiles.get(i)); } for (int i = 0; i < classFiles.size(); i++) { CreateFile createFile = (CreateFile) classFiles.get(i); if (!createFile.isAbstract() && CreateFile.JAVA_INTERFACE != createFile.getFileType()) { writeMinimalClass(writer, createFile, name2createFile); } } writer.close(); } static public void writeMinimalClass( FileWriter writer, CreateFile createFile, Map name2createFile ) throws Exception { List classes = new ArrayList(); String name = null; classes.add(createFile); while ( null != (name = ((CreateFile) classes.get(0)).getBaseClassFileName()) ) { classes.add(0, name2createFile.get(name)); } List assnInfos = new ArrayList(); for (int i = 0; i < classes.size(); i++) { if (null != ((CreateFile) classes.get(i)).getAssociationInfo()) { assnInfos.addAll( ((CreateFile) classes.get(i)).getAssociationInfo() ); } } if (0 < assnInfos.size()) { writer.write(createFile.getClassFileName() + "\t" + "Associations" + NEWLINE); for (int i = 0; i < assnInfos.size(); i++) { CreateFile.AssociationAttrInformation assnInfo = (CreateFile.AssociationAttrInformation) assnInfos.get(i); int min = assnInfo.getMinCard(); int max = assnInfo.getMaxCard(); String card = min + ".." + (max == -1? "n" : Integer.toString(max)); if (XMIParseHelpers.COMPOSITE == assnInfo.getTypeAggregated()) { card = "owned " + card; } writer.write(card + "\t" + assnInfo.getName()); writer.write("\t" + assnInfo.getOriginalType() + NEWLINE); } writer.write(NEWLINE); } List attrInfos = new ArrayList(); for (int i = 0; i < classes.size(); i++) { if (null != ((CreateFile) classes.get(i)).getAttrInfo()) { attrInfos.addAll( ((CreateFile) classes.get(i)).getAttrInfo() ); } } if (0 < attrInfos.size()) { writer.write(createFile.getClassFileName() + "\t" + "Attributes" + NEWLINE); for (int i = 0; i < attrInfos.size(); i++) { CreateClassFile.DataTypeAttrInformation attrInfo = (CreateClassFile.DataTypeAttrInformation) attrInfos.get(i); boolean required = attrInfo.isRequired(); String card = (required? '1' : '0') + ".." + "1"; writer.write(card + "\t" + attrInfo.getName() + NEWLINE); } writer.write(NEWLINE); } } }