001: /*
002: * Created on Jan 20, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package com.openedit.modules.html;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import com.openedit.OpenEditException;
013: import com.openedit.WebPageRequest;
014: import com.openedit.modules.edit.BaseEditorModule;
015: import com.openedit.page.Page;
016: import com.openedit.util.PathUtilities;
017: import com.openedit.util.URLUtilities;
018:
019: /**
020: * @author dbrown
021: * @author Matt Avery, mavery@einnovation.com
022: */
023: public class HtmlEditorModule extends BaseEditorModule {
024: private static final Log log = LogFactory
025: .getLog(HtmlEditorModule.class);
026:
027: /**
028: * Loads the WYSIWYG view
029: * @param inReq
030: * @throws Exception
031: */
032: public void loadView(WebPageRequest inReq) throws Exception {
033: EditorSession session = startEditSession(inReq);
034: inReq.putPageValue("viewcontent", session
035: .getWysiwygSourceVariable());
036: //inReq.putPageValue("rawviewcontent", session.getWysiwygSource() );
037: inReq.putPageValue("csspath", session.getCssPath());
038: //inReq.putPageValue( "documentModified", new Boolean( session.isDocumentModified() ) );
039: }
040:
041: /**
042: * Load the source code into the source code editor
043: * @param inReq
044: * @throws Exception
045: */
046: public void loadSource(WebPageRequest inReq) throws Exception {
047: EditorSession session = startEditSession(inReq);
048: /* String type = inReq.getRequestParameter("type");
049: if ( "text".equals( type ))
050: {
051: inReq.putPageValue( "sourcecontent", session.getOriginalSource() );
052: }
053: else
054: {
055: */inReq.putPageValue("sourcecontent", session
056: .getEscapedSource());
057: // }
058: inReq.putPageValue("documentModified", new Boolean(session
059: .isDocumentModified()));
060: }
061:
062: /**
063: * @param inEditorSession
064: * @param inEditPath
065: */
066: protected EditorSession startEditSession(WebPageRequest inReq)
067: throws OpenEditException {
068: EditorSession inEditorSession = new EditorSession();
069: String editPath = inReq.getRequestParameter("editPath");
070:
071: String content = null;
072: Page editPage = getPageManager().getPage(editPath);
073:
074: boolean multipleLang = true;
075: String savein = inReq.getPageProperty("usemultiplelanguages");
076: if (savein != null) {
077: multipleLang = Boolean.parseBoolean(savein);
078: } else {
079: multipleLang = false;
080: }
081:
082: String selectedcode = inReq.getLanguage();
083: String rootdir = "/translations/" + selectedcode;
084: if (multipleLang) {
085: if (selectedcode == null || selectedcode.equals("default")
086: || editPath.startsWith(rootdir)) {
087: multipleLang = false;
088: }
089: }
090: if (multipleLang) {
091: editPath = rootdir + editPath;
092: }
093: boolean useDraft = createDraft(editPage, inReq.getUser());
094: if (useDraft) {
095: editPath = PathUtilities.createDraftPath(editPath);
096: Page draft = getPageManager().getPage(editPath);
097: if (draft.exists()) {
098: //then use this content.
099: content = draft.getContent();
100: } else {
101: if (editPage.exists()) {
102: content = editPage.getContent();
103: }
104: }
105: } else if (editPage.exists()) {
106: content = editPage.getContent();
107: }
108: if (content == null) {
109: content = "";
110: }
111: editPage = getPageManager().getPage(editPath);
112: inEditorSession.setEditPage(editPage);
113: inEditorSession.setOriginalSource(content);
114: inEditorSession.setWorkingSource(content);
115: String origUrl = inReq.getRequestParameter("origURL");
116: inEditorSession.setOriginalUrl(origUrl);
117:
118: URLUtilities urlUtilities = (URLUtilities) inReq
119: .getPageValue("url_util");
120: if (urlUtilities != null) {
121: inEditorSession.setBasePath(urlUtilities
122: .buildStandard(editPath));
123: }
124: //Is this being used anymore?
125: String parentName = inReq.getRequestParameter("parentName");
126: inEditorSession.setParentName(parentName);
127:
128: String location = editPage.get("editstylesheet");
129: if (location == null) {
130: String il = editPage.getInnerLayout();
131: if (il != null) {
132: location = PathUtilities.extractDirectoryPath(il)
133: + "/style.css";
134: }
135: }
136: if (location == null) {
137: location = "/_styles.css";
138: }
139: inEditorSession.setCssPath(location);
140:
141: Page styles = getPageManager().getPage(location);
142: if (!styles.exists()) {
143: log.error("No CSS file used in editor: " + location);
144: }
145: inReq.putPageValue("editorSession", inEditorSession);
146: inReq.putPageValue("editPath", inEditorSession.getEditPath());
147:
148: return inEditorSession;
149: }
150:
151: public void save(WebPageRequest inReq) throws Exception {
152: //Strip the body junk
153: EditorSession session = startEditSession(inReq);
154: String content = inReq.getRequestParameter("content");
155: if (content == null) {
156: content = "";
157: }
158: String type = inReq.getRequestParameter("contenttype");
159: if (!"text".equals(type)) {
160: content = session.removeBaseHrefAndFixQuotes(content);
161: }
162: inReq.setRequestParameter("content", content);
163: session.setWorkingSource(content);
164: String path = inReq.getRequestParameter("savepath");
165: if (path == null) {
166: inReq
167: .setRequestParameter("savepath", session
168: .getEditPath());
169: }
170: inReq.setRequestParameter("editPath", session.getEditPath());
171: writeContent(inReq);
172: }
173:
174: protected String getContent(String inPath) throws Exception {
175: Page page = getPageManager().getPage(inPath);
176: if (page.exists()) {
177: return page.getContent();
178: }
179: return "";
180: }
181: }
|