001    /*
002     * EntryParser.java
003     *
004     * Created on July 20, 2006, 3:06 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.dataset;
025    
026    import gov.bnl.star.offline.scheduler.catalog.PhysicalFile;
027    import java.util.ArrayList;
028    import java.util.Hashtable;
029    import java.util.List;
030    
031    /**
032     *This class is used to define how a line in the dataset file is to be parsed 
033     * to extract the relevant bits of data. It defines the format of the entries 
034     * in the dataset. How to extract each of the attributes is defined by generic 
035     * JAVA regular expression capture groups.
036     *
037     * @author Levente B. Hajdu
038     */
039    public class EntryParser {
040        
041        /** Creates a new instance of EntryParser */
042        public EntryParser() {}
043        
044        /** Configures the parser to parse out the LFN.
045         *  @param regX : A regular expression having a capture group for the logical file name. */
046        public void addLFN(String regX){ AddValue("LFN",regX, "$1"); }
047        
048        /** Configures the parser to parse out the storage servise (examples: HPSS, NFS, Locla disk).
049         *  @param regX : A regular expression having a capture group for the storage servise. */
050        public void addSTORAGE_SERVISE(String regX){ AddValue("STORAGE_SERVISE",  regX, "$1"); }
051        
052        /** Configures the parser to parse out the site of the entry.
053         *  @param regX : A regular expression having a capture group for the site of the entry.*/
054        public void addSITE(String regX){ AddValue("SITE", regX, "$1"); }
055        
056        /** Configures the parser to parse out the host name of the entry (node name or IP).
057         *  @param regX : A regular expression having a capture group for the host of the entry.*/
058        public void addHOST(String regX){ AddValue("HOST", regX, "$1"); }
059        
060       /** Configures the parser to parse out the path to the entry.
061        *  @param regX : A regular expression having a capture group for the path of the entry.*/
062        public void addPATH(String regX){ AddValue("PATH", regX, "$1"); }
063        
064       /** Configures the parser to parse out the host name of the file.
065        *  @param regX : A regular expression having a capture group for the file name*/
066        public void addFILE_NAME(String regX){ AddValue("FILE_NAME", regX, "$1"); }
067        
068          /** Configures the parser to parse out the host name of the number of events (If given).
069        *  @param regX : A regular expression having a capture group for the number of events in the file*/
070        public void addNUMBER_OF_EVENTS(String regX){ AddValue("NUMBER_OF_EVENTS", regX, "$1"); }
071        
072        
073        
074        
075        
076        /**Parses the entry string 
077         * @return LFN
078         **/
079        public String getLFN(String entry){ return GetValue("LFN", entry); }
080        
081        /**Parses the entry string 
082         * @return storage servise
083         **/
084        public String getSTORAGE_SERVISE(String entry){ return GetValue("STORAGE_SERVISE", entry); }
085        
086        /**Parses the entry string 
087         * @return site of entry 
088         **/
089        public String getSITE(String entry){ return GetValue("SITE", entry); }
090        
091        /**Parses the entry string 
092         * @return hostname of the node on which the entry resides 
093         **/
094        public String getHOST(String entry){ return GetValue("HOST", entry); }
095        
096        /**Parses the entry string 
097         * @return path of the file (can be a local path or physical file path) 
098         **/
099        public String getPATH(String entry){ return GetValue("PATH", entry); }
100        
101        /**Parses the entry string 
102         * @return file name (can be logical flie name or physical file naem)
103         **/
104        public String getFILE_NAME(String entry){ return GetValue("FILE_NAME", entry); }
105        
106        /**Parses the entry string 
107         * @return number of events
108         **/
109        public String getNUMBER_OF_EVENTS(String entry){ return GetValue("NUMBER_OF_EVENTS", entry); }
110        
111        
112        
113    
114        Hashtable regx = new Hashtable();
115        public Hashtable getRegx(){return this.regx; }
116        public void setRegx(Hashtable regx){this.regx = regx;}
117        
118        Hashtable replacment = new Hashtable();
119        public Hashtable getReplacment(){return this.replacment; }
120        public void setReplacment(Hashtable replacment){this.replacment = replacment;}
121        
122        
123        
124        
125        /**
126         *     This function configures the entry parser. It must be configured with regular expressions. Here is an example of how it is configured for the STAR involvement:
127         *
128         *     Basic format example:
129         *
130         *     {FLN}::{STORAGE_SERVISE}::{SITE}::{HOST}::{PATH}::{FILE_NAME}::{NUMBER_OF_EVENTS}
131         *     Typcal entry: 9404397::NFS::BNL::localhost::/star/data41/reco/productionMinBias/ReversedFullField/P05ic/2004/023::st_physics_adc_5023001_raw_1050014.MuDst.root::155
132         *
133         *     AddValue(“LFN”,              "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$1”);
134         *     AddValue(“STORAGE_SERVISE”,  "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$2”);
135         *     AddValue(“SITE”,             "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$3”);
136         *     AddValue(“HOST”,             "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$4”);
137         *     AddValue(“PATH”,             "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$5”);
138         *     AddValue(“FILE_NAME”,        "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$6”);
139         *     AddValue(“NUMBER_OF_EVENTS”, "([0-9]*)::([a-zA-Z]*)::([a-zA-Z]*)::([a-zA-Z0-9:.]*)::([^:]*)::([^:/]*)::([0-9]*)(.*)" , “$7”);
140         *
141         *
142         *     @param name  The name of this value. Examples are: LFN, STORAGE_SERVISE, SITE, HOST, PATH,  FILE_NAME, NUMBER_OF_EVENTS 
143         *     @param regX A regular expression having a capture group for this item.
144         *     @param replament A string with the capture group(s) of interest. For example in the above example if the path with the file name 
145         *                 was wanted as one string "$5/$6" would recover the 5th and 6th apture groups with the slash in between.
146         *
147         *      Note: This should be configured in the XML configuration file. 
148         */
149        private void AddValue(String name, String regX, String replament) { 
150            this.regx.put(name, regX); 
151            this.replacment.put(name, replament); 
152        }
153        
154        
155        /**  
156         * This class parses the entry. If there is an error and the entry is not matched, null is returned.
157         *
158         *  @param entry The entry string
159         *  @name name of the value that was configured
160         *
161         */
162        String GetValue(String name, String entry){     
163            if(! regx.containsKey(name)) throw new RuntimeException("There was a dataset EntryParser config error. The value name=\"" + name + "\" was called however this value was not configured.");
164            String value = entry.replaceAll(((String) regx.get(name)), ((String) replacment.get(name))); //Its a little be wastful to do two lookups, however in this case the tables are very small.
165            if( entry.matches(value) ) return null; //if no parsing was done just return null
166            if( entry.matches("") ) return null; //if no parsing was done just return null
167            return value;
168        }
169        
170        
171        
172         /**converts an entry string to a PhysicalFile string*/
173         public PhysicalFile entryToPhysicalFile(String entry){
174    
175            int events = -1;
176            Hashtable hashtableAtrib = new Hashtable();
177            String eventsString = getNUMBER_OF_EVENTS(entry);
178             
179            try { 
180                events = Integer.parseInt(eventsString);  
181           } catch (NumberFormatException ex) {}
182             
183            
184    
185            if(events != -1){
186                hashtableAtrib.put( "events", eventsString );
187                return new PhysicalFile(getLFN(entry), getSTORAGE_SERVISE(entry), getHOST(entry), getPATH(entry), getFILE_NAME(entry), hashtableAtrib );
188            }
189            else
190                return new PhysicalFile(getLFN(entry), getSTORAGE_SERVISE(entry), getHOST(entry), getPATH(entry), getFILE_NAME(entry), null );
191    
192         }
193         
194         
195         
196         
197         public List entryListToPhysicalFileList(List entrys){
198             
199            List physicalFiles = new ArrayList();
200            for(int i = 0; i != entrys.size(); i++){
201                physicalFiles.add(entryToPhysicalFile((String) entrys.get(i)));
202            }
203            
204            return physicalFiles;         
205         }
206         
207         
208         
209         
210         
211         
212        
213    }