-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#3] start implementing comment formatting.
- Loading branch information
Showing
5 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
src/main/java/org/codehaus/mojo/tidy/task/EnsureCommentsOnSingleLine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.codehaus.mojo.tidy.task; | ||
|
||
import static org.codehaus.mojo.tidy.task.XMLEventReaderFactory.createEventReaderForPom; | ||
|
||
import javax.xml.stream.Location; | ||
import javax.xml.stream.XMLEventReader; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.events.XMLEvent; | ||
import java.util.Collections; | ||
|
||
public class EnsureCommentsOnSingleLine implements TidyTask | ||
{ | ||
|
||
@Override | ||
public String tidyPom( String pom, Format format ) throws XMLStreamException | ||
{ | ||
XMLEventReader eventReader = createEventReaderForPom( pom ); | ||
try | ||
{ | ||
return moveTrailingComments( pom, eventReader, format ); | ||
} | ||
finally | ||
{ | ||
eventReader.close(); | ||
} | ||
} | ||
|
||
private String moveTrailingComments( String pom, XMLEventReader reader, Format format ) throws XMLStreamException | ||
{ | ||
StringBuilder outputPom = new StringBuilder(); | ||
XMLEvent previousEvent = null; | ||
int indent = 0; | ||
int startPrevEl = 0; | ||
int startCurrentEl; | ||
|
||
while ( reader.hasNext() ) | ||
{ | ||
XMLEvent event = reader.nextEvent(); | ||
final Location location = event.getLocation(); | ||
startCurrentEl = location.getCharacterOffset(); | ||
switch ( event.getEventType() ) | ||
{ | ||
case XMLEvent.COMMENT: | ||
final int startOfNextElement = getPosOfNextEvent( reader ); | ||
|
||
if ( previousElementInSameRow( location, previousEvent )) | ||
{ | ||
final String whitespace = String.join( "", Collections.nCopies( indent, " " ) ); | ||
final String textToInsert = | ||
pom.substring( startCurrentEl, startOfNextElement ) | ||
+ format.getLineSeparator() | ||
+ whitespace; | ||
outputPom.insert( startPrevEl, textToInsert ); | ||
} | ||
else | ||
{ | ||
outputPom.append( pom, startCurrentEl, startOfNextElement ); | ||
} | ||
|
||
break; | ||
|
||
case XMLEvent.END_DOCUMENT: | ||
outputPom.append( pom.substring( startCurrentEl ) ); | ||
break; | ||
|
||
case XMLEvent.START_ELEMENT: | ||
startPrevEl = startCurrentEl; | ||
indent = location.getColumnNumber() - 1; | ||
// pass-through | ||
default: | ||
final int next = getPosOfNextEvent( reader ); | ||
previousEvent = event; | ||
|
||
outputPom.append( pom, startCurrentEl, next ); | ||
break; | ||
} | ||
} | ||
|
||
return outputPom.toString(); | ||
} | ||
|
||
private boolean previousElementInSameRow( Location location, XMLEvent previousElement ) | ||
{ | ||
if ( previousElement == null ) | ||
{ | ||
return false; | ||
} | ||
|
||
return location.getLineNumber() == previousElement.getLocation().getLineNumber(); | ||
} | ||
|
||
private boolean nextElementInSameRow( Location location, XMLEventReader reader ) throws XMLStreamException | ||
{ | ||
final XMLEvent peek = reader.peek(); | ||
|
||
if ( peek == null ) | ||
{ | ||
return false; | ||
} | ||
|
||
if ( peek.getEventType() != XMLEvent.START_ELEMENT ) | ||
{ | ||
return false; | ||
} | ||
|
||
return peek.getLocation().getLineNumber() == location.getLineNumber(); | ||
} | ||
|
||
private int getPosOfNextEvent( XMLEventReader reader ) | ||
throws XMLStreamException | ||
{ | ||
return reader.peek().getLocation().getCharacterOffset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/resources/org/codehaus/mojo/tidy/task/TIDY-3_comment/pom-expected.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.codehaus.mojo.tidy.its</groupId> | ||
<artifactId>pom-space-indent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<description>Test of tidy:pom on a pom with XML comments.</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<!-- bla bla 1 --> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.integration</groupId> | ||
<artifactId>spring-integration-java-dsl</artifactId> | ||
<!-- bla bla 2 --> | ||
<version>1.1.2.RELEASE</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
29 changes: 29 additions & 0 deletions
29
src/test/resources/org/codehaus/mojo/tidy/task/TIDY-3_comment/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.codehaus.mojo.tidy.its</groupId> | ||
<artifactId>pom-space-indent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<description>Test of tidy:pom on a pom with XML comments.</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<!-- bla bla 1 --> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.integration</groupId> | ||
<artifactId>spring-integration-java-dsl</artifactId> | ||
<version>1.1.2.RELEASE</version> <!-- bla bla 2 --> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |