001    /*
002     * Copyright 2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net.ftp.parser;
017    
018    import org.apache.commons.net.ftp.FTPClientConfig;
019    import org.apache.commons.net.ftp.FTPFile;
020    
021    /**
022     * Implementation of FTPFileEntryParser and FTPFileListParser for IBM MVS Systems.
023     *
024     * @author <a href="jnadler@srcginc.com">Jeff Nadler</a>
025     * @author <a href="wnoto@openfinance.com">William Noto</a>
026     * @version $Id$
027     * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
028     */
029    public class MVSFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
030    {  
031        /**
032         * This is the regular expression used by this parser.
033         */
034            private static final String REGEX = "(.*)\\s+([^\\s]+)\\s*";
035            
036        /**
037         * Although this parser is now ignoring dates, someone may someday
038         * figure out a way to accomodate this and this appears to be the 
039         * format used.  For now, it won't be used.
040         * SMC 2005/04/08
041         */
042        static final String DEFAULT_DATE_FORMAT 
043                    = "yyyy/MM/dd"; // 2001/11/09
044    
045            
046            // This is not at all the tightest possible regexp for MVS LIST
047            // output, but I'm not a mainframe guru so I have little idea what the
048            // range of valid values are.  I just needed to get the filename (Dsname);
049            // note that no other FTPFile fields can be filled in with the results of
050            // a LIST on MVS.  The 'Referred' date seems to be 'last accessed date'
051            // and not 'last modified date' so I didn't bother parsing it.
052            //
053            // Of course it works perfectly as-is and it distinguishes header lines from
054            // file results so that's the important thing.  
055            //
056            // This parser should be used when SYST returns:
057            // 'MVS is the operating system of this server. FTP Server is running on z/OS.'
058            //
059            // Also note that there is no concept of directories in MVS, just datasets,
060            // which have names composed of four dot separated names of up to 8 chars.
061            // As a result, FTPFile.FILE_TYPE is always used. -JN 6/2004 jnadler<at>srcginc<dotcom>
062    
063            // Sample LIST results from MVS:
064            //
065            //Volume Unit    Referred Ext Used Recfm Lrecl BlkSz Dsorg Dsname
066            //FPFS42 3390   2004/06/23  1    1  FB     128  6144  PS  INCOMING.RPTBM023.D061704
067            //FPFS41 3390   2004/06/23  1    1  FB     128  6144  PS  INCOMING.RPTBM056.D061704
068            //FPFS25 3390   2004/06/23  1    1  FB     128  6144  PS  INCOMING.WTM204.D061704
069    
070        /**
071         * The sole constructor for a MVSFTPEntryParser object.
072         *
073         * @exception IllegalArgumentException
074         * Thrown if the regular expression is unparseable.  Should not be seen
075         * under normal conditions.  It it is seen, this is a sign that
076         * <code>REGEX</code> is  not a valid regular expression.
077         */
078        public MVSFTPEntryParser()
079        {
080            super(REGEX);
081        }
082    
083        /**
084         * Parses a line of an MVS FTP server file listing and converts it into a
085         * usable format in the form of an <code> FTPFile </code> instance.  If the
086         * file listing line doesn't describe a file, <code> null </code> is
087         * returned, otherwise a <code> FTPFile </code> instance representing the
088         * files in the directory is returned.
089         * <p>
090         * @param entry A line of text from the file listing
091         * @return An FTPFile instance corresponding to the supplied entry
092         */
093        public FTPFile parseFTPEntry(String entry)
094        {       
095            FTPFile f = null;
096            if (matches(entry))          
097            {
098                f = new FTPFile();
099                String dataSetName = group(2);
100                f.setType(FTPFile.FILE_TYPE);
101                            f.setName(dataSetName);
102    
103                            return (f);
104            }
105            return null;
106        }
107    
108        /* 
109         * @return
110         */
111        protected FTPClientConfig getDefaultConfiguration() {
112            return new FTPClientConfig(
113                    FTPClientConfig.SYST_MVS,
114                    DEFAULT_DATE_FORMAT,
115                    null, null, null, null);
116        }
117    }