001package org.apache.maven.doxia;
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 org.apache.maven.doxia.logging.PlexusLoggerWrapper;
023import org.apache.maven.doxia.parser.ParseException;
024import org.apache.maven.doxia.parser.Parser;
025import org.apache.maven.doxia.parser.manager.ParserManager;
026import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
027import org.apache.maven.doxia.sink.Sink;
028
029import org.codehaus.plexus.component.annotations.Component;
030import org.codehaus.plexus.component.annotations.Requirement;
031import org.codehaus.plexus.logging.AbstractLogEnabled;
032
033import java.io.Reader;
034
035/**
036 * Simple implementation of the Doxia interface:
037 * uses a ParserManager to lookup a parser.
038 *
039 * @author Jason van Zyl
040 * @since 1.0
041 */
042@Component( role = Doxia.class )
043public class DefaultDoxia
044    extends AbstractLogEnabled
045    implements Doxia
046{
047    @Requirement
048    private ParserManager parserManager;
049
050    // ----------------------------------------------------------------------
051    // This remains because the sinks are not threadsafe which they probably
052    // should be. In some places a constructor is used to initialize a sink
053    // which can probably be done away with.
054    // ----------------------------------------------------------------------
055
056    /** {@inheritDoc} */
057    public void parse( Reader source, String parserId, Sink sink )
058        throws ParserNotFoundException, ParseException
059    {
060        this.parse( source, parserId, sink, null );
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public void parse( Reader source, String parserId, Sink sink, String reference )
066        throws ParserNotFoundException, ParseException
067    {
068        Parser parser = parserManager.getParser( parserId );
069
070        parser.enableLogging( new PlexusLoggerWrapper( getLogger() ) );
071
072        parser.parse( source, sink, reference );
073    }
074
075    /** {@inheritDoc} */
076    public Parser getParser( String parserId )
077        throws ParserNotFoundException
078    {
079        return parserManager.getParser( parserId );
080    }
081}