package org.apache.maven.bootstrap.util;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * 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.
 */

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Parse an XML file.
 *
 * @version $Id$
 */
public abstract class AbstractReader
    extends DefaultHandler
{
    private SAXParserFactory saxFactory;

    protected File pomFile;

    public void parse( File file )
        throws ParserConfigurationException, SAXException, IOException
    {
        pomFile = file;

        saxFactory = SAXParserFactory.newInstance();

        SAXParser parser = saxFactory.newSAXParser();

        // Cheap and cheerful. Please add more to skip if the parser chokes (or use the actual
        StringWriter output = new StringWriter();
        IOUtil.copy( new FileReader( file ), output);
        String out = output.toString();
        out = StringUtils.replace( out, "&oslash;", "\u00f8" );

        InputSource is = new InputSource( new StringReader( out ) );

        try
        {
            parser.parse( is, this );
        }
        catch ( SAXException e )
        {
            System.err.println( "Error reading POM: " + file );
            throw e;
        }
    }

    public void warning( SAXParseException spe )
    {
        printParseError( "Warning", spe );
    }

    public void error( SAXParseException spe )
    {
        printParseError( "Error", spe );
    }

    public void fatalError( SAXParseException spe )
    {
        printParseError( "Fatal Error", spe );
    }

    private final void printParseError( String type, SAXParseException spe )
    {
        System.err.println( type + " [line " + spe.getLineNumber() + ", row " + spe.getColumnNumber() + "]: " +
                            spe.getMessage() );
    }
}

@@ -957,39 +957,66 @@
 .io.
-File;%0Aimport java.io.FileReader
+ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream
 ;%0Aim
@@ -1028,27 +1028,20 @@
 java.io.
-IOException
+File
 ;%0Aimport
@@ -1049,28 +1049,31 @@
 java.io.
-StringReader
+FileInputStream
 ;%0Aimport
@@ -1081,28 +1081,27 @@
 java.io.
-StringWriter
+IOException
 ;%0A%0A/**%0A 
@@ -1635,46 +1635,64 @@
     
-StringWriter output = new StringWriter
+ByteArrayOutputStream output = new ByteArrayOutputStream
 ();%0A
@@ -1720,22 +1720,27 @@
 new File
-Reader
+InputStream
 ( file )
@@ -1747,16 +1747,17 @@
 , output
+ 
 );%0A     
@@ -1788,16 +1788,25 @@
 oString(
+ %22UTF-8%22 
 );%0A     
@@ -1915,25 +1915,53 @@
 new 
-StringReader( out
+ByteArrayInputStream( out.getBytes( %22UTF-8%22 )
  ) )
@@ -2606,16 +2606,28 @@
 println(
+%0A           
  type + 
@@ -2706,36 +2706,8 @@
  %22 +
-%0A                           
  spe

fix encoding issues in parser