Changeset 7626


Ignore:
Timestamp:
Mar 7, 2019, 2:43:04 PM (5 years ago)
Author:
Nicklas Nordborg
Message:

References #2159: It should be possible to specify a character set in the "Edit file" dialog when saving

This should work now. If the user selects a character set that can't encode the characters used in the text the "Save" action will display an error message.

Location:
trunk/www/filemanager/files
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/www/filemanager/files/ajax.jsp

    r7625 r7626  
    3737  import="java.io.Writer"
    3838  import="java.io.Reader"
     39  import="java.nio.charset.Charset"
     40  import="java.nio.charset.CodingErrorAction"
     41  import="java.nio.charset.CharsetEncoder"
     42  import="java.nio.charset.CharacterCodingException"
    3943%>
    4044<%
     
    6266  else if ("StoreFile".equals(cmd))
    6367  {
     68    String charset = null;
    6469    try
    6570    {
    6671      int size = Values.getInt(request.getParameter("size"), -1);
     72      charset = Values.getStringOrNull(request.getParameter("charset"));
     73     
    6774      dc = sc.newDbControl();
    6875      File f = File.getById(dc, itemId);
    6976      Reader in = request.getReader();
    70       String charset = f.getCharacterSet();
    7177      if (charset == null)
    7278      {
    73         charset = "UTF-8";
    74         f.setCharacterSet(charset);
     79        charset = Values.getString(f.getCharacterSet(), "UTF-8");
    7580      }
     81      boolean charsetModified = !charset.equals(f.getCharacterSet());
     82      if (charsetModified) f.setCharacterSet(charset);
     83      Charset cs = Charset.forName(charset);
     84      if (!cs.canEncode())
     85      {
     86        throw new UnsupportedOperationException("Encoding is not supported by '" + charset + "'.");
     87      }
     88      CharsetEncoder encoder = Charset.forName(charset).newEncoder();
     89      encoder.onMalformedInput(CodingErrorAction.REPORT);
     90      encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    7691      Writer toFile = new OutputStreamWriter(
    77           f.getUploadStream(false, f.isCompressed()), charset);
     92          f.getUploadStream(false, f.isCompressed()), encoder);
    7893      int copied = (int)FileUtil.copy(in, toFile);
    7994      if (size >= 0 && copied != size)
     
    85100      toFile.close();
    86101      dc.commit();
    87       json.put("message", "File saved successfully");
     102      json.put("message", "File saved " + (charsetModified ? " with character set " + charset : " successfully"));
     103    }
     104    catch (CharacterCodingException ex)
     105    {
     106      throw new Exception("Save failed: The text contains characters that can't be encoded in '" + charset + "'.");
    88107    }
    89108    catch (Exception ex)
    90109    {
    91       ex.printStackTrace();
    92110      throw new Exception("Save failed: " + ex.getMessage(), ex);
    93111    }
  • trunk/www/filemanager/files/edit_file_data.js

    r7604 r7626  
    2828  var editFile = {};
    2929  var originalText = '';
     30  var originalCharset = '';
    3031  var isModified = false;
    3132 
     
    4142    Events.addEventHandler('filedata', 'keydown', editFile.detectTab);
    4243    Events.addEventHandler('filedata', 'keyup', editFile.detectChanges);
     44    Events.addEventHandler('filedata', 'blur', editFile.detectChangesFull);
     45    Events.addEventHandler('charset', 'change', editFile.detectChangesFull);
    4346  }
    4447 
     
    5861    frm.filedata.value = request.responseText;
    5962    originalText = frm.filedata.value;
     63    originalCharset = frm.charset.value;
    6064    editFile.setModified(false);
    6165    editFile.updateLineNumbers();
     
    123127    url += '&cmd=StoreFile&item_id='+Data.get('page-data', 'file-id');
    124128    url += '&size='+frm.filedata.value.length;
     129    url += '&charset='+frm.charset.value;
    125130    var request = Ajax.getXmlHttpRequest();
    126131    request.open("POST", url, true);
     
    134139    if (response.status != 'ok')
    135140    {
    136       Forms.showNotification('btnSave', response.message);
     141      Forms.showNotification('btnSave', response.message, 'bigger-notify');
    137142      return;
    138143    }
     
    155160  }
    156161
     162  editFile.detectChangesFull = function()
     163  {
     164    var frm = document.forms['file'];
     165    var modified = frm.filedata.value != originalText || frm.charset.value != originalCharset;
     166    editFile.setModified(modified);
     167   
     168    Doc.addOrRemoveClass('charsetSection','utf8-warning', frm.charset.value != 'UTF-8' && frm.charset.value != originalCharset);
     169  }
     170 
    157171  editFile.detectChanges = function()
    158172  {
    159     if (!isModified)
    160     {
    161       var frm = document.forms['file'];
    162       if (frm.filedata.value != originalText)
    163       {
    164         editFile.setModified(true);
    165       }
    166     }
    167   }
    168 
     173    if (!isModified) editFile.detectChangesFull();
     174  }
    169175 
    170176  editFile.detectTab = function(event)
  • trunk/www/filemanager/files/edit_file_data.jsp

    r6378 r7626  
    3939  import="net.sf.basedb.clients.web.util.HTML"
    4040  import="net.sf.basedb.util.Values"
     41  import="net.sf.basedb.core.Config"
    4142  import="java.util.List"
    4243  import="java.util.Set"
    4344  import="java.util.HashSet"
     45  import="java.nio.charset.Charset"
    4446%>
    4547<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
     
    7476    {
    7577      position: absolute;
    76       top: 0em;
    77       left: 0em;
    78       bottom: 0em;
     78      top: 0;
     79      left: 0;
     80      bottom: 0;
    7981      width: 3.6em;
     82      padding-top: 2px;
    8083      padding-right: 0.4em;
    8184      text-align: right;
     
    9093    {
    9194      position: absolute;
    92       top: 0em;
     95      top: 2px;
    9396      left: 4em;
    94       bottom: 0em;
    95       right: 0em;
     97      bottom: 0;
     98      right: 0;
    9699      margin-left: 2px;
    97100    }
     
    101104      width: 100%;
    102105      height: 100%;
    103       border: 0px;
    104       margin: 0px;
    105       padding: 0px;
     106      border: 0;
     107      margin: 0;
     108      padding: 0;
     109    }
     110   
     111    .bigger-notify
     112    {
     113      width: 30em;
     114    }
     115   
     116    #charsetSection
     117    {
     118      top: auto;
     119      bottom: 0;
     120      height: 2.4em;
     121      left: auto;
     122      right: calc(50% + 7em);
     123      z-index: 2;
     124    }
     125   
     126    #nonUtf8Warning
     127    {
     128      visibility: hidden;
     129    }
     130   
     131    #charsetSection.utf8-warning #nonUtf8Warning
     132    {
     133      visibility: visible;
    106134    }
    107135    </style>
     
    125153        >Loading file; please wait...</textarea>
    126154      </div>
     155    </div>
     156
     157    <div id="charsetSection" class="absolutefull">
     158      <b>Character set</b>
     159      <select name="charset" id="charset">
     160      <%
     161      String currentCharset = Values.getString(file.getCharacterSet(), "UTF-8");
     162      int numCommon = Config.getCommonCharsets().size();
     163      int i = 0;
     164      boolean hasSelected = false;
     165      for (String charset : Config.getAllCharsets())
     166      {
     167        // The UTF-32 charset is not working well in browsers and we do not include non-encoding character sets
     168        if (charset.startsWith("UTF-32") || !Charset.forName(charset).canEncode()) continue;
     169        boolean selected = false;
     170        if (!hasSelected)
     171        {
     172          selected = charset.equalsIgnoreCase(currentCharset);
     173          hasSelected |= selected;
     174        }
     175        %>
     176        <option value="<%=charset%>" <%=selected ? "selected" : ""%>
     177          <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%>
     178        <%
     179        i++;
     180      }
     181      if (!hasSelected && currentCharset != null)
     182      {
     183        %>
     184        <option value="<%=currentCharset%>" selected><%=currentCharset%>
     185        <%
     186      }
     187      %>
     188      </select>
     189      <span id="nonUtf8Warning"><img src="../../images/warning.png"
     190        title="The selected character set may not be able to encode all characters"></span>
    127191    </div>
    128192    </form>
Note: See TracChangeset for help on using the changeset viewer.