// Copyright Keith D Gregory
//
// 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 com.kdgregory.pomutil.cleaner;

import java.io.File;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

import net.sf.practicalxml.ParseUtil;

import com.kdgregory.pomutil.cleaner.transform.InsertCommonProperties;
import com.kdgregory.pomutil.cleaner.transform.NormalizeDependencies;
import com.kdgregory.pomutil.cleaner.transform.SortDependencies;
import com.kdgregory.pomutil.cleaner.transform.ReplaceExplicitVersionsWithProperties;
import com.kdgregory.pomutil.util.InvocationArgs;
import com.kdgregory.pomutil.util.OutputHandler;
import com.kdgregory.pomutil.util.PomWrapper;


/**
 *  Driver program for single-file cleanup. See README for invocation instructions.
 *  <p>
 *  Successful execution results in a 0 return code. Any exception will be written
 *  to StdErr, and the program will terminate with a non-zero return code.
 */
public class Cleaner
{
    public static void main(String[] argv)
    {
        try
        {
            InvocationArgs args = new InvocationArgs(argv);
            PomWrapper pom = new PomWrapper(readDocument(args));

            new InsertCommonProperties(pom, args).transform();
            new NormalizeDependencies(pom, args).transform();
            new SortDependencies(pom, args).transform();
            new ReplaceExplicitVersionsWithProperties(pom, args).transform();

            new OutputHandler().writeOutput(pom.getDom(), args);
            System.exit(0);
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
            System.exit(1);
        }
    }


//----------------------------------------------------------------------------
//  Internals
//----------------------------------------------------------------------------

    /**
     *  Creates the DOM, either by reading the first entry in the passed list
     *  (which is then removed), or by reading StdIn (if there aren't any
     *  entries in the list).
     */
    private static Document readDocument(InvocationArgs args)
    throws Exception
    {
        String filename = args.shift();
        if (filename != null)
        {
            return ParseUtil.parse(new File(filename));
        }
        else
        {
            return ParseUtil.parse(new InputSource(System.in));
        }
    }
}

@@ -1552,60 +1552,749 @@
     
-InvocationArgs args = new InvocationArgs(argv);%0A    
+new Cleaner(new InvocationArgs(argv)).run();%0A            System.exit(0);%0A        %7D%0A        catch (Throwable ex)%0A        %7B%0A            ex.printStackTrace();%0A            System.exit(1);%0A        %7D%0A    %7D%0A%0A%0A//----------------------------------------------------------------------------%0A//  Instance variables and constructor%0A//----------------------------------------------------------------------------%0A%0A    private InvocationArgs args;%0A%0A    public Cleaner(InvocationArgs args)%0A    %7B%0A        this.args = args;%0A    %7D%0A%0A%0A//----------------------------------------------------------------------------%0A//  Public methods%0A//----------------------------------------------------------------------------%0A%0A    public void run()%0A    throws Exception%0A    %7B%0A
     
@@ -2355,28 +2355,24 @@
 );%0A%0A        
-    
 new InsertCo
@@ -2414,28 +2414,24 @@
 ();%0A        
-  
-  
 new Normaliz
@@ -2472,28 +2472,24 @@
 ();%0A        
-    
 new SortDepe
@@ -2525,28 +2525,24 @@
 ();%0A        
- 
-   
 new ReplaceE
@@ -2592,28 +2592,24 @@
 ansform();%0A%0A
-    
         new 
@@ -2665,158 +2665,10 @@
     
-        System.exit(0);%0A        %7D%0A        catch (Throwable ex)%0A        %7B%0A            ex.printStackTrace();%0A            System.exit(1);%0A        %7D%0A    
 %7D
+%0A
 %0A%0A%0A/

convert to instantiable class