/* * $Id$ * * Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.jct.utils.glossarygen; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.FileScanner; import org.apache.tools.ant.taskdefs.MatchingTask; /** * A utility to generate a glossary from a set of HTML files and directories. * The glossary terms are taken from the individual <h1> tags in the various files; * The glossary definitions are the body of those files. * Thus, to add a new entry into the glossary, it is simply necessary to add a * new file into the set of files passed to this program. * *
The output can be in one or both of two forms. * *
The glossary can be output as a JavaHelp-compatible glossary.xml file, with an associated * map file. The map file can be merged with any other maps with the "mapmerge" * utility. * *
Or, the glossary can be output as a single glossary.html file, containing the * sorted set of terms and their definitions. In the form, any <h*> tags in the * body are replaced with <p class="glossaryHead*">. * *
The input files can have keywords associated with them, which can be used to
* filter the files selected for the glossary. These keywords can be provided in
* META tag, as follows: and default class to glossaryHeadn
out.write(start ? "p" : "/p");
// standard head code
// if (!start)
// out.write('/');
// out.write('h');
// out.write(String.valueOf(n));
copyMode = COPY;
String className = null;
skipSpace();
while (c != '>') {
String name = scanIdentifier();
String value = scanValue();
if (name.equalsIgnoreCase("class"))
className = value;
skipSpace();
}
if (start && className == null) {
// write default class
out.write(" class=\"glossaryHead");
out.write(String.valueOf(n));
out.write('"');
}
nextCh(); // skip past >
if (level == 1)
inHead1 = start;
// if (start && autoNumberLevel > 0) {
// hNums[n - 1]++;
// if (n < 6)
// hNums[n] = 0;
// if (n <= autoNumberLevel) {
// for (int i = 0; i < n; i++) {
// out.write(String.valueOf(hNums[i]));
// out.write('.');
// }
// out.write(" ");
// }
// }
}
}
// private boolean isImage(String tag) {
// return tag.equals("img");
// }
//
// /**
// * Process the contents of
// */
// private void scanImage() throws IOException {
// out.write(pendingCopy.toString());
// copyMode = COPY;
//
// skipSpace();
// while (c != '>') {
// String att = scanIdentifier();
// if (att.equalsIgnoreCase("src") && copyMode == COPY) {
// // the current character should be a whitespace or =
// // either way, we just write out =
// out.write('=');
// copyMode = NO_COPY;
// String src = scanValue();
// URL u = new URL(currURL, src);
// String srcPath = u.getFile();
// // if the path refers to an entry in the /images directory,
// // check for a matching entry in the /pdfImages directory
// // and use that if found.
// int imagesIndex = srcPath.indexOf("/images/");
// if (imagesIndex >= 0) {
// String pdfImagePath = srcPath.substring(0, imagesIndex)
// + "/pdfImages/"
// + srcPath.substring(imagesIndex + "/images/".length());
// if (new File(pdfImagePath).exists())
// srcPath = pdfImagePath;
// }
// out.write('"');
// out.write(srcPath);
// out.write('"');
// copyMode = COPY;
// }
// else if (att.equalsIgnoreCase("usemap") && copyMode == COPY) {
// // the current character should be a whitespace or =
// // either way, we just write out =
// out.write('=');
// copyMode = NO_COPY;
// String target = scanValue();
// URL t = new URL(currURL, target);
// String link = t.getFile();
// if (link.startsWith(basePath))
// link = link.substring(basePath.length());
// if (link.endsWith(".html"))
// link = link.substring(0, link.length() - 5);
// String ref = t.getRef();
// if (ref != null && ref.length() > 0)
// link = link + "!" + ref;
// out.write("\"");
// out.write('#' + link);
// out.write("\" ");
// copyMode = COPY;
// }
// else
// scanValue();
// skipSpace();
// }
// nextCh();
// }
private boolean isLink(String tag) {
return tag.equals("a");
}
/**
* Process the contents of
*/
private void scanLink() throws IOException {
out.write(pendingCopy.toString());
copyMode = COPY;
skipSpace();
while (c != '>') {
String att = scanIdentifier();
if (att.equalsIgnoreCase("href") && copyMode == COPY) {
// the current character should be a whitespace or =
// either way, we just write out =
out.write('=');
copyMode = NO_COPY;
String target = scanValue();
URL t = new URL(file.toURL(), target);
// String link = t.getFile();
String link = target;
// if (link.startsWith(basePath))
// link = link.substring(basePath.length());
if (link.endsWith(".html"))
link = link.substring(0, link.length() - 5);
String ref = t.getRef();
if (ref != null && ref.length() > 0)
link = link + "!" + ref;
out.write('"');
out.write('#' + link);
out.write('"');
copyMode = COPY;
}
else if (att.equalsIgnoreCase("name") && copyMode == COPY) {
// the current character should be a whitespace or =
// either way, we just write out =
out.write('=');
copyMode = NO_COPY;
String oldName = scanValue();
// String name = currURL.getFile();
String name = file.getPath();
// if (name.startsWith(basePath))
// name = name.substring(basePath.length());
if (name.endsWith(".html"))
name = name.substring(0, name.length() - 5);
name = name + "!" + oldName;
out.write('"');
out.write(name);
out.write('"');
copyMode = COPY;
}
else
scanValue();
skipSpace();
}
nextCh();
}
// private boolean isMap(String tag) {
// return tag.equals("map");
// }
//
// /**
// * Process the contents of
* <META name="glossaryKeywords" content="space-separated list of keywords">
*/
public class Main {
/**
* An exception to report bad command line arguments.
*/
public static class BadArgs extends Exception {
BadArgs(String msg) {
super(msg);
}
}
/**
* Command line entry point.
* @param args Command line arguments, per the usage as described.
*/
public static void main(String[] args) {
try {
if (args.length == 0)
usage(System.err);
else {
Main m = new Main(args);
m.run();
}
}
catch (BadArgs e) {
System.err.println(e);
usage(System.err);
System.exit(1);
}
catch (Throwable t) {
t.printStackTrace();
System.exit(2);
}
}
/**
* Write out short command line help.
* @param out A stream to which to write the help.
*/
private static void usage(PrintStream out) {
String program = System.getProperty("program", "java " + Main.class.getName());
out.println("Usage:");
out.println(" " + program + " options files...");
out.println("");
out.println("Arguments:");
out.println("-mapOut map.xml");
out.println(" Specify the location of the map.xml file to be written.");
out.println("-htmlOut glossary.html");
out.println(" Specify the location of the glossary.html file.");
out.println("-xmlOut glossary.xml");
out.println(" Specify the location of the glossary.xml file.");
out.println("-key keyword");
out.println(" Specify a keyword to filter HTML files.");
out.println("files...");
out.println(" HTML files and directories.");
}
public Main() { }
/**
* Create an object based on command line args.
* It is an error if no input files or no output file is given.
* @param args Command line args.
* @see #main
* @throws Main.BadArgs if problems are found in the given arguments.
*/
public Main(String[] args) throws BadArgs {
for (int i = 0; i < args.length; i++) {
if (args[i].equalsIgnoreCase("-htmlout") && i + 1 < args.length) {
htmlOutFile = new File(args[++i]);
}
else if (args[i].equalsIgnoreCase("-xmlout") && i + 1 < args.length) {
xmlOutFile = new File(args[++i]);
}
else if (args[i].equalsIgnoreCase("-mapout") && i + 1 < args.length) {
mapOutFile = new File(args[++i]);
}
else if (args[i].equalsIgnoreCase("-mapdir") && i + 1 < args.length) {
mapDir = new File(args[++i]);
}
else if (args[i].equalsIgnoreCase("-key") && i + 1 < args.length) {
keyword = args[++i];
}
else {
inFiles = new File[args.length - i];
for (int j = 0; j < inFiles.length; j++)
inFiles[j] = new File(args[i++]);
}
}
}
public static class Ant extends MatchingTask {
private Main m = new Main();
public void setHtmlOutFile(File file) {
m.htmlOutFile = file;
}
public void setXmlOutFile(File file) {
m.xmlOutFile = file;
}
public void setMapOutFile(File file) {
m.mapOutFile = file;
}
public void setMapDir(File file) {
m.mapDir = file;
}
public void setKeyword(String key) {
m.keyword = key;
}
public void setDir(File dir) {
getImplicitFileSet().setDir(dir);
}
public void execute() {
FileScanner s = getImplicitFileSet().getDirectoryScanner(getProject());
m.addFiles(s.getBasedir(), s.getIncludedFiles());
try {
m.run();
} catch (BadArgs e) {
throw new BuildException(e.getMessage());
} catch (IOException e) {
throw new BuildException(e);
}
}
}
public void addFiles(File baseDir, String[] paths) {
if (paths == null)
return;
List