【问题标题】:String manipulation for VML Path 2VML 路径 2 的字符串操作
【发布时间】:2014-06-19 06:57:46
【问题描述】:

我正在尝试解析 VML 路径值(这篇文章与 String manipulation for VML Path 不同)。 这有一个更复杂的路径值需要处理。 我有这个字符串值

m@0,l@0@0,0@0,0@2@0@2@0,21600@1,21600@1@2,21600@2,21600@0@1@0@1,250,350,450,@5,xe

我在这里有四个命令 m、l、x 和 e。以下总结了我的目标。

m@0,
l
  @0 @0
  0 @0
  0 @2
  @0 @2
  @0,21600
  @1,21600
  @1@2
  21600@2
  21600@0
  @1@0
  @1,250
  350,450
  @5,
x
e

在代码中,我认为下面是一个很好的表示。

String command_type = "m"        List<String, String> parameters =   add("@0", "0")  // because the y parameter is not specified I need to force it to 0
String command_type = "l"        List<String, String> parameters =   add("@0", "@0")
                                                                     add("0", "@0")
                                                                     add("0", "@2")
                                                                     add("@0", "@2")
                                                                     add("@0", "216000")
                                                                     add("@1", "216000")
                                                                     add("@1", "@2")
                                                                     add("21600", "@2")
                                                                     add("21600", "@0")
                                                                     add("@1", "@0")
                                                                     add("@1", "250")
                                                                     add("350", "450")
                                                                     add("@5", "0") //because the y parameter is not specified I need to force it to 0
String command_type = "x"        (can have no parameter )
String command_type = "e"        (can have no parameter )

这是我从 VML 中注意到的事情。

  1. 命令前面没有逗号 ,(适用于所有 vml 命令,除了第一个命令 m

  2. 参数成对出现(x 和 y 对)

我看到了这个链接Java String.split() Regex,也许可以使用正则表达式来检索参数。

【问题讨论】:

    标签: java string string-matching stringtokenizer vml


    【解决方案1】:

    更新 此函数/方法仅适用于每个命令(如 m、l 和其他 vml 命令)。如果您想检索所有命令的所有参数,请实现您自己的方法,该方法使用 vml 参数遍历所有 vml 命令。

    这是我的问题的解决方案。足以满足我的需要。如果您有更多建议或更好的算法,请分享。谢谢:-)

    private List<List<String>> createStructuredParameterArray(String vmlParameter){
    
            int totalParameterCount = 0;
    
            String currentXorYParameter = "";
            List<String> listOfParameters = new ArrayList<String>();
            boolean previousIsComma = false;
            boolean firstRun = true;
            for(int i = 0; i < vmlParameter.length(); i++){
                if (i != 0)
                    firstRun = false;
                char nextChar = vmlParameter.charAt(i);
                if (nextChar == '@'){
                    /**
                     * check the currentXorYParameter if it is a valid format already, 
                     * <br> if True - then proceed to handling the next parameter 
                     * <br> ex. from handling X parameter to Y parameter
                     * <br> ex. from handling Y parameter to X parameter  
                     */
                    if (checkIfValidSingleParameter(currentXorYParameter)){
                        totalParameterCount++;
                        listOfParameters.add(currentXorYParameter);
                        currentXorYParameter = "";
                        currentXorYParameter= currentXorYParameter+nextChar;    
                    }else{
                        currentXorYParameter= currentXorYParameter+nextChar;    
                    }
                    previousIsComma = false;
                }else if (nextChar != '@' && nextChar != ','){ //numeric character
                    currentXorYParameter= currentXorYParameter+nextChar;
                    previousIsComma = false;
                }else if (nextChar == ','){
                    if (checkIfValidSingleParameter(currentXorYParameter)){
                        totalParameterCount++;
                        listOfParameters.add(currentXorYParameter);
                        currentXorYParameter = "";
                    }else{
                        if (firstRun){
                            totalParameterCount++;
                            currentXorYParameter = "0";
                            listOfParameters.add(currentXorYParameter);
                            currentXorYParameter = "";
                            firstRun = false;
                        }
                    }
                    if (previousIsComma){
                        totalParameterCount++;
                        currentXorYParameter = "0";
                        listOfParameters.add(currentXorYParameter);
                        currentXorYParameter = "";
                    }
                    previousIsComma = true;
                }
                if ((i+1)==vmlParameter.length()){
                    if (checkIfValidSingleParameter(currentXorYParameter)){
                        totalParameterCount++;
                        listOfParameters.add(currentXorYParameter);
                        currentXorYParameter = "";
                        /**
                         * check if parameterCounter is odd or even, 
                         * <br> if odd  - add additional "0" to make it even
                         * <br> if even - do nothing
                         */
                        if ((totalParameterCount % 2)==1){
                            listOfParameters.add("0");
                        }
                    }
                }
            }
            if ((listOfParameters.size()%2)==1){
                listOfParameters.add("0");
            }
            /**
             * Group the parameters by 2 in a List<String> object
             * <br>List<String> object
             *      <bR> object.add("\<value here\>"); => for x coordinate
             *      <bR> object.add("\<value here\>"); => for y coordinate
             * 
             * <br>Then create a list of all List<String> object created from above
             * <br> uncomment below if the desired result return value is as described above 
             */
            List<List<String>> result = new ArrayList<List<String>>();
            List<String> temp = new ArrayList<String>();
            if(listOfParameters.size()>1){
                int counter = 1;
                for (int i = 0; i<listOfParameters.size(); i++){
                    String xyCoordinate = listOfParameters.get(i);
                    if ((counter%2)==1){ //X coordinate
                        temp.add(xyCoordinate);
                    }else if ((counter%2)==0){ //Y coordinate
                        temp.add(xyCoordinate);
                        result.add(temp);
                        //temp.clear();
                        temp = new ArrayList<String>();
                    }
                    counter++;
                }
            }
    
            return result;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2019-01-08
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多