Index: test/java/fr/jayasoft/ivy/xml/updated.xml
===================================================================
--- test/java/fr/jayasoft/ivy/xml/updated.xml	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/updated.xml	(working copy)
@@ -1,6 +1,8 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<?xml-stylesheet type="text/xsl" href="http://www.jayasoft.fr/org/ivyrep/ivy-doc.xsl"?>
-<ivy-module version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://jayasoft.org/ivy/ivy.xsd">
+<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="http://www.jayasoft.fr/org/ivyrep/ivy-doc.xsl"?>
+<!-- A comment before the first tag -->
+<ivy-module version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://jayasoft.org/ivy/ivy.xsd">
+	<!-- A comment with accent Ã© & special characters -->
 	<info organisation="myorg" module="mymodule" revision="mynewrev" status="release" publication="20050322143254">
 	       
 		<license name="MyLicense" url="http://www.my.org/mymodule/mylicense.html"/>
Index: test/java/fr/jayasoft/ivy/xml/test-update.xml
===================================================================
--- test/java/fr/jayasoft/ivy/xml/test-update.xml	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/test-update.xml	(working copy)
@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <?xml-stylesheet type="text/xsl" href="http://www.jayasoft.fr/org/ivyrep/ivy-doc.xsl"?>
+<!-- A comment before the first tag -->
 <ivy-module version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://jayasoft.org/ivy/ivy.xsd">
+	<!-- A comment with accent é & special characters -->
 	<info organisation="myorg"
 	       module="mymodule"
 	       revision="myrev"
Index: test/java/fr/jayasoft/ivy/xml/test-write-full.xml
===================================================================
--- test/java/fr/jayasoft/ivy/xml/test-write-full.xml	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/test-write-full.xml	(working copy)
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <ivy-module version="1.0">
 	<info organisation="myorg"
 		module="mymodule"
Index: test/java/fr/jayasoft/ivy/xml/XmlIvyConfigurationParserTest.java
===================================================================
--- test/java/fr/jayasoft/ivy/xml/XmlIvyConfigurationParserTest.java	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/XmlIvyConfigurationParserTest.java	(working copy)
@@ -6,7 +6,6 @@
 package fr.jayasoft.ivy.xml;
 
 import java.io.File;
-import java.net.URL;
 import java.util.List;
 
 import junit.framework.TestCase;
@@ -21,11 +20,7 @@
 import fr.jayasoft.ivy.report.ResolveReport;
 import fr.jayasoft.ivy.resolver.ChainResolver;
 import fr.jayasoft.ivy.resolver.FileSystemResolver;
-import fr.jayasoft.ivy.resolver.IvyRepResolver;
 import fr.jayasoft.ivy.resolver.MockResolver;
-import fr.jayasoft.ivy.url.URLHandler;
-import fr.jayasoft.ivy.url.URLHandlerDispatcher;
-import fr.jayasoft.ivy.url.URLHandlerRegistry;
 import fr.jayasoft.ivy.version.ChainVersionMatcher;
 import fr.jayasoft.ivy.version.MockVersionMatcher;
 import fr.jayasoft.ivy.version.VersionMatcher;
@@ -332,13 +327,6 @@
         assertTrue(testOutputter instanceof MyOutputter);
     }
     
-    private void configureURLHandler() {
-        URLHandlerDispatcher dispatcher = new URLHandlerDispatcher();
-        URLHandler httpHandler = URLHandlerRegistry.getHttp();
-        dispatcher.setDownloader("http", httpHandler);
-        dispatcher.setDownloader("https", httpHandler);
-        URLHandlerRegistry.setDefault(dispatcher);
-    }
     
     public static class MyOutputter implements ReportOutputter {
 
Index: test/java/fr/jayasoft/ivy/xml/test-write-dependencies.xml
===================================================================
--- test/java/fr/jayasoft/ivy/xml/test-write-dependencies.xml	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/test-write-dependencies.xml	(working copy)
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <ivy-module version="1.0">
 	<info organisation="myorg"
 		module="mymodule"
Index: test/java/fr/jayasoft/ivy/xml/test-write-simple.xml
===================================================================
--- test/java/fr/jayasoft/ivy/xml/test-write-simple.xml	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/test-write-simple.xml	(working copy)
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <ivy-module version="1.0">
 	<info organisation="myorg"
 		module="mymodule"
Index: test/java/fr/jayasoft/ivy/xml/test-dependencies.xml
===================================================================
--- test/java/fr/jayasoft/ivy/xml/test-dependencies.xml	(revision 608)
+++ test/java/fr/jayasoft/ivy/xml/test-dependencies.xml	(working copy)
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <ivy-module version="1.0">
 	<info organisation="myorg"
 	       module="mymodule"
Index: .classpath
===================================================================
--- .classpath	(revision 608)
+++ .classpath	(working copy)
@@ -3,12 +3,18 @@
 	<classpathentry kind="src" path="src/java"/>
 	<classpathentry kind="src" path="test/java"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-	<classpathentry kind="con" path="org.jayasoft.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/ivy.xml/default,"/>
 	<classpathentry sourcepath="ECLIPSE_HOME/plugins/org.eclipse.jdt.source_3.0.0/src/org.junit_3.8.1/junitsrc.zip" kind="var" path="JUNIT_HOME/junit.jar"/>
 	<classpathentry sourcepath="/ANT_HOME/src/main" kind="var" path="ANT_HOME/lib/ant.jar"/>
 	<classpathentry kind="var" path="ANT_HOME/lib/ant-launcher.jar"/>
 	<classpathentry kind="var" path="ANT_HOME/lib/xercesImpl.jar"/>
 	<classpathentry kind="var" path="ANT_HOME/lib/ant-trax.jar"/>
 	<classpathentry kind="var" path="ANT_HOME/lib/xml-apis.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/apache/commons-cli/jars/commons-cli-1.0.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/apache/commons-vfs/jars/commons-vfs-20060920.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/apache/oro/jars/oro-2.0.8.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/jcraft/jsch/jars/jsch-0.1.25.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/apache/commons-httpclient/jars/commons-httpclient-3.0.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/apache/commons-logging/jars/commons-logging-1.0.4.jar"/>
+	<classpathentry kind="var" path="IVY_CACHE/apache/commons-codec/jars/commons-codec-1.2.jar"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
Index: src/java/fr/jayasoft/ivy/Ivy.java
===================================================================
--- src/java/fr/jayasoft/ivy/Ivy.java	(revision 608)
+++ src/java/fr/jayasoft/ivy/Ivy.java	(working copy)
@@ -2198,9 +2198,9 @@
             md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(mrid, revision));
             md.setResolvedPublicationDate(pubdate);
         } catch (MalformedURLException e) {
-            throw new RuntimeException("malformed url obtained for file "+ivyFile);
+            throw new RuntimeException("malformed url obtained for file "+ivyFile , e);
         } catch (ParseException e) {
-            throw new IllegalStateException("bad ivy file in cache for "+mrid+": please clean and resolve again");
+            throw new RuntimeException("bad ivy file in cache for "+mrid+": please clean and resolve again" , e);
         }
         
         // 2) parse resolvedRevisions From properties file
@@ -2248,7 +2248,7 @@
                     new File(publishedIvy),
                     resolvedDependencies, status, revision, pubdate, null, true);
         } catch (SAXException ex) {
-            throw new IllegalStateException("bad ivy file in cache for "+mrid+": please clean and resolve again");
+            throw new RuntimeException("bad ivy file in cache for "+mrid+": please clean and resolve again" , ex);
         }
         
         Message.verbose("\tdeliver done ("+(System.currentTimeMillis()-start)+"ms)");
Index: src/java/fr/jayasoft/ivy/xml/XmlModuleDescriptorWriter.java
===================================================================
--- src/java/fr/jayasoft/ivy/xml/XmlModuleDescriptorWriter.java	(revision 608)
+++ src/java/fr/jayasoft/ivy/xml/XmlModuleDescriptorWriter.java	(working copy)
@@ -8,6 +8,7 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Collection;
@@ -30,8 +31,9 @@
         if (output.getParentFile() != null) {
             output.getParentFile().mkdirs();
         }
-        PrintWriter out = new PrintWriter(new FileOutputStream(output));
+        PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output) , "UTF-8"));
         try {
+        	out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 	        out.println("<ivy-module version=\"1.0\">"); 
 	    	out.println("\t<info organisation=\""+md.getModuleRevisionId().getOrganisation()+"\"");
 	    	out.println("\t\tmodule=\""+md.getModuleRevisionId().getName()+"\"");
Index: src/java/fr/jayasoft/ivy/xml/XmlModuleDescriptorUpdater.java
===================================================================
--- src/java/fr/jayasoft/ivy/xml/XmlModuleDescriptorUpdater.java	(revision 608)
+++ src/java/fr/jayasoft/ivy/xml/XmlModuleDescriptorUpdater.java	(working copy)
@@ -13,6 +13,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.URL;
 import java.util.Arrays;
@@ -86,227 +87,259 @@
         }
     }
     
-    public static void update(final Ivy ivy, InputStream inStream, OutputStream outStream, final Map resolvedRevisions, final String status, 
-            final String revision, final Date pubdate, final Namespace ns, final boolean replaceInclude) 
-                                throws IOException, SAXException {
-        final PrintWriter out = new PrintWriter(outStream);
-        final BufferedInputStream in = new BufferedInputStream(inStream);
+    private static class UpdaterHandler extends DefaultHandler implements LexicalHandler {
+    	
+    	private final Ivy ivy;
+		private final PrintWriter out;
+		private final Map resolvedRevisions;
+		private final String status;
+		private final String revision;
+		private final Date pubdate;
+		private final Namespace ns;
+		private final boolean replaceInclude;
+		private boolean inHeader = true;
+		
+		public UpdaterHandler(final Ivy ivy, final PrintWriter out, final Map resolvedRevisions, final String status, 
+            final String revision, final Date pubdate, final Namespace ns, final boolean replaceInclude) {
+				this.ivy = ivy;
+				this.out = out;
+				this.resolvedRevisions = resolvedRevisions;
+				this.status = status;
+				this.revision = revision;
+				this.pubdate = pubdate;
+				this.ns = ns;
+				this.replaceInclude = replaceInclude;
+    		
+    	}
+    	
+        // never print *ln* cause \n is found in copied characters stream
+        // nor do we need do handle indentation, original one is maintained except for attributes
         
-        in.mark(10000); // assume the header is never larger than 10000 bytes.
-        copyHeader(in, out);
-        in.reset(); // reposition the stream at the beginning
-            
-        try {
-            XMLHelper.parse(in, null, new DefaultHandler() {
-                // never print *ln* cause \n is found in copied characters stream
-                // nor do we need do handle indentation, original one is maintained except for attributes
-                
-                private String _organisation = null;
-                private String _defaultConfMapping = null; // defaultConfMapping of imported configurations, if any
-                private Boolean _confMappingOverride = null; // confMappingOverride of imported configurations, if any
-                private String _justOpen = null; // used to know if the last open tag was empty, to adjust termination with /> instead of ></qName>
-                private Stack _context = new Stack();
-                public void startElement(String uri, String localName,
-                        String qName, Attributes attributes)
-                        throws SAXException {
-                    if (_justOpen != null) {
-                        out.print(">");
-                    }
-                    _context.push(qName);
-                    if ("info".equals(qName)) {
-                        _organisation = substitute(ivy, attributes.getValue("organisation"));
-                        out.print("<info organisation=\""+_organisation
-                                				+"\" module=\""+substitute(ivy, attributes.getValue("module"))+"\"");
-                        if (revision != null) {
-                            out.print(" revision=\""+revision+"\"");
-                        } else if (attributes.getValue("revision") != null) {
-                            out.print(" revision=\""+substitute(ivy, attributes.getValue("revision"))+"\"");
-                        }
-                        if (status != null) {
-                            out.print(" status=\""+status+"\"");
-                        } else {
-                            out.print(" status=\""+substitute(ivy, attributes.getValue("status"))+"\"");
-                        }
-                        if (pubdate != null) {
-                            out.print(" publication=\""+Ivy.DATE_FORMAT.format(pubdate)+"\"");
-                        } else if (attributes.getValue("publication") != null) {
-                            out.print(" publication=\""+substitute(ivy, attributes.getValue("publication"))+"\"");
-                        }
-                        Collection stdAtts = Arrays.asList(new String[] {"organisation", "module", "revision", "status", "publication", "namespace"});
-                        if (attributes.getValue("namespace") != null) {
-                            out.print(" namespace=\""+substitute(ivy, attributes.getValue("namespace"))+"\"");
-                        }
-                        for (int i=0; i<attributes.getLength(); i++) {
-                        	if (!stdAtts.contains(attributes.getQName(i))) {
-                        		out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
-                        	}
-                        }
-                    } else if (replaceInclude && "include".equals(qName) && _context.contains("configurations")) {
-                        try {
-                            URL url;
-                            String fileName = substitute(ivy, attributes.getValue("file"));
-                            if (fileName == null) {
-                                String urlStr = substitute(ivy, attributes.getValue("url"));
-                                url = new URL(urlStr);
-                            } else {
-                                url = new File(fileName).toURL();
-                            }     
-                            XMLHelper.parse(url, null, new DefaultHandler() {
-                                boolean _first = true;
-                                public void startElement(String uri, String localName,
-                                        String qName, Attributes attributes)
-                                        throws SAXException {
-                                    if ("configurations".equals(qName)) {
-                                        String defaultconf = substitute(ivy, attributes.getValue("defaultconfmapping"));
-                                        if (defaultconf != null) {
-                                            _defaultConfMapping = defaultconf;
-                                        }
-                                        String mappingOverride = substitute(ivy, attributes.getValue("confmappingoverride"));
-                                        if (mappingOverride != null) {
-                                           _confMappingOverride = Boolean.valueOf(mappingOverride);
-                                        }
-                                    } else if ("conf".equals(qName)) {
-                                        // copy
-                                        if (!_first) {
-                                            out.print("/>\n\t\t");
-                                        } else {
-                                            _first = false;
-                                        }
-                                        out.print("<"+qName);
-                                        for (int i=0; i<attributes.getLength(); i++) {
-                                            out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
-                                        }
-                                    }
+        private String _organisation = null;
+        private String _defaultConfMapping = null; // defaultConfMapping of imported configurations, if any
+        private Boolean _confMappingOverride = null; // confMappingOverride of imported configurations, if any
+        private String _justOpen = null; // used to know if the last open tag was empty, to adjust termination with /> instead of ></qName>
+        private Stack _context = new Stack();
+        public void startElement(String uri, String localName,
+                String qName, Attributes attributes)
+                throws SAXException {
+        	inHeader = false;
+            if (_justOpen != null) {
+                out.print(">");
+            }
+            _context.push(qName);
+            if ("info".equals(qName)) {
+                _organisation = substitute(ivy, attributes.getValue("organisation"));
+                out.print("<info organisation=\""+_organisation
+                        				+"\" module=\""+substitute(ivy, attributes.getValue("module"))+"\"");
+                if (revision != null) {
+                    out.print(" revision=\""+revision+"\"");
+                } else if (attributes.getValue("revision") != null) {
+                    out.print(" revision=\""+substitute(ivy, attributes.getValue("revision"))+"\"");
+                }
+                if (status != null) {
+                    out.print(" status=\""+status+"\"");
+                } else {
+                    out.print(" status=\""+substitute(ivy, attributes.getValue("status"))+"\"");
+                }
+                if (pubdate != null) {
+                    out.print(" publication=\""+Ivy.DATE_FORMAT.format(pubdate)+"\"");
+                } else if (attributes.getValue("publication") != null) {
+                    out.print(" publication=\""+substitute(ivy, attributes.getValue("publication"))+"\"");
+                }
+                Collection stdAtts = Arrays.asList(new String[] {"organisation", "module", "revision", "status", "publication", "namespace"});
+                if (attributes.getValue("namespace") != null) {
+                    out.print(" namespace=\""+substitute(ivy, attributes.getValue("namespace"))+"\"");
+                }
+                for (int i=0; i<attributes.getLength(); i++) {
+                	if (!stdAtts.contains(attributes.getQName(i))) {
+                		out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
+                	}
+                }
+            } else if (replaceInclude && "include".equals(qName) && _context.contains("configurations")) {
+                try {
+                    URL url;
+                    String fileName = substitute(ivy, attributes.getValue("file"));
+                    if (fileName == null) {
+                        String urlStr = substitute(ivy, attributes.getValue("url"));
+                        url = new URL(urlStr);
+                    } else {
+                        url = new File(fileName).toURL();
+                    }     
+                    XMLHelper.parse(url, null, new DefaultHandler() {
+                        boolean _first = true;
+                        public void startElement(String uri, String localName,
+                                String qName, Attributes attributes)
+                                throws SAXException {
+                            if ("configurations".equals(qName)) {
+                                String defaultconf = substitute(ivy, attributes.getValue("defaultconfmapping"));
+                                if (defaultconf != null) {
+                                    _defaultConfMapping = defaultconf;
                                 }
-                            });
-                        } catch (Exception e) {
-                            Message.warn("exception occured while importing configurations: "+e.getMessage());
-                            throw new SAXException(e);
-                        }
-                    } else if ("dependency".equals(qName)) {
-                        out.print("<dependency");
-                        String org = substitute(ivy, attributes.getValue("org"));
-                        org = org == null ? _organisation : org;
-                        String module = substitute(ivy, attributes.getValue("name"));
-                        String branch = substitute(ivy, attributes.getValue("branch"));
-                        String revision = substitute(ivy, attributes.getValue("rev"));
-                        ModuleRevisionId localMid = ModuleRevisionId.newInstance(org, module, branch, revision);
-                        ModuleRevisionId systemMid = ns == null ? 
-                                localMid : 
-                                ns.getToSystemTransformer().transform(localMid);
-                        
-                        for (int i=0; i<attributes.getLength(); i++) {
-                            String attName = attributes.getQName(i);
-                            if ("rev".equals(attName)) {
-                                String rev = (String)resolvedRevisions.get(systemMid);
-                                if (rev != null) {
-                                    out.print(" rev=\""+rev+"\"");
+                                String mappingOverride = substitute(ivy, attributes.getValue("confmappingoverride"));
+                                if (mappingOverride != null) {
+                                   _confMappingOverride = Boolean.valueOf(mappingOverride);
+                                }
+                            } else if ("conf".equals(qName)) {
+                                // copy
+                                if (!_first) {
+                                    out.print("/>\n\t\t");
                                 } else {
-                                    out.print(" rev=\""+systemMid.getRevision()+"\"");
+                                    _first = false;
                                 }
-                            } else if ("org".equals(attName)) {
-                                out.print(" org=\""+systemMid.getOrganisation()+"\"");
-                            } else if ("name".equals(attName)) {
-                                out.print(" name=\""+systemMid.getName()+"\"");
-                            } else if ("branch".equals(attName)) {
-                                out.print(" branch=\""+systemMid.getBranch()+"\"");
-                            } else {
-                                out.print(" "+attName+"=\""+substitute(ivy, attributes.getValue(attName))+"\"");
+                                out.print("<"+qName);
+                                for (int i=0; i<attributes.getLength(); i++) {
+                                    out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
+                                }
                             }
                         }
-                    } else if ("dependencies".equals(qName)) {
-                        // copy
-                        out.print("<"+qName);
-                        for (int i=0; i<attributes.getLength(); i++) {
-                            out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
+                    });
+                } catch (Exception e) {
+                    Message.warn("exception occured while importing configurations: "+e.getMessage());
+                    throw new SAXException(e);
+                }
+            } else if ("dependency".equals(qName)) {
+                out.print("<dependency");
+                String org = substitute(ivy, attributes.getValue("org"));
+                org = org == null ? _organisation : org;
+                String module = substitute(ivy, attributes.getValue("name"));
+                String branch = substitute(ivy, attributes.getValue("branch"));
+                String revision = substitute(ivy, attributes.getValue("rev"));
+                ModuleRevisionId localMid = ModuleRevisionId.newInstance(org, module, branch, revision);
+                ModuleRevisionId systemMid = ns == null ? 
+                        localMid : 
+                        ns.getToSystemTransformer().transform(localMid);
+                
+                for (int i=0; i<attributes.getLength(); i++) {
+                    String attName = attributes.getQName(i);
+                    if ("rev".equals(attName)) {
+                        String rev = (String)resolvedRevisions.get(systemMid);
+                        if (rev != null) {
+                            out.print(" rev=\""+rev+"\"");
+                        } else {
+                            out.print(" rev=\""+systemMid.getRevision()+"\"");
                         }
-                        // add default conf mapping if needed
-                        if (_defaultConfMapping != null && attributes.getValue("defaultconfmapping") == null) {
-                            out.print(" defaultconfmapping=\""+_defaultConfMapping+"\"");
-                        }
-                        // add confmappingoverride if needed
-                        if (_confMappingOverride != null && attributes.getValue("confmappingoverride") == null) {
-                           out.print(" confmappingoverride=\""+_confMappingOverride.toString()+"\"");
-                        }
+                    } else if ("org".equals(attName)) {
+                        out.print(" org=\""+systemMid.getOrganisation()+"\"");
+                    } else if ("name".equals(attName)) {
+                        out.print(" name=\""+systemMid.getName()+"\"");
+                    } else if ("branch".equals(attName)) {
+                        out.print(" branch=\""+systemMid.getBranch()+"\"");
                     } else {
-                        // copy
-                        out.print("<"+qName);
-                        for (int i=0; i<attributes.getLength(); i++) {
-                            out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
-                        }
+                        out.print(" "+attName+"=\""+substitute(ivy, attributes.getValue(attName))+"\"");
                     }
-                    _justOpen = qName;
-//                    indent.append("\t");
                 }
-
-                private String substitute(Ivy ivy, String value) {
-                    return ivy == null ? value : ivy.substitute(value);
+            } else if ("dependencies".equals(qName)) {
+                // copy
+                out.print("<"+qName);
+                for (int i=0; i<attributes.getLength(); i++) {
+                    out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
                 }
-
-                public void characters(char[] ch, int start, int length)
-                        throws SAXException {
-                    if (_justOpen != null) {
-                        out.print(">"); 
-                        _justOpen = null;
-                    }
-                    for (int i = start; i < start + length; i++) {
-                        out.print(ch[i]);
-                    }
+                // add default conf mapping if needed
+                if (_defaultConfMapping != null && attributes.getValue("defaultconfmapping") == null) {
+                    out.print(" defaultconfmapping=\""+_defaultConfMapping+"\"");
                 }
-
-                public void endElement(String uri, String localName,
-                        String qName) throws SAXException {
-                    if (qName.equals(_justOpen)) {
-                        out.print("/>");
-                    } else {
-                        out.print("</"+qName+">");
-                    }
-                    _justOpen = null;
-                    _context.pop();
+                // add confmappingoverride if needed
+                if (_confMappingOverride != null && attributes.getValue("confmappingoverride") == null) {
+                   out.print(" confmappingoverride=\""+_confMappingOverride.toString()+"\"");
                 }
-
-                public void endDocument() throws SAXException {
-                    out.print(LINE_SEPARATOR);
-                    out.flush();
-                    out.close();
+            } else {
+                // copy
+                out.print("<"+qName);
+                for (int i=0; i<attributes.getLength(); i++) {
+                    out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
                 }
-                
-                public void warning(SAXParseException e) throws SAXException {
-                    throw e;
-                }
-                public void error(SAXParseException e) throws SAXException {
-                    throw e;
-                }
-                public void fatalError(SAXParseException e) throws SAXException {
-                    throw e;
-                }
-            }, new LexicalHandler() {
-				public void endCDATA() throws SAXException {
-				}
+            }
+            _justOpen = qName;
+//            indent.append("\t");
+        }
 
-				public void endDTD() throws SAXException {
-				}
+        private String substitute(Ivy ivy, String value) {
+            return ivy == null ? value : ivy.substitute(value);
+        }
 
-				public void startCDATA() throws SAXException {
-				}
+        public void characters(char[] ch, int start, int length)
+                throws SAXException {
+            if (_justOpen != null) {
+                out.print(">"); 
+                _justOpen = null;
+            }
+            for (int i = start; i < start + length; i++) {
+                out.print(ch[i]);
+            }
+        }
 
-				public void comment(char[] ch, int start, int length) throws SAXException {
-					StringBuffer comment = new StringBuffer();
-					comment.append(ch, start, length);
-					out.print("<!--");
-					out.print(comment.toString());
-					out.print("-->");
-				}
+        public void endElement(String uri, String localName,
+                String qName) throws SAXException {
+            if (qName.equals(_justOpen)) {
+                out.print("/>");
+            } else {
+                out.print("</"+qName+">");
+            }
+            _justOpen = null;
+            _context.pop();
+        }
 
-				public void endEntity(String name) throws SAXException {
-				}
+        public void endDocument() throws SAXException {
+            out.print(LINE_SEPARATOR);
+            out.flush();
+            out.close();
+        }
+        
+        public void warning(SAXParseException e) throws SAXException {
+            throw e;
+        }
+        public void error(SAXParseException e) throws SAXException {
+            throw e;
+        }
+        public void fatalError(SAXParseException e) throws SAXException {
+            throw e;
+        }
+        
+        
+		public void endCDATA() throws SAXException {
+		}
 
-				public void startEntity(String name) throws SAXException {
-				}
+		public void endDTD() throws SAXException {
+		}
 
-				public void startDTD(String name, String publicId, String systemId) throws SAXException {
-				}
-            });
+		public void startCDATA() throws SAXException {
+		}
+
+		public void comment(char[] ch, int start, int length) throws SAXException {
+			if (!inHeader) {
+				StringBuffer comment = new StringBuffer();
+				comment.append(ch, start, length);
+				out.print("<!--");
+				out.print(comment.toString());
+				out.print("-->");
+			}
+		}
+
+		public void endEntity(String name) throws SAXException {
+		}
+
+		public void startEntity(String name) throws SAXException {
+		}
+
+		public void startDTD(String name, String publicId, String systemId) throws SAXException {
+		}
+
+    }
+    
+    public static void update(final Ivy ivy, InputStream inStream, OutputStream outStream, final Map resolvedRevisions, final String status, 
+            final String revision, final Date pubdate, final Namespace ns, final boolean replaceInclude) 
+                                throws IOException, SAXException {
+        final PrintWriter out = new PrintWriter(new OutputStreamWriter(outStream , "UTF-8"));
+        final BufferedInputStream in = new BufferedInputStream(inStream);
+        
+        in.mark(10000); // assume the header is never larger than 10000 bytes.
+        copyHeader(in, out);
+        in.reset(); // reposition the stream at the beginning
+            
+        try {
+        	UpdaterHandler updaterHandler = new UpdaterHandler(ivy,out,resolvedRevisions,status,revision,pubdate,ns,replaceInclude);
+			XMLHelper.parse(in, null, updaterHandler, updaterHandler);
         } catch (ParserConfigurationException e) {
             IllegalStateException ise = new IllegalStateException("impossible to update Ivy files: parser problem");
             ise.initCause(e);
@@ -319,20 +352,27 @@
      * In fact, copies everything before <ivy-module to out, except
      * if <ivy-module is not found, in which case nothing is copied.
      * 
+     * The prolog <?xml version="..." encoding="...."?> is also replaced by
+     * <?xml version="1.0" encoding="UTF-8"?> if it was present.
+     * 
      * @param in
      * @param out
      * @throws IOException
      */
     private static void copyHeader(InputStream in, PrintWriter out) throws IOException {
-        StringBuffer buf = new StringBuffer();
         BufferedReader r = new BufferedReader(new InputStreamReader(in));
-        for (String line = r.readLine(); line != null; line = r.readLine()) {
+        String line = r.readLine();
+        if (line!=null && line.startsWith("<?xml ")) {
+        	out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+        	line = line.substring(line.indexOf(">")+1 , line.length());
+        }
+        for (; line != null; line = r.readLine()) {        	
             int index = line.indexOf("<ivy-module");
             if (index == -1) {
-                buf.append(line).append(LINE_SEPARATOR);
+                out.write(line);
+                out.write(LINE_SEPARATOR);
             } else {
-                buf.append(line.substring(0, index));
-                out.print(buf.toString());
+            	out.write(line.substring(0, index));
                 break;
             }
         }
