uidesigner/com.nokia.sdt.component.symbian/src/com/nokia/sdt/component/symbian/sourcegen/macrohelp/MacroHelpGenerator.java
changeset 577 b46382ab04d2
parent 0 fb279309251b
equal deleted inserted replaced
574:7b5e8f6c4370 577:b46382ab04d2
     1 /*
     1 /*
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    23 import com.nokia.sdt.emf.component.DocumentRoot;
    23 import com.nokia.sdt.emf.component.DocumentRoot;
    24 import com.nokia.sdt.emf.component.MacroArgumentType;
    24 import com.nokia.sdt.emf.component.MacroArgumentType;
    25 import com.nokia.sdt.emf.component.loader.Loader;
    25 import com.nokia.sdt.emf.component.loader.Loader;
    26 import com.nokia.cpp.internal.api.utils.core.FileUtils;
    26 import com.nokia.cpp.internal.api.utils.core.FileUtils;
    27 import com.nokia.cpp.internal.api.utils.core.MessageLocation;
    27 import com.nokia.cpp.internal.api.utils.core.MessageLocation;
    28 import com.sun.org.apache.html.internal.dom.HTMLDOMImplementationImpl;
       
    29 
    28 
    30 import org.eclipse.core.runtime.Path;
    29 import org.eclipse.core.runtime.Path;
    31 import org.eclipse.emf.common.util.TreeIterator;
    30 import org.eclipse.emf.common.util.TreeIterator;
    32 import org.eclipse.emf.common.util.URI;
    31 import org.eclipse.emf.common.util.URI;
    33 import org.eclipse.emf.ecore.EObject;
    32 import org.eclipse.emf.ecore.EObject;
       
    33 import org.w3c.dom.Document;
    34 import org.w3c.dom.Element;
    34 import org.w3c.dom.Element;
    35 import org.w3c.dom.Node;
    35 import org.w3c.dom.Node;
    36 import org.w3c.dom.html.HTMLDOMImplementation;
       
    37 import org.w3c.dom.html.HTMLDocument;
       
    38 
    36 
    39 import java.io.File;
    37 import java.io.File;
    40 import java.io.FileOutputStream;
    38 import java.io.FileOutputStream;
    41 import java.util.ArrayList;
    39 import java.util.ArrayList;
    42 import java.util.Collections;
    40 import java.util.Collections;
    44 import java.util.HashMap;
    42 import java.util.HashMap;
    45 import java.util.LinkedHashMap;
    43 import java.util.LinkedHashMap;
    46 import java.util.List;
    44 import java.util.List;
    47 import java.util.Map;
    45 import java.util.Map;
    48 
    46 
       
    47 import javax.xml.parsers.DocumentBuilderFactory;
    49 import javax.xml.transform.Result;
    48 import javax.xml.transform.Result;
    50 import javax.xml.transform.Transformer;
    49 import javax.xml.transform.Transformer;
    51 import javax.xml.transform.TransformerFactory;
    50 import javax.xml.transform.TransformerFactory;
    52 import javax.xml.transform.dom.DOMSource;
    51 import javax.xml.transform.dom.DOMSource;
    53 import javax.xml.transform.stream.StreamResult;
    52 import javax.xml.transform.stream.StreamResult;
    60  *
    59  *
    61  */
    60  */
    62 public class MacroHelpGenerator {
    61 public class MacroHelpGenerator {
    63 	private SourceGenMacroSupport macroSupport; 
    62 	private SourceGenMacroSupport macroSupport; 
    64 	private Map<String, List<ResolvedMacro>> macroMap;
    63 	private Map<String, List<ResolvedMacro>> macroMap;
       
    64 	private Document document;
    65 
    65 
    66 	public static void main(String[] args) {
    66 	public static void main(String[] args) {
    67 		MacroHelpGenerator generator = new MacroHelpGenerator();
    67 		MacroHelpGenerator generator = new MacroHelpGenerator();
    68 		
    68 		
    69 		// first, parse them all
    69 		// first, parse them all
   128 		else
   128 		else
   129             throw new IllegalArgumentException("Cannot load " + fName);
   129             throw new IllegalArgumentException("Cannot load " + fName);
   130 
   130 
   131 	}
   131 	}
   132 	private void generate(String fName) throws Exception {
   132 	private void generate(String fName) throws Exception {
   133 		HTMLDOMImplementation domImpl = HTMLDOMImplementationImpl.getHTMLDOMImplementation();
   133 		document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
   134 		HTMLDocument document = domImpl.createHTMLDocument(
   134 		Element html = document.createElement("html");
   135 				"Help for Macros in " + new File(fName).getName());
   135 		document.appendChild(html);
   136 		
   136 		html.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
       
   137 		Element head = document.createElement("head");
       
   138 		html.appendChild(head);
       
   139 		Element title = document.createElement("title");
       
   140 		head.appendChild(title);
       
   141 		title.setTextContent("Help for Macros in " + new File(fName).getName());
       
   142 		
       
   143 		Element body = document.createElement("body");
       
   144 		html.appendChild(body);
   137 		Element tocParent = document.createElement("p");
   145 		Element tocParent = document.createElement("p");
   138 		document.getBody().appendChild(tocParent);
   146 		body.appendChild(tocParent);
   139 		Element tocTitle = document.createElement("h1");
   147 		Element tocTitle = document.createElement("h1");
   140 		tocTitle.setTextContent("Table of contents");
   148 		tocTitle.setTextContent("Table of contents");
   141 		tocParent.appendChild(tocTitle);
   149 		tocParent.appendChild(tocTitle);
   142 		
   150 		
   143 		Element helpParent = document.createElement("p");
   151 		Element helpParent = document.createElement("p");
   144 		document.getBody().appendChild(helpParent);
   152 		body.appendChild(helpParent);
   145 
   153 
   146 		for (ResolvedMacro macro : macroMap.get(fName)) {
   154 		for (ResolvedMacro macro : macroMap.get(fName)) {
   147 			generateMacroHelp(document,
   155 			generateMacroHelp(tocParent,
   148 					tocParent,
       
   149 					helpParent,
   156 					helpParent,
   150 					macro);
   157 					macro);
   151 			tocParent.appendChild(document.createTextNode("\n"));
   158 			tocParent.appendChild(document.createTextNode("\n"));
   152 			helpParent.appendChild(document.createTextNode("\n"));
   159 			helpParent.appendChild(document.createTextNode("\n"));
   153 		}
   160 		}
   171 		fileOutputStream.close();
   178 		fileOutputStream.close();
   172 	}
   179 	}
   173 	
   180 	
   174 
   181 
   175 	private void generateMacroHelp(
   182 	private void generateMacroHelp(
   176 			HTMLDocument document,
       
   177 			Element tocParent,
   183 			Element tocParent,
   178 			Element helpParent, 
   184 			Element helpParent,
   179 			ResolvedMacro macro) {
   185 			ResolvedMacro macro) {
   180 		
   186 		
   181 		String id = macro.macro.getId();
   187 		String id = macro.macro.getId();
   182 		
   188 		
   183 		// add entry to table of contents
   189 		// add entry to table of contents
   199 		
   205 		
   200 		Element helpEntry = document.createElement("h2");
   206 		Element helpEntry = document.createElement("h2");
   201 		helpEntry.setTextContent(id);
   207 		helpEntry.setTextContent(id);
   202 		helpParent.appendChild(helpEntry);
   208 		helpParent.appendChild(helpEntry);
   203 		
   209 		
   204 		generateMacroDescription(document, helpParent, macro);
   210 		generateMacroDescription(helpParent, macro);
   205 	}
   211 	}
   206 
   212 
   207 	private void generateMacroDescription(HTMLDocument document,
   213 	private void generateMacroDescription(Element helpEntry,
   208 			Element helpEntry, ResolvedMacro macro) {
   214 			ResolvedMacro macro) {
   209 		DefineMacroType dmt = macro.macro;
   215 		DefineMacroType dmt = macro.macro;
   210 		String help = dmt.getHelp();
   216 		String help = dmt.getHelp();
   211 		
   217 		
   212 		if (help == null) {
   218 		if (help == null) {
   213 			Element para = document.createElement("p");
   219 			Element para = document.createElement("p");
   214 			para.setTextContent("This macro is undocumented (add a 'help' attribute)");
   220 			para.setTextContent("This macro is undocumented (add a 'help' attribute)");
   215 			helpEntry.appendChild(para);
   221 			helpEntry.appendChild(para);
   216 		}
   222 		}
   217 		else {
   223 		else {
   218 			helpEntry.appendChild(encodeHTML(document, help));
   224 			helpEntry.appendChild(encodeHTML(help));
   219 		}
   225 		}
   220 		
   226 		
   221 		Element header = document.createElement("h3");
   227 		Element header = document.createElement("h3");
   222 		header.setTextContent("Arguments");
   228 		header.setTextContent("Arguments");
   223 		helpEntry.appendChild(header);
   229 		helpEntry.appendChild(header);
   224 		
   230 		
   225 		//Element ul = document.createElement("ul");
   231 		//Element ul = document.createElement("ul");
   226 		generateMacroArgumentsHelp(document, helpEntry, macro);
   232 		generateMacroArgumentsHelp(helpEntry, macro);
   227 		//helpEntry.appendChild(ul);
   233 		//helpEntry.appendChild(ul);
   228 	}
   234 	}
   229 
   235 
   230 	private void generateMacroArgumentsHelp(HTMLDocument document,
   236 	private void generateMacroArgumentsHelp(Element ul,
   231 			Element ul, ResolvedMacro macro) {
   237 			ResolvedMacro macro) {
   232 
   238 
   233 		// organize arguments by their origin: 
   239 		// organize arguments by their origin: 
   234 		// these are already ordered as <imported arguments>
   240 		// these are already ordered as <imported arguments>
   235 		// then <custom arguments>.  We will make a linked map that
   241 		// then <custom arguments>.  We will make a linked map that
   236 		// places custom arguments first followed by imported arguments.
   242 		// places custom arguments first followed by imported arguments.
   267 					entry.getKey(), entry.getValue());
   273 					entry.getKey(), entry.getValue());
   268 			
   274 			
   269 		}
   275 		}
   270 	}
   276 	}
   271 	
   277 	
   272 	private void emitArgumentGroup(HTMLDocument document,
   278 	private void emitArgumentGroup(Document document,
   273 			Element parent,
   279 			Element parent,
   274 			boolean isInherited,
   280 			boolean isInherited,
   275 			ResolvedMacro fromMacro,
   281 			ResolvedMacro fromMacro,
   276 			List<MacroArgumentType> arguments) {
   282 			List<MacroArgumentType> arguments) {
   277 		
   283 		
   319 						return 0;
   325 						return 0;
   320 					}
   326 					}
   321 			
   327 			
   322 		});
   328 		});
   323 		for (MacroArgumentType argument : arguments) {
   329 		for (MacroArgumentType argument : arguments) {
   324 			generateMacroArgumentHelp(document, table, fromMacro, argument);
   330 			generateMacroArgumentHelp(table, fromMacro, argument);
   325 		}
   331 		}
   326 
   332 
   327 		
   333 		
   328 	}
   334 	}
   329 	private void generateMacroArgumentHelp(HTMLDocument document,
   335 	private void generateMacroArgumentHelp(Element table,
   330 			Element table, ResolvedMacro macro, MacroArgumentType argument) {
   336 			ResolvedMacro macro, MacroArgumentType argument) {
   331 		Element tr = document.createElement("tr");
   337 		Element tr = document.createElement("tr");
   332 		table.appendChild(tr);
   338 		table.appendChild(tr);
   333 
   339 
   334 		Element td;
   340 		Element td;
   335 		td = document.createElement("td");
   341 		td = document.createElement("td");
   347 			td.setTextContent("\u00A0");
   353 			td.setTextContent("\u00A0");
   348 		tr.appendChild(td);
   354 		tr.appendChild(td);
   349 
   355 
   350 		td = document.createElement("td");
   356 		td = document.createElement("td");
   351 		if (argument.getHelp() != null && argument.getHelp().length() > 0)
   357 		if (argument.getHelp() != null && argument.getHelp().length() > 0)
   352 			td.appendChild(encodeHTML(document, argument.getHelp()));
   358 			td.appendChild(encodeHTML(argument.getHelp()));
   353 		else
   359 		else
   354 			td.setTextContent("\u00A0");
   360 			td.setTextContent("\u00A0");
   355 		tr.appendChild(td);
   361 		tr.appendChild(td);
   356 	}
   362 	}
   357 	
   363 	
   358 	//static Pattern PARAGRAPH_PATTERN = Pattern.compile("(.*)(\r?\n)\\s*(\r?\n)", Pattern.MULTILINE);
   364 	//static Pattern PARAGRAPH_PATTERN = Pattern.compile("(.*)(\r?\n)\\s*(\r?\n)", Pattern.MULTILINE);
   359 	
   365 	
   360 	private Node encodeHTML(HTMLDocument document, String help) {
   366 	private Node encodeHTML(String help) {
   361 		Node el = document.createTextNode("");
   367 		Node el = document.createTextNode("");
   362 		boolean hadParagraphs = false;
   368 		boolean hadParagraphs = false;
   363 		int idx = 0;
   369 		int idx = 0;
   364 		while (idx < help.length()) {
   370 		while (idx < help.length()) {
   365 			int para = help.indexOf("$p$", idx);
   371 			int para = help.indexOf("$p$", idx);