org.chromium.sdk/src/org/chromium/sdk/internal/tools/v8/request/DebuggerMessage.java
changeset 2 e4420d2515f1
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.sdk.internal.tools.v8.request;
       
     6 
       
     7 import java.io.IOException;
       
     8 import java.io.Writer;
       
     9 import java.util.HashMap;
       
    10 import java.util.LinkedHashMap;
       
    11 import java.util.Map;
       
    12 
       
    13 import org.json.simple.JSONStreamAware;
       
    14 import org.json.simple.JSONValue;
       
    15 
       
    16 /**
       
    17  * Represents a generic JSONStreamAware V8 request message (so that it can
       
    18  * serialize itself into JSON.)
       
    19  */
       
    20 public class DebuggerMessage implements JSONStreamAware {
       
    21 
       
    22   private final int sequence;
       
    23 
       
    24   private final String command;
       
    25 
       
    26   private final Map<String, Object> arguments = new HashMap<String, Object>();
       
    27 
       
    28 
       
    29   public DebuggerMessage(String command) {
       
    30     this.sequence = SeqGenerator.getInstance().next();
       
    31     this.command = command;
       
    32   }
       
    33 
       
    34   public Integer getSeq() {
       
    35     return sequence;
       
    36   }
       
    37 
       
    38   public String getType() {
       
    39     return V8MessageType.REQUEST.value;
       
    40   }
       
    41 
       
    42   public String getCommand() {
       
    43     return command;
       
    44   }
       
    45 
       
    46   public Map<String, Object> getArguments() {
       
    47     return arguments;
       
    48   }
       
    49 
       
    50   protected final void putArgument(String key, Object object) {
       
    51     if (object != null) {
       
    52       arguments.put(key, object);
       
    53     }
       
    54   }
       
    55 
       
    56   private final void putArgumentString(String key, Object object) {
       
    57     arguments.put(key, object.toString());
       
    58   }
       
    59 
       
    60   protected final void putArgumentStringIfNotNull(String key, Object object) {
       
    61     if (object != null) {
       
    62       putArgumentString(key, object);
       
    63     }
       
    64   }
       
    65 
       
    66   public void writeJSONString(Writer out) throws IOException {
       
    67     LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
       
    68     obj.put("seq", sequence);
       
    69     obj.put("type", V8MessageType.REQUEST.value);
       
    70     obj.put("command", command);
       
    71     if (!arguments.isEmpty()) {
       
    72       obj.put("arguments", arguments);
       
    73     }
       
    74     JSONValue.writeJSONString(obj, out);
       
    75   }
       
    76 }