Example usage for com.google.gwt.json.client JSONValue isString

List of usage examples for com.google.gwt.json.client JSONValue isString

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isString.

Prototype

public JSONString isString() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONString.

Usage

From source file:com.ait.lienzo.client.core.shape.json.JSONDeserializer.java

License:Open Source License

/**
 * Creates a IJSONSerializable from the JSONObject, using the ValidationContext.
 * <p>/* ww w  .j  a v  a  2 s .  c om*/
 * You should only call this when you're writing your own node class
 * and you're building a custom {@link IFactory}.
 * 
 * @param json JSONObject
 * @param ctx ValidationContext
 * @return IJSONSerializable
 * @throws ValidationException
 */
public final IJSONSerializable<?> fromJSON(JSONObject json, ValidationContext ctx) throws ValidationException {
    if (null == json) {
        return null;
    }
    String type = null;

    IFactory<?> factory = null;

    JSONValue tval = json.get("type");

    ctx.push("type");

    if (null == tval) {
        ctx.addRequiredError();
    } else {
        JSONString styp = tval.isString();

        if (null == styp) {
            ctx.addBadTypeError("String");
        } else {
            type = styp.stringValue();

            if (null == m_registry) {
                m_registry = FactoryRegistry.getInstance();
            }
            factory = m_registry.getFactory(type);

            if (null == factory) {
                ctx.addMissingNodeFactoryError(type);
            }
        }
    }
    ctx.pop(); // type

    if (null == factory) {
        return null;
    } else {
        if (ctx.isValidate()) {
            // we don't need to validate during a copy operation!

            validateAttributes(json, factory, type, ctx);
        }
        if (factory.isPostProcessed()) {
            IJSONSerializable<?> node = factory.create(json, ctx);

            if (null != node) {
                factory.process(node, ctx);
            }
            return node;
        } else {
            return factory.create(json, ctx);
        }
    }
}

From source file:com.ait.lienzo.client.core.shape.json.validators.ColorValidator.java

License:Open Source License

@Override
public void validate(JSONValue jval, ValidationContext ctx) throws ValidationException {
    if (null == jval) {
        ctx.addBadTypeError("Color");

        return;//ww w . j av a  2  s. c  o m
    }
    JSONString s = jval.isString();

    if (null == s) {
        ctx.addBadTypeError("Color");

        return;
    }

    // see http://www.w3.org/TR/css3-color/
    // "#00f", "#0f0f0f", "#00F", "#0F0F0F", "rgb(255,0,0)", "rgba(0,0,0,0)", "red",
    // "rgb(100%, 0%, 0%)", "hsl(0, 100%, 50%)", "hsla(120, 100%, 50%, 1)",
    // "transparent", "inherit", "currentcolor"

    // White space characters are allowed around the numerical values.
    // Alpha should be between 0.0 and 1.0 inclusive
    // All color names are a single word (no spaces or special characters)
    // Color names are case-insensitive.

    if (false == isValidColorName(s.stringValue())) {
        ctx.addBadValueError("Color", jval);
    }
}

From source file:com.ait.lienzo.client.core.shape.json.validators.EnumValidator.java

License:Open Source License

@Override
public void validate(JSONValue jval, ValidationContext ctx) throws ValidationException {
    if (null == jval) {
        ctx.addBadTypeError(m_type);//from   ww  w  . ja  v a  2 s  .c  om

        return;
    }
    JSONString sval = jval.isString();

    if (null == sval) {
        ctx.addBadTypeError(m_type);
    } else {
        String string = sval.stringValue();

        if (null != string) {
            for (T value : m_values) {
                if (string.equals(value.getValue())) {
                    return;
                }
            }
        }
        ctx.addBadValueError(m_type, jval);
    }
}

From source file:com.ait.lienzo.client.core.shape.json.validators.ObjectValidator.java

License:Open Source License

protected void checkHardcodedAttribute(String attrName, String requiredAttrValue, JSONValue jval,
        ValidationContext ctx) throws ValidationException {
    // ASSUMPTION: requiredness was already checked and reported on

    JSONObject jobj = jval.isObject();/*from  w  ww  .  j a  v  a  2 s  .c om*/

    if (null != jobj) {
        JSONValue aval = jobj.get(attrName);

        if (null != aval) {
            JSONString s = aval.isString();

            if ((null == s) || (false == requiredAttrValue.equals(s.stringValue()))) {
                ctx.push(attrName);

                ctx.addRequiredAttributeValueError(requiredAttrValue);

                ctx.pop(); // attrName
            }
        }
    }
}

From source file:com.ait.lienzo.client.core.shape.json.validators.StringValidator.java

License:Open Source License

@Override
public void validate(JSONValue jval, ValidationContext ctx) throws ValidationException {
    if (null == jval) {
        ctx.addBadTypeError("String");

        return;/*from ww  w.j  av  a2 s.c  o m*/
    }
    JSONString s = jval.isString();

    if (null == s) {
        ctx.addBadTypeError("String");
    }
}

From source file:com.ait.lienzo.client.core.shape.json.validators.URLValidator.java

License:Open Source License

@Override
public void validate(JSONValue jval, ValidationContext ctx) throws ValidationException {
    if (null == jval) {
        ctx.addBadTypeError("URL");

        return;//from  www. j  av  a  2s.  c om
    }
    JSONString str = jval.isString();

    if (null == str) {
        ctx.addBadTypeError("URL");

        return;
    }
    String url = str.toString();

    if (url.startsWith("data:")) {
        return;
    }
    if ((null == url) || ((url = url.trim()).isEmpty()) || (url.startsWith("#"))) {
        ctx.addBadTypeError("URL");

        return;
    }
    url = UriUtils.fromString(url).asString();

    if ((null == url) || ((url = url.trim()).isEmpty()) || (url.startsWith("#"))) {
        ctx.addBadTypeError("URL");

        return;
    }
}

From source file:com.akanoo.client.dto.MessageCreator.java

License:Apache License

@Override
public Message newInstance(JSONValue context) {
    Message message = null;/*from   w  w w .  java2s.  c o m*/
    JSONObject jsonObject = context.isObject();
    if (jsonObject != null) {
        JSONValue codeValue = jsonObject.get("code");
        JSONValue bodyValue = jsonObject.get("body");
        if (codeValue != null && bodyValue != null) {
            JSONString codeJSONString = codeValue.isString();
            JSONObject bodyJSONValue = bodyValue.isObject();
            if (codeJSONString != null && bodyJSONValue != null) {
                String codeString = codeJSONString.stringValue();

                MessageConstants messageConstants = MessageConstants.fromString(codeString);

                if (messageConstants != null) {
                    // initialize message
                    message = new Message();
                    message.code = codeString;

                    JsonReader<? extends Sendable> reader = readerMap.get(messageConstants);
                    message.body = reader.read(bodyJSONValue);
                }
            }
        }
    }
    return message;
}

From source file:com.arcbees.analytics.client.ClientAnalytics.java

License:Apache License

public void fallback(JsArrayMixed arguments) {
    if ("send".equals(arguments.getString(0))) {
        JSONObject jsonOptions = new JSONObject(arguments.getObject(arguments.length() - 1));
        StringBuilder url = new StringBuilder();
        url.append(fallbackPath).append("?");
        url.append(ProtocolTranslator.getFieldName("hitType")).append("=")
                .append(URL.encodeQueryString(arguments.getString(1)));

        for (String key : jsonOptions.keySet()) {
            if (!"hitCallback".equals(key)) {
                JSONValue jsonValue = jsonOptions.get(key);
                String strValue = "";
                if (jsonValue.isBoolean() != null) {
                    strValue = jsonValue.isBoolean().booleanValue() + "";
                } else if (jsonValue.isNumber() != null) {
                    strValue = jsonValue.isNumber().doubleValue() + "";
                } else if (jsonValue.isString() != null) {
                    strValue = jsonValue.isString().stringValue();
                }/* www  .  ja v  a 2 s. c om*/
                url.append("&").append(ProtocolTranslator.getFieldName(key)).append("=")
                        .append(URL.encodeQueryString(strValue));
            }
        }
        try {
            RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url.toString());
            requestBuilder.setCallback(new RequestCallback() {

                @Override
                public void onResponseReceived(Request request, Response response) {
                    // TODO call hitcallback if needed.
                }

                @Override
                public void onError(Request request, Throwable exception) {
                    // TODO Auto-generated method stub
                }
            });
            requestBuilder.send();
        } catch (RequestException e) {
        }
    }
}

From source file:com.blockwithme.util.client.webworkers.impl.AbstractWebWorkerImpl.java

License:Apache License

@Override
public final void handleEvent(final elemental.events.Event evt) {
    if (evt instanceof MessageEvent) {
        final Object data = ((MessageEvent) evt).getData();
        if (data == null) {
            logger.severe("got MessageEvent with null data");
        } else {//from ww  w  .j a  va 2  s  .  c  o m
            try {
                JSONObject message = new JSONObject((JavaScriptObject) data);
                if (message.isObject() == null) {
                    logger.log(Level.SEVERE,
                            "Problem parsing data: " + stringify(data) + " : data not a JSONObject");
                } else {
                    if (message.containsKey("jsObject")) {
                        final JSONValue jsObject = message.get("jsObject");
                        if (jsObject.isObject() != null) {
                            message = jsObject.isObject();
                        } else {
                            logger.log(Level.SEVERE, "Problem parsing data: " + stringify(data)
                                    + " : data.jsObject not a JSONObject");
                            message = null;
                        }
                    }
                    if (message != null) {
                        // Get and remove channel at the same time
                        final JSONValue channel = message.put("_channel_", null);
                        String ch = "";
                        if ((channel != null) && (channel != JSONNull.getInstance())) {
                            final JSONString str = channel.isString();
                            if (str != null) {
                                // Cannot be null
                                ch = str.stringValue();
                            } else {
                                ch = stringify(channel);
                            }
                        }
                        if ("java.util.logging".equals(ch)) {
                            final LogRecord record = WebWorkerLogHandler.fromJSONObject(message);
                            if (record != null) {
                                setThreadName(record, name);
                                Logger.getLogger(record.getLoggerName()).log(record);
                            }
                        } else if ("set.thread.name".equals(ch)) {
                            final String name = message.get("value").isString().stringValue();
                            setThreadName(Thread.currentThread(), name);
                        } else {
                            listener.onMessage(ch, message);
                        }
                    }
                }
            } catch (final Throwable t) {
                logger.log(Level.SEVERE, "Problem parsing data: " + stringify(data), t);
            }
        }
    } else if (evt instanceof ErrorEvent) {
        final ErrorEvent errEvt = (ErrorEvent) evt;
        final String filename = errEvt.getFilename();
        final int lineno = errEvt.getLineno();
        final String message = errEvt.getMessage();
        logger.severe(filename + ":" + lineno + ": " + message);
    } else if (evt != null) {
        logger.severe("Cannot handle message of type " + evt.getClass() + " : " + stringify(evt));
    }
}

From source file:com.blockwithme.util.client.webworkers.thread.impl.WebWorkerLogHandler.java

License:Apache License

/** Reads LogRecord from a JSONObject. */
public static LogRecord fromJSONObject(final JSONObject json) {
    if (json == null) {
        return null;
    }//from  w  ww .  jav a2  s  .  co m
    Level level = Level.INFO;
    String loggerName = "";
    String message = null;
    long millis = 0;
    String thrown_type = null;

    final JSONValue levelValue = json.get("level");
    if ((levelValue != null) && (levelValue.isString() != null)) {
        try {
            level = Level.parse(levelValue.isString().stringValue());
        } catch (final Throwable t) {
            // NOP
        }
    }
    final JSONValue loggerNameValue = json.get("loggerName");
    if ((loggerNameValue != null) && (loggerNameValue.isString() != null)) {
        loggerName = loggerNameValue.isString().stringValue();
    }
    final JSONValue messageValue = json.get("message");
    if ((messageValue != null) && (messageValue.isString() != null)) {
        message = messageValue.isString().stringValue();
    }
    final JSONValue millisValue = json.get("millis");
    if ((millisValue != null) && (millisValue.isNumber() != null)) {
        millis = (long) millisValue.isNumber().doubleValue();
    }
    final JSONValue thrown_typeValue = json.get("thrown_type");
    if ((thrown_typeValue != null) && (thrown_typeValue.isString() != null)) {
        thrown_type = thrown_typeValue.isString().stringValue();
    }

    final LogRecord result = new LogRecord(level, message);
    result.setLoggerName(loggerName);
    result.setMillis(millis);
    if (thrown_type != null) {
        final JSONValue thrown_msgValue = json.get("thrown_msg");
        String thrown_msg = null;
        if ((thrown_msgValue != null) && (thrown_msgValue.isString() != null)) {
            thrown_msg = thrown_msgValue.isString().stringValue();
        }
        result.setThrown(new JsonLogRecordThrowable(thrown_type, thrown_msg));
    } else if ((message == null) || message.isEmpty()) {
        return null;
    }

    return result;
}