001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.wiki;
004:
005: import java.util.*;
006: import fitnesse.util.*;
007: import org.w3c.dom.*;
008:
009: public class PageXmlizer {
010: public Document xmlize(WikiPage page) throws Exception {
011: Document document = XmlUtil.newDocument();
012: Element pageElement = createXmlFromPage(document, page);
013: document.appendChild(pageElement);
014:
015: return document;
016: }
017:
018: public void deXmlize(Document doc, WikiPage context,
019: XmlizerPageHandler handler) throws Exception {
020: Element pageElement = doc.getDocumentElement();
021: addChildFromXml(pageElement, context, handler);
022: }
023:
024: public void deXmlizeSkippingRootLevel(Document document,
025: WikiPage context, XmlizerPageHandler handler)
026: throws Exception {
027: Element pageElement = document.getDocumentElement();
028: addChildrenFromXml(pageElement, context, handler);
029: }
030:
031: public Document xmlize(PageData data) throws Exception {
032: Document document = XmlUtil.newDocument();
033: Element dataElement = document.createElement("data");
034: XmlUtil.addCdataNode(document, dataElement, "content", data
035: .getContent());
036:
037: Element propertiesElement = data.getProperties()
038: .makeRootElement(document);
039: dataElement.appendChild(propertiesElement);
040:
041: document.appendChild(dataElement);
042:
043: return document;
044: }
045:
046: public PageData deXmlizeData(Document document) throws Exception {
047: PageData data = new PageData(new MockWikiPage());
048: Element dataElement = document.getDocumentElement();
049:
050: String content = XmlUtil.getLocalTextValue(dataElement,
051: "content");
052: data.setContent(content);
053:
054: Element propertiesElement = XmlUtil.getLocalElementByTagName(
055: dataElement, "properties");
056: WikiPageProperties properties = new WikiPageProperties(
057: propertiesElement);
058: data.setProperties(properties);
059:
060: return data;
061: }
062:
063: private Element createXmlFromPage(Document document, WikiPage page)
064: throws Exception {
065: Element pageElement = document.createElement("page");
066: XmlUtil.addTextNode(document, pageElement, "name", page
067: .getName());
068:
069: addXmlFromChildren(page, document, pageElement);
070:
071: return pageElement;
072: }
073:
074: private void addXmlFromChildren(WikiPage page, Document document,
075: Element pageElement) throws Exception {
076: Element childrenElement = document.createElement("children");
077: List children = page.getChildren();
078: for (Iterator iterator = children.iterator(); iterator
079: .hasNext();) {
080: WikiPage child = (WikiPage) iterator.next();
081: Element childElement = createXmlFromPage(document, child);
082: childrenElement.appendChild(childElement);
083: }
084: pageElement.appendChild(childrenElement);
085: }
086:
087: private void addChildFromXml(Element pageElement, WikiPage context,
088: XmlizerPageHandler handler) throws Exception {
089: String name = XmlUtil.getTextValue(pageElement, "name");
090: WikiPage newPage = context.addChildPage(name);
091: handler.pageAdded(newPage);
092: addChildrenFromXml(pageElement, newPage, handler);
093: handler.exitPage();
094: }
095:
096: private void addChildrenFromXml(Element pageElement,
097: WikiPage contextPage, XmlizerPageHandler handler)
098: throws Exception {
099: Element childrenElement = XmlUtil.getLocalElementByTagName(
100: pageElement, "children");
101: NodeList childNodes = childrenElement.getChildNodes();
102: for (int i = 0; i < childNodes.getLength(); i++) {
103: Node node = childNodes.item(i);
104: if ("page".equals(node.getNodeName()))
105: addChildFromXml((Element) node, contextPage, handler);
106: }
107: }
108: }
|