Index: trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/build.properties
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/build.properties	(revision 40)
+++ trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/build.properties	(revision 41)
@@ -3,5 +3,6 @@
                .,\
                plugin.xml,\
-               lib/commons-lang-2.5.jar
+               lib/commons-lang-2.5.jar,\
+               resources/
 
 source.. = src/,\
Index: trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/resources/html-footer.html
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/resources/html-footer.html	(revision 40)
+++ 	(revision )
@@ -1,7 +1,0 @@
-<p class=PL><span lang=EN-GB style='mso-no-proof:no'><o:p>&nbsp;</o:p></span></p>
-
-</div>
-
-</body>
-
-</html>
Index: trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/resources/html-header.html
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/resources/html-header.html	(revision 40)
+++ trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/resources/html-header.html	(revision 41)
@@ -1,3 +1,3 @@
-html xmlns:v="urn:schemas-microsoft-com:vml"
+<html xmlns:v="urn:schemas-microsoft-com:vml"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:w="urn:schemas-microsoft-com:office:word"
Index: trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java	(revision 40)
+++ trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java	(revision 41)
@@ -144,7 +144,14 @@
 		StringBuffer whiteSpaces = new StringBuffer();
 		int currentPosition = position;
-		while (isWhitespace(str.charAt(currentPosition))) {
+		if (currentPosition >= str.length() || currentPosition < 0)
+			return "";
+		char ch;
+		ch = str.charAt(currentPosition);
+		while (isWhitespace(ch)) {
 			whiteSpaces.append(str.charAt(currentPosition));
 			currentPosition--;
+			if (currentPosition < 0)
+				break;
+			ch = str.charAt(currentPosition);
 		}
 		return whiteSpaces.toString();
Index: trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfHtmlFormatterVisitor.java
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfHtmlFormatterVisitor.java	(revision 40)
+++ trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfHtmlFormatterVisitor.java	(revision 41)
@@ -8,6 +8,8 @@
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.xtext.parsetree.LeafNode;
+import org.eclipse.xtext.parsetree.NodeUtil;
 
 import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
+import de.ugoe.cs.swe.bnftools.ebnf.Import;
 import de.ugoe.cs.swe.bnftools.ebnf.Rule;
 import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
@@ -20,4 +22,5 @@
 	private ArrayList<MetaTextEntry> transformedTexts = new ArrayList<MetaTextEntry>();
 	private int lastMetaTextBlock = 0;
+	private boolean isParagraphOpen = false;
 	
 	public EbnfHtmlFormatterVisitor(EObject rootNode, FormatterConfig config) {
@@ -111,5 +114,5 @@
 				}
 			} else {
-				if (lastMetaTextBlock == 0 && (currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces == currentMetaText.getOffset())) {
+				while (lastMetaTextBlock == 0 && (currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces >= currentMetaText.getOffset())) {
 					result.append(currentMetaText.getText());
 					metaTexts.remove(currentMetaText);
@@ -118,5 +121,7 @@
 					else
 						currentMetaText = null;
-					lastMetaTextBlock = 1;
+					
+					if ((currentMetaText == null) && (bufferPositionFormattedTextNoWhitespaces != currentMetaText.getOffset()))
+						lastMetaTextBlock = 1;
 				}
 
@@ -139,15 +144,29 @@
 				}
 
-				if (lastMetaTextBlock == 1 && (currentMetaText != null) && (bufferPositionFormattedTextNoWhitespaces == currentMetaText.getOffset())) {
+				while ( lastMetaTextBlock == 1 && currentMetaText != null && bufferPositionFormattedTextNoWhitespaces >= currentMetaText.getOffset()) {
 					result.append(currentMetaText.getText());
 					metaTexts.remove(currentMetaText);
-					if (metaTexts.iterator().hasNext())
+					if (metaTexts.iterator().hasNext()) {
 						currentMetaText = metaTexts.iterator().next();
-					else
+					} else {
 						currentMetaText = null;
-					lastMetaTextBlock = 0;
+					}
+					
+					if ((currentMetaText == null) || (bufferPositionFormattedTextNoWhitespaces != currentMetaText.getOffset()))
+						lastMetaTextBlock = 0;
 				}
 			}
 		}
+		while ( bufferPositionFormattedTextNoWhitespaces >= formattedTextNoWhitespaces.length()-1 && currentMetaText != null) {
+			result.append(currentMetaText.getText());
+			metaTexts.remove(currentMetaText);
+			if (metaTexts.iterator().hasNext()) {
+				currentMetaText = metaTexts.iterator().next();
+			} else {
+				currentMetaText = null;
+			}
+		}
+
+		
 		buf = result;
 	}
@@ -164,11 +183,61 @@
 		newLineOffsetCounter += original.length();
 	}
-
+	
+	protected void newLine() {
+		buf.append("\n");
+		if (!isParagraphOpen) {
+			metaText("\n<p class=PL style='mso-pagination:widow-orphan lines-together;page-break-after: avoid'>\n");
+			isParagraphOpen = true;
+		} else {
+			metaText("\n</p>\n");
+			isParagraphOpen = false;
+		}
+		
+		if ((ruleSpacingStack != null) && (!ruleSpacingStack.empty())) {
+			newLineOffsetCounter = ruleSpacingStack.peek();
+		} else {
+			newLineOffsetCounter = 0;
+		}
+	}
+	
 	// -----------------------------------------------------------------------------
 	
+	protected void visitBefore(EtsiBnf node) {
+		parserEtsiBnfNode = NodeUtil.getNodeAdapter(node).getParserNode();
+		collectAllComments(parserEtsiBnfNode);
+		originalText = NodeUtil.getNodeAdapter(node).getParserNode().serialize();
+		originalTextNoWhitespaces = originalText.replaceAll("[ \t\n\r]", "");
+		
+		textTransformed("grammar " + node.getName(),"");
+		if (node.getType() != null)
+			textTransformed(node.getType(),"");
+		textTransformed(";","");
+		metaText("<body lang=EN-US link=blue vlink=purple style='tab-interval:14.15pt'>\n");
+		metaText("<div class=WordSection1>\n");
+		
+//		newLine();
+//		newLine();
+	}
+	
 	protected void visitAfter(EtsiBnf node) {
+		metaText("</div>\n");
+		metaText("</body>\n");
+		metaText("</html>\n");
 		weaveComments();
 	}
 
+	protected void visitBefore(Import node) {
+		textTransformed("import \"" + node.getImportURI() + "\"","");
+		if (node.getGrammarType() != null) {
+			textTransformed("/" + node.getGrammarType(), "");
+		}
+		if (node.getLabel() != null) {
+			space();
+			textTransformed("label: " + node.getLabel(), "");
+		}
+		textTransformed(";", "");
+//		text("\n");
+	}
+	
 	protected void visitBefore(SectionHeading node) {
 		if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
@@ -177,7 +246,9 @@
 		lastWasSectionHeading=true;
 		
-		metaText("<h1>");
-		text(node.getSectionHeader().replaceAll("[\n\r]", ""));
-		metaText("</h1>");
+		metaText("<h3><span lang=EN-GB>\n");
+		String text = node.getSectionHeader().replaceAll("[\n\r]", "");
+		textTransformed(text, StringEscapeUtils.escapeHtml(text).replaceAll("[\n\r]", ""));
+//		textTransformed(text, text.replaceAll("[\t]", "\\<span style='mso-tab-count:1'\\>"));
+		metaText("\n</span></h3>\n");
 	}
 
@@ -188,7 +259,7 @@
 		wrap();
 		
-		metaText("<a href=\"#" + node.getRuleref().getName() + "\">");
+		metaText("<u><span style='color:blue'><a href=\"#T" + node.getRuleref().getName() + "\">");
 		text(node.getRuleref().getName());
-		metaText("</a>");
+		metaText("</a></span></u>");
 	}
 
@@ -197,5 +268,5 @@
 
 	protected void visitBefore(Rule node) {
-		if (lastWasSectionHeading)
+//		if (lastWasSectionHeading)
 			newLine();
 		
@@ -206,7 +277,7 @@
 		if (node.getRulenumber() > 0)
 			text(node.getRulenumber() + ". ");
-		
-		metaText("<a name=\"#" + node.getName() + "\">");
-		text(node.getName());
+
+		metaText("<span lang=EN-GB style='mso-no-proof:no'>\n<a name=\"#T" + node.getName() + "\">");
+		textTransformed(node.getName(), node.getName().replaceAll("[ \t\r\n]", ""));
 		metaText("</a>");
 		text(" ::= ");
@@ -217,7 +288,8 @@
 	
 	protected void visitAfter(Rule node) {
-		metaText("<br/>");
+		metaText("<o:p></o:p>\n</span>");
 		text(";");
 		newLine();
+		text("\n");
 		ruleSpacingStack.pop();
 	}
@@ -227,9 +299,9 @@
 		if (node.getLiteral() != null) {
 //			text("\"" + node.getLiteral() + "\"");
-			textTransformed("\"" + node.getLiteral() + "\"", StringEscapeUtils.escapeHtml("\"" + node.getLiteral() + "\""));
+			textTransformed("\"" + node.getLiteral() + "\"", StringEscapeUtils.escapeHtml("\"" + node.getLiteral() + "\"").replaceAll("[ \t\n\r]", ""));
 //			textTransformed("\"" + node.getLiteral() + "\"", "gnabar");
 		} else if (node.getColon() != null) {
 //			text("\"\"\"");
-			textTransformed("\"\"\"", StringEscapeUtils.escapeHtml("\"\"\""));
+			textTransformed("\"\"\"", StringEscapeUtils.escapeHtml("\"\"\"").replaceAll("[ \t\n\r]", ""));
 //			textTransformed("\"\"\"", "gnabar");
 		}
Index: trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/HtmlFormatterHandler.java
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/HtmlFormatterHandler.java	(revision 40)
+++ trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/HtmlFormatterHandler.java	(revision 41)
@@ -1,7 +1,13 @@
 package de.ugoe.cs.swe.bnftools.ui.formatter;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
 
 import org.eclipse.core.commands.AbstractHandler;
@@ -20,4 +26,5 @@
 import org.eclipse.xtext.ui.editor.XtextEditor;
 import org.eclipse.xtext.ui.editor.model.XtextDocument;
+import org.osgi.framework.Bundle;
 
 import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
@@ -46,10 +53,28 @@
 		visitor.accept();
 
-		writeFile(visitor.getBuf().toString(), parent.getLocationURI().getPath() + "/" + editor.getResource().getName() + ".html");
+		Bundle bundle = Platform.getBundle("de.ugoe.cs.swe.bnftools.ebnf.ui");
+		String bundlePath = bundle.getLocation().replaceAll("reference:file:","");
+		String resourcePath = bundle.getResource("resources/html-header.html").getPath(); 
+		String result = "";
+		try {
+			result = readFileAsString(bundlePath + resourcePath);
+		} catch (IOException e) {
+		}
+		result = result + visitor.getBuf();
+		
 		System.out.println(visitor.getBuf());
+		
+		writeFile(result, editor.getResource().getLocation().toPortableString().toString() + ".html");
 		
 		return null;
 	}
 
+	private String readFileAsString(String filePath) throws java.io.IOException{
+	    byte[] buffer = new byte[(int) new File(filePath).length()];
+	    FileInputStream f = new FileInputStream(filePath);
+	    f.read(buffer);
+	    return new String(buffer);
+	}
+	
 	private void writeFile(String str, String filename) {
 		try {
