001/**
002 * Copyright (c) 2012, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.tools.twitter.options;
031
032import java.io.BufferedWriter;
033import java.io.File;
034import java.io.FileNotFoundException;
035import java.io.FileOutputStream;
036import java.io.IOException;
037import java.io.InputStream;
038import java.io.OutputStreamWriter;
039import java.io.PrintStream;
040import java.io.PrintWriter;
041import java.io.UnsupportedEncodingException;
042import java.util.Iterator;
043import java.util.List;
044
045import org.kohsuke.args4j.CmdLineException;
046import org.openimaj.tools.FileToolsUtil;
047import org.openimaj.twitter.USMFStatus;
048import org.openimaj.twitter.collection.FileTwitterStatusList;
049import org.openimaj.twitter.collection.StreamTwitterStatusList;
050import org.openimaj.twitter.collection.TwitterStatusList;
051
052/**
053 * The single processing command line version of the twitter tool
054 * 
055 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
056 *
057 */
058public class TwitterPreprocessingToolOptions extends  AbstractTwitterPreprocessingToolOptions{
059        
060        /**
061         * this is available mainly for testing
062         */
063        public static InputStream sysin = System.in;
064        List<File> inputFiles;
065        File inputFile;
066        File outputFile;
067        private PrintWriter outWriter = null;
068        private boolean stdout;
069        private Iterator<File> fileIterator;
070        private boolean stdin;
071        
072        /**
073         * See: {@link AbstractTwitterPreprocessingToolOptions#AbstractTwitterPreprocessingToolOptions(String[])}
074         * @param args 
075         */
076        public TwitterPreprocessingToolOptions(String[] args) throws CmdLineException{
077                super(args);
078                
079                if(!this.stdin) this.fileIterator = this.inputFiles.iterator();
080        }
081
082        @Override
083        public boolean validate() throws CmdLineException{
084                try{
085                        if(FileToolsUtil.isStdin(this)){
086                                this.stdin = true;
087                        }
088                        else{                           
089                                this.inputFiles = FileToolsUtil.validateLocalInput(this);
090                        }
091                        if(FileToolsUtil.isStdout(this)){
092                                this.stdout = true;
093                        }
094                        else
095                        {
096                                this.outputFile = FileToolsUtil.validateLocalOutput(this);
097                        }
098                        return true;
099                }
100                catch(Exception e){
101                        throw new CmdLineException(null,e.getMessage());
102                }
103                
104        }
105
106        /**
107         * @return the list of tweets from the input file
108         * @throws IOException
109         */
110        public TwitterStatusList<USMFStatus> getTwitterStatusList() throws IOException {
111                if(this.stdin){
112                        this.stdin = false;
113                        if(this.nTweets == -1)
114                        {                               
115                                return StreamTwitterStatusList.readUSMF(sysin, this.statusType.type(),this.encoding);
116                        }
117                        else{
118                                return StreamTwitterStatusList.readUSMF(sysin, this.nTweets,this.statusType.type(),this.encoding);
119                        }
120                }
121                else{
122                        if(this.nTweets == -1){
123                                return FileTwitterStatusList.readUSMF(this.inputFile, this.encoding,this.statusType.type());
124                        }
125                        else{
126                                return FileTwitterStatusList.readUSMF(this.inputFile, this.nTweets, this.encoding,this.statusType.type());
127                        }
128                }
129                
130                
131        }
132
133        /**
134         * @return a print writer to the output file or stdout
135         * @throws UnsupportedEncodingException
136         * @throws FileNotFoundException
137         */
138        public PrintWriter outputWriter() throws UnsupportedEncodingException, FileNotFoundException {
139                if(this.outWriter == null){
140                        if(this.stdout){
141                                PrintStream sysout = new PrintStream(System.out, true, this.encoding);
142                                this.outWriter = new PrintWriter(sysout);
143                        }else{
144                                this.outWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.outputFile),this.encoding)),true);
145                        }
146                }
147                        
148                return this.outWriter;
149        }
150
151        /**
152         * @return is there another file to analyse
153         */
154        public boolean hasNextFile() {
155                if(!this.stdin) {
156                        if(fileIterator == null) return false;
157                        return fileIterator.hasNext();
158                }
159                return true;
160        }
161
162        /**
163         * Prepare the next file
164         */
165        public void nextFile() {
166                if(this.stdin) return;
167                if(fileIterator.hasNext())
168                        TwitterPreprocessingToolOptions.this.inputFile = fileIterator.next();
169                else
170                        TwitterPreprocessingToolOptions.this.inputFile = null;
171                
172        }
173}