6 Replies Latest reply on Mar 17, 2009 6:47 AM by Ilya Shaikovsky

    Unicode problem in rich:tree

    ZERO ROC Newbie

      HI ,I have a problem with rich:tree .when i click on a node with unicode character it throw exception :

      WARNING: org.apache.jasper.el.JspELException: /ListFileAndDirectory.jsp(9,0) '#{listFileAndDirectoryBean.processSelection}' java.lang.IllegalStateException: No tree element available or row key not set!
      
      


      Is this a richfaces bugs ? .Please help me

      Here is jsp page code :


      <h:outputText id="selectedNode" escape="true" value="Selected Node : #{listFileAndDirectoryBean.nodeTitle}"/>
      
      <rich:tree id="Tree" value="#{listFileAndDirectoryBean.rootNode}" nodeSelectListener="#{listFileAndDirectoryBean.processSelection}"
       reRender="Tree,selectedNode" switchType="ajax" var="item" ajaxKeys="#{null}" ajaxSubmitSelection="true"
       treeNodeVar="treenode" style="width:300px">
       <rich:componentControl event="onClick" for="menu" operation="show"></rich:componentControl>
      </rich:tree>
      
      
      <rich:contextMenu event="oncontextmenu" attached="true" id="menu" submitMode="server">
       <rich:menuItem ajaxSingle="true" value="Download" actionListener="#{listFileAndDirectoryBean.form_download_actionlisener}">
       </rich:menuItem>
      </rich:contextMenu>
      



      And a part Bean code with fuction that throw exception :

      
      public void processSelection(NodeSelectedEvent event)
       {
       HtmlTree tree=(HtmlTree)event.getComponent();
      
       //Error this line
       TreeNode currentNode =(TreeNode) tree.getTreeNode();
       ListRowKey<File> ls=(ListRowKey<File>)tree.getTreeNodeRowKey(currentNode);
       int size= ls.depth();
       if (size>0)
       {
       File f=ls.get(size-1);
       nodeTitle=f.getAbsolutePath();
       if (f.isFile())
       {
       System.out.println("download");
       // downloadFileFromServer(f);
       myfile=f;
       }
       else
       {
       addNodes(currentNode, f.getAbsolutePath());
       }
       }
      
       }
      


      And picture :

      [img]
      http://upload69.net/images/lasmkhzz8lqfwhkupggv.png
      [/img]

        • 1. Re: Unicode problem in rich:tree
          Nick Belaevski Master

          Hello,

          Please post full page code.

          • 2. Re: Unicode problem in rich:tree
            ZERO ROC Newbie

            Thanks your reply!

            Here is full BackBean (listFileAndDirectoryBean) Code :

            
            package com.ui.bean;
            
            import java.io.File;
            import java.io.FileInputStream;
            import java.io.IOException;
            import java.io.InputStream;
            import java.io.OutputStream;
            import java.util.ArrayList;
            import java.util.List;
            import java.util.Properties;
            
            import javax.faces.FacesException;
            import javax.faces.context.FacesContext;
            import javax.faces.event.ActionEvent;
            import javax.servlet.http.HttpServletResponse;
            
            import org.richfaces.component.html.HtmlTree;
            import org.richfaces.event.NodeSelectedEvent;
            import org.richfaces.event.UploadEvent;
            import org.richfaces.model.ListRowKey;
            import org.richfaces.model.TreeNode;
            import org.richfaces.model.UploadItem;
            
            import com.app.context.FileUtil;
            import com.app.custom.MyTreeNodeImpl;
            import com.ui.api.ListFileAndDirectoryFormAPI;
            
            public class ListFileAndDirectoryBean extends ListFileAndDirectoryFormAPI
            {
             private TreeNode rootNode=null;
             private String nodeTitle="";
             private File myfile;
             private String showModal;
             private List list=new ArrayList();
             private List<UploadItem> arrUploadItem;
            
             private static final String DATA_PATH="config.properties";
             private static final String PropertiesKey="resourceFolder";
            
             public TreeNode getRootNode() {
             if (rootNode==null)
             {
             loadTree();
             }
             return rootNode;
             }
            
            
             public void setRootNode(TreeNode rootNode) {
             this.rootNode = rootNode;
             }
            
             public String getNodeTitle() {
             return nodeTitle;
             }
            
             public void setNodeTitle(String nodeTitle) {
             this.nodeTitle = nodeTitle;
             }
            
             public List<UploadItem> getArrUploadItem() {
             return arrUploadItem;
             }
            
             public void setArrUploadItem(List<UploadItem> arrUploadItem) {
             this.arrUploadItem = arrUploadItem;
             }
            
             public String getShowModal() {
             return showModal;
             }
            
             public void setShowModal(String showModal) {
             this.showModal = showModal;
             }
            
            
             public ListFileAndDirectoryBean()
             {
             reset();
             }
            
             private void loadTree()
             {
             ClassLoader cl=this.getClass().getClassLoader();
             InputStream dataStream=cl.getResourceAsStream(DATA_PATH);
             try {
             Properties properties = new Properties();
             properties.load(dataStream);
             String path= properties.getProperty(PropertiesKey);
             rootNode = new MyTreeNodeImpl();
             addNodes(rootNode, path);
            
             } catch (IOException e) {
             e.printStackTrace();
             throw new FacesException(e.getMessage(), e);
            
             } finally {
             if (dataStream != null) {
             try {
             dataStream.close();
             } catch (IOException e) {
            // externalContext.log(e.getMessage(), e);
             }
             }
             }
             }
            
             private void addNodes(TreeNode node,String path)
             {
             File[] files=null;
             files=getListFileAndDirectory(path);
             for (int i = 0; i < files.length; i++) {
             File f=files;
             MyTreeNodeImpl nodeImpl=new MyTreeNodeImpl();
             nodeImpl.setData(f.getName());
             if (f.isFile()) {
             nodeImpl.setType(MyTreeNodeImpl.fileType);
             }
             else
             {
             nodeImpl.setType(MyTreeNodeImpl.folderType);
             }
            
             // node.addChild(f, nodeImpl);
             node.addChild(f.getAbsolutePath(), nodeImpl);
             }
             }
            
             private File[] getListFileAndDirectory(String path)
             {
             File f=new File(path);
             File[] files=f.listFiles();
             return files;
             }
            
             public void processSelection(NodeSelectedEvent event)
             {
             HtmlTree tree=(HtmlTree)event.getComponent();
             TreeNode currentNode =(TreeNode) tree.getTreeNode();
             ListRowKey ls=(ListRowKey)tree.getTreeNodeRowKey(currentNode);
             int size= ls.depth();
             if (size>0)
             {
            
             // File f=ls.get(size-1);
             // nodeTitle=f.getAbsolutePath();
            
             String path=(String)ls.get(size-1);
             nodeTitle=path;
             File f=new File(path);
             if (f.isFile())
             {
             showModal="false";
             myfile=f;
             System.out.println(myfile.getAbsolutePath());
             }
             else
             {
             showModal="true";
             myfile=f;
             System.out.println(myfile.getAbsolutePath());
            
             addNodes(currentNode, f.getAbsolutePath());
            
             }
             }
            
             }
            
             private void downloadFileFromServer(File f)
             {
             //We must get first our context
             FacesContext context = FacesContext.getCurrentInstance();
            
             //Then we have to get the Response where to write our file
             HttpServletResponse response =
             ( HttpServletResponse ) context.getExternalContext().getResponse();
            
             //Now we create some variables we will use for writting the file to the response
             String filePath = null;
             String fileName= null;
             String mimeType="";
             int read = 0;
             byte[] bytes = new byte[1024];
            
             //Be sure to retrieve the absolute path to the file with the required method
             // filePath = f.getParent();
             filePath = f.getAbsolutePath();
             fileName = f.getName();
             //Now set the content type for our response, be sure to use the best suitable content type depending on your file
             //the content type presented here is ok for, lets say, text files and others (like CSVs, PDFs)
             mimeType=getMimeType(fileName);
             response.setContentType(mimeType);
             response.setContentLength((int)f.length());
             //This is another important attribute for the header of the response
             //Here fileName, is a String with the name that you will suggest as a name to save as
             //I use the same name as it is stored in the file system of the server.
            
             response.setHeader("Content-Disposition", "attachment;filename=\"" +
             fileName+"\"");
            
             //Streams we will use to read, write the file bytes to our response
             FileInputStream fis = null;
             OutputStream os = null;
            
             try
             {
             //First we load the file in our InputStream
             // fis = new FileInputStream(new File(filePath,fileName));
             fis = new FileInputStream(new File(filePath));
             os = response.getOutputStream();
            
             //While there are still bytes in the file, read them and write them to our OutputStream
             while((read = fis.read(bytes)) != -1){
             os.write(bytes,0,read);
             }
             //Clean resources
             os.flush();
             os.close();
             context.responseComplete();
             }
             catch (Exception e) {
             e.printStackTrace();
             }
            
             }
            
             private synchronized void uploadFileToServer(File f,UploadItem item)
             {
             try
             {
             String fileName=FileUtil.trimFilePath(item.getFileName());
             // File uniqueFile=FileUtil.uniqueFile(new File(f.getAbsolutePath()), fileName);
             File uniqueFile=new File(f.getAbsolutePath()+ "\\" + fileName);
             FileUtil.copy(item.getFile(), uniqueFile, true);
            
             }
             catch (Exception e)
             {
             e.printStackTrace();
             }
             }
            
             public void form_download_actionlisener(ActionEvent event)
             {
             downloadFileFromServer(myfile);
             }
            
             public void form_upload_actionlisener(UploadEvent event)
             {
             UploadItem item=this.getArrUploadItem().get(0);
             uploadFileToServer(myfile,item);
             }
            
             private String getMimeType(String filename)
             {
             String type="application/octet-stream";
             String extension="";
             extension=filename.substring(filename.indexOf(".")+1);
             extension=extension.toLowerCase();
            
             //TEXT-DOCUMENT
             if (extension.equals("pdf"))
             {
             type="application/pdf";
             }
             else if(extension.equals("doc") || extension.equals("docx"))
             {
             type="application/msword";
             }
             else if(extension.equals("xls") || extension.equals("xlsx") || extension.equals("csv"))
             {
             type="application/vnd.ms-excel";
             }
             else if(extension.equals("ppt") || extension.equals("pptx") || extension.equals("pps"))
             {
             type="application/vnd.ms-powerpoint";
             }
             else if(extension.equals("txt") || extension.equals("rtx") || extension.equals("rtf"))
             {
             type="text/plain";
             }
             else if(extension.equals("html") || extension.equals("htm"))
             {
             type="text/html";
             }
             else if(extension.equals("xml"))
             {
             type="text/xml";
             }
             else if(extension.equals("css"))
             {
             type="text/css";
             }
             else if(extension.equals("js"))
             {
             type="application/x-javascript";
             }
            
             //IMAGE
            
             else if(extension.equals("jpeg") || extension.equals("jpe") || extension.equals("jpg"))
             {
             type="image/jpeg";
             }
             else if(extension.equals("png"))
             {
             type="image/png";
             }
             else if(extension.equals("tiff") || extension.equals("tif"))
             {
             type="image/tiff";
             }
             else if(extension.equals("gif") || extension.equals("gif"))
             {
             type="image/gif";
             }
            
             //AUDIO
            
             else if(extension.equals("mp3") || extension.equals("mp2") || extension.equals("mpga"))
             {
             type="audio/mpeg";
             }
             else if(extension.equals("midi") || extension.equals("mid"))
             {
             type="audio/midi";
             }
            
             //VIDEO
            
             else if(extension.equals("mp4"))
             {
             type="video/mp4";
             }
             else if(extension.equals("mpe") || extension.equals("mpeg") || extension.equals("mpg"))
             {
             type="video/mpeg";
             }
             else if(extension.equals("mov") || extension.equals("qt"))
             {
             type="video/quicktime";
             }
             else if(extension.equals("asx"))
             {
             type="video/x-ms-asx";
             }
             else if(extension.equals("asf"))
             {
             type="video/x-ms-asf";
             }
             else if(extension.equals("wmv"))
             {
             type="video/x-ms-wmv";
             }
             else if(extension.equals("avi"))
             {
             type="video/x-msvideo";
             }
             else if(extension.equals("flv"))
             {
             type="video/flv";
             }
            
             //COMPRESSED
            
             else if(extension.equals("zip"))
             {
             type="application/zip";
             }
             else if(extension.equals("rar"))
             {
             type="application/x-rar-compressed";
             }
             else if(extension.equals("gz"))
             {
             type="application/x-gzip";
             }
             else if(extension.equals("tar.gz") || extension.equals("tgz"))
             {
             type="application/x-tar-gz";
             }
            
             //FLASH
            
             else if(extension.equals("swf"))
             {
             type="application/x-shockwave-flash";
             }
             else if(extension.equals("rar"))
             {
             type="application/x-rar-compressed";
             }
             return type;
             }
            
             private void reset()
             {
             this.arrUploadItem=new ArrayList<UploadItem>();
             showModal="false";
             }
             }
            
            
            



            This is ListFileAndDirectory.jsp page with rich:tree :

            
            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
            <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
            <%@ taglib uri="http://richfaces.org/rich" prefix="rich" %>
            <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
            
            <h:outputText id="selectedNode" escape="true" value="Selected Node : #{listFileAndDirectoryBean.nodeTitle}"/>
            
            <rich:tree id="Tree" value="#{listFileAndDirectoryBean.rootNode}" nodeSelectListener="#{listFileAndDirectoryBean.processSelection}"
             reRender="Tree,selectedNode" switchType="ajax" var="item" ajaxKeys="#{null}" ajaxSubmitSelection="true"
             treeNodeVar="treenode" style="width:300px" onselected="changeStatus();">
             <rich:componentControl event="onclick" for="menu" operation="show"></rich:componentControl>
            </rich:tree>
            
            
            <rich:contextMenu event="oncontextmenu" attached="true" id="menu" submitMode="server" >
             <rich:menuItem ajaxSingle="false" value="Download" actionListener="#{listFileAndDirectoryBean.form_download_actionlisener}">
             </rich:menuItem>
             <rich:menuItem id="upload" ajaxSingle="false" value="Upload">
             <rich:componentControl for="panel" attachTo="upload" operation="show" event="onclick"/>
             </rich:menuItem>
            </rich:contextMenu>
            
            





            • 4. Re: Unicode problem in rich:tree
              Nick Belaevski Master

              Yes, it is.

              • 5. Re: Unicode problem in rich:tree
                ZERO ROC Newbie

                Thanks Nick , That's mean i have to wait this bug was fix in next 3.3.1 version ?

                • 6. Re: Unicode problem in rich:tree
                  Ilya Shaikovsky Master

                  Become a watcher for this issue. then you could try the snapshot right after the issue will be fixed.