/* Copyright (C) 2005 Tresys Technology, LLC * License: refer to COPYING file for license information. * Authors: Spencer Shimko * * Converter.java: The reference policy documentation converter * Version: @version@ */ import policy.*; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; import java.util.Map; /** * The reference policy documentation generator and xml analyzer class. * It pulls in XML describing reference policy, transmogrifies it, * and spits it back out in some other arbitrary format. */ public class Converter{ private Policy policy; private static final String HTMLEXT = ".html"; private static final String indexContent = "

Welcome to the reference policy API!

" + "Please choose from the navigation options to the left.

"; private StringBuffer headerOutput = new StringBuffer(); private StringBuffer footerOutput = new StringBuffer(); private File outDir; public Converter(Policy pol, File headerFile, File footerFile) throws IOException{ policy = pol; /* * Setup header and footer. */ FileReader headIn = new FileReader(headerFile); BufferedReader br = new BufferedReader(headIn); String line = null; //not declared within while loop while (( line = br.readLine()) != null){ headerOutput.append(line); headerOutput.append(System.getProperty("line.separator")); } FileReader footerIn = new FileReader(footerFile); br = new BufferedReader(footerIn); line = null; //not declared within while loop while (( line = br.readLine()) != null){ footerOutput.append(line); footerOutput.append(System.getProperty("line.separator")); } } public void Convert(File _outDir) throws IOException{ outDir = _outDir; // write the index document FileWrite("index" + HTMLEXT, headerOutput.toString() + Menu().toString() + indexContent + footerOutput.toString()); // walk the policy and write pages for each module for(Map.Entry lay:policy.Children.entrySet()){ Layer layer = lay.getValue(); // the base output contains the menu and layer content header StringBuffer baseOutput = new StringBuffer(); // the layer output will be filled with modules and summarys for content StringBuffer layerOutput = new StringBuffer(); // create the navigation menu baseOutput.append(Menu(layer.Name)); baseOutput.append("
\n"); baseOutput.append("\t

Layer: " + layer.Name + "

\n"); layerOutput.append("\n"); layerOutput.append("" + ""); for(Map.Entry mod:layer.Children.entrySet()){ // module output will be filled with in-depth module info. StringBuffer moduleOutput = new StringBuffer(); Module module = mod.getValue(); // get the content for the module's document moduleOutput.append(moduleContent(mod.getValue()).toString() + ""); // get the summary and name for the layer's document layerOutput.append("" + "\n\n"); // write module document FileWrite(layer.Name + "_" + module.Name + HTMLEXT, headerOutput.toString() + "\n" + baseOutput.toString() + moduleOutput.toString() + footerOutput.toString()); } // write layer document FileWrite(layer.Name + HTMLEXT, headerOutput.toString() + "\n" + baseOutput.toString() + layerOutput.toString() + "" + footerOutput.toString()); } } private StringBuffer Menu(String key){ StringBuffer out = new StringBuffer(); out.append("
\n"); for(Map.Entry layer:policy.Children.entrySet()){ String layerName = layer.getKey(); // show the modules for the current key if (layerName.equals(key)){ out.append("\t" + layerName + "
\n"); out.append("\t
\n"); for(Map.Entry module:layer.getValue().Children.entrySet()){ String moduleName = module.getKey(); out.append("\t\t- " + moduleName + "
\n"); } out.append("\t
\n"); } else { out.append("\t+ " + layerName + "
\n"); } } out.append("
"); return out; } private StringBuffer Menu(){ StringBuffer out = new StringBuffer(); out.append("
\n"); for(Map.Entry layer:policy.Children.entrySet()){ String layerName = layer.getKey(); out.append("\t" + layerName + "
\n"); out.append("\t
\n"); for(Map.Entry module:layer.getValue().Children.entrySet()){ String moduleName = module.getKey(); out.append("\t\t- " + moduleName + "
\n"); } out.append("\t
\n"); } out.append("
"); return out; } private StringBuffer moduleContent(Module module){ StringBuffer out = new StringBuffer(); out.append("\t

Module: "+ module.Name + "
\n"); out.append("\tSummary: " + module.PCDATA + "

\n"); for (Map.Entry inter:module.Children.entrySet()){ Interface iface = inter.getValue(); // main table header out.append("
Module NameSummary
" + module.Name + "" + module.PCDATA + "
\n"); out.append("\n"); // only show weight when type isnt none if (iface.Type != InterfaceType.None){ out.append("\n" + "\n"); out.append("\t\n"); } else { out.append("\n"); out.append("\n"); } out.append("\n"); out.append("
Interface
Name" + iface.Name + "
Flow Type" + iface.Type.toString() + "
Flow Weight" + iface.Weight + "
Name" + iface.Name + "
Flow Type" + iface.Type.toString() + "
Description" + iface.PCDATA + "
\n" + "" + ""); for (Map.Entry param:iface.Children.entrySet()){ Parameter parameter = param.getValue(); out.append("\t\n"); out.append("\t\n"); String opt = parameter.GetAttribute("optional"); if (opt != null && opt.equalsIgnoreCase("true")){ out.append("\t\n"); } else { out.append("\t\n"); } } out.append("\n
ParameterDescriptionOptional
" + parameter.Name + "" + parameter.PCDATA + "
Yes
No


\n"); } return out; } /** * Write to sub directory. * @param path * @param outFilename * @param content * @return */ private void FileWrite (String path, String outFilename, String content){ try { // create parent if necessary File outParent = new File(outDir, path ); File outFile = new File(outParent, outFilename ); if (outParent.exists() && !outParent.isDirectory()){ throw new IOException("Output directory not really a directory"); } else if (!outParent.exists()){ outParent.mkdirs(); } PrintStream stream = new PrintStream(new FileOutputStream(outFile)); stream.println(content); stream.flush(); stream.close(); } catch (Exception e){ System.err.println (e.getMessage()); System.exit(1); } } /** * Write to output directory directly. * * @param path * @param outFilename * @param content * @return */ private void FileWrite (String outFilename, String content){ try { File out = new File(outDir, outFilename ); PrintStream stream = new PrintStream(new FileOutputStream(out)); stream.println(content); stream.flush(); stream.close(); } catch (Exception e){ System.err.println (e.getMessage()); System.exit(1); } } }