/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.ivy.plugins.parser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorParser;
import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser;
import org.apache.ivy.plugins.repository.Resource;
import org.apache.ivy.util.Message;

public class ModuleDescriptorParserRegistry extends AbstractModuleDescriptorParser {
    private static ModuleDescriptorParserRegistry INSTANCE = new ModuleDescriptorParserRegistry();

    public static ModuleDescriptorParserRegistry getInstance() {
        return INSTANCE;
    }

    private List _parsers = new LinkedList();

    private ModuleDescriptorParserRegistry() {
        _parsers.add(PomModuleDescriptorParser.getInstance());
        _parsers.add(XmlModuleDescriptorParser.getInstance());
    }

    /**
     * Adds a the given parser to this registry.
     * 
     * @param parser
     *            the parser to add
     */
    public void addParser(ModuleDescriptorParser parser) {
        /*
         * The parser is added in the front of the list of parsers. This is necessary because the
         * XmlModuleDescriptorParser accepts all resources!
         */
        _parsers.add(0, parser);
    }

    public ModuleDescriptorParser[] getParsers() {
        return (ModuleDescriptorParser[]) _parsers.toArray(new ModuleDescriptorParser[_parsers
                .size()]);
    }

    public ModuleDescriptorParser getParser(Resource res) {
        for (Iterator iter = _parsers.iterator(); iter.hasNext();) {
            ModuleDescriptorParser parser = (ModuleDescriptorParser) iter.next();
            if (parser.accept(res)) {
                return parser;
            }
        }
        return null;
    }

    public ModuleDescriptor parseDescriptor(ParserSettings settings, URL descriptorURL, Resource res,
            boolean validate) throws ParseException, IOException {
        ModuleDescriptorParser parser = getParser(res);
        if (parser == null) {
            Message.warn("no module descriptor parser found for " + res);
            return null;
        }
        return parser.parseDescriptor(settings, descriptorURL, res, validate);
    }

    public boolean accept(Resource res) {
        return getParser(res) != null;
    }

    public void toIvyFile(InputStream is, Resource res, File destFile, ModuleDescriptor md)
            throws ParseException, IOException {
        ModuleDescriptorParser parser = getParser(res);
        if (parser == null) {
            Message.warn("no module descriptor parser found for " + res);
        } else {
            parser.toIvyFile(is, res, destFile, md);
        }
    }

}

@@ -1354,16 +1354,22 @@
 %0Apublic 
+final 
 class Mo
@@ -1449,32 +1449,38 @@
  private static 
+final 
 ModuleDescriptor
@@ -1503,16 +1503,25 @@
 NSTANCE 
+%0A        
 = new Mo
@@ -1667,17 +1667,16 @@
 te List 
-_
 parsers 
@@ -1743,33 +1743,32 @@
 try() %7B%0A        
-_
 parsers.add(PomM
@@ -1805,33 +1805,32 @@
 nce());%0A        
-_
 parsers.add(XmlM
@@ -2252,17 +2252,16 @@
         
-_
 parsers.
@@ -2376,17 +2376,16 @@
 rser%5B%5D) 
-_
 parsers.
@@ -2419,17 +2419,16 @@
 rParser%5B
-_
 parsers%0A
@@ -2554,9 +2554,8 @@
 r = 
-_
 pars
@@ -2880,16 +2880,29 @@
 torURL, 
+%0A            
 Resource
@@ -2906,28 +2906,16 @@
 rce res,
-%0A           
  boolean

fix style