001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.ui;
006:
007: import java.io.OutputStream;
008: import java.io.StringWriter;
009:
010: import javax.xml.parsers.DocumentBuilder;
011: import javax.xml.parsers.DocumentBuilderFactory;
012: import javax.xml.parsers.ParserConfigurationException;
013: import javax.xml.transform.Transformer;
014: import javax.xml.transform.TransformerConfigurationException;
015: import javax.xml.transform.TransformerException;
016: import javax.xml.transform.TransformerFactory;
017: import javax.xml.transform.dom.DOMSource;
018: import javax.xml.transform.stream.StreamResult;
019:
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022:
023: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.CreateFolderResult;
024: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.FileUploadResult;
025: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.Folder;
026: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.FoldersAndFiles;
027: import com.opensymphony.xwork.ActionInvocation;
028: import com.opensymphony.xwork.Result;
029:
030: /**
031: * <!-- START SNIPPET: javadoc -->
032: *
033: * Abstract result for all Rich Text Editor results. It contains common methods
034: * that might come in handy to its subclass.
035: *
036: * Configuration of result necessary in xwork.xml (is already there by default) are
037: * as follows:
038: *
039: * <pre>
040: * <!-- Results necessary when using 'browse server' and 'upload' feature of Richtexteditor -->
041: * <result-type name="richtexteditorGetFolders"
042: * class="com.opensymphony.webwork.views.jsp.ui.RichtexteditorGetFoldersResult" />
043: * <result-type name="richtexteditorGetFoldersAndFiles"
044: * class="com.opensymphony.webwork.views.jsp.ui.RichtexteditorGetFoldersAndFilesResult" />
045: * <result-type name="richtexteditorCreateFolder"
046: * class="com.opensymphony.webwork.views.jsp.ui.RichtexteditorCreateFolderResult" />
047: * <result-type name="richtexteditorFileUpload"
048: * class="com.opensymphony.webwork.views.jsp.ui.RichtexteditorFileUploadResult" />
049: * </pre>
050: *
051: * <!-- END SNIPPET: javadoc -->
052: *
053: * @author tm_jee
054: * @version $Date: 2006-03-08 21:22:35 +0100 (Wed, 08 Mar 2006) $ $Id: AbstractRichtexteditorResult.java 2338 2006-03-08 20:22:35Z rainerh $
055: */
056: public abstract class AbstractRichtexteditorResult implements Result {
057:
058: /**
059: * Build an xml <code>Document</code>
060: *
061: * @return An XML Document
062: * @throws ParserConfigurationException
063: */
064: protected Document buildDocument()
065: throws ParserConfigurationException {
066: DocumentBuilderFactory factory = DocumentBuilderFactory
067: .newInstance();
068: DocumentBuilder builder = factory.newDocumentBuilder();
069: Document document = builder.newDocument();
070:
071: return document;
072: }
073:
074: /**
075: * Build a common xml structure for all xml based result. For example:
076: *
077: * <pre>
078: * <?xml version="1.0" encoding="utf-8" ?>
079: * <Connector command="RequestedCommandName" resourceType=" RequestedResourceType">
080: * <CurrentFolder path="CurrentFolderPath" url="CurrentFolderUrl" />
081: * <!-- Here goes all specific command data -->
082: * </Connector>
083: * </pre>
084: *
085: * @param document
086: * @param command
087: * @param type
088: * @param folderPath
089: * @param serverPath
090: * @return A common xml structure for all xml based result.
091: */
092: protected Element buildCommonResponseXml(Document document,
093: String command, String type, String folderPath,
094: String serverPath) {
095: Element connectorElement = document.createElement("Connector");
096: connectorElement.setAttribute("command", command);
097: connectorElement.setAttribute("resourceType", type);
098: document.appendChild(connectorElement);
099:
100: Element myEl = document.createElement("CurrentFolder");
101: myEl.setAttribute("path", folderPath);
102: myEl.setAttribute("url", serverPath);
103: connectorElement.appendChild(myEl);
104:
105: return connectorElement;
106: }
107:
108: /**
109: * Convert a <code>Document<code> to its string representation.
110: *
111: * @param document
112: * @return The documents String representation.
113: * @throws TransformerConfigurationException
114: * @throws TransformerException
115: */
116: protected String stringFromDocument(Document document)
117: throws TransformerConfigurationException,
118: TransformerException {
119: //document.normalizeDocument();
120: StringWriter writer = new StringWriter();
121: String result = null;
122: try {
123: writer = new StringWriter();
124: TransformerFactory factory = TransformerFactory
125: .newInstance();
126: Transformer transformer = factory.newTransformer();
127: transformer.transform(new DOMSource(document),
128: new StreamResult(writer));
129: result = writer.getBuffer().toString();
130:
131: } finally {
132: if (writer != null)
133: try {
134: writer.close();
135: } catch (Exception e) {
136: }
137: writer = null;
138: }
139: return result;
140: }
141:
142: /**
143: * Write a <code>Document</code> to an OutputStream <code>out</code>
144: *
145: * @param document
146: * @param out
147: * @throws TransformerConfigurationException
148: * @throws TransformerException
149: */
150: protected void writeDocumentToStream(Document document,
151: OutputStream out) throws TransformerConfigurationException,
152: TransformerException {
153: //document.normalizeDocument();
154: TransformerFactory factory = TransformerFactory.newInstance();
155: Transformer transformer = factory.newTransformer();
156: transformer.transform(new DOMSource(document),
157: new StreamResult(out));
158: }
159:
160: /**
161: * Get the command send by the Rich Text Editor. It would be one of the followings.
162: * Only valid when rich text editor issue a server-side 'Browse' not 'Upload'.
163: *
164: * <ul>
165: * <li>GetFolders</li>
166: * <li>GetFoldersAndFiles</li>
167: * <li>CreateFolder</li>
168: * <li>FileUpload</li>
169: * </ul>
170: *
171: * @param invocation
172: * @return The command send by the Rich Text Editor.
173: */
174: protected String getCommand(ActionInvocation invocation) {
175: return (String) invocation.getStack().getContext().get(
176: "__richtexteditorCommand");
177: }
178:
179: /**
180: * Get the type send by the Rich Text Editor. It could be one of the followings:
181: *
182: * <ul>
183: * <li>Image</li>
184: * <li>File</li>
185: * <li>Flash</li>
186: * </ul>
187: *
188: * @param invocation
189: * @return The type send by the Rich Text Editor.
190: */
191: protected String getType(ActionInvocation invocation) {
192: return (String) invocation.getStack().getContext().get(
193: "__richtexteditorType");
194: }
195:
196: /**
197: * Get the folder path send by the Rich Text Editor.
198: *
199: * @param invocation
200: * @return The folder path send by the Rich Text Editor.
201: */
202: protected String getFolderPath(ActionInvocation invocation) {
203: return (String) invocation.getStack().getContext().get(
204: "__richtexteditorFolderPath");
205: }
206:
207: /**
208: * Get the server path calculated from AbstractRichtexteditoConnector or its
209: * decendant through AbstractRichtexteditorConnector#calculate#calculateServerPath(String, String String)
210: *
211: * @param invocation
212: * @return The server path calculated from AbstractRichtexteditoConnector
213: * @see com.opensymphony.webwork.components.AbstractRichtexteditorConnector#calculateServerPath(String, String, String)
214: */
215: protected String getServerPath(ActionInvocation invocation) {
216: return (String) invocation.getStack().getContext().get(
217: "__richtexteditorServerPath");
218: }
219:
220: /**
221: * Get the <code>Folder[]</code> computed from AbstractRichtexteditorConnector or its
222: * decendant through AbstractRichtexteditorConnector#getFolders(String, String). Only
223: * valid if it is a 'GetFolder' command.
224: *
225: * @param invocation
226: * @return The <code>Folder[]</code> computed from AbstractRichtexteditorConnector
227: * @see com.opensymphony.webwork.components.AbstractRichtexteditorConnector#getFolders(String, String)
228: */
229: protected Folder[] richtexteditorFolders(ActionInvocation invocation) {
230: return (Folder[]) invocation.getStack().getContext().get(
231: "__richtexteditorGetFolders");
232: }
233:
234: /**
235: * Get the <code>FoldersAndFiles</code> computed from AbstractRichtexteditorConnector or its
236: * decendant through AbstractRichtexteditorConnector#getFoldersAndFiles(String, String). Only
237: * valid if it is a 'GetFoldersAndFiles' command.
238: *
239: * @param invocation
240: * @return The <code>FoldersAndFiles</code> computed from AbstractRichtexteditorConnector
241: * @see com.opensymphony.webwork.components.AbstractRichtexteditorConnector#getFoldersAndFiles(String, String)
242: */
243: protected FoldersAndFiles richtexteditorFoldersAndFiles(
244: ActionInvocation invocation) {
245: return (FoldersAndFiles) invocation.getStack().getContext()
246: .get("__richtexteditorGetFoldersAndFiles");
247: }
248:
249: /**
250: * Get the <code>CreateFolderResult</code> computed from AbstractRichtexteditorConnector or its
251: * decendant through AbstractRichtexteditorConnector#createFolder(String, String, String). Only
252: * valid if it is a 'CreateFolder' command.
253: *
254: * @param invocation
255: * @return The <code>CreateFolderResult</code> computed from AbstractRichtexteditorConnector
256: * @see com.opensymphony.webwork.components.AbstractRichtexteditorConnector#createFolder(String, String, String)
257: */
258: protected CreateFolderResult richtexteditorCreateFolderResult(
259: ActionInvocation invocation) {
260: return (CreateFolderResult) invocation.getStack().getContext()
261: .get("__richtexteditorCreateFolder");
262: }
263:
264: /**
265: * Get the <code>FileUploadResult</code> computed from AbstractRichtexteditorConnector or its
266: * decendant through AbstractRichtexteditorConnector#fileUpload(String, String, String, String, File).
267: * Only valid if it is a 'FileUpload' command
268: *
269: * @param invocation
270: * @return The <code>FileUploadResult</code> computed from AbstractRichtexteditorConnector
271: * @see com.opensymphony.webwork.components.AbstractRichtexteditorConnector#fileUpload(String, String, String, String, java.io.File)
272: */
273: protected FileUploadResult richtexteditorFileUploadResult(
274: ActionInvocation invocation) {
275: return (FileUploadResult) invocation.getStack().getContext()
276: .get("__richtexteditorFileUpload");
277: }
278: }
|