Commit in lcsim/src/org/lcsim/util/lcio on MAIN
LCIOWriter.java+48-301.7 -> 1.8
Don't write out illegal LCIO collection names (LCSIM-176)

lcsim/src/org/lcsim/util/lcio
LCIOWriter.java 1.7 -> 1.8
diff -u -r1.7 -r1.8
--- LCIOWriter.java	30 Apr 2007 23:40:09 -0000	1.7
+++ LCIOWriter.java	12 Jul 2007 18:01:29 -0000	1.8
@@ -52,8 +52,8 @@
     */
    public LCIOWriter(File file, Collection<String> ignoreCollections) throws IOException
    {
-       this(file);
-       this.ignore = new HashSet(ignoreCollections);
+      this(file);
+      this.ignore = new HashSet(ignoreCollections);
    }
    /**
     * Create a writer for writing LCIO files
@@ -62,8 +62,8 @@
     */
    public LCIOWriter(String file, Collection<String> ignoreCollections) throws IOException
    {
-       this(file);
-       this.ignore = new HashSet(ignoreCollections);
+      this(file);
+      this.ignore = new HashSet(ignoreCollections);
    }
    /**
     * Add an entry to the list of collections to ignore (not write out)
@@ -104,17 +104,21 @@
             Class type = md.getType();
             LCIOBlockHandler bh = manager.handlerForClass(type);
             if (bh == null) log.warning("No handler found for block "+md.getName()+" of class "+type.getName());
-            else if(!ignore.contains(md.getName())) blocks.put(md.getName(),bh.getType());
-         }   
+            else if(!ignore.contains(md.getName()))
+            {
+               if (!isValidCollectionName(md.getName())) throw new IOException("Collection name "+md.getName()+" is invalid for LCIO");
+               blocks.put(md.getName(),bh.getType());
+            }
+         }
          
          out.writeInt(blocks.size());
          for (Map.Entry<String,String> entry : blocks.entrySet() )
          {
-	    if(!ignore.contains(entry.getKey())) 
+            if(!ignore.contains(entry.getKey()))
             {
-		out.writeString(entry.getKey());
-		out.writeString(entry.getValue());
-	    }
+               out.writeString(entry.getKey());
+               out.writeString(entry.getValue());
+            }
          }
          out.writeInt(0);
          out.writeInt(0);
@@ -128,27 +132,27 @@
          {
             LCMetaData md = event.getMetaData(collection);
             Class type = md.getType();
-	    if(!ignore.contains(md.getName())) 
+            if(!ignore.contains(md.getName()))
             {
-		LCIOBlockHandler bh = manager.handlerForClass(type);
-		if (bh != null) bh.writeBlock(writer,collection,md);
-	    }
+               LCIOBlockHandler bh = manager.handlerForClass(type);
+               if (bh != null) bh.writeBlock(writer,collection,md);
+            }
          }
       }
    }
    private void writeData(LCIORunHeader header) throws IOException
    {
-         SIOOutputStream out = writer.createBlock(LCIOConstants.runBlockName, LCIOConstants.MAJORVERSION, LCIOConstants.MINORVERSION);
-         out.writeInt(header.getRunNumber());
-         out.writeString(header.getDetectorName());
-         out.writeString(header.getDescription());
-         String[] active = header.getActiveSubdetectors();
-         out.writeInt(active.length);
-         for (int i=0; i<active.length; i++) out.writeString(active[i]);
-         out.writeInt(0);
-         out.writeInt(0);
-         out.writeInt(0);
-         out.close();
+      SIOOutputStream out = writer.createBlock(LCIOConstants.runBlockName, LCIOConstants.MAJORVERSION, LCIOConstants.MINORVERSION);
+      out.writeInt(header.getRunNumber());
+      out.writeString(header.getDetectorName());
+      out.writeString(header.getDescription());
+      String[] active = header.getActiveSubdetectors();
+      out.writeInt(active.length);
+      for (int i=0; i<active.length; i++) out.writeString(active[i]);
+      out.writeInt(0);
+      out.writeInt(0);
+      out.writeInt(0);
+      out.close();
    }
    public void write(EventHeader event) throws IOException
    {
@@ -173,32 +177,46 @@
    {
       private final int run;
       private final String name;
-      private final static String[] noDetectors = new String[0]; 
+      private final static String[] noDetectors = new String[0];
       DefaultRunHeader(int run, String name)
       {
          this.run = run;
          this.name = name;
       }
-
+      
       public String[] getActiveSubdetectors()
       {
          return noDetectors;
       }
-
+      
       public String getDescription()
       {
          return "";
       }
-
+      
       public String getDetectorName()
       {
          return name;
       }
-
+      
       public int getRunNumber()
       {
          return run;
       }
       
    }
+   private boolean isValidCollectionName(String name)
+   {
+      int len = name.length() ;
+      if(name.length()==0) return false ;
+      char c0 = name.charAt(0) ;      
+      if (!Character.isLetter(c0) && c0 != '_') return false;
+      
+      for (int i=1; i< len; i++)
+      {
+         char c = name.charAt(i) ;
+         if (!Character.isLetterOrDigit(c0) && c != '_' ) return false ;
+      }
+      return true ;
+   }
 }
CVSspam 0.2.8