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.io.InputStream;
006: import java.io.OutputStream;
007: import java.io.Serializable;
008: import java.util.ArrayList;
009: import java.util.Collections;
010: import java.util.Date;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Node;
020: import org.w3c.dom.NodeList;
021:
022: import fitnesse.components.XmlWriter;
023: import fitnesse.util.XmlUtil;
024:
025: public class WikiPageProperties extends WikiPageProperty implements
026: Serializable {
027: public static final String VIRTUAL_WIKI_ATTRIBUTE = "VirtualWiki";
028: private Map symbolicLinks;
029:
030: public WikiPageProperties() throws Exception {
031: symbolicLinks = new HashMap();
032: }
033:
034: public WikiPageProperties(InputStream inputStream) throws Exception {
035: this ();
036: loadFromXmlStream(inputStream);
037: }
038:
039: public WikiPageProperties(Element rootElement) throws Exception {
040: this ();
041: loadFromRootElement(rootElement);
042: }
043:
044: public WikiPageProperties(WikiPageProperties that) throws Exception {
045: if (that != null && that.children != null)
046: children = new HashMap<String, WikiPageProperty>(
047: that.children);
048: symbolicLinks = new HashMap(that.symbolicLinks);
049: }
050:
051: public void loadFromXmlStream(InputStream inputStream)
052: throws Exception {
053: Document document = XmlUtil.newDocument(inputStream);
054: Element root = document.getDocumentElement();
055: loadFromRootElement(root);
056: }
057:
058: public void loadFromRootElement(Element root) throws Exception {
059: NodeList nodes = root.getChildNodes();
060: for (int i = 0; i < nodes.getLength(); i++) {
061: Node node = nodes.item(i);
062: if (node.getNodeType() != Node.ELEMENT_NODE)
063: continue;
064: String key = node.getNodeName();
065: LoadElement(this , (Element) node, key);
066: }
067: }
068:
069: private void LoadElement(WikiPageProperty context, Element element,
070: String key) {
071: WikiPageProperty newProperty = new WikiPageProperty();
072: context.set(key, newProperty);
073:
074: NodeList nodes = element.getChildNodes();
075: if (element.hasAttribute("value"))
076: newProperty.setValue(element.getAttribute("value"));
077: else if (nodes.getLength() == 1)
078: newProperty.setValue(nodes.item(0).getNodeValue());
079:
080: for (int i = 0; i < nodes.getLength(); i++) {
081: Node childNode = nodes.item(i);
082: if (childNode instanceof Element)
083: LoadElement(newProperty, (Element) childNode, childNode
084: .getNodeName());
085: }
086: }
087:
088: public void save(OutputStream outputStream) throws Exception {
089: Document document = XmlUtil.newDocument();
090: document.appendChild(makeRootElement(document));
091:
092: XmlWriter writer = new XmlWriter(outputStream);
093: writer.write(document);
094: writer.flush();
095: writer.close();
096: }
097:
098: public Element makeRootElement(Document document) {
099: Element root = document.createElement("properties");
100: List keys = new ArrayList(keySet());
101: Collections.sort(keys);
102:
103: for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
104: String key = (String) iterator.next();
105: WikiPageProperty childProperty = getProperty(key);
106: toXml(childProperty, key, document, root);
107: }
108:
109: return root;
110: }
111:
112: private void toXml(WikiPageProperty context, String key,
113: Document document, Element parent) {
114: Element element = document.createElement(key);
115:
116: String value = context.getValue();
117: if (context.hasChildren()) {
118: if (value != null)
119: element.setAttribute("value", value);
120:
121: Set childKeys = context.keySet();
122: for (Iterator iterator = childKeys.iterator(); iterator
123: .hasNext();) {
124: String childKey = (String) iterator.next();
125: WikiPageProperty child = context.getProperty(childKey);
126: toXml(child, childKey, document, element);
127: }
128: } else if (value != null)
129: element.appendChild(document.createTextNode(value));
130:
131: parent.appendChild(element);
132: }
133:
134: public String toString() {
135: StringBuffer s = new StringBuffer();
136: s.append(super .toString("WikiPageProperties", 0));
137: return s.toString();
138: }
139:
140: public Date getLastModificationTime() throws Exception {
141: String dateStr = get("LastModified");
142: if (dateStr == null)
143: return new Date();
144: else
145: return getTimeFormat().parse(dateStr);
146: }
147:
148: public void setLastModificationTime(Date date) {
149: set("LastModified", getTimeFormat().format(date));
150: }
151: }
|