/******************************************************************************* * ALMA - Atacama Large Millimeter Array * Copyright (c) ESO - European Southern Observatory, 2011 * (in the framework of the ALMA collaboration). * All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *******************************************************************************/ /* * Created on Oct 5, 2006 by mschilli */ package alma.acs.jhelpgen; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Gen { public static void main (String[] args) { try { if (args.length < 1) throw new IllegalArgumentException("too few arguments"); File helpDir = new File(args[0]); if (!helpDir.isDirectory()) throw new IllegalArgumentException("not a directory: "+helpDir); List fArgs = new LinkedList(); if (args.length == 1) fArgs.add (helpDir); else for (int i=1; i fArgs) throws IOException { StringBuilder worklog = new StringBuilder(); StringBuilder sbToc = new StringBuilder(); StringBuilder sbMap = new StringBuilder(); List files = new Vector(); for (File f : fArgs) if (f.isDirectory()) Util.findFiles (files, f, ".html", ".htm", ".HTML", ".HTM", ".input"); else files.add(f); String firstFileId = null; for (File f : files) { String fileContents = Util.readFile(f); String fileId = f.getPath().substring(helpDir.getName().length()+1).replace("\\", "/"); if (firstFileId == null) firstFileId = fileId; AnchorNode root = htmlToDom(fileContents); // tweak anchors a little for (AnchorNode n : root.depthFirst(new Vector())) n.anchorName = fileId +"#"+ n.anchorName; domToToc (root.children, sbToc); domToMap (root.children, sbMap); } File fToc = new File(helpDir, Const.TOC_FILENAME); File fMap = new File(helpDir, Const.MAP_FILENAME); File fSet = new File(helpDir, Const.SET_FILENAME); String toc = finishToc(sbToc); String map = finishMap(sbMap, firstFileId); String set = finishHelpset(fToc.getName(), fMap.getName()); Util.writeFile(toc, fToc); Util.writeFile(map, fMap); Util.writeFile(set, fSet); worklog.append("Created "+fToc.getPath()+"\n"); worklog.append("Created "+fMap.getPath()+"\n"); worklog.append("Created "+fSet.getPath()+"\n"); // copy images from our jar to helpdir String[] imageNames = new String[]{"chapTopic.gif", "topic.gif", "toplevel.gif"}; File imageDir = new File(helpDir, "images"); imageDir.mkdir(); for (String imageName : imageNames) { String imageContent = Util.readResource("/alma/acs/jhelpgen/content/" + imageName); File fImage = new File(imageDir, imageName); Util.writeFile(imageContent, fImage); worklog.append("Created " + fImage.getPath()+"\n"); } System.out.println("\n" + worklog); } protected AnchorNode htmlToDom (String contents) { AnchorNode root = new AnchorNode(0, "root", ""); AnchorNode latest = root; Pattern headingPattern = Pattern.compile("<[Hh]([1-6]).*?"); Pattern anchorPattern = Pattern.compile("<[Aa].* name=['\"]?(\\w*)['\"]?.*>(.*)"); Matcher hm = headingPattern.matcher(contents); while (hm.find()) { String heading = hm.group().trim(); int level = Integer.parseInt(hm.group(1)); Matcher am = anchorPattern.matcher(heading); if (am.find()) { String anchor = am.group(1).trim(); String pretty = am.group(2).trim(); AnchorNode n = new AnchorNode(level, anchor, pretty); // sort into tree AnchorNode cand = latest; while (cand.level >= n.level) cand = cand.parent; cand.add(n); latest = n; } } return root; } protected void domToMap (List nn, StringBuilder sb) { for (AnchorNode m : nn) domToMap(m, sb); } protected void domToMap (AnchorNode n, StringBuilder sb) { sb.append("\n\t"); sb.append(""); domToMap (n.children, sb); } protected void domToToc (List nn, StringBuilder sb) { for (AnchorNode m : nn) domToToc(m, sb); } protected void domToToc (AnchorNode n, StringBuilder sb) { sb.append("\n").append("\t\t\t\t\t\t".substring(0, n.level)); sb.append(""); else { sb.append(">"); domToToc (n.children, sb); sb.append("\n").append("\t\t\t\t\t\t".substring(0, n.level)); sb.append(""); } } protected String finishToc (StringBuilder sb) { String begin = "" + "\n" + "\n"; String end = "\n"; return begin + sb + end; } protected String finishMap (StringBuilder sb, String introFileId) { String begin = "" + "\n " + "\n " + "\n\t " + "\n\t " + "\n\t" + "\n" + "\n\t"; String end = "\n"; return begin + sb + end; } protected String finishHelpset (String fTocName, String fMapName) { String b = " " + " Online Help" + "\n" + "\n\t_intro" + "\n\t" + "\n" + "\n" + "\n\tTOC" + "\n\t" + " \n\tjavax.help.TOCView" + "\n\t"+fTocName+"" + "\n" + "\n"; return b; } protected class AnchorNode { public int level; public String anchorName; public String prettyName; public AnchorNode parent; public List children = new Vector(); AnchorNode(int level, String anchorName, String prettyName) { this.level = level; this.anchorName = anchorName; this.prettyName = prettyName; } void add (AnchorNode n) { n.parent = this; children.add(n); } List depthFirst (List ret) { ret.add(this); for (AnchorNode m : children) m.depthFirst(ret); return ret; } @Override public String toString() { return "AnchorNode[level="+level+", anchor="+anchorName+", caption="+prettyName+"]"; } } }