/** * * 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.3 $ * */ package org.biomage.tools.generate_classes ; /*--------------------------------------------------------------------------*/ /* Imported classes. */ /*--------------------------------------------------------------------------*/ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.StringReader; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Vector; import org.biomage.tools.generate_doc.CreateDocFile; import org.biomage.tools.generate_dtd.WriteDTDFile; import org.biomage.tools.generate_dtd.WriteDTDFile.CreateClassTransformer; import org.biomage.tools.helpers.StringOutputHelpers; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.InputSource; import org.apache.xerces.parsers.*; import org.apache.xerces.jaxp.*; /*--------------------------------------------------------------------------*/ /* CreateMageClasses class. */ /*--------------------------------------------------------------------------*/ public class CreateMageDTDClasses { /** * * * Description: * Constructor for the CreateMageDTDClasses object. * * * @param * * * @return * An instance of the object. * *
* @exception Exception *
* */ public CreateMageDTDClasses( String args[] ) throws Exception { this.args = args; } /** * Description: * The command line arguments. */ protected String args[] = null; protected Document cfgInfo = null; /** * Description: * The default namespace. */ final static protected String DEFAULT_PATH = "org.biomage"; /** * Description: * Obtains the top node of the document. * *
* @param args: list of command line arguments. *
*/ protected Element initialize() throws Exception { String cfgFile = getArgumentToken(args, "-cfg") ; if (null != cfgFile && 0 < cfgFile.length()) { cfgInfo = getDOMFromFile(cfgFile); } String verbose = getArgumentToken(args, "-verbose") ; if (null != verbose && 0 < verbose.length()) { StringOutputHelpers.setVerbose((new Integer(verbose)).intValue()); } String xmiFile = getArgumentToken(args, "-xmiFile") ; return getXMINodeFromFile(xmiFile); } /** * Description: * Reads the file and creates the DOM structure and gets the * XMI element. * *
* @param xmiFile: the file to parse *
* *
* @return the top-level node as an Element *
*/ public Element getXMINodeFromFile( String xmlFile ) throws Exception { Node node = null; try { Document xmiDocument = getDOMFromFile(xmlFile) ; node = xmiDocument.getElementsByTagName("XMI").item(0); } catch (Exception e) { throw new Exception ("getXMINodeFromFile(): " + e) ; } return (Element) node; } /** * Description: * Reads the file and creates the DOM structure. * *
* @param xmiFile: the file to parse *
* *
* @return the xml parsed as a DOM from the file *
*/ public Document getDOMFromFile( String xmlFile ) throws Exception { Document document = null; try { FileReader fr = new FileReader(xmlFile); BufferedReader br = new BufferedReader(fr); InputSource is = new InputSource(br); StringOutputHelpers.writeOutput("Begin parsing...", 0); // Setup the DOM parser try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //factory.setValidating(true); //factory.setNamespaceAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); document = parser.parse(is); } catch (Exception e) { DOMParser xmlParser = new DOMParser() ; xmlParser.parse(is); document = xmlParser.getDocument() ; } StringOutputHelpers.writeOutput("DOM created for " + xmlFile, 0); } catch (Exception e) { throw new Exception ("getXMIDomFromFile(): " + e) ; } return document; } /** * Description: * Reads the file and creates the DOM structure and gets the * XMI element. * *
* @param xmiFile: the file to parse *
* *
* @return the top-level node as an Element *
*/ public Element getPackageNodeFromCfg() throws Exception { Node node = null; try { if (null != cfgInfo) { node = cfgInfo.getElementsByTagName("package-list").item(0); } } catch (Exception e) { throw new Exception ("getPackageNodeFromCfg(): " + e) ; } return (Element) node; } /** * Description: * Writes the header to the DTD header then creates a WriteDTDFile * to write the rest of the file. * *
* @param clases: vector of CreateClassFiles, one per model object. *
* */ public void writeDTDFile( Vector classes ) throws Exception { Element packageOrder = null; Element topElements = null; String transformClass = null; try { packageOrder = (Element) cfgInfo.getElementsByTagName("package-list").item(0); topElements = (Element) cfgInfo.getElementsByTagName("top-elements").item(0); transformClass = XMIParseHelpers.getTokenValue(cfgInfo, "transformClass"); } catch (Exception e) { throw new Exception("writeDTDFile(): Requires XML configuration file for the information on package order and topElements.\n\t" + e); } StringOutputHelpers.writeOutput("Creating the MAGE-ML.dtd", 0); String outputDir = getArgumentToken(args, "-outputDir") ; if (null != outputDir) { File baseDir = new File(outputDir); if (!baseDir.exists() && !baseDir.mkdirs()) { throw new Exception("Could not create the output path " + outputDir); } } String outputFile = getArgumentToken(args, "-outputFile") ; if ("".equals(outputFile)) { outputFile = "MAGE-ML.dtd"; } File header = new File(getArgumentToken(args, "-tmplDir") + File.separatorChar + "dtd_header.tmpl"); // The c'tor of WriteDTDFile will create the file itself new WriteDTDFile(classes, outputFile, outputDir, header, transformClass, topElements); } /** * Description: * Writes the documentation out to the specified file.. * *
* @param clases: vector of CreateClassFiles, one per model object. *
* */ public void writeDocFile( Vector classes ) throws Exception { StringOutputHelpers.writeOutput("Writing the documentation file", 0); String outputDir = getArgumentToken(args, "-outputDir") ; if (null != outputDir) { File baseDir = new File(outputDir); if (!baseDir.exists() && !baseDir.mkdirs()) { throw new Exception("Could not create the output path " + outputDir); } } String outputFile = getArgumentToken(args, "-outputFile") ; if ("".equals(outputFile)) { outputFile = "MAGE-ML.txt"; } // The c'tor of WriteDocFile will create the file itself String wrap = getArgumentToken(args, "-wrap") ; if (null != wrap && (wrap.toLowerCase().startsWith("t") || wrap.toLowerCase().startsWith("y"))) { new CreateDocFile(classes, outputFile, outputDir, true); } else { new CreateDocFile(classes, outputFile, outputDir); } String transformClassName = XMIParseHelpers.getTokenValue(cfgInfo, "transformClass"); if (null != transformClassName && 0 != transformClassName.length()) { Class transformClass = Class.forName(transformClassName); CreateClassTransformer transformer = (CreateClassTransformer) transformClass.newInstance(); StringOutputHelpers.writeOutput("Calling " + transformClassName + " to transform the class list.", 0); transformer.transform(classes); } CreateDocFile.writeMinimal(classes, "names.txt", outputDir); } /** * Description: * Picks the specified platform and calls the class that creates * that platform. * *
* @param clases: vector of CreateClassFiles, one per model object. *
* */ public void writePlatform( Vector classes ) throws Exception { String platform = getArgumentToken(args, "-platform") ; if ( "".equals(platform) || platform.equalsIgnoreCase("dtd") ) { writeDTDFile(classes); } else if(platform.equalsIgnoreCase("doc")) { writeDocFile(classes); } } static public String getName() { return "Create MAGE DTD classes" ; } static public String getVersion() { return "1.0" ; } public void dispose() { System.exit(0); } /** * * * Description: * Common code to obtain the argument associated with the option * from the command line. If it isn't on the command line, will * check the xml configuration file, if it was provided. * * * @param args the command line. * @param token option whose argument is desired. * * * @return * The desired argument. * *
*
*
*/
public
String
getArgumentToken
(
String[] args,
String token
) throws Exception
{
String value = "" ;
for ( int i = 0; i < args.length; i++ )
{
if ( args[i].equalsIgnoreCase(token) )
{
value = args[i + 1] ;
break ;
}
}
if (0 == value.length() && null != cfgInfo)
{
// look for the tag in the xml configuration file
int index = 0;
if ('-' == token.charAt(0))
{
index = 1;
}
// must be of the form
* * * @return * An instance of the object. * *
*
*
*/
public
static
void
main
(
String[] args
)
{
CreateMageDTDClasses driver = null;
CreateMageClassFileList creator = null;
Element xmiElement = null;
// Get this from the parameter file so that the CreateFiles will
// have their associations ordered out of the box
// --mm 5/22/02
Element packageList = null;
try
{
StringOutputHelpers.writeOutput("Create driver...", 0);
StringOutputHelpers.writeOutput(driver.getName(), 0);
driver = new CreateMageDTDClasses(args) ;
xmiElement = driver.initialize();
packageList = driver.getPackageNodeFromCfg();
StringOutputHelpers.writeOutput("done.", 0);
StringOutputHelpers.writeOutput("Create creator...", 0);
creator = new CreateMageClassFileList() ;
StringOutputHelpers.writeOutput("done.", 0);
}
catch ( Exception e )
{
System.err.println(e) ;
System.exit(0);
}
// *********************************
// ** DO WORK **
// *********************************
try
{
driver.writePlatform(creator.create(xmiElement,packageList));
}
catch (Exception e)
{
System.err.println(e) ;
e.printStackTrace(System.err) ;
}
catch (Throwable t)
{
System.err.println(t) ;
t.printStackTrace(System.err) ;
}
finally
{
if (creator != null )
{
driver.dispose();
}
}
}
/* End of main() */
}