You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by re...@apache.org on 2014/07/23 03:44:07 UTC

svn commit: r1612741 - in /incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap: DeviceMapClient.java data/DeviceType.java data/Pattern.java loader/parser/JsonParser.java

Author: rezan
Date: Wed Jul 23 01:44:06 2014
New Revision: 1612741

URL: http://svn.apache.org/r1612741
Log:
toString() outputs JSON

Added:
    incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loader/parser/JsonParser.java
Modified:
    incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java
    incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/DeviceType.java
    incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java

Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java?rev=1612741&r1=1612740&r2=1612741&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java Wed Jul 23 01:44:06 2014
@@ -125,7 +125,7 @@ public class DeviceMapClient {
                     hits.put(pattern, dlist);
 
                     for (DeviceType device : dlist) {
-                        Util.debugLog("Hit found: " + pattern + " => " + device.getId() + " " + device.getPatterns());
+                        Util.debugLog("Hit found: '" + pattern + "' => id: '" + device.getId() + "' " + device.getPatterns());
                     }
                 }
             }

Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/DeviceType.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/DeviceType.java?rev=1612741&r1=1612740&r2=1612741&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/DeviceType.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/DeviceType.java Wed Jul 23 01:44:06 2014
@@ -20,6 +20,7 @@ package org.apache.devicemap.data;
 
 import java.util.Collections;
 import java.util.Map;
+import org.apache.devicemap.loader.parser.JsonParser;
 
 public class DeviceType {
 
@@ -39,11 +40,17 @@ public class DeviceType {
 
     @Override
     public String toString() {
-        return "id='" + id + "',"
-                + "parentId='" + parentId + "',"
-                + "type='" + type + "',"
-                + "pattern=" + pattern + ","
-                + "attributes=" + attributes + ".";
+        StringBuilder sb = new StringBuilder();
+        
+        sb.append('{');
+        sb.append(JsonParser.outputKeyValue("id",id)).append(',');
+        sb.append(JsonParser.outputKeyValue("parentId",parentId)).append(',');
+        sb.append(JsonParser.outputKeyValue("type",type)).append(',');
+        sb.append(JsonParser.outputString("pattern")).append(':').append(pattern.toString()).append(',');
+        sb.append(JsonParser.outputString("attributes")).append(':').append(JsonParser.outputMap(attributes));
+        sb.append('}');
+        
+        return sb.toString();
     }
 
     public String getId() {

Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java?rev=1612741&r1=1612740&r2=1612741&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java Wed Jul 23 01:44:06 2014
@@ -21,6 +21,7 @@ package org.apache.devicemap.data;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import org.apache.devicemap.loader.parser.JsonParser;
 
 public class Pattern {
 
@@ -34,7 +35,11 @@ public class Pattern {
 
     @Override
     public String toString() {
-        return patterns.toString();
+        List<String> orPatterns = new ArrayList<String>();
+        for(List<String> andPattern:patterns) {
+            orPatterns.add(JsonParser.outputList(andPattern,true));
+        }
+        return JsonParser.outputList(orPatterns,false);
     }
 
     /*

Added: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loader/parser/JsonParser.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loader/parser/JsonParser.java?rev=1612741&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loader/parser/JsonParser.java (added)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loader/parser/JsonParser.java Wed Jul 23 01:44:06 2014
@@ -0,0 +1,73 @@
+/*
+ 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 org.apache.devicemap.loader.parser;
+
+import java.util.List;
+import java.util.Map;
+
+public class JsonParser {
+
+    public static String outputString(String s) {
+        if (s == null) {
+            return "null";
+        }
+        return "\"" + s.replace("\"", "\\\"").replace("\n", " ") + "\"";
+    }
+
+    public static String outputKeyValue(String key, String value) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(outputString(key));
+        sb.append(':');
+        sb.append(outputString(value));
+        return sb.toString();
+    }
+
+    public static String outputMap(Map<String, String> map) {
+        StringBuilder sb = new StringBuilder();
+        for (String key : map.keySet()) {
+            String value = map.get(key);
+            if (sb.length() == 0) {
+                sb.append('{');
+            } else {
+                sb.append(',');
+            }
+            sb.append(outputKeyValue(key, value));
+        }
+        sb.append('}');
+        return sb.toString();
+    }
+
+    public static String outputList(List<String> list, boolean format) {
+        StringBuilder sb = new StringBuilder();
+        for (String value : list) {
+            if (sb.length() == 0) {
+                sb.append('[');
+            } else {
+                sb.append(',');
+            }
+            if(format) {
+                sb.append(outputString(value));
+            } else {
+                sb.append(value);
+            }
+        }
+        sb.append(']');
+        return sb.toString();
+    }
+}