[ C:TaPiC ]
Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7 Lab 8
A major aim throughout these notes will be to develop resources that can be easily reused in different forms. A first step in this direction is to look at generating wiki markup from OpenOffice.org documents. OpenOffice.org can save files in the OpenDocument format and it is this format that will be used as the primary data source for developing the OpenEd resources.
OpenDocument files are saved as zipped archives. Inside the archive are a number of xml files and some other related files. These include:
Here we'll concentrate on the content.xml file.
using System; using System.Xml; using ICSharpCode.SharpZipLib.Zip;
namespace OpenEd
{
/// <summary>
/// Summary description for od2wiki.
/// </summary>
public class od2wiki
{
private string wikiText;
private const string OFFICE_PREFIX = "office"; private const string OFFICE_URN = "urn:oasis:names:tc:opendocument:xmlns:office:1.0"; private const string TEXT_PREFIX = "text"; private const string TEXT_URN = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
private XmlNamespaceManager namespaceManager;
public od2wiki()
{
wikiText = "";
NameTable nameTable = new NameTable();
namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace(OFFICE_PREFIX, OFFICE_URN);
namespaceManager.AddNamespace(TEXT_PREFIX, TEXT_URN);
}
public void ConvertODT(string fileName)
{
ZipFile zipFile = new ZipFile(fileName);
ZipEntry contentXml = zipFile.GetEntry("content.xml");
XmlDocument content = new XmlDocument();
content.Load(zipFile.GetInputStream(contentXml));
XmlElement textElement = (XmlElement) content.SelectSingleNode("//office:text", namespaceManager);
XmlNodeList textPs = textElement.SelectNodes("text:p|text:list/text:list-item/text:p", namespaceManager);
foreach(XmlElement textP in textPs)
ConvertTextP(textP);
}
private void ConvertTextP(XmlElement p)
{
XmlAttribute style = p.Attributes["style-name", TEXT_URN];
if (style == null)
return;
switch (style.Value) {
case "Section":
wikiText += Section(p.InnerText, 2);
break;
case "Subsection":
wikiText += Section(p.InnerText, 3);
break;
case "Sub-subsection":
wikiText += Section(p.InnerText, 4);
break;
case "Paragraph":
wikiText += p.InnerText + "\n\n";
break;
case "Preformatted_20_Text":
XmlElement previousSibling = (XmlElement) p.PreviousSibling;
// This conditio is not complete.
if (previousSibling != null) {
XmlAttribute previousStyle = previousSibling.Attributes["style-name", TEXT_URN];
if (previousStyle == null || previousStyle.Value != "Preformatted_20_Text")
wikiText += " \n";
}
wikiText += " " + p.InnerText + "\n";
XmlElement nextSibling = (XmlElement) p.NextSibling;
if (nextSibling == null)
wikiText += " \n";
break;
case "P1":
wikiText += "* " + p.InnerText + "\n";
break;
default:
break;
}
}
private string Section(string text, int level)
{
return Heading(text, level) + "\n\n";
}
private string Heading(string text, int level)
{
if (level > 0) {
return "=" + Heading(text, level - 1) + "=";
}
return " " + text + " ";
}
public string WikiText
{
get { return wikiText; }
}
}
}
© 2009 Novell, Inc. All Rights Reserved.