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;
020:
021: import java.io.IOException;
022: import java.io.StringReader;
023:
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.FactoryConfigurationError;
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.openharmonise.commons.xml.*;
029: import org.openharmonise.commons.xml.namespace.*;
030: import org.openharmonise.him.editors.report.*;
031: import org.openharmonise.vfs.*;
032: import org.openharmonise.vfs.status.*;
033: import org.w3c.dom.Document;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * Handles editing of system reports.
038: *
039: * @author Matthew Large
040: * @version $Revision: 1.1 $
041: *
042: */
043: public class SystemReportEditor implements Editor {
044:
045: private boolean m_bResourceCreated = false;
046:
047: /**
048: *
049: */
050: public SystemReportEditor() {
051: super ();
052: }
053:
054: /* (non-Javadoc)
055: * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
056: */
057: public PathStatusWrapper open(String sPath,
058: AbstractVirtualFileSystem vfs) {
059: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
060: if (vfFile != null) {
061: Document xml = null;
062: try {
063: xml = DocumentBuilderFactory.newInstance()
064: .newDocumentBuilder().parse(
065: new org.xml.sax.InputSource(
066: new StringReader(new String(
067: vfFile.getContent()))));
068: } catch (SAXException e) {
069: e.printStackTrace();
070: } catch (IOException e) {
071: e.printStackTrace();
072: } catch (ParserConfigurationException e) {
073: e.printStackTrace();
074: } catch (FactoryConfigurationError e) {
075: e.printStackTrace();
076: }
077:
078: ReportEditor editor = null;
079: if (xml != null) {
080: editor = new ReportEditor(sPath, xml);
081: } else {
082: editor = new ReportEditor(sPath);
083: }
084: editor.show();
085:
086: xml = editor.getQuery();
087: if (xml != null) {
088: XMLPrettyPrint printer = new XMLPrettyPrint();
089: String sXML = null;
090: try {
091: sXML = printer.printNode(xml.getDocumentElement());
092: vfFile.setContent(sXML.getBytes());
093: } catch (NamespaceClashException e1) {
094: e1.printStackTrace();
095: }
096: }
097: }
098:
099: return new PathStatusWrapper(null, new VFSStatus());
100: }
101:
102: /* (non-Javadoc)
103: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
104: */
105: public PathStatusWrapper createNew(String sPath,
106: AbstractVirtualFileSystem vfs) {
107: PathStatusWrapper statusWrapper = new PathStatusWrapper(null,
108: new VFSStatus());
109:
110: Document xml = null;
111: try {
112: xml = DocumentBuilderFactory.newInstance()
113: .newDocumentBuilder().newDocument();
114: } catch (ParserConfigurationException e1) {
115: e1.printStackTrace();
116: } catch (FactoryConfigurationError e1) {
117: e1.printStackTrace();
118: }
119:
120: if (xml != null) {
121: xml.appendChild(xml.createElement("ReportQuery"));
122:
123: XMLPrettyPrint printer = new XMLPrettyPrint();
124: try {
125: String sXML = printer.printNode(xml
126: .getDocumentElement());
127: statusWrapper = this .createNew(sPath, sXML.getBytes(),
128: vfs);
129: } catch (NamespaceClashException e) {
130: e.printStackTrace();
131: }
132: }
133:
134: return statusWrapper;
135: }
136:
137: /* (non-Javadoc)
138: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, byte[], com.simulacramedia.vfs.AbstractVirtualFileSystem)
139: */
140: public PathStatusWrapper createNew(String sPath, byte[] content,
141: AbstractVirtualFileSystem vfs) {
142: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
143: null, new VFSStatus());
144:
145: VirtualFile vfFile = new VirtualFile(sPath);
146:
147: vfs.getVirtualFileSystemView().setContentType(vfFile,
148: "text/xml");
149:
150: vfFile.setContent(content);
151:
152: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
153:
154: vfFile = vfs.getVirtualFile(sPath).getResource();
155:
156: if (statusWrapper.getStatus().isOK()) {
157: this .m_bResourceCreated = true;
158: }
159:
160: return new PathStatusWrapper(null, statusWrapper.getStatus());
161: }
162:
163: /* (non-Javadoc)
164: * @see com.simulacramedia.contentmanager.editors.Editor#discardChanges(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
165: */
166: public StatusData discardChanges(String sPath,
167: AbstractVirtualFileSystem vfs) {
168: return new VFSStatus();
169: }
170:
171: /* (non-Javadoc)
172: * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
173: */
174: public StatusData export(String sPath, AbstractVirtualFileSystem vfs) {
175: return new VFSStatus();
176: }
177:
178: /* (non-Javadoc)
179: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
180: */
181: public boolean hasResourceBeenCreated() {
182: return this .m_bResourceCreated;
183: }
184:
185: /* (non-Javadoc)
186: * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
187: */
188: public PathStatusWrapper preview(String sPath,
189: AbstractVirtualFileSystem vfs) {
190: return new PathStatusWrapper(null, new VFSStatus());
191: }
192:
193: /* (non-Javadoc)
194: * @see com.simulacramedia.contentmanager.editors.Editor#upload(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
195: */
196: public PathStatusWrapper upload(String path,
197: AbstractVirtualFileSystem vfs) {
198: return new PathStatusWrapper(null, new VFSStatus());
199: }
200:
201: }
|