001package org.apache.maven.doxia.parser.module;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collection;
023import java.util.LinkedHashMap;
024import java.util.Map;
025
026import org.codehaus.plexus.component.annotations.Component;
027import org.codehaus.plexus.component.annotations.Requirement;
028
029/**
030 * Simple implementation of the ParserModuleManager interface.
031 *
032 * @since 1.6
033 */
034@Component( role = ParserModuleManager.class )
035public class DefaultParserModuleManager
036    implements ParserModuleManager
037{
038    @Requirement( role = ParserModule.class )
039    private Map<String, ParserModule> parserModules;
040
041    private Collection<ParserModule> parserModulesValues;
042
043    /** {@inheritDoc} */
044    public Collection<ParserModule> getParserModules()
045    {
046        if ( parserModulesValues == null )
047        {
048            Map<Class<?>, ParserModule> parserModulesTmp = new LinkedHashMap<Class<?>, ParserModule>();
049            for ( ParserModule module : parserModules.values() )
050            {
051                parserModulesTmp.put( module.getClass(), module );
052            }
053            parserModulesValues = parserModulesTmp.values();
054        }
055
056        return parserModulesValues;
057    }
058
059    /** {@inheritDoc} */
060    public ParserModule getParserModule( String id )
061        throws ParserModuleNotFoundException
062    {
063        ParserModule parserModule = parserModules.get( id );
064
065        if ( parserModule == null )
066        {
067            throw new ParserModuleNotFoundException( "Cannot find parser module id = " + id );
068        }
069
070        return parserModule;
071    }
072}