⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafmarshallingcontext.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return new STAFMarshallingContext(map);
        }
        else if (data.startsWith(CONTEXT_MARKER))
        {
            int colonIndex = data.indexOf(':');
            int contextIndex = data.indexOf(':', colonIndex + 1) + 1;
            int contextLength = parseInt(data.substring(colonIndex + 1,
                                                        contextIndex - 1), 0);

            colonIndex = data.indexOf(':', contextIndex);
            int mapIndex = contextIndex;
            int mapDataIndex = data.indexOf(':', colonIndex + 1) + 1;
            int mapLength = parseInt(data.substring(colonIndex + 1,
                                                    mapDataIndex - 1), 0);
            Map contextMap =
                (Map)unmarshall(data.substring(mapIndex,
                                               mapDataIndex + mapLength),
                                context, flags)
                     .getPrimaryObject();
            Map mapClassMap = (Map)contextMap.get(MAP_CLASS_MAP_KEY);
            STAFMarshallingContext newContext =
                               new STAFMarshallingContext(null, mapClassMap);

            colonIndex = data.indexOf(':', mapDataIndex + mapLength);

            int rootObjIndex = mapDataIndex + mapLength;
            int rootObjDataIndex = data.indexOf(':', colonIndex + 1) + 1;
            int rootObjLength = parseInt(data.substring(colonIndex + 1,
                                    rootObjDataIndex - 1), 0);

            newContext.setRootObject(
                unmarshall(data.substring(rootObjIndex,
                                          rootObjDataIndex + rootObjLength),
                           newContext, flags)
                .getPrimaryObject());

            return newContext;
        }
        else if (data.startsWith(MARSHALLED_DATA_MARKER))
        {
            // Here, we don't know what the type is

            return new STAFMarshallingContext(new String(data));
        }
        else
        {
            return new STAFMarshallingContext(new String(data));
        }
    }

    private static int parseInt(String data, int theDefault)
    {
        int theValue = theDefault;

        try
        {
            theValue = Integer.parseInt(data);
        }
        catch (NumberFormatException nfe)
        { /* Do Nothing */ }

        return theValue;
    }

    private static String quoteString(String input)
    {
        if (input.indexOf("'") == -1)
            return "'" + input + "'";

        if (input.indexOf("\"") == -1)
            return "\"" + input + "\"";

        StringTokenizer tokens = new StringTokenizer(input, "'");

        String output = "'" + tokens.nextToken();

        while (tokens.hasMoreTokens())
            output = output + "\'" + tokens.nextToken();

        return output + "'";
    }

    public String toString()
    {
        return formatObject(rootObject, this, 0, 0);
    }

    public String toString(int flags)
    {
        return formatObject(rootObject, this, 0, flags);
    }

    public static String formatObject(Object obj)
    {
        return formatObject(obj, null, 0, 0);
    }

    public static String formatObject(Object obj, int flags)
    {
        return formatObject(obj, null, 0, flags);
    }

    static String formatObject(Object obj, STAFMarshallingContext context,
                               int indentLevel, int flags)
    {
        String lineSep = System.getProperty("line.separator");
        StringBuffer output = new StringBuffer();

        if (obj instanceof List)
        {
            List list = (List)obj;

            output.append("[");

            ++indentLevel;

            if (list.size() > 0) output.append(lineSep);

            // Format each object

            for (Iterator iter = list.iterator(); iter.hasNext();)
            {
                Object thisObj = iter.next();

                if ((thisObj instanceof List) ||
                    (thisObj instanceof Map) ||
                    (thisObj instanceof STAFMarshallingContext))
                {
                    output.append(
                        SPACES.substring(0, indentLevel * INDENT_DELTA));

                    output.append(formatObject(thisObj, context, indentLevel,
                                               flags));
                }
                else
                {
                    output.append(
                        SPACES.substring(0, indentLevel * INDENT_DELTA));

                    if (thisObj == null)
                        output.append(NONE_STRING);
                    else
                        output.append(thisObj.toString());
                }

                if (iter.hasNext()) output.append(ENTRY_SEPARATOR);

                output.append(lineSep);
            }

            --indentLevel;

            if (list.size() > 0)
                output.append(SPACES.substring(0, indentLevel * INDENT_DELTA));

            output.append("]");
        }
        else if (obj instanceof Map)
        {
            Map map = (Map)obj;

            output.append("{");

            ++indentLevel;

            if (map.size() > 0) output.append(lineSep);

            // Check if the map object has a map class key and if the context
            // is valid and contains a map class definition for this map class.
            // If not, treat as a plain map class.

            if (map.containsKey(MAP_CLASS_NAME_KEY) &&
                (context != null) &&
                (context instanceof STAFMarshallingContext) &&
                (context.hasMapClassDefinition(
                    (String)map.get(MAP_CLASS_NAME_KEY))))
            {
                STAFMapClassDefinition mapClass =
                    context.getMapClassDefinition(
                        (String)map.get(MAP_CLASS_NAME_KEY));

                // Determine maximum key length

                int maxKeyLength = 0;

                for (Iterator iter = mapClass.keyIterator(); iter.hasNext();)
                {
                    Map theKey = (Map)iter.next();
                    String theKeyString = (String)theKey.get("key");

                    if (theKey.containsKey(DISPLAY_NAME_KEY))
                        theKeyString = (String)theKey.get(DISPLAY_NAME_KEY);

                    if (theKeyString.length() > maxKeyLength)
                        maxKeyLength = theKeyString.length();
                }

                // Now print each object in the map

                for (Iterator iter = mapClass.keyIterator(); iter.hasNext();)
                {
                    Map theKey = (Map)iter.next();
                    String theKeyString = (String)theKey.get("key");

                    if (theKey.containsKey(DISPLAY_NAME_KEY))
                        theKeyString = (String)theKey.get(DISPLAY_NAME_KEY);

                    output.append(SPACES.substring(0,
                                                   indentLevel * INDENT_DELTA))
                          .append(theKeyString)
                          .append(SPACES.substring(0, maxKeyLength -
                                                   theKeyString.length()))
                          .append(": ");

                    Object thisObj = map.get(theKey.get("key"));

                    if ((thisObj instanceof List) ||
                        (thisObj instanceof Map) ||
                        (thisObj instanceof STAFMarshallingContext))
                    {
                        output.append(
                            formatObject(thisObj, context, indentLevel, flags));
                    }
                    else if (thisObj == null)
                    {
                        output.append(NONE_STRING);
                    }
                    else
                    {
                        output.append(thisObj.toString());
                    }

                    if (iter.hasNext()) output.append(ENTRY_SEPARATOR);

                    output.append(lineSep);
                }
            }
            else
            {
                // Determine maximum key length

                int maxKeyLength = 0;

                for (Iterator iter = map.keySet().iterator(); iter.hasNext();)
                {
                    String theKeyString = (String)iter.next();

                    if (theKeyString.length() > maxKeyLength)
                        maxKeyLength = theKeyString.length();
                }

                // Now print each object in the map

                for (Iterator iter = map.keySet().iterator(); iter.hasNext();)
                {
                    String theKeyString = (String)iter.next();

                    output.append(SPACES.substring(0,
                                                   indentLevel * INDENT_DELTA))
                          .append(theKeyString)
                          .append(SPACES.substring(0, maxKeyLength -
                                                   theKeyString.length()))
                          .append(": ");

                    Object thisObj = map.get(theKeyString);

                    if ((thisObj instanceof List) ||
                        (thisObj instanceof Map) ||
                        (thisObj instanceof STAFMarshallingContext))
                    {
                        output.append(
                            formatObject(thisObj, context, indentLevel, flags));
                    }
                    else if (thisObj == null)
                    {
                        output.append(NONE_STRING);
                    }
                    else
                    {
                        output.append(thisObj.toString());
                    }

                    if (iter.hasNext()) output.append(ENTRY_SEPARATOR);

                    output.append(lineSep);
                }
            }

            --indentLevel;

            if (map.size() > 0)
                output.append(SPACES.substring(0, indentLevel * INDENT_DELTA));

            output.append("}");
        }
        else if (obj instanceof STAFMarshallingContext)
        {
            STAFMarshallingContext inputContext = (STAFMarshallingContext)obj;

            return formatObject(inputContext.getRootObject(), inputContext,
                                indentLevel, flags);
        }
        else if (obj == null) return NONE_STRING;
        else return obj.toString();

        return output.toString();
    }

    // Class data

    private static final String MARSHALLED_DATA_MARKER = new String("@SDT/");
    private static final String NONE_MARKER = new String("@SDT/$0:0:");
    private static final String SCALAR_MARKER = new String("@SDT/$");
    private static final String LIST_MARKER = new String("@SDT/[");
    private static final String MAP_MARKER = new String("@SDT/{");
    private static final String MC_INSTANCE_MARKER = new String("@SDT/%");
    private static final String CONTEXT_MARKER = new String("@SDT/*");
    private static final String NONE_STRING = new String("<None>");
    private static final String DISPLAY_NAME_KEY = new String("display-name");
    private static final String MAP_CLASS_MAP_KEY = new String("map-class-map");
    private static final String MAP_CLASS_NAME_KEY =
        new String("staf-map-class-name");
    private static final String ENTRY_SEPARATOR = new String("");
    // 80 spaces
    private static final String SPACES = new String(
        "                                         " + 
        "                                         ");
    private static final int INDENT_DELTA = 2;

    private Map mapClassMap = new HashMap();
    private Object rootObject = null;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -