You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Robert Scholte <ap...@sourcegrounds.com> on 2012/02/01 22:33:15 UTC

AbstractRewritePomsPhase.transformProject XML-parser

Hi

while trying to resolve some MRELEASE issues until I hit the  
org.apache.maven.shared.release.phase.AbstractRewritePomsPhase.transformProject(MavenProject,  
ReleaseDescriptor, ReleaseEnvironment, List<MavenProject>, boolean,  
ReleaseResult), see below.

It kind of scared me. Shouldn't there be a better solution by now, a  
better XML-parser/transformer to solve this? Anyone any suggestion?
Does anyone know if this part of the code is covered enough in order to  
give it a try to replace it?

I'm pretty sure that a solid implementation could solve several issues.

-Robert


private void transformProject( MavenProject project, ReleaseDescriptor  
releaseDescriptor,
                                    ReleaseEnvironment releaseEnvironment,  
List<MavenProject> reactorProjects,
                                    boolean simulate, ReleaseResult result )
         throws ReleaseExecutionException, ReleaseFailureException
     {
         Document document;
         String intro = null;
         String outtro = null;
         try
         {
             String content = ReleaseUtil.readXmlFile(  
ReleaseUtil.getStandardPom( project ), ls );
             // we need to eliminate any extra whitespace inside elements,  
as JDOM will nuke it
             content = content.replaceAll( "<([^!][^>]*?)\\s{2,}([^>]*?)>",  
"<$1 $2>" );
             content = content.replaceAll( "(\\s{2,}|[^\\s])/>", "$1 />" );

             SAXBuilder builder = new SAXBuilder();
             document = builder.build( new StringReader( content ) );

             // Normalize line endings to platform's style (XML processors  
like JDOM normalize line endings to "\n" as
             // per section 2.11 of the XML spec)
             normaliseLineEndings( document );

             // rewrite DOM as a string to find differences, since text  
outside the root element is not tracked
             StringWriter w = new StringWriter();
             Format format = Format.getRawFormat();
             format.setLineSeparator( ls );
             XMLOutputter out = new XMLOutputter( format );
             out.output( document.getRootElement(), w );

             int index = content.indexOf( w.toString() );
             if ( index >= 0 )
             {
                 intro = content.substring( 0, index );
                 outtro = content.substring( index + w.toString().length()  
);
             }
             else
             {
                 /*
                  * NOTE: Due to whitespace, attribute reordering or entity  
expansion the above indexOf test can easily
                  * fail. So let's try harder. Maybe some day, when JDOM  
offers a StaxBuilder and this builder employes
                  * XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, this whole  
mess can be avoided.
                  */
                 final String SPACE = "\\s++";
                 final String XML =  
"<\\?(?:(?:[^\"'>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+>";
                 final String INTSUB =  
"\\[(?:(?:[^\"'\\]]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+\\]";
                 final String DOCTYPE =
                     "<!DOCTYPE(?:(?:[^\"'\\[>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+')|(?:"  
+ INTSUB + "))*+>";
                 final String PI = XML;
                 final String COMMENT = "<!--(?:[^-]|(?:-[^-]))*+-->";

                 final String INTRO =
                     "(?:(?:" + SPACE + ")|(?:" + XML + ")|(?:" + DOCTYPE +  
")|(?:" + COMMENT + ")|(?:" + PI + "))*";
                 final String OUTRO = "(?:(?:" + SPACE + ")|(?:" + COMMENT  
+ ")|(?:" + PI + "))*";
                 final String POM = "(?s)(" + INTRO + ")(.*?)(" + OUTRO +  
")";

                 Matcher matcher = Pattern.compile( POM ).matcher( content  
);
                 if ( matcher.matches() )
                 {
                     intro = matcher.group( 1 );
                     outtro = matcher.group( matcher.groupCount() );
                 }
             }
         }
         catch ( JDOMException e )
         {
             throw new ReleaseExecutionException( "Error reading POM: " +  
e.getMessage(), e );
         }
         catch ( IOException e )
         {
             throw new ReleaseExecutionException( "Error reading POM: " +  
e.getMessage(), e );
         }

         ScmRepository scmRepository;
         ScmProvider provider;
         try
         {
             scmRepository =  
scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor,
                                                                                releaseEnvironment.getSettings()  
);

             provider = scmRepositoryConfigurator.getRepositoryProvider(  
scmRepository );
         }
         catch ( ScmRepositoryException e )
         {
             throw new ReleaseScmRepositoryException( e.getMessage(),  
e.getValidationMessages() );
         }
         catch ( NoSuchScmProviderException e )
         {
             throw new ReleaseExecutionException( "Unable to configure SCM  
repository: " + e.getMessage(), e );
         }

         transformDocument( project, document.getRootElement(),  
releaseDescriptor, reactorProjects, scmRepository,
                            result, simulate );

         File pomFile = ReleaseUtil.getStandardPom( project );

         if ( simulate )
         {
             File outputFile = new File( pomFile.getParentFile(),  
pomFile.getName() + "." + pomSuffix );
             writePom( outputFile, document, releaseDescriptor,  
project.getModelVersion(), intro, outtro );
         }
         else
         {
             writePom( pomFile, document, releaseDescriptor,  
project.getModelVersion(), intro, outtro, scmRepository,
                       provider );
         }
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: AbstractRewritePomsPhase.transformProject XML-parser

Posted by Robert Scholte <ap...@sourcegrounds.com>.
Some googling points me to Joost[1]
Based on version (0.9.1) and its release-date (2009-09-26) I don't think  
this would be a wise choice.
I'll have a look at the versions-m-p

-Robert

[1] http://joost.sourceforge.net/


On Wed, 01 Feb 2012 22:51:22 +0100, Stephen Connolly  
<st...@gmail.com> wrote:

> Some of the pom rewriting tricks I use in v-m-p might help... but
> ultimately I think the solution lies with something like
> xevpp.codehaus.org (if I ever get time to finish that :rolleyes: :-) )
>
> On 1 February 2012 21:33, Robert Scholte <ap...@sourcegrounds.com>  
> wrote:
>> Hi
>>
>> while trying to resolve some MRELEASE issues until I hit the
>> org.apache.maven.shared.release.phase.AbstractRewritePomsPhase.transformProject(MavenProject,
>> ReleaseDescriptor, ReleaseEnvironment, List<MavenProject>, boolean,
>> ReleaseResult), see below.
>>
>> It kind of scared me. Shouldn't there be a better solution by now, a  
>> better
>> XML-parser/transformer to solve this? Anyone any suggestion?
>> Does anyone know if this part of the code is covered enough in order to  
>> give
>> it a try to replace it?
>>
>> I'm pretty sure that a solid implementation could solve several issues.
>>
>> -Robert
>>
>>
>> private void transformProject( MavenProject project, ReleaseDescriptor
>> releaseDescriptor,
>>                                   ReleaseEnvironment releaseEnvironment,
>> List<MavenProject> reactorProjects,
>>                                   boolean simulate, ReleaseResult  
>> result )
>>        throws ReleaseExecutionException, ReleaseFailureException
>>    {
>>        Document document;
>>        String intro = null;
>>        String outtro = null;
>>        try
>>        {
>>            String content = ReleaseUtil.readXmlFile(
>> ReleaseUtil.getStandardPom( project ), ls );
>>            // we need to eliminate any extra whitespace inside  
>> elements, as
>> JDOM will nuke it
>>            content = content.replaceAll(  
>> "<([^!][^>]*?)\\s{2,}([^>]*?)>",
>> "<$1 $2>" );
>>            content = content.replaceAll( "(\\s{2,}|[^\\s])/>", "$1 />"  
>> );
>>
>>            SAXBuilder builder = new SAXBuilder();
>>            document = builder.build( new StringReader( content ) );
>>
>>            // Normalize line endings to platform's style (XML processors
>> like JDOM normalize line endings to "\n" as
>>            // per section 2.11 of the XML spec)
>>            normaliseLineEndings( document );
>>
>>            // rewrite DOM as a string to find differences, since text
>> outside the root element is not tracked
>>            StringWriter w = new StringWriter();
>>            Format format = Format.getRawFormat();
>>            format.setLineSeparator( ls );
>>            XMLOutputter out = new XMLOutputter( format );
>>            out.output( document.getRootElement(), w );
>>
>>            int index = content.indexOf( w.toString() );
>>            if ( index >= 0 )
>>            {
>>                intro = content.substring( 0, index );
>>                outtro = content.substring( index +  
>> w.toString().length() );
>>            }
>>            else
>>            {
>>                /*
>>                 * NOTE: Due to whitespace, attribute reordering or  
>> entity
>> expansion the above indexOf test can easily
>>                 * fail. So let's try harder. Maybe some day, when JDOM
>> offers a StaxBuilder and this builder employes
>>                 * XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, this  
>> whole
>> mess can be avoided.
>>                 */
>>                final String SPACE = "\\s++";
>>                final String XML =
>> "<\\?(?:(?:[^\"'>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+>";
>>                final String INTSUB =
>> "\\[(?:(?:[^\"'\\]]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+\\]";
>>                final String DOCTYPE =
>>
>>  "<!DOCTYPE(?:(?:[^\"'\\[>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+')|(?:" +  
>> INTSUB +
>> "))*+>";
>>                final String PI = XML;
>>                final String COMMENT = "<!--(?:[^-]|(?:-[^-]))*+-->";
>>
>>                final String INTRO =
>>                    "(?:(?:" + SPACE + ")|(?:" + XML + ")|(?:" + DOCTYPE  
>> +
>> ")|(?:" + COMMENT + ")|(?:" + PI + "))*";
>>                final String OUTRO = "(?:(?:" + SPACE + ")|(?:" +  
>> COMMENT +
>> ")|(?:" + PI + "))*";
>>                final String POM = "(?s)(" + INTRO + ")(.*?)(" + OUTRO +  
>> ")";
>>
>>                Matcher matcher = Pattern.compile( POM ).matcher(  
>> content );
>>                if ( matcher.matches() )
>>                {
>>                    intro = matcher.group( 1 );
>>                    outtro = matcher.group( matcher.groupCount() );
>>                }
>>            }
>>        }
>>        catch ( JDOMException e )
>>        {
>>            throw new ReleaseExecutionException( "Error reading POM: " +
>> e.getMessage(), e );
>>        }
>>        catch ( IOException e )
>>        {
>>            throw new ReleaseExecutionException( "Error reading POM: " +
>> e.getMessage(), e );
>>        }
>>
>>        ScmRepository scmRepository;
>>        ScmProvider provider;
>>        try
>>        {
>>            scmRepository =
>> scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor,
>>
>>   releaseEnvironment.getSettings() );
>>
>>            provider = scmRepositoryConfigurator.getRepositoryProvider(
>> scmRepository );
>>        }
>>        catch ( ScmRepositoryException e )
>>        {
>>            throw new ReleaseScmRepositoryException( e.getMessage(),
>> e.getValidationMessages() );
>>        }
>>        catch ( NoSuchScmProviderException e )
>>        {
>>            throw new ReleaseExecutionException( "Unable to configure SCM
>> repository: " + e.getMessage(), e );
>>        }
>>
>>        transformDocument( project, document.getRootElement(),
>> releaseDescriptor, reactorProjects, scmRepository,
>>                           result, simulate );
>>
>>        File pomFile = ReleaseUtil.getStandardPom( project );
>>
>>        if ( simulate )
>>        {
>>            File outputFile = new File( pomFile.getParentFile(),
>> pomFile.getName() + "." + pomSuffix );
>>            writePom( outputFile, document, releaseDescriptor,
>> project.getModelVersion(), intro, outtro );
>>        }
>>        else
>>        {
>>            writePom( pomFile, document, releaseDescriptor,
>> project.getModelVersion(), intro, outtro, scmRepository,
>>                      provider );
>>        }
>>    }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: AbstractRewritePomsPhase.transformProject XML-parser

Posted by Stephen Connolly <st...@gmail.com>.
Some of the pom rewriting tricks I use in v-m-p might help... but
ultimately I think the solution lies with something like
xevpp.codehaus.org (if I ever get time to finish that :rolleyes: :-) )

On 1 February 2012 21:33, Robert Scholte <ap...@sourcegrounds.com> wrote:
> Hi
>
> while trying to resolve some MRELEASE issues until I hit the
> org.apache.maven.shared.release.phase.AbstractRewritePomsPhase.transformProject(MavenProject,
> ReleaseDescriptor, ReleaseEnvironment, List<MavenProject>, boolean,
> ReleaseResult), see below.
>
> It kind of scared me. Shouldn't there be a better solution by now, a better
> XML-parser/transformer to solve this? Anyone any suggestion?
> Does anyone know if this part of the code is covered enough in order to give
> it a try to replace it?
>
> I'm pretty sure that a solid implementation could solve several issues.
>
> -Robert
>
>
> private void transformProject( MavenProject project, ReleaseDescriptor
> releaseDescriptor,
>                                   ReleaseEnvironment releaseEnvironment,
> List<MavenProject> reactorProjects,
>                                   boolean simulate, ReleaseResult result )
>        throws ReleaseExecutionException, ReleaseFailureException
>    {
>        Document document;
>        String intro = null;
>        String outtro = null;
>        try
>        {
>            String content = ReleaseUtil.readXmlFile(
> ReleaseUtil.getStandardPom( project ), ls );
>            // we need to eliminate any extra whitespace inside elements, as
> JDOM will nuke it
>            content = content.replaceAll( "<([^!][^>]*?)\\s{2,}([^>]*?)>",
> "<$1 $2>" );
>            content = content.replaceAll( "(\\s{2,}|[^\\s])/>", "$1 />" );
>
>            SAXBuilder builder = new SAXBuilder();
>            document = builder.build( new StringReader( content ) );
>
>            // Normalize line endings to platform's style (XML processors
> like JDOM normalize line endings to "\n" as
>            // per section 2.11 of the XML spec)
>            normaliseLineEndings( document );
>
>            // rewrite DOM as a string to find differences, since text
> outside the root element is not tracked
>            StringWriter w = new StringWriter();
>            Format format = Format.getRawFormat();
>            format.setLineSeparator( ls );
>            XMLOutputter out = new XMLOutputter( format );
>            out.output( document.getRootElement(), w );
>
>            int index = content.indexOf( w.toString() );
>            if ( index >= 0 )
>            {
>                intro = content.substring( 0, index );
>                outtro = content.substring( index + w.toString().length() );
>            }
>            else
>            {
>                /*
>                 * NOTE: Due to whitespace, attribute reordering or entity
> expansion the above indexOf test can easily
>                 * fail. So let's try harder. Maybe some day, when JDOM
> offers a StaxBuilder and this builder employes
>                 * XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, this whole
> mess can be avoided.
>                 */
>                final String SPACE = "\\s++";
>                final String XML =
> "<\\?(?:(?:[^\"'>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+>";
>                final String INTSUB =
> "\\[(?:(?:[^\"'\\]]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+\\]";
>                final String DOCTYPE =
>
>  "<!DOCTYPE(?:(?:[^\"'\\[>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+')|(?:" + INTSUB +
> "))*+>";
>                final String PI = XML;
>                final String COMMENT = "<!--(?:[^-]|(?:-[^-]))*+-->";
>
>                final String INTRO =
>                    "(?:(?:" + SPACE + ")|(?:" + XML + ")|(?:" + DOCTYPE +
> ")|(?:" + COMMENT + ")|(?:" + PI + "))*";
>                final String OUTRO = "(?:(?:" + SPACE + ")|(?:" + COMMENT +
> ")|(?:" + PI + "))*";
>                final String POM = "(?s)(" + INTRO + ")(.*?)(" + OUTRO + ")";
>
>                Matcher matcher = Pattern.compile( POM ).matcher( content );
>                if ( matcher.matches() )
>                {
>                    intro = matcher.group( 1 );
>                    outtro = matcher.group( matcher.groupCount() );
>                }
>            }
>        }
>        catch ( JDOMException e )
>        {
>            throw new ReleaseExecutionException( "Error reading POM: " +
> e.getMessage(), e );
>        }
>        catch ( IOException e )
>        {
>            throw new ReleaseExecutionException( "Error reading POM: " +
> e.getMessage(), e );
>        }
>
>        ScmRepository scmRepository;
>        ScmProvider provider;
>        try
>        {
>            scmRepository =
> scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor,
>
>   releaseEnvironment.getSettings() );
>
>            provider = scmRepositoryConfigurator.getRepositoryProvider(
> scmRepository );
>        }
>        catch ( ScmRepositoryException e )
>        {
>            throw new ReleaseScmRepositoryException( e.getMessage(),
> e.getValidationMessages() );
>        }
>        catch ( NoSuchScmProviderException e )
>        {
>            throw new ReleaseExecutionException( "Unable to configure SCM
> repository: " + e.getMessage(), e );
>        }
>
>        transformDocument( project, document.getRootElement(),
> releaseDescriptor, reactorProjects, scmRepository,
>                           result, simulate );
>
>        File pomFile = ReleaseUtil.getStandardPom( project );
>
>        if ( simulate )
>        {
>            File outputFile = new File( pomFile.getParentFile(),
> pomFile.getName() + "." + pomSuffix );
>            writePom( outputFile, document, releaseDescriptor,
> project.getModelVersion(), intro, outtro );
>        }
>        else
>        {
>            writePom( pomFile, document, releaseDescriptor,
> project.getModelVersion(), intro, outtro, scmRepository,
>                      provider );
>        }
>    }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org