/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gagravarr.mpxj.json;

import java.io.File;

import net.sf.mpxj.MPXJException;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.mpp.MPPReader;
import net.sf.mpxj.mpx.MPXReader;

/**
 * Command line wrapper around {@link DojoGanttJSON}
 */
public class ProjectToJSON {
    public static void main(String[] args) throws Exception {
        if (args.length < 1) {
            System.err.println("Use:");
            System.err.println("   ProjectToJSON <project file> [json file]");
            System.exit(1);
        }
        
        File input = new File(args[0]);
        if (! input.exists()) {
            System.err.println("Not found: " + input);
            System.exit(2);
        }
        
        ProjectFile project = null;
        try {
            if (input.getName().endsWith(".mpx")) {
                MPXReader reader = new MPXReader();
                project = reader.read(input);
            } else {
                MPPReader reader = new MPPReader();
                project = reader.read(input);
            }
        } catch (MPXJException e) {
            throw new RuntimeException("Error reading project file", e);
        }
        
        System.out.println(DojoGanttJSON.toJSON(project));
    }
}

@@ -609,16 +609,44 @@
 io.File;
+%0Aimport java.io.PrintStream;
 %0A%0Aimport
@@ -1766,23 +1766,140 @@
         
-System.
+PrintStream out = System.out;%0A        if (args.length %3E= 2) %7B%0A            out = new PrintStream(args%5B1%5D);%0A        %7D%0A        
 out.prin
@@ -1902,16 +1902,17 @@
 println(
+ 
 DojoGant
@@ -1932,16 +1932,38 @@
 project)
+ );%0A        out.flush(
 );%0A    %7D

Support writing the json to a file, if requested