//------------------------------------------------------------------------------
// COMPONENT NAME: LPEX Editor
//
// All Rights Reserved.
//
// DESCRIPTION:
// HtmlSource - sample windowless use of LPEX: generate HTML listing
//------------------------------------------------------------------------------
package com.ibm.lpex.samples;
import com.ibm.lpex.core.LpexCommand;
import com.ibm.lpex.core.LpexConstants;
import com.ibm.lpex.core.LpexResources;
import com.ibm.lpex.core.LpexStringTokenizer;
import com.ibm.lpex.core.LpexView;
/**
* Sample windowless use of the LPEX widget, command <b>htmlSource</b> -
* generate HTML listing. This utility generates the HTML listing of a file.
*
* <p>Here is the HtmlSource
* <a href="doc-files/HtmlSource.java.html">source code</a>
* (listing created with HtmlSource itself).</p>
*
* <p>To run this sample:
* <pre>
* java com.ibm.lpex.samples.HtmlSource <i>inputFile</i> [<i>outputFile</i>]</pre>
* The class implements LpexCommand, so it can also be run from the command line:
* <ul>
* <li>Define the command via an editor preference page, where available, or
* from the editor command line:
* <pre>set commandClass.htmlSource com.ibm.lpex.samples.HtmlSource</pre></li>
* <li>Run it from the editor command line:
* <pre>htmlSource <i>inputFile</i> [<i>outputFile</i>]</pre></li>
* </ul></p>
*
* @see com.ibm.lpex.samples All the samples
*/
public class HtmlSource implements LpexCommand
{
/**
* Entry point when running this utility from the system prompt.
*/
public static void main(String[] args)
{
System.exit(generateListing(args, null)? 0 : 1);
}
/**
* Entry point when running this utility as an editor command.
*/
public boolean doCommand(LpexView lpexView, String parameters)
{
return generateListing(LpexStringTokenizer.split(parameters), lpexView);
}
/**
* Generates the HTML listing.
*
* @param args input file, optional output file parameters
* @param runFromView the editor view from which the command is run, or
* null if run from the system prompt
* @return true if successful
*/
private static boolean generateListing(String[] args, LpexView runFromView)
{
// validate arguments
if (args.length == 0 || args.length > 2 || args[0].length() == 0)
{
if (runFromView == null)
{
System.err.println("ERROR HtmlSource. Usage:");
System.err.println(" java com.ibm.lpex.samples.HtmlSource inputFile [outputFile]");
}
else
{
runFromView.doCommand("set messageText Error. Command parameters: inputFile [outputFile]");
}
return false;
}
// determine input and output files
String inputFile = LpexStringTokenizer.trimQuotes(args[0]);
String outputFile =
(args.length == 2)? LpexStringTokenizer.trimQuotes(args[1]) : inputFile + ".html";
// load input file, do an explicit "updateProfile" later
LpexView lpexView = new LpexView(inputFile, false);
String status = lpexView.query("status");
if (status == null)
{
// ensure a gray palette regardless of current user settings
lpexView.doCommand("set updateProfile.palette gray");
// initialize and run the appropriate document parser
lpexView.doCommand("updateProfile");
// add some extra styles to Java source files
String parser = lpexView.query("parser");
if ("java".equals(parser))
{
String style = lpexView.query("styleAttributes.c"); // comments in italics
lpexView.doCommand("set styleAttributes.c " + style + " italic");
style = lpexView.query("styleAttributes.C"); // javadoc comments in italics
lpexView.doCommand("set styleAttributes.C " + style + " italic");
style = lpexView.query("styleAttributes.k"); // keywords in bold
lpexView.doCommand("set styleAttributes.k " + style + " bold");
style = lpexView.query("styleAttributes.t"); // javadoc comment tags in bold italics
lpexView.doCommand("set styleAttributes.t " + style + " bold italic");
}
// and to properties files
else if ("prop".equals(parser))
{
String style = lpexView.query("styleAttributes.c"); // comments in italics
lpexView.doCommand("set styleAttributes.c " + style + " italic");
style = lpexView.query("styleAttributes.k"); // keywords in bold
lpexView.doCommand("set styleAttributes.k " + style + " bold");
}
// save the parsed view as HTML
lpexView.doCommand("saveAsHtml \"" + outputFile + "\"");
status = lpexView.query("status");
}
// analyze any problems encountered
if (status != null)
{
// see if an LPEX error message is available for this status
String error = status.equals(LpexConstants.STATUS_FILE_NOTFOUND)?
LpexResources.message(status, inputFile) : LpexResources.message(status);
// write an error message
String message = (error != null)? error : "LPEX status=\"" + status + "\".";
if (runFromView == null)
{
String command = "ERROR HtmlSource " + inputFile +
((args.length == 2)? " " + outputFile : "");
System.err.println(command + ": " + message);
}
else
{
runFromView.doCommand("set messageText " + message);
}
}
lpexView.dispose();
return status == null;
}
}