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 BobHarner:
http://wiki.apache.org/lenya/HowToIntegrateFCKEditor

The comment on the change is:
Removed the java source code from this page, since they are now attachments

------------------------------------------------------------------------------
  
  }}}  
  
+ 11. Copy the attached FredSaveAction.java as a new Action in 
`org.apache.lenya.cms.cocoon.acting` that saves the content to a temporary file.
- 11. 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;
- 
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- 
- import org.apache.avalon.framework.parameters.Parameters;
- import org.apache.avalon.framework.thread.ThreadSafe;
- import org.apache.cocoon.acting.AbstractConfigurableAction;
- import org.apache.cocoon.environment.ObjectModelHelper;
- import org.apache.cocoon.environment.Redirector;
- import org.apache.cocoon.environment.SourceResolver;
- import org.apache.cocoon.environment.http.HttpRequest;
- import org.apache.lenya.xml.DocumentHelper;
- import org.apache.lenya.xml.RelaxNG;
- import org.apache.log4j.Category;
- import org.xml.sax.SAXException;
- 
- 
- public class FredSaveAction
-     extends AbstractConfigurableAction
-     implements ThreadSafe {
-     Category log = Category.getInstance(FredSaveAction.class);
- 
-     /**
-      * Save data to temporary file
-      *
-      * @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> value
-      *
-      * @exception Exception if an error occurs
-      */
-     public Map act(
-         Redirector redirector,
-         SourceResolver resolver,
-         Map objectModel,
-         String source,
-         Parameters parameters)
-         throws Exception {
- 
-         HttpRequest request = 
(HttpRequest)ObjectModelHelper.getRequest(objectModel);
- 
-         // Aggregate content
-         String encoding = request.getCharacterEncoding();
-         String content = "<?xml version=\"1.0\" encoding=\""
-                 + encoding
-                 + "\"?>\n" +
-               "<body>\n" + request.getParameter("content") +"\n" + 
"</body>\n";
- 
-         // Save file temporarily
-         File sitemap = new File(new 
URL(resolver.resolveURI("").getURI()).getFile());
-         File file =
-             new File(
-                 sitemap.getAbsolutePath()
-                     + File.separator
-                     + parameters.getParameter("file"));
- 
-         File parentFile = new File(file.getParent());
-         if (!parentFile.exists()) {
-             parentFile.mkdirs();
-         }
-               FileOutputStream fileoutstream = new FileOutputStream(file);
-         Writer writer = new OutputStreamWriter(fileoutstream, encoding);
-         writer.write(content, 0, content.length());
-         writer.close();
- 
-         //TODO: Need to Validate content against schema.
-         
-         return null;
-     }
- 
- }
- 
- }}} 
  
  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.
  
+ 12. Copy the attached UploadFredAsset.java as a new Action in 
`org.apache.lenya.cms.cocoon.acting`.
- 12. 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;
- 
- 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;
- 
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- 
- 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 {
- 
-     
-     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);
-       
-         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 unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to