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 java.io.*;
007: import org.w3c.dom.*;
008: import fitnesse.util.XmlUtil;
009: import fitnesse.components.XmlWriter;
010:
011: public class WikiPageProperties implements Serializable {
012: private static final long serialVersionUID = 1L;
013:
014: private Map properties;
015:
016: public static final String VIRTUAL_WIKI_ATTRIBUTE = "VirtualWiki";
017:
018: private Map symbolicLinks;
019:
020: public WikiPageProperties() throws Exception {
021: properties = new HashMap();
022: symbolicLinks = new HashMap();
023: }
024:
025: public WikiPageProperties(Map map) throws Exception {
026: this ();
027: for (Iterator iterator = map.keySet().iterator(); iterator
028: .hasNext();) {
029: String key = (String) iterator.next();
030: String value = (String) map.get(key);
031: if (!"false".equals(value))
032: this .properties.put(key, value);
033: }
034: }
035:
036: public WikiPageProperties(InputStream inputStream) throws Exception {
037: this ();
038: loadFromXmlStream(inputStream);
039: }
040:
041: public WikiPageProperties(Element rootElement) throws Exception {
042: this ();
043: loadFromRootElement(rootElement);
044: }
045:
046: public WikiPageProperties(WikiPageProperties that) throws Exception {
047: properties = new HashMap(that.properties);
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: if (key.equals("symbolicLink"))
066: loadSymbolicLink(node);
067: else {
068: String value = node.hasChildNodes() ? node
069: .getFirstChild().getNodeValue() : "true";
070: // Patch in "STIQTest" for backwards compatibility with
071: // "SeleniumTest"
072: if (key == "SeleniumTest") {
073: key = WikiPage.STIQ_TEST;
074: }
075: properties.put(key, value);
076: }
077: }
078: }
079:
080: public void save(OutputStream outputStream) throws Exception {
081: Document document = XmlUtil.newDocument();
082: document.appendChild(makeRootElement(document));
083:
084: XmlWriter writer = new XmlWriter(outputStream);
085: writer.write(document);
086: writer.flush();
087: writer.close();
088: }
089:
090: public Element makeRootElement(Document document) {
091: Element root = document.createElement("properties");
092: List keys = new ArrayList(properties.keySet());
093: Collections.sort(keys);
094:
095: for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
096: String key = (String) iterator.next();
097: String value = (String) properties.get(key);
098: Element element = document.createElement(key);
099: if (!"true".equals(value))
100: element.appendChild(document.createTextNode(value));
101: root.appendChild(element);
102: }
103:
104: addSymbolicLinkElements(document, root);
105: return root;
106: }
107:
108: public boolean has(String key) {
109: return properties.containsKey(key);
110: }
111:
112: public boolean is(String key) {
113: return (properties.containsKey(key) && (properties.get(key) == "true"));
114: }
115:
116: public String get(String key) throws Exception {
117: return (String) properties.get(key);
118: }
119:
120: public void set(String key, String value) {
121: properties.put(key, value);
122: }
123:
124: public void set(String key) {
125: set(key, "true");
126: }
127:
128: public void remove(String key) {
129: properties.remove(key);
130: }
131:
132: public Set keySet() {
133: return properties.keySet();
134: }
135:
136: public String toString() {
137: StringBuffer s = new StringBuffer("WikiPageProperties:\n");
138: for (Iterator iterator = properties.keySet().iterator(); iterator
139: .hasNext();) {
140: String key = (String) iterator.next();
141: String value = (String) properties.get(key);
142: s.append("\t").append(key).append(" = ").append(value)
143: .append("\n");
144: }
145: symbolicLinksToString(s);
146: return s.toString();
147: }
148:
149: public void addSymbolicLink(String linkName, WikiPagePath pagePath) {
150: symbolicLinks.put(linkName, pagePath);
151: }
152:
153: public boolean hasSymbolicLink(String linkName) {
154: return symbolicLinks.containsKey(linkName);
155: }
156:
157: public WikiPagePath getSymbolicLink(String linkName) {
158: return (WikiPagePath) symbolicLinks.get(linkName);
159: }
160:
161: public Set getSymbolicLinkNames() {
162: return symbolicLinks.keySet();
163: }
164:
165: public void removeSymbolicLink(String linkName) {
166: symbolicLinks.remove(linkName);
167: }
168:
169: private void addSymbolicLinkElements(Document document, Element root) {
170: for (Iterator iterator1 = symbolicLinks.keySet().iterator(); iterator1
171: .hasNext();) {
172: String linkName = (String) iterator1.next();
173: WikiPagePath path = (WikiPagePath) symbolicLinks
174: .get(linkName);
175: Element linkElement = document
176: .createElement("symbolicLink");
177: XmlUtil
178: .addTextNode(document, linkElement, "name",
179: linkName);
180: XmlUtil.addTextNode(document, linkElement, "path",
181: PathParser.render(path));
182: root.appendChild(linkElement);
183: }
184: }
185:
186: private void loadSymbolicLink(Node node) throws Exception {
187: Element linkElement = (Element) node;
188: String name = XmlUtil.getLocalTextValue(linkElement, "name");
189: WikiPagePath path = PathParser.parse(XmlUtil.getLocalTextValue(
190: linkElement, "path"));
191: addSymbolicLink(name, path);
192: }
193:
194: private void symbolicLinksToString(StringBuffer s) {
195: s.append("\tSymbolic Links:\n");
196: for (Iterator iterator = symbolicLinks.keySet().iterator(); iterator
197: .hasNext();) {
198: String linkName = (String) iterator.next();
199: WikiPagePath path = getSymbolicLink(linkName);
200: s.append("\t\t").append(linkName).append(" -> ").append(
201: path).append("\n");
202: }
203: }
204: }
|