001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors.preview;
020:
021: import java.io.File;
022: import java.io.FileReader;
023: import java.io.IOException;
024: import java.io.StringReader;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Vector;
029:
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.parsers.FactoryConfigurationError;
032: import javax.xml.parsers.ParserConfigurationException;
033:
034: import org.jaxen.dom.DOMXPath;
035: import org.openharmonise.commons.xml.*;
036: import org.w3c.dom.DOMException;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.NodeList;
040: import org.w3c.dom.Text;
041: import org.xml.sax.SAXException;
042:
043: /**
044: * Manages Page State XML documents, persisting them to the local
045: * file system.
046: *
047: * @author matt treanor
048: * @version $Revision: 1.1 $
049: *
050: */
051: public class StateContainer {
052: private HashMap m_states = null;
053: private HashMap m_members = null;
054: private Document m_stateDoc = null;
055: private XMLPrettyPrint m_printer = null;
056: private String m_vFilePath = null;
057:
058: /**
059: *
060: */
061: public StateContainer() {
062: super ();
063: setup();
064: }
065:
066: public StateContainer(String vFilePath) {
067: super ();
068: m_vFilePath = vFilePath;
069: setup();
070: }
071:
072: public void setVFilePath(String vFilePath) {
073: m_vFilePath = vFilePath;
074: }
075:
076: private Document getBlankState() {
077: Document stateXml = null;
078: try {
079: stateXml = DocumentBuilderFactory.newInstance()
080: .newDocumentBuilder().newDocument();
081: Element stateEl = stateXml.createElement("State");
082: stateXml.appendChild(stateEl);
083: } catch (DOMException e) {
084: e.printStackTrace();
085: } catch (ParserConfigurationException e) {
086: e.printStackTrace();
087: } catch (FactoryConfigurationError e) {
088: e.printStackTrace();
089: }
090: return stateXml;
091: }
092:
093: /**
094: * 'borrowed' from matt's workflowlayout store
095: *
096: */
097: private void setup() {
098: m_printer = new XMLPrettyPrint();
099: m_states = new HashMap();
100: m_members = new HashMap();
101: File fXML = new File("C:\\ContentManager\\states.xml");
102: if (fXML.exists()) {
103: try {
104: Document xml = DocumentBuilderFactory.newInstance()
105: .newDocumentBuilder().parse(
106: new org.xml.sax.InputSource(
107: new FileReader(fXML)));
108:
109: DOMXPath xpScenarios = new DOMXPath("child::Scenario");
110: DOMXPath xpStates = new DOMXPath("child::State");
111: DOMXPath xpDocs = new DOMXPath("child::Members/Member");
112: List scenarios = xpScenarios.selectNodes(xml
113: .getDocumentElement());
114: Element stateEl;
115: Element docEl;
116: List documents;
117: Iterator itor = scenarios.iterator();
118: Vector vMembers;
119: while (itor.hasNext()) {
120: Element scenarioEl = (Element) itor.next();
121: String sName = scenarioEl.getAttribute("name");
122: stateEl = (Element) xpStates
123: .selectSingleNode(scenarioEl);
124: if (stateEl != null) {
125: Document stateXml = DocumentBuilderFactory
126: .newInstance().newDocumentBuilder()
127: .newDocument();
128: Element newStateEl = stateXml
129: .createElement("State");
130: XMLUtils.copyChildren(newStateEl, stateEl,
131: stateXml);
132: stateXml.appendChild(newStateEl);
133: m_states.put(sName, stateXml);
134: documents = xpDocs.selectNodes(scenarioEl);
135: Iterator docItor = documents.iterator();
136: vMembers = new Vector();
137: while (docItor.hasNext()) {
138: docEl = (Element) docItor.next();
139: NodeList nodes = docEl.getChildNodes();
140: if (nodes.getLength() > 0) {
141: String path = nodes.item(0)
142: .getNodeValue();
143: vMembers.add(path);
144: }
145: }
146: if (vMembers.size() > 0) {
147: m_members.put(sName, vMembers);
148: }
149: }
150: }
151:
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: } else {
156: m_states.put("None", getBlankState());
157: }
158: }
159:
160: public Document getState(String scenario) {
161: return (Document) m_states.get(scenario);
162: }
163:
164: public Document getMemberState(String name) {
165: Iterator itor = m_members.keySet().iterator();
166: Vector vMembers = null;
167: Document stateDoc = null;
168: String key = null;
169: while (itor.hasNext()) {
170: key = (String) itor.next();
171: vMembers = (Vector) m_members.get(key);
172: if (vMembers.contains(name)) {
173: stateDoc = (Document) m_states.get(key);
174: break;
175: }
176: }
177: return stateDoc;
178: }
179:
180: public String getMemberStateName(String name) {
181: Iterator itor = m_members.keySet().iterator();
182: Vector vMembers = null;
183: String stateName = null;
184: String key = null;
185: while (itor.hasNext()) {
186: key = (String) itor.next();
187: vMembers = (Vector) m_members.get(key);
188: if (vMembers.contains(name)) {
189: stateName = key;
190: break;
191: }
192: }
193: return stateName;
194: }
195:
196: public HashMap getStates() {
197: return m_states;
198: }
199:
200: public Document getState() {
201: return m_stateDoc;
202: }
203:
204: public void addState(String sXml) throws SAXException {
205: Document stateXml = parseXml(sXml);
206: setState(stateXml);
207: }
208:
209: public void setState(Document data) {
210: m_stateDoc = data;
211: }
212:
213: public void save() {
214: try {
215: Document xml = DocumentBuilderFactory.newInstance()
216: .newDocumentBuilder().newDocument();
217:
218: Element elRoot = xml.createElement("Scenarios");
219:
220: Iterator itor = this .m_states.keySet().iterator();
221: while (itor.hasNext()) {
222: String sScenario = (String) itor.next();
223:
224: Element elScenario = xml.createElement("Scenario");
225: elScenario.setAttribute("name", sScenario);
226: Element elState = xml.createElement("State");
227: elScenario.appendChild(elState);
228: elRoot.appendChild(elScenario);
229:
230: Document stateXml = (Document) this .m_states
231: .get(sScenario);
232: XMLUtils.copyChildren(elState, stateXml
233: .getDocumentElement(), xml);
234: elScenario.appendChild(elState);
235:
236: if (m_members != null && m_members.size() > 0) {
237: if (m_members.containsKey(sScenario)) {
238: Element elMemberRoot = xml
239: .createElement("Members");
240: Element elMember = null;
241:
242: String sMemberPath = null;
243: String sMemberScenario = null;
244: Iterator itorDocs = m_members.keySet()
245: .iterator();
246: Vector vMembers;
247: while (itorDocs.hasNext()) {
248: sMemberScenario = (String) itorDocs.next();
249: if (sMemberScenario.equals(sScenario)) {
250: vMembers = (Vector) m_members
251: .get(sMemberScenario);
252: for (int i = 0; i < vMembers.size(); i++) {
253: sMemberPath = (String) vMembers
254: .get(i);
255: elMember = xml
256: .createElement("Member");
257: Text txt = xml
258: .createTextNode(sMemberPath);
259: elMember.appendChild(txt);
260: elMemberRoot.appendChild(elMember);
261: }
262: }
263: }
264: elScenario.appendChild(elMemberRoot);
265: }
266: }
267: }
268:
269: File fXML = new File("C:\\ContentManager\\states.xml");
270: m_printer.printNodeToFile(elRoot, fXML);
271:
272: } catch (ParserConfigurationException e) {
273: e.printStackTrace();
274: } catch (FactoryConfigurationError e) {
275: e.printStackTrace();
276: }
277: }
278:
279: public void removeMemberFromScenarios() {
280: Iterator iter = m_members.values().iterator();
281: while (iter.hasNext()) {
282: Vector vMembers = (Vector) iter.next();
283: if (vMembers.contains(m_vFilePath)) {
284: vMembers.remove(m_vFilePath);
285: }
286: }
287: }
288:
289: public void addMemberToScenario(String scenario) {
290: Vector vMembers = (Vector) m_members.get(scenario);
291: if (vMembers != null) {
292: if (vMembers.contains(m_vFilePath) == false) {
293: vMembers.add(m_vFilePath);
294: }
295: } else {
296: vMembers = new Vector();
297: vMembers.add(m_vFilePath);
298: }
299: m_members.put(scenario, vMembers);
300: }
301:
302: public boolean addToStates(String sName, String sState)
303: throws SAXException {
304: boolean success = false;
305: if (sName != null && sName.length() > 0 && sState != null) {
306: Document stateXml = parseXml(sState);
307: if (stateXml != null) {
308: m_states.put(sName, stateXml);
309: success = true;
310: }
311: }
312: return success;
313: }
314:
315: public boolean removeFromStates(String sName) {
316: boolean success = false;
317: if (sName != null && sName.length() > 0
318: && sName.equalsIgnoreCase("None") == false) {
319: m_states.remove(sName);
320: success = true;
321: }
322: return success;
323: }
324:
325: private Document parseXml(String sXml) throws SAXException {
326: Document xml = null;
327: try {
328: xml = DocumentBuilderFactory
329: .newInstance()
330: .newDocumentBuilder()
331: .parse(
332: new org.xml.sax.InputSource(
333: new StringReader(new String(sXml))));
334: } catch (IOException e) {
335: e.getStackTrace();
336: } catch (ParserConfigurationException e) {
337: e.getStackTrace();
338: } catch (FactoryConfigurationError e) {
339: e.getStackTrace();
340: }
341: return xml;
342: }
343:
344: public String getScenario(String sVFilePath) {
345: String sScenario = null;
346: String sMemberScenario;
347: String sMemberPath;
348: Vector vMembers;
349: Iterator itorDocs = m_members.keySet().iterator();
350: while (itorDocs.hasNext()) {
351: sMemberScenario = (String) itorDocs.next();
352: vMembers = (Vector) m_members.get(sMemberScenario);
353: for (int i = 0; i < vMembers.size(); i++) {
354: sMemberPath = (String) vMembers.get(i);
355: if (sVFilePath.equals(sMemberPath)) {
356: sScenario = sMemberScenario;
357: break;
358: }
359: }
360: if (sScenario != null) {
361: break;
362: }
363: }
364: return sScenario;
365: }
366: }
|