Skip to content

Some enhancements for issues/14 #15

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 47 additions & 4 deletions src/main/java/org/apache/maven/io/util/WriterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.apache.maven.io.util;

import org.apache.commons.io.IOUtils;
import org.apache.maven.model.PatternSet;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jdom2.CDATA;
Expand All @@ -35,7 +36,7 @@
public final class WriterUtils
{

private static final String INDENT = " ";
private static final String defaultIndent = " ";

private static final String lineSeparator = "\n";

Expand Down Expand Up @@ -248,6 +249,7 @@ public static void insertAtPreferredLocation( final Element parent, final Elemen
lastText = (Text) next;
}
}
String indent = deduceIndent(parent, counter);
if ( lastText != null && lastText.getTextTrim().length() == 0 )
{
lastText = lastText.clone();
Expand All @@ -257,23 +259,64 @@ public static void insertAtPreferredLocation( final Element parent, final Elemen
String starter = lineSeparator;
for ( int i = 0; i < counter.getDepth(); i++ )
{
starter = starter + INDENT; // TODO make settable?
starter = starter + indent; // TODO make settable?
}
lastText = factory.text( starter );
}
if ( parent.getContentSize() == 0 )
{
final Text finalText = lastText.clone();
finalText.setText( finalText.getText().substring( 0, finalText.getText().length() - INDENT.length() ) );
finalText.setText( finalText.getText().substring( 0, finalText.getText().length() - indent.length() ) );
parent.addContent( contentIndex, finalText );
}
parent.addContent( contentIndex, child );
parent.addContent( contentIndex, lastText );
} // -- void insertAtPreferredLocation( Element, Element, Counter )


public static String deduceIndent(Element parent, IndentationCounter counter) {
String indent = defaultIndent;
try {
boolean isDeduced = false;
for (int level = counter.getDepth(); level > 0; level--) {
for (int i = parent.getContentSize() - 1; i >= 0; i--) {
Content content = parent.getContent().get(i);
if (i == parent.getContentSize() - 1) {
continue;
}
if (content instanceof Text) {
String text = content.getValue();
String[] textArr = text.split(IOUtils.LINE_SEPARATOR_UNIX);
if (textArr.length > 0) {
String lineText = textArr[textArr.length - 1];
lineText = lineText.replace(IOUtils.LINE_SEPARATOR_WINDOWS, "").
replace(IOUtils.LINE_SEPARATOR_UNIX, "");
int indentLen = (int) Math.ceil((double) lineText.length() / level);
String lastTest = lineText.substring(0, indentLen);
if (lastTest.trim().length() == 0) {
indent = lastTest;
isDeduced = true;
break;
}
}
}
}
if (isDeduced) {
break;
}
parent = parent.getParentElement();
}
} catch (Exception e) {
System.out.println("Deduce indent failed");
}

return indent;
}


/**
* Method findAndReplaceProperties.
*
*
* @param counter
* @param props
* @param name
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/apache/maven/model/io/jdom/MavenJDOMWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ protected void iterateDependency( final IndentationCounter counter, final Elemen
{
final Iterator it = list.iterator();
Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
int elItCounter = 0;
if ( !elIt.hasNext() )
{
elIt = null;
Expand All @@ -181,6 +182,7 @@ protected void iterateDependency( final IndentationCounter counter, final Elemen
if ( elIt != null && elIt.hasNext() )
{
el = (Element) elIt.next();
elItCounter++;
if ( !elIt.hasNext() )
{
elIt = null;
Expand All @@ -191,6 +193,7 @@ protected void iterateDependency( final IndentationCounter counter, final Elemen
el = factory.element( childTag, element.getNamespace() );
insertAtPreferredLocation( element, el, innerCount );
}
populateDepIfExist(value, childTag, element, el, elItCounter);
updateDependency( value, childTag, innerCount, el );
innerCount.increaseCount();
}
Expand All @@ -205,6 +208,35 @@ protected void iterateDependency( final IndentationCounter counter, final Elemen
}
} // -- void iterateDependency( Counter, Element, java.util.Collection, java.lang.String, java.lang.String )

/***
* If the dependency already exists in doc, will clone the content to the target element, to avoid format issue
* @param dep the dependency in pomModel, which is writing to doc
* @param childTag target element tag
* @param parent dependencies element
* @param targetEle the element which is writing to
*/
private void populateDepIfExist(Dependency dep, String childTag,Element parent, Element targetEle, int targetCounter) {
Iterator elIt = parent.getChildren(childTag, parent.getNamespace() ).iterator();
int counter = 0;
while (elIt.hasNext()) {
Element el = (Element) elIt.next();
counter++;
if (counter < targetCounter) {
continue;
}
if (dep.getArtifactId() != null && dep.getGroupId() != null) {
Element artifactEl = el.getChild("artifactId", el.getNamespace());
Element groupEl = el.getChild("groupId", el.getNamespace());
if (artifactEl != null && groupEl != null
&& dep.getArtifactId().equals(artifactEl.getText())
&& dep.getGroupId().equals(groupEl.getText())) {
targetEle.setContent(el.cloneContent());
break;
}
}
}
}

/**
* Method iterateDeveloper.
*
Expand Down