com.polyvi.xface.extension.XExtensionResult.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.XExtensionResult.java

Source

/*
 This file was modified from or inspired by Apache Cordova.
    
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you 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.polyvi.xface.extension;

import org.json.JSONArray;
import org.json.JSONObject;

import com.polyvi.xface.util.XBase64;

public class XExtensionResult {

    private final int mStatus;
    /** < js??? */
    private final int messageType;
    private String mMessage;
    /** < js???? */
    private boolean mKeepCallBack;
    /** < js??js?? */
    private String mEncodedMessage;

    public static final int MESSAGE_TYPE_STRING = 1;
    public static final int MESSAGE_TYPE_JSON = 2;
    public static final int MESSAGE_TYPE_NUMBER = 3;
    public static final int MESSAGE_TYPE_BOOLEAN = 4;
    public static final int MESSAGE_TYPE_NULL = 5;
    public static final int MESSAGE_TYPE_ARRAYBUFFER = 6;
    public static final int MESSAGE_TYPE_BINARYSTRING = 7;

    // js???
    public enum Status {
        NO_RESULT, PROGRESS_CHANGING, OK, CLASS_NOT_FOUND_EXCEPTION, ILLEGAL_ACCESS_EXCEPTION, INSTANTIATION_EXCEPTION, MALFORMED_URL_EXCEPTION, IO_EXCEPTION, INVALID_ACTION, JSON_EXCEPTION, ERROR
    };

    // js????
    public static String[] StatusMessages = new String[] { "No Result", "Progress Changing", "OK",
            "Class Not Found", "Illegal access", "Instantiation", "Malformed", "IO error", "Invalid action",
            "JSON error", "Error" };

    /**
     * 
     *
     * @param status
     *            ?
     */
    public XExtensionResult(Status status) {
        this(status, XExtensionResult.StatusMessages[status.ordinal()]);
    }

    /**
     * 
     *
     * @param status
     *            ?
     * @param message
     *            ?
     */
    public XExtensionResult(Status status, JSONArray message) {
        this.mStatus = status.ordinal();
        this.messageType = MESSAGE_TYPE_JSON;
        this.mEncodedMessage = message.toString();
    }

    /**
     * 
     *
     * @param status
     *            ?
     * @param message
     *            ?
     */
    public XExtensionResult(Status status, JSONObject message) {
        this.mStatus = status.ordinal();
        this.messageType = MESSAGE_TYPE_JSON;
        this.mEncodedMessage = message.toString();
    }

    /**
     * 
     *
     * @param status
     *            ?
     * @param message
     *            ?
     */
    public XExtensionResult(Status status, String message) {
        this.mStatus = status.ordinal();
        this.messageType = message == null ? MESSAGE_TYPE_NULL : MESSAGE_TYPE_STRING;
        this.mMessage = message;
    }

    public XExtensionResult(Status status, int i) {
        this.mStatus = status.ordinal();
        this.messageType = MESSAGE_TYPE_NUMBER;
        this.mEncodedMessage = "" + i;
    }

    public XExtensionResult(Status status, float f) {
        this.mStatus = status.ordinal();
        this.messageType = MESSAGE_TYPE_NUMBER;
        this.mEncodedMessage = "" + f;
    }

    public XExtensionResult(Status status, boolean b) {

        this.mStatus = status.ordinal();
        this.messageType = MESSAGE_TYPE_BOOLEAN;
        this.mEncodedMessage = Boolean.toString(b);
    }

    public XExtensionResult(Status status, byte[] data) {
        this(status, data, false);
    }

    public XExtensionResult(Status status, byte[] data, boolean binaryString) {
        this.mStatus = status.ordinal();
        this.messageType = binaryString ? MESSAGE_TYPE_BINARYSTRING : MESSAGE_TYPE_ARRAYBUFFER;
        this.mEncodedMessage = XBase64.encodeToString(data, XBase64.NO_WRAP);
    }

    /**
     * ?
     * 
     * @return
     */
    public String getMessage() {
        if (mEncodedMessage == null) {
            mEncodedMessage = JSONObject.quote(mMessage);
        }
        return this.mEncodedMessage;
    }

    /**
     *  messageType == MESSAGE_TYPE_STRING, ?. ? null
     */
    public String getStrMessage() {
        return mMessage;
    }

    public int getMessageType() {
        return messageType;
    }

    /**
     * ???js
     * 
     * @return
     */
    public boolean getKeepCallback() {
        return this.mKeepCallBack;
    }

    /**
     * ??js
     * 
     * @param keepCallBack
     */
    public void setKeepCallback(boolean keepCallBack) {
        this.mKeepCallBack = keepCallBack;
    }

    /**
     * jsjson
     * 
     * @return
     */
    public String getJSONString() {
        return "{status:" + this.mStatus + ",message:" + this.getMessage() + ",keepCallback:" + this.mKeepCallBack
                + "}";
    }

    /**
     * ?
     * 
     * @return
     */
    public int getStatus() {
        return mStatus;
    }

    /**
     * js???js?
     *
     * @param callbackId
     *            jsid
     * @return ?js?
     */
    public String toSuccessCallbackString(String callbackId) {
        return "xFace.callbackSuccess('" + callbackId + "'," + this.getJSONString() + ");";
    }

    /**
     * js??js?
     *
     * @param callbackId
     *            jsid
     * @return ?js?
     */
    public String toErrorCallbackString(String callbackId) {
        return "xFace.callbackError('" + callbackId + "'," + this.getJSONString() + ");";
    }

    /**
     * js?js?
     *
     * @param callbackId
     *            jsid
     * @return ?js?
     */
    public String toStatusChangeCallbackString(String callbackId) {
        return "xFace.callbackStatusChanged('" + callbackId + "'," + this.getJSONString() + ");";
    }
}