Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOXIA-614] support source reference in doxia parser #35

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 22 additions & 11 deletions doxia-core/src/main/java/org/apache/maven/doxia/Doxia.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,35 @@ public interface Doxia
* 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.
* You could use <code>newReader</code> methods from {@link org.codehaus.plexus.util.ReaderFactory}.
* @param parserId Identifier for the parser to use.
* @param sink A sink that consumes the Doxia events.
* @throws org.apache.maven.doxia.parser.manager.ParserNotFoundException
* if no parser could be found for the given id.
* @throws org.apache.maven.doxia.parser.ParseException if the model could not be parsed.
* @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
* @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 )
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 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably doesn't matter a great deal, but adding methods to interfaces is not backwards compatible

throws ParserNotFoundException, ParseException;

/**
* Return a parser for the given <code>parserId</code>.
*
* @param parserId Identifier for the parser to use.
* @return the parser defining by parserId.
* @throws org.apache.maven.doxia.parser.manager.ParserNotFoundException
* if no parser could be found for the given id.
* @param parserId identifier for the parser to use
* @return the parser identified by parserId
* @throws ParserNotFoundException if no parser could be found for the given id
*/
Parser getParser( String parserId )
throws ParserNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public abstract class AbstractParser
/**
* {@inheritDoc}
*
* @return a int.
* @return a int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just delete this, if there's nothing more to say than the type

*/
public int getType()
{
Expand All @@ -117,7 +117,7 @@ public void setEmitComments( boolean emitComments )
/**
* <p>isEmitComments.</p>
*
* @return a boolean.
* @return a boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

*/
public boolean isEmitComments()
{
Expand All @@ -127,11 +127,11 @@ public boolean isEmitComments()
/**
* Execute a macro on the given sink.
*
* @param macroId An id to lookup the macro.
* @param request The corresponding MacroRequest.
* @param sink The sink to receive the events.
* @throws org.apache.maven.doxia.macro.MacroExecutionException if an error occurred during execution.
* @throws org.apache.maven.doxia.macro.manager.MacroNotFoundException if the macro could not be found.
* @param macroId an id to lookup the macro
* @param request the corresponding MacroRequest
* @param sink the sink to receive the events
* @throws org.apache.maven.doxia.macro.MacroExecutionException if an error occurred during execution
* @throws org.apache.maven.doxia.macro.manager.MacroNotFoundException if the macro could not be found
*/
// Made public right now because of the structure of the APT parser and
// all its inner classes.
Expand All @@ -148,7 +148,7 @@ public void executeMacro( String macroId, MacroRequest request, Sink sink )
/**
* Returns the current base directory.
*
* @return The base directory.
* @return the base directory
* @deprecated this does not work in multi-module builds, see DOXIA-373
*/
protected File getBasedir()
Expand All @@ -171,29 +171,46 @@ protected File getBasedir()
*
* Convenience method to parse an arbitrary string and emit events into the given sink.
*
* @param string A string that provides the source input.
* @param sink A sink that consumes the Doxia events.
* @throws org.apache.maven.doxia.parser.ParseException if the string could not be parsed.
* @param string a string that provides the source input
* @param sink a sink that consumes the Doxia events
* @throws org.apache.maven.doxia.parser.ParseException if the string could not be parsed
* @since 1.1
*/
public void parse( String string, Sink sink )
throws ParseException
{
parse( new StringReader( string ), sink );
this.parse( string, sink, null );
}


/**
* {@inheritDoc}
*
* Convenience method to parse an arbitrary string and emit events into the given sink.
*
* @param string a string that provides the source input
* @param sink a sink that consumes the Doxia events
* @param reference a string containing the reference to the source of the input string (e.g. filename)
* @throws org.apache.maven.doxia.parser.ParseException if the string could not be parsed
* @since 1.9.2
*/
public void parse( String string, Sink sink, String reference )
throws ParseException
{
parse( new StringReader( string ), sink, reference );
}

/** {@inheritDoc} */
@Override
public void parse( Reader source, Sink sink, String reference )
public void parse( Reader source, Sink sink )
throws ParseException
{
parse( source, sink );
parse( source, sink, null );
}

/**
* Set <code>secondParsing</code> to true, if we need a second parsing.
*
* @param second True for second parsing.
* @param second true for second parsing
*/
public void setSecondParsing( boolean second )
{
Expand All @@ -203,7 +220,7 @@ public void setSecondParsing( boolean second )
/**
* Indicates if we are currently parsing a second time.
*
* @return true if we are currently parsing a second time.
* @return true if we are currently parsing a second time
* @since 1.1
*/
protected boolean isSecondParsing()
Expand Down Expand Up @@ -237,7 +254,7 @@ protected Log getLog()
/**
* Gets the current {@link MacroManager}.
*
* @return The current {@link MacroManager}.
* @return the current {@link MacroManager}
* @since 1.1
*/
protected MacroManager getMacroManager()
Expand All @@ -260,7 +277,7 @@ protected void init()
/**
* The current Doxia version.
*
* @return the current Doxia version as a String.
* @return the current Doxia version as a String
* @since 1.2
*/
protected static String doxiaVersion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public abstract class AbstractXmlParser
private boolean validate = false;

/** {@inheritDoc} */
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
slachiewicz marked this conversation as resolved.
Show resolved Hide resolved
throws ParseException
{
init();
Expand Down Expand Up @@ -153,7 +153,7 @@ public void parse( Reader source, Sink sink )
setSecondParsing( false );
init();
}

/**
* Initializes the parser with custom entities or other options.
*
Expand All @@ -166,18 +166,6 @@ protected void initXmlParser( XmlPullParser parser )
// nop
}

/**
* {@inheritDoc}
*
* Convenience method to parse an arbitrary string and emit any xml events into the given sink.
*/
@Override
public void parse( String string, Sink sink )
throws ParseException
{
super.parse( string, sink );
}

/** {@inheritDoc} */
@Override
public final int getType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ public class Xhtml5BaseParser

/** {@inheritDoc} */
@Override
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to follow from the diff, but are any of the apparent method signature changes real? Or is it just Github diff being funky?

If these are real changes is it possible to add overloads with the old variants that pass null for reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really.

To see the changes probably, the image in the PR description summarizes the changes (@ means overrride method), at least it helped me to track the changes.
The problems is the parse(String,String,String) method in AbstractParser was calling parse(String, String) without the third argument. So the "reference" was totally lost and no one could use it.
Even with fixing the call in AbstractParser, the problem is that all implementations add logic in their respective parse and relly on the others using super.parse(String,String) calls. So only that change would mean that someone overriding Xhtml5Parser::parse(String,String,String) would find a total different behauviour than extending the 2 args method because the will be extending the AbstractParser really and missing a lot of logic.

The only solution I saw was to replace 2 args method by the 3 args in all implementations and call the 3 args in the AbstractParser with null. That makes reference available and should not brake current doxia components.

throws ParseException
{
init();

try
{
super.parse( source, sink );
super.parse( source, sink, reference );
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public class XhtmlBaseParser

/** {@inheritDoc} */
@Override
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
throws ParseException
{
init();

try
{
super.parse( source, sink );
super.parse( source, sink, reference );
}
finally
{
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 );
slachiewicz marked this conversation as resolved.
Show resolved Hide resolved
fail( "Call should fail with ParserNotFoundException" );
}
catch ( ParserNotFoundException e )
{
assertEquals( "Cannot find parser with id = a-parser", e.getMessage() );
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class FmlParser
private Map<String, Object> macroParameters = new HashMap<>();

/** {@inheritDoc} */
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
throws ParseException
{
this.faqs = null;
Expand Down Expand Up @@ -118,7 +118,7 @@ public void parse( Reader source, Sink sink )
this.faqs = new Faqs();

// this populates faqs
super.parse( tmp, sink );
super.parse( tmp, sink, reference );

writeFaqs( sink );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public int getType()

/** {@inheritDoc} */
@Override
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
throws ParseException
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class XdocParser
private boolean hasTitle;

/** {@inheritDoc} */
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
throws ParseException
{
this.sourceContent = null;
Expand All @@ -110,7 +110,7 @@ public void parse( Reader source, Sink sink )

try
{
super.parse( new StringReader( sourceContent ), sink );
super.parse( new StringReader( sourceContent ), sink, reference );
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ protected void init()
}

/** {@inheritDoc} */
public void parse( Reader source, Sink sink )
public void parse( Reader source, Sink sink, String reference )
throws ParseException
{
this.sourceContent = null;
Expand All @@ -355,7 +355,7 @@ public void parse( Reader source, Sink sink )

try
{
super.parse( new StringReader( sourceContent ), sink );
super.parse( new StringReader( sourceContent ), sink, reference );
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.FileFilter;
import java.io.StringReader;
import java.util.Iterator;
import java.util.regex.Pattern;

Expand Down
Loading