incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r662835 - /incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
Date Tue, 03 Jun 2008 16:41:43 GMT
Author: cziegeler
Date: Tue Jun  3 09:41:42 2008
New Revision: 662835

URL: http://svn.apache.org/viewvc?rev=662835&view=rev
Log:
Better filter invalid characters when creating node names.

Modified:
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java?rev=662835&r1=662834&r2=662835&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
(original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
Tue Jun  3 09:41:42 2008
@@ -503,17 +503,52 @@
         }
     }
 
+    public static final String ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789_,.-+*#!ยค$%&()=[]?";
+    public static final char REPLACEMENT_CHAR = '_';
+
+    public static String filter(final String nodeName) {
+        final StringBuffer sb  = new StringBuffer();
+        char lastAdded = 0;
+
+        for(int i=0; i < nodeName.length(); i++) {
+            final char c = nodeName.charAt(i);
+            char toAdd = c;
+
+            if (ALLOWED_CHARS.indexOf(c) < 0) {
+                if (lastAdded == REPLACEMENT_CHAR) {
+                    // do not add several _ in a row
+                    continue;
+                }
+                toAdd = REPLACEMENT_CHAR;
+
+            } else if(i == 0 && Character.isDigit(c)) {
+                sb.append(REPLACEMENT_CHAR);
+            }
+
+            sb.append(toAdd);
+            lastAdded = toAdd;
+        }
+
+        if (sb.length()==0) {
+            sb.append(REPLACEMENT_CHAR);
+        }
+
+        return sb.toString();
+    }
+
     /**
      * Create a unique node name for the job.
      */
     protected String getNodeName(Event event) {
         final String jobId = (String)event.getProperty(EventUtil.PROPERTY_JOB_ID);
+        final String name;
         if ( jobId != null ) {
-            final String jobTopic = ((String)event.getProperty(EventUtil.PROPERTY_JOB_TOPIC)).replace('/',
'.');
-            return jobTopic + " " + jobId.replace('/', '.');
+            final String jobTopic = ((String)event.getProperty(EventUtil.PROPERTY_JOB_TOPIC));
+            name = jobTopic + " " + jobId;
+        } else {
+            name = "Job " + UUID.randomUUID().toString();
         }
-
-        return "Job " + UUID.randomUUID().toString();
+        return filter(name);
     }
 
     /**



Mime
View raw message