Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Lenya Wiki" for change 
notification.

The following page has been changed by Kristen:
http://wiki.apache.org/lenya/HowToIntegrateFCKEditor

The comment on the change is:
Total re-write to include asset upload and insertion

------------------------------------------------------------------------------
  = Integrating the FCKeditor with Lenya =
  
- This is still very much a work in progress.  This will get the famous 
FCKeditor (which we have nicknamed "Fred" to reduce snickering) to load with 
content and save the content.  However, validation still needs to be 
implemented, as well as asset insertion (maybe next week).  And we've only 
tried this with FCKeditor 2.1 and Lenya 1.2.3/1.2.4.  Full credit for all this 
goes to K. Haenni.
+ This is still very much a work in progress.  This will get the famous 
FCKeditor (which we have nicknamed "Fred" to reduce snickering) to load with 
content, save the content, asset upload and insertion, and link creation.  
However, validation still needs to be implemented when saving.  And we've only 
tried this with FCKeditor 2.1 and Lenya 1.2.3/1.2.4.  Full credit for all this 
goes to K. Haenni.
  
- 1. Make sure the Fred (the FCK Editor) is in the 
`lenya/webapp/lenya/resource/fckeditor` directory.  This can be downloaded from 
http://www.fckeditor.net/.  Remove any unnecessary folders and files in the 
fckeditor directory (anything that begins with an “_”).
+ 1. Make sure that Fred (the FCK Editor) is in the 
`lenya/webapp/lenya/resource/fckeditor` directory.  This can be downloaded from 
http://www.fckeditor.net/.  Remove any unnecessary folders and files in the 
fckeditor directory (anything that begins with an “_”).
  
  2. Create a new javascript file in `lenya/webapp/lenya/resources/fckeditor` 
called `invokeEditor.js` with the following contents:
  
  {{{
  function invoke()
  {
+ //Need to store pub id and doc id in cookie so fred can use 
+ //in connector url with inserting assets. If don't then Lenya has 
+ //no way of knowing what pub and document the user is in.
+ 
+ var url = document.URL;
+ var urlArray = url.split("/");
+ var finalValue = '';
+ var isDocId = 'false';
+ for(var i=0; i < urlArray.length; i++){
+       if(isDocId == 'true'){
+               finalValue = finalValue + urlArray[i];
+               if(i < urlArray.length-1){
+                       finalValue = finalValue + "/";
+               }
+       }
+       
+       if(urlArray[i] == 'authoring'){
+               finalValue = value + "/";
+               isDocId = 'true'
+       }
+       var value = urlArray[i];
+ }
+ var period = finalValue.indexOf(".");
+ var finalValue = finalValue.substring(0,period);
+ document.cookie = "url=" + finalValue + "; path=/";
-     var oFCKeditor = new FCKeditor( 'content' ) ;
+ var oFCKeditor = new FCKeditor( 'content' ) ;
-     oFCKeditor.ReplaceTextarea() ;
+ oFCKeditor.ReplaceTextarea() ;
  }
  }}}
  
+ 3. Add the following to `lenya/webapp/lenya/resources/fckeditor/fckconfig.js` 
after the line `FCKConfig.ImageDlgHideAdvanced` (around line 105):
+ 
+ {{{
+ // Need to get the publication id and doc id from the cookie.
+ var dc = document.cookie.split(";");
+ var path = "";
+ for(var j = 0; j < dc.length; j++){
+       var value = dc[j];
+       if(value.indexOf("url") > -1){
+               path = value.substring(5,value.length);
+       }
+ }
+ }}}
+ 
+ Then, modify the `FCKConfig.LinkBrowserURL` and `FCKConfig.ImageBrowserURL`:
+ {{{
+ FCKConfig.LinkBrowserURL = FCKConfig.BasePath + 
'filemanager/browser/default/browser.html?Type=File&Connector=' + path + 
'/connectors/lenya/connector.lenya';
+ FCKConfig.ImageBrowserURL = FCKConfig.BasePath + 
'filemanager/browser/default/browser.html?Type=Image&Connector=' + path + 
'/connectors/lenya/connector.lenya';
+ }}}
+ 
- 3. In `lenya/webapp/lenya/resources/misc/` copy the entire `bxeng` folder and 
paste it into the same directory and rename it `fckeditor`.  Modify the new 
folder's `index.xhtml` to contain the following:
+ 4. In `lenya/webapp/lenya/resources/misc/` copy the entire `bxeng` folder and 
paste it into the same directory and rename it `fckeditor`.  Modify the new 
folder's `index.xhtml` to contain the following:
  
  {{{
  <?xml version="1.0"?>
@@ -37, +82 @@

  </html>
  }}}
  
- 4. Copy `lenya/webapp/lenya/xslt/bxeng` to 
`lenya/webapp/lenya/xslt/fckeditor`.  Modify `aggregate.xsl` to contain the 
following:
+ 5. Also in `lenya/webapp/lenya/resources/misc/fckeditor`, create a new file 
called `uploadComplete.xhtml` that contains just beginning and ending `<html>` 
tags.  The remaining html will be added during the transformation.  This is the 
response that's sent to Fred after an asset has been uploaded which contains 
the name of the file being uploaded and an error code.
+ 
+ 6. Add the following actions to `lenya/webapp/lenya/sitemap.xmap`:
  
  {{{
+ <map:action name="frededitorsave" logger="sitemap.action.frededitorsave" 
src="org.apache.lenya.cms.cocoon.acting.FredSaveAction" />
+ <map:action name="upload-fred-asset" 
logger="sitemap.action.upload-fred-asset" 
src="org.apache.lenya.cms.cocoon.acting.UploadFredAsset" />
- <?xml version="1.0"?>
- <xsl:stylesheet version="1.0"
-   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
-   xmlns:xhtml="http://www.w3.org/1999/xhtml";>
- 
- <xsl:template match="/">
- <xsl:apply-templates select="fckeditor"/>
- </xsl:template>
- 
- <xsl:template match="fckeditor">
- <html xmlns="http://www.w3.org/1999/xhtml";>
- <xsl:apply-templates select="xhtml:html/xhtml:[EMAIL PROTECTED] = 
'EditorHead']"/>
- <body onload="invoke();">
- 
- <form id="myform" 
action="/default/authoring/index.html?lenya.usecase=fckeditor&amp;lenya.step=save"
 method="POST">
- <textarea id="content" rows="40" cols="80"><xsl:copy-of 
select="xhtml:html/xhtml:body/node()"/></textarea>
- </form>
- 
- </body>
- </html>
- </xsl:template>
- 
- <xsl:template match="xhtml:head">
- <head xmlns="http://www.w3.org/1999/xhtml";>
- <xsl:for-each select="/fckeditor/namespaces/xmlns">
- <meta name="fckNS" content="{.}"/>
- </xsl:for-each>
- <xsl:copy-of select="@*|node()"/>
- </head>
- </xsl:template>
- 
- </xsl:stylesheet>
  }}}
  
- 5. Add the following to `lenya/webapp/global-sitemap.xmap` after  `<!-- 
Editor BXE -->`
+ 7. Add the following to `lenya/webapp/global-sitemap.xmap` after  `<!-- 
Editor BXE -->`
  
  {{{
+ <!-- FCK Editor Connector for asset upload and insertion as well as link 
creation --> 
+ <map:pipeline>
+    <map:match 
pattern="fckeditor/editor/filemanager/browser/default/*/**/connectors/**.lenya">
+        <map:mount uri-prefix="fckeditor/editor/filemanager/browser/default" 
src="lenya/pubs/{1}/sitemap.xmap" check-reload="true" reload-method="synchron"/>
+    </map:match>
+ </map:pipeline>
+ 
  <!-- FCK Editor -->
  <map:pipeline>
-   <map:match pattern="fckeditor/**">
+    <map:match pattern="fckeditor/**">
-     <map:read src="lenya/resources/fckeditor/{1}"/>
+        <map:read src="lenya/resources/fckeditor/{1}"/>
-   </map:match>
+    </map:match>
- </map:pipeline>
+ </map:pipeline>    
  }}}
  
+ 8. Add the following to 
`lenya/webapp/lenya/pubs/[pubname]/publication-sitemap.xmap` before the 
pipeline that builds the page (around line 116):
- 6. Add the following line to `lenya/pubs/pubname/config/menus/generic.xsp` 
after
- `<item wf:event="edit" uc:usecase="bxeng" uc:step="open" 
href="?"><i18n:text>Edit with BXE</i18n:text></item>` (around line 127):
  
  {{{
- <item wf:event="edit" uc:usecase="fckeditor" uc:step="open" 
href="?"><i18n:text>Edit with FCKeditor</i18n:text></item>
+ <!-- This pipeline is used for uploading and inserting assets into Fred -->
+     <map:pipeline>
+       <map:match pattern="*/**/connectors/**.lenya">
+          <map:select type="request-parameter">
+               <map:parameter name="parameter-name" value="Command"/>
+            <!-- If request parameter Command is GetFoldersAndFiles, returns 
an xml file containing the list of assets for a page -->
+                <map:when test="GetFoldersAndFiles">
+                   <map:select type="request-parameter">
+                      <map:parameter name="parameter-name" value="Type"/>
+                   <!-- Returns only assets that are images if the request 
parameter Type=Image -->
+                         <map:when test="Image">
+                            <map:generate src="resources/authoring/{2}" 
type="directory">
+                               <map:parameter name="depth" value="5"/>
+                               <map:parameter name="dateFormat" 
value="dd-MMM-yyyy hh:mm:ss"/>
+                               <map:parameter name="include" 
value="\.jpg$|\.gif$|\.bmp$|\.png$"/>
+                            </map:generate>
+                          </map:when>
+                   <!-- Returns all assets, but excludes the meta files -->
+                          <map:otherwise>
+                            <map:generate src="resources/authoring/{2}" 
type="directory">
+                               <map:parameter name="depth" value="5"/>
+                               <map:parameter name="dateFormat" 
value="dd-MMM-yyyy hh:mm:ss"/>
+                               <map:parameter name="exclude" value="\.meta$"/>
+                            </map:generate>
+                          </map:otherwise>
+                   </map:select>
+                   <map:transform src="../../xslt/fckeditor/asset.xsl">
+                      <map:parameter name="pubid" value="{1}"/>
+                      <map:parameter name="docid" value="{2}"/>
+                      <map:parameter name="type" value="{request-param:Type}"/>
+                   </map:transform>                              
+                   <map:serialize type="xml" />
+                </map:when>
+                  <!-- If Command request parameter is not GetFoldersAndFiles, 
must be an upload.  So, upload asset to pages directory using 
+                   upload-fred-asset action. -->
+                <map:otherwise>
+                   <map:act type="upload-fred-asset">
+                   <map:generate 
src="../../resources/misc/fckeditor/uploadComplete.xhtml"/>
+                   <map:transform 
src="../../xslt/fckeditor/uploadAssetComplete.xsl">
+                      <map:parameter name="errorCode" value="{errorCode}"/>
+                      <map:parameter name="name" value="{name}"/>
+                   </map:transform>
+                   <map:serialize type="xhtml"/>
+                   </map:act>
+                </map:otherwise>
+         </map:select>
+      </map:match>
+   </map:pipeline>
+ 
  }}}
  
- 7. Copy `lenya/pubs/pubname/usecase-bxeng.xmap` to same directory and rename 
it to `usecase-fckeditor.xmap`
+ 9. Copy `lenya/pubs/pubname/usecase-bxeng.xmap` to same directory and rename 
it to `usecase-fckeditor.xmap`
  
  a. Modify the `<!-- == Components == -->` section of the document to only 
contain `<map:components/>`
  
@@ -180, +251 @@

      </map:match>
  }}}
  
+ 10. Add the following line to `lenya/pubs/pubname/config/menus/generic.xsp` 
after
+ `<item wf:event="edit" uc:usecase="bxeng" uc:step="open" 
href="?"><i18n:text>Edit with BXE</i18n:text></item>` (around line 127):
+ 
+ {{{
+ <item wf:event="edit" uc:usecase="fckeditor" uc:step="open" 
href="?"><i18n:text>Edit with FCKeditor</i18n:text></item>
+ }}}
+ 
+ 11. Copy `lenya/webapp/lenya/xslt/bxeng` to 
`lenya/webapp/lenya/xslt/fckeditor`.  
+ 
+ a. Modify `aggregate.xsl` to contain the following:
+ 
+ {{{
+ <?xml version="1.0"?>
+ <xsl:stylesheet version="1.0"
+   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+   xmlns:xhtml="http://www.w3.org/1999/xhtml";>
+ 
+ <xsl:template match="/">
+ <xsl:apply-templates select="fckeditor"/>
+ </xsl:template>
+ 
+ <xsl:template match="fckeditor">
+ <html xmlns="http://www.w3.org/1999/xhtml";>
+ <xsl:apply-templates select="xhtml:html/xhtml:[EMAIL PROTECTED] = 
'EditorHead']"/>
+ <body onload="invoke();">
+ 
+ <form id="myform" 
action="/default/authoring/index.html?lenya.usecase=fckeditor&amp;lenya.step=save"
 method="POST">
+ <textarea id="content" rows="40" cols="80"><xsl:copy-of 
select="xhtml:html/xhtml:body/node()"/></textarea>
+ </form>
+ 
+ </body>
+ </html>
+ </xsl:template>
+ 
+ <xsl:template match="xhtml:head">
+ <head xmlns="http://www.w3.org/1999/xhtml";>
+ <xsl:for-each select="/fckeditor/namespaces/xmlns">
+ <meta name="fckNS" content="{.}"/>
+ </xsl:for-each>
+ <xsl:copy-of select="@*|node()"/>
+ </head>
+ </xsl:template>
+ 
+ </xsl:stylesheet>
+ }}}
+ 
+ b. Modify asset.xsl to containt the following:
+ 
+ {{{
+ <?xml version="1.0" encoding="UTF-8"?>
+ 
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
+               xmlns:dir="http://apache.org/cocoon/directory/2.0";>
+    
+ <xsl:param name="pubid"/>
+ <xsl:param name="docid"/>
+ <xsl:param name="type"/>
+ 
+ <xsl:template match="/">
+     <Connector command="GetFoldersAndFiles" resourceType="File">
+     <xsl:if test="$type='Image'">
+           <CurrentFolder path="/" url="/{$pubid}/authoring/{$docid}/" />
+       </xsl:if>
+       <xsl:if test="$type='File'">
+               <CurrentFolder path="/" url="/{$docid}/" />
+       </xsl:if>
+       <Folders>
+           </Folders>
+           <Files>
+           <xsl:for-each select="dir:directory/dir:file">
+               <xsl:variable name="fileName" select="@name"/>
+               <xsl:variable name="size" select="@size"/>
+                       <File name="{$fileName}" size="{$size}"/>
+          </xsl:for-each>
+     </Files>
+     </Connector>
+ </xsl:template>
+ 
+ </xsl:stylesheet>  
+ 
+ }}}
+ 
+ c. Add a file called change-image-path.xsl that contains the following:
+ 
+ {{{
+ <?xml version="1.0" encoding="UTF-8"?>
+ 
+ <xsl:stylesheet version="1.0"
+     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+     xmlns:lenya="http://apache.org/cocoon/lenya/page-envelope/1.0";
+     xmlns:xhtml="http://www.w3.org/1999/xhtml";
+     >
+   <xsl:param name="documentid" />
+   <xsl:param name="publicationid" />
+   
+   <xsl:template match="img/@src[not(starts-with(., '/'))]">
+     <xsl:attribute name="src">
+       <xsl:value-of select="concat('/',$publicationid,'/authoring/',.)" />
+     </xsl:attribute>
+   </xsl:template>
+   
+   
+   <xsl:template match="@*|node()">
+     <xsl:copy>
+       <xsl:apply-templates select="@*|node()" />
+     </xsl:copy>
+   </xsl:template>
+   
+ </xsl:stylesheet>
+ }}}
+ 
+ d. Add a file called uploadAssetComplete.xsl that contains the following:
+ 
+ {{{
+ <?xml version="1.0" encoding="UTF-8"?>
+ 
+ <xsl:stylesheet version="1.0"
+     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+     xmlns:lenya="http://apache.org/cocoon/lenya/page-envelope/1.0";
+     xmlns:xhtml="http://www.w3.org/1999/xhtml";
+     xmlns:lxslt="http://xml.apache.org/xslt";
+     >
+ 
+ <xsl:param name="errorCode" />
+ <xsl:param name="name" />
+   
+ <xsl:template match="html">
+ <html>
+ <script type="text/javascript">
+ var errorCode = <xsl:value-of select="$errorCode"/>;
+ var name = "<xsl:value-of select="$name"/>";
+ alert(name);
+ window.parent.frames['frmUpload'].OnUploadCompleted(0,name);
+ </script>
+ </html>
+     
+ </xsl:template>
+ </xsl:stylesheet>
+ }}}
+ 
+ e. Add a new xsl file called saveContent.xsl.  It should look like the 
following:
+ 
+ {{{
+ <?xml version="1.0"?>
+ 
+ <xsl:stylesheet version="1.0"
+     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+     xmlns="http://www.w3.org/1999/xhtml";
+     xmlns:xhtml="http://www.w3.org/1999/xhtml";
+     xmlns:page="http://apache.org/cocoon/lenya/cms-page/1.0";
+     xmlns:lenya="http://apache.org/cocoon/lenya/page-envelope/1.0"; 
+     xmlns:dc="http://purl.org/dc/elements/1.1/";
+     xmlns:dcterms="http://purl.org/dc/terms/";
+     xmlns:i18n="http://apache.org/cocoon/i18n/2.1";
+     exclude-result-prefixes="page xhtml"
+ >
+ 
+ <xsl:param name="title"/>
+ <xsl:param name="creator"/>
+ <xsl:param name="subject"/>
+ <xsl:param name="publisher"/>
+ <xsl:param name="contributor"/>
+ <xsl:param name="date"/>
+ <xsl:param name="type"/>
+ <xsl:param name="format"/>
+ <xsl:param name="identifier"/>
+ <xsl:param name="source"/>
+ <xsl:param name="language"/>
+ <xsl:param name="relation"/>
+ <xsl:param name="coverage"/>
+ <xsl:param name="rights"/>
+ <xsl:param name="issued"/>
+ <xsl:param name="modified"/>
+ 
+ <xsl:template match="/">
+ <html xmlns="http://www.w3.org/1999/xhtml";>
+       <lenya:meta>
+               <dc:title><xsl:value-of select="$title"/></dc:title>
+               <dc:creator><xsl:value-of select="$creator"/></dc:creator>
+               <dc:subject><xsl:value-of select="$subject"/></dc:subject>
+               <dc:publisher><xsl:value-of select="$publisher"/></dc:publisher>
+               <dc:contributor><xsl:value-of 
select="$contributor"/></dc:contributor>
+               <dc:date><xsl:value-of select="$date"/></dc:date>
+               <dc:type><xsl:value-of select="$type"/></dc:type>
+               <dc:format><xsl:value-of select="$format"/></dc:format>
+               <dc:identifier><xsl:value-of 
select="$identifier"/></dc:identifier>
+               <dc:source><xsl:value-of select="$source"/></dc:source>
+               <dc:language><xsl:value-of select="$language"/></dc:language>
+               <dc:relation><xsl:value-of select="$relation"/></dc:relation>
+               <dc:coverage><xsl:value-of select="$coverage"/></dc:coverage>
+               <dc:rights><xsl:value-of select="$rights"/></dc:rights>
+               <dcterms:issued><xsl:value-of 
select="$issued"/></dcterms:issued>
+               <dcterms:modified><xsl:value-of 
select="$modified"/></dcterms:modified>
+       </lenya:meta>   
+       <head>
+     <title><xsl:value-of select="$title"/></title>
+       </head>
+       <xsl:copy-of select="body"/>
+ </html>       
+ </xsl:template>
+  
+ </xsl:stylesheet>  
+ 
+ }}}  
+ 
- 7. Create a new Action in `org.apache.lenya.cms.cocoon.acting` called 
`FredSaveAction.java` (or whatever you'd like to call it) that saves the 
content to a temporary file.  Mine looks like this:
+ 12. Create a new Action in `org.apache.lenya.cms.cocoon.acting` called 
`FredSaveAction.java` (or whatever you'd like to call it) that saves the 
content to a temporary file.  Mine looks like this:
  
  {{{
  package org.apache.lenya.cms.cocoon.acting;
@@ -269, +545 @@

  
  This code is basically the same as 
`org.apache.lenya.cms.cocoon.acting.OneFormEditorSaveAction.java`, with a few 
modifications.  Validation needs to be added here.
  
- 8. Add the following action to `lenya/webapp/lenya/sitemap.xmap`:
+ 13. Create a new Action in `org.apache.lenya.cms.cocoon.acting` called 
`UploadFredAsset.java` (or whatever you'd like to call it) that saves the 
content to a temporary file.  Mine looks like this:
  
  {{{
+ package org.apache.lenya.cms.cocoon.acting;
- <map:action name="frededitorsave" logger="sitemap.action.frededitormsave" 
src="org.apache.lenya.cms.cocoon.acting.FredSaveAction" />
- }}}
  
- 9. Add a new xsl file to `lenya/xslt/fckeditor` called saveContent.xsl.  It 
should look like the following:
+ import java.io.File;
+ import java.io.FileOutputStream;
+ import java.io.FileInputStream;
+ import java.io.IOException;
+ import java.util.Collections;
+ import java.util.Enumeration;
+ import java.util.HashMap;
+ import java.util.Iterator;
+ import java.util.Map;
  
- {{{
- <?xml version="1.0"?>
+ import javax.xml.parsers.ParserConfigurationException;
+ import javax.xml.transform.TransformerConfigurationException;
+ import javax.xml.transform.TransformerException;
  
- <xsl:stylesheet version="1.0"
-     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
-     xmlns="http://www.w3.org/1999/xhtml";
-     xmlns:xhtml="http://www.w3.org/1999/xhtml";
-     xmlns:page="http://apache.org/cocoon/lenya/cms-page/1.0";
-     xmlns:lenya="http://apache.org/cocoon/lenya/page-envelope/1.0"; 
-     xmlns:dc="http://purl.org/dc/elements/1.1/";
-     xmlns:dcterms="http://purl.org/dc/terms/";
-     xmlns:i18n="http://apache.org/cocoon/i18n/2.1";
-     exclude-result-prefixes="page xhtml"
- >
+ import org.apache.avalon.excalibur.io.FileUtil;
+ import org.apache.avalon.framework.parameters.Parameters;
+ import org.apache.cocoon.acting.AbstractConfigurableAction;
+ import org.apache.cocoon.environment.ObjectModelHelper;
+ import org.apache.cocoon.environment.Redirector;
+ import org.apache.cocoon.environment.Request;
+ import org.apache.cocoon.environment.SourceResolver;
+ import org.apache.lenya.cms.publication.*;
+ import org.apache.lenya.cms.publication.PublicationFactory;
+ import org.apache.lenya.xml.DocumentHelper;
+ import org.apache.lenya.xml.NamespaceHelper;
+ import org.w3c.dom.Element;
  
+ /**
+  * The class <code>UploadFredAsset</code> implements an action that allows 
for asset
+  * upload using the Fck Editor. An upload consists of a file upload plus 
optionally a file creation for the meta data of
+  * the asset.
+  * 
+  * Also see org.apache.lenya.cms.authoring.UploadHelper
+  */
+ public class UploadFredAsset extends AbstractConfigurableAction {
- <xsl:param name="title"/>
- <xsl:param name="creator"/>
- <xsl:param name="subject"/>
- <xsl:param name="publisher"/>
- <xsl:param name="contributor"/>
- <xsl:param name="date"/>
- <xsl:param name="type"/>
- <xsl:param name="format"/>
- <xsl:param name="identifier"/>
- <xsl:param name="source"/>
- <xsl:param name="language"/>
- <xsl:param name="relation"/>
- <xsl:param name="coverage"/>
- <xsl:param name="rights"/>
- <xsl:param name="issued"/>
- <xsl:param name="modified"/>
  
- <xsl:template match="/">
- <html xmlns="http://www.w3.org/1999/xhtml";>
-       <lenya:meta>
-               <dc:title><xsl:value-of select="$title"/></dc:title>
-               <dc:creator><xsl:value-of select="$creator"/></dc:creator>
-               <dc:subject><xsl:value-of select="$subject"/></dc:subject>
-               <dc:publisher><xsl:value-of select="$publisher"/></dc:publisher>
-               <dc:contributor><xsl:value-of 
select="$contributor"/></dc:contributor>
-               <dc:date><xsl:value-of select="$date"/></dc:date>
-               <dc:type><xsl:value-of select="$type"/></dc:type>
-               <dc:format><xsl:value-of select="$format"/></dc:format>
-               <dc:identifier><xsl:value-of 
select="$identifier"/></dc:identifier>
-               <dc:source><xsl:value-of select="$source"/></dc:source>
-               <dc:language><xsl:value-of select="$language"/></dc:language>
-               <dc:relation><xsl:value-of select="$relation"/></dc:relation>
-               <dc:coverage><xsl:value-of select="$coverage"/></dc:coverage>
-               <dc:rights><xsl:value-of select="$rights"/></dc:rights>
-               <dcterms:issued><xsl:value-of 
select="$issued"/></dcterms:issued>
-               <dcterms:modified><xsl:value-of 
select="$modified"/></dcterms:modified>
-       </lenya:meta>   
-       <head>
-     <title><xsl:value-of select="$title"/></title>
-       </head>
-       <xsl:copy-of select="body"/>
- </html>       
- </xsl:template>
-  
- </xsl:stylesheet>  
+     
+     private PageEnvelope pageEnvelope;
+     private String imgPath;
+ 
+     public static final String UPLOADASSET_PARAM_PREFIX = "properties.asset.";
+     public static final String FILE_NAME_REGEXP = "[-a-zA-Z0-9_. ]+";
+ 
+     // optional parameters for meta data according to dublin core
+     public static final String[] DUBLIN_CORE_PARAMETERS = { "title", 
"creator", "subject",
+             "description", "publisher", "contributor", "date", "type", 
"format", "identifier",
+             "source", "language", "relation", "coverage", "rights" };
+ 
+     /**
+      * Retrieve the file from the request and store it in the corresponding 
directory, optionally
+      * create a meta file and optionally insert an image tag in the 
requesting document.
+      * 
+      * @param redirector a <code>Redirector</code> value
+      * @param resolver a <code>SourceResolver</code> value
+      * @param objectModel a <code>Map</code> value
+      * @param source a <code>String</code> value
+      * @param parameters a <code>Parameters</code> value
+      * 
+      * @return a <code>Map</code> containing the referer or null if the name 
of the file to be
+      *         uploaded contains characters that are not allowed (@see 
FILE_NAME_REGEXP).
+      * 
+      * @exception Exception if an error occurs
+      */
+     public Map act(Redirector redirector, SourceResolver resolver, Map 
objectModel, String source,
+             Parameters parameters) throws Exception {
+ 
+         Map results = new HashMap();
+        
+         Request request = ObjectModelHelper.getRequest(objectModel);
+         String uri = request.getRequestURI();
+         String[] uriArray = uri.split("/");
+ 
+         //Getting document Id from URL
+         boolean isDefault = false;
+         boolean isPub = false;
+         String docId = "";
+         for(int i = 0; i < uriArray.length; i ++){
+               if(uriArray[i].equals("connectors")){
+                       isPub = false;
+                       isDefault=false;
+               }
+               if(isPub){
+                       if(docId.length() > 0){
+                               docId = docId + "/";
+                       }
+                       docId = docId + uriArray[i];
+               }
+               if(isDefault){
+                       isPub = true;
+               }
+               if(uriArray[i].equalsIgnoreCase("default")){
+                       isDefault = true;
+               }
+               
+         }
+         
+       DefaultDocumentBuilder builder = DefaultDocumentBuilder.getInstance();
+       //Need to figure out how to get the context.
+       Publication pub = PublicationFactory.getPublication("default", 
"C:\\lenyaStandalone\\apache-lenya-1.2.4\\build\\lenya\\webapp");
+       //Publication pub = PublicationFactory.getPublication("default", 
"C:\\apache-lenya-1.2.4\\build\\lenya\\webapp");
+       Document doc = builder.buildDocument(pub, pub.getId() + "/authoring/" + 
docId + ".html");
+       
+         File assetFile;
+ 
+         logRequestParameters(request);
+ 
+         Map dublinCoreParams = getDublinCoreParameters(request);
+ 
+         String filePath = request.getParameter("NewFile");
+         int lastIndex = filePath.lastIndexOf("\\");
+         String fileName = filePath.substring(lastIndex + 1);
+         // upload the file to the uploadDir
+         File uploadFile = new File(filePath);
+         System.out.println("upload file " + uploadFile.getAbsolutePath());
+       
+         if (!fileName.matches(FILE_NAME_REGEXP) || 
FileUtil.getExtension(fileName).equals("")) {
+             // the file name contains characters which mean trouble
+             // and are therefore not allowed.
+             getLogger().warn("The filename [" + fileName + "]Â is not valid 
for an asset.");
+             return null;
+         }
+         // convert spaces in the file name to underscores
+         fileName = fileName.replace(' ', '_');
+         ResourcesManager resourcesMgr = new ResourcesManager(doc);
+         File pagePath = resourcesMgr.getPath();
+         assetFile = new File(pagePath, fileName);
+         if (!pagePath.exists()) {
+            pagePath.mkdirs();
+         }
+             
+        // create an extra file containing the meta description for
+        // the asset.
+        File metaDataFile = new File(pagePath, fileName + ".meta");
+        createMetaData(metaDataFile, dublinCoreParams);
+ 
+         results.put("errorCode", "0");
+         results.put("name", fileName);
+         saveAsset(assetFile, uploadFile);
+        
+ 
+         return Collections.unmodifiableMap(results);
+     }
+ 
+     /**
+      * Saves the asset to a file.
+      * 
+      * @param assetFile The asset file.
+      * @param part The part of the multipart request.
+      * @throws Exception if an error occurs.
+      */
+     protected void saveAsset(File assetFile, File part) throws Exception {
+         if (!assetFile.exists()) {
+             boolean created = assetFile.createNewFile();
+             if (!created) {
+                 throw new RuntimeException("The file [" + assetFile + "]Â 
could not be created.");
+             }
+         }
+ 
+         byte[] buf = new byte[4096];
+         FileOutputStream out = new FileOutputStream(assetFile);
+         try {
+             FileInputStream in = new FileInputStream(part);//InputStream in = 
part.getInputStream();
+             int read = in.read(buf);
+ 
+             while (read > 0) {
+                 out.write(buf, 0, read);
+                 read = in.read(buf);
+             }
+         } finally {
+             out.close();
+         }
+     }
+ 
+     /**
+      * Logs the request parameters.
+      * @param request The request.
+      */
+     protected void logRequestParameters(Request request) {
+         for (Enumeration myenum = request.getParameterNames(); 
myenum.hasMoreElements();) {
+             String param = (String) myenum.nextElement();
+             getLogger().debug(
+                     param + ": " + request.getParameter(param) + " [" + 
request.get(param) + "]");
+         }
+     }
+ 
+     /**
+      * Retrieves optional parameters for the meta file which contains dublin 
core information from
+      * the request.
+      * @param request The request.
+      * @return A map.
+      */
+     protected Map getDublinCoreParameters(Request request) {
+         HashMap dublinCoreParams = new HashMap();
+ 
+         for (int i = 0; i < DUBLIN_CORE_PARAMETERS.length; i++) {
+             String paramName = DUBLIN_CORE_PARAMETERS[i];
+             String paramValue = request.getParameter(UPLOADASSET_PARAM_PREFIX 
+ paramName);
+ 
+             if (paramValue == null) {
+                 paramValue = "";
+             }
+ 
+             dublinCoreParams.put(paramName, paramValue);
+         }
+         
+         Iterator iter = dublinCoreParams.keySet().iterator();
+         while (iter.hasNext()) {
+             String paramName = (String) iter.next();
+             getLogger().debug(paramName + ": " + 
dublinCoreParams.get(paramName));
+         }
+         
+         return dublinCoreParams;
+     }
+ 
+     /**
+      * Create the meta data file given the dublin core parameters.
+      * 
+      * @param metaDataFile the file where the meta data file is to be created
+      * @param dublinCoreParams a <code>Map</code> containing the dublin core 
values
+      * @throws TransformerConfigurationException if an error occurs.
+      * @throws TransformerException if an error occurs.
+      * @throws IOException if an error occurs
+      * @throws ParserConfigurationException if an error occurs.
+      */
+     protected void createMetaData(File metaDataFile, Map dublinCoreParams)
+             throws TransformerConfigurationException, TransformerException, 
IOException,
+             ParserConfigurationException {
+ 
+         assert (metaDataFile.getParentFile().exists());
+ 
+         NamespaceHelper helper = new 
NamespaceHelper("http://purl.org/dc/elements/1.1/";, "dc",
+                 "metadata");
+ 
+         Element root = helper.getDocument().getDocumentElement();
+ 
+         Iterator iter = dublinCoreParams.keySet().iterator();
+ 
+         while (iter.hasNext()) {
+             String tagName = (String) iter.next();
+             String tagValue = (String) dublinCoreParams.get(tagName);
+             root.appendChild(helper.createElement(tagName, tagValue));
+         }
+ 
+         DocumentHelper.writeDocument(helper.getDocument(), metaDataFile);
+     }
+ }
  
  }}}
  
+ This code uploads an asset to the page's asset directory.  When creating the 
Publication, one of the parameters required is the context.  Right now, this is 
hard coded.  I have yet to figure out a way to get this.  Obviously, hard 
coding is not the solution - still working on this.  Thinking about putting the 
context in a properties files somewhere.  Most of this code was taken from the 
action `org.apache.lenya.cms.cocoon.acting.UploadAction.java` and modified to 
work with Fred.
+ 
  To get Fred to work with all your pubs, you will need to copy 
`usecase-fckeditor.xmap` to each publication.  
  
- This is still very much a work in progress.  This will get Fred to load with 
content and save the content, but the validation and asset insertion have not 
been implemented yet.  In addition, I'm sure there's a better way to get the 
lenya tags in the document when saving rather than hard coding them in the xsl 
file. The `fckeditor-usecase.xmap` also needs to be cleaned up.  Any 
suggestions or comments are welcome!
+ There are a lot of steps here, and this is still very much a work in 
progress, but should get the basics of Fred working.  In addition, I'm sure 
there's a better way to get the lenya tags in the document when saving rather 
than hard coding them in the xsl file. The `fckeditor-usecase.xmap` also needs 
to be cleaned up.  Any suggestions or comments are welcome!
  
- Note:  To change the height or width of the editor text area, go to 
`lenya/webapp/lenya/resources/fckeditor/fckeditor.js` and change the `width` 
and `height` properties.
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to