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

Welcome to the reference policy API!

" +

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

Layer: " + layer.Name + "

\n");

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

Module: "+ module.Name + "
\n");

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

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