Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.ursaj.hfs.server;
 
 
Utility class for communication with JS upload library.
 
 public final class HfsUploadLibrary {
    
JS upload library resource path.
 
     public static final String UPLOAD_LIBRARY_PATH = "/hfs/upload.js";

    
ASCII symbol's mapping to quote string into JS string.
 
     private static final String[] MAPPING = new String[0x7F];
 
     static {
         for (char i = 0x20; i < .i++)
             [i] = Character.toString(i);
 
         ['\b'] = "\\b";
         ['\t'] = "\\t";
         ['\f'] = "\\f";
         ['\n'] = "\\n";
         ['\r'] = "\\r";
         ['\''] = "\\'";
         ['\\'] = "\\\\";
     }

    
JS upload library content.
 
     private static String cached;

    
Utility class constructor should never be called.
 
     private HfsUploadLibrary() {
         throw new AssertionError("This constructor should never be called.");
     }

    
Quote string into serialized JS string.

Parameters:
str String to quote.
Returns:
Serialized JS string.
 
     public static String quote(String str) {
         if (str == null)
             return "null";
 
         int size = str.length();
 
         StringBuilder builder = new StringBuilder(size << 1 + 2).append('\'');
 
         for (char ch : str.toCharArray()) {
             String mapped = ch < . ? [ch] : null;
 
             if (mapped == null)
                 builder.append("\\u").append(Integer.toHexString(0x10000 | ch), 1, 5);
             else
                 builder.append(mapped);
         }
 
         return builder.append('\'').toString();
     }

    
Render HTML content of the upload result frame, compatible with JS upload library.

Parameters:
code Response code.
js JS object to send to client, e.g. "{ message: 'OK', files: [ { id: 'id1', name: 'name1.txt' } ]"
Returns:
Content of the JS-library upload result frame.
Throws:
java.io.IOException In case of IO exception
 
     public static String renderUploadFrame(int codeString jsthrows IOException {
         return "<html>\n"
             + "<head><title>HFS Upload Result</title></head>\n"
             + "<body><script type='text/javascript'>\n"
             + "    /* Provide upload result from this frame into parent document. */\n"
             + "    window.parent.hfs.Uploader.complete(" + code + ", " + js + ", window);\n"
             + "</script></body>\n"
             + "</html>";
     }

    
Gets JS upload library content.

Returns:
JS upload library content.
Throws:
java.io.IOException In case of IO exception
    public static String getJsLib() throws IOException {
        if ( != null)
            return ;
        synchronized (HfsUploadLibrary.class) {
            if ( != null)
                return ;
            if (in == null)
                throw new IOException("Resource not found: " + );
             = new String(HfsUtils.getContent(in, 1 << 20), .);
        }
        return ;
    }
New to GrepCode? Check out our FAQ X