Skip to content

Commit

Permalink
[DOXIA-614] Support passing source reference to Doxia instance
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
abelsromero authored and slachiewicz committed May 30, 2021
1 parent cb017d8 commit 497a438
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ public class DefaultDoxia
/** {@inheritDoc} */
public void parse( Reader source, String parserId, Sink sink )
throws ParserNotFoundException, ParseException
{
this.parse( source, parserId, sink, null );
}

/** {@inheritDoc} */
@Override
public void parse( Reader source, String parserId, Sink sink, String reference )
throws ParserNotFoundException, ParseException
{
Parser parser = parserManager.getParser( parserId );

parser.enableLogging( new PlexusLoggerWrapper( getLogger() ) );

parser.parse( source, sink );
parser.parse( source, sink, reference );
}

/** {@inheritDoc} */
Expand Down
14 changes: 14 additions & 0 deletions doxia-core/src/main/java/org/apache/maven/doxia/Doxia.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public interface Doxia
void parse( Reader source, String parserId, Sink sink )
throws ParserNotFoundException, ParseException;

/**
* Parses the given source model using a parser with given id,
* and emits Doxia events into the given sink.
*
* @param source not null reader that provides the source document
* @param parserId identifier for the parser to use
* @param sink a sink that consumes the Doxia events
* @param reference string containing the reference to the source (e.g. filename)
* @throws ParserNotFoundException if no parser could be found for the given id
* @throws ParseException if the model could not be parsed
*/
void parse( Reader source, String parserId, Sink sink, String reference )
throws ParserNotFoundException, ParseException;

/**
* Return a parser for the given <code>parserId</code>.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.apache.maven.doxia;

/*
* 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.
*/

import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
import org.codehaus.plexus.PlexusTestCase;
import org.junit.Test;

public class DefaultDoxiaTest extends PlexusTestCase
{

@Test
public void testCreatesDefaultDoxia()
{
final DefaultDoxia defaultDoxia = new DefaultDoxia();

assertNotNull( defaultDoxia );
}

@Test
public void testFailsWhenParserIdDoesNotExist() throws Exception
{
final String parserId = "a-parser";
final Doxia doxia = lookup( Doxia.class );

try
{
doxia.getParser( parserId );
fail( "Call should fail with ParserNotFoundException" );
}
catch ( ParserNotFoundException e )
{
assertEquals( "Cannot find parser with id = a-parser", e.getMessage() );
}
}

}

0 comments on commit 497a438

Please # to comment.