Skip to content

Commit

Permalink
set attributes indent scale #9
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Mar 19, 2023
1 parent 3d4fe26 commit 94c143c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/reandroid/xml/XMLAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ public class XMLAttribute extends XMLNode{
private int mValueId;
private String mName;
private String mValue;
private XMLElement mParent;
public XMLAttribute(String name, String val){
mName=name;
mValue= XMLUtil.escapeXmlChars(val);
}
public XMLElement getParent(){
return mParent;
}
void setParent(XMLElement parent){
this.mParent = parent;
}
public void setNameId(int id){
mNameId=id;
}
Expand Down
76 changes: 59 additions & 17 deletions src/main/java/com/reandroid/xml/XMLElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class XMLElement extends XMLNode{
private final List<XMLText> mTexts = new ArrayList<>();
private XMLElement mParent;
private int mIndent;
private float mAttributeIndentScale = 1.0f;
private Object mTag;
private int mResId;
private float mIndentScale;
Expand Down Expand Up @@ -301,7 +302,11 @@ public XMLAttribute getAttribute(String name){
return mAttributes.get(name);
}
public XMLAttribute removeAttribute(String name){
return mAttributes.remove(name);
XMLAttribute attribute = mAttributes.remove(name);
if(attribute!=null){
attribute.setParent(null);
}
return attribute;
}
public XMLAttribute setAttribute(String name, int value){
return setAttribute(name, String.valueOf(value));
Expand Down Expand Up @@ -348,6 +353,7 @@ public void addAttribute(XMLAttribute attr){
return;
}
mAttributes.put(name, attr);
attr.setParent(this);
}
public void sortChildes(Comparator<XMLElement> comparator){
if(comparator==null){
Expand Down Expand Up @@ -425,21 +431,54 @@ public void setIndent(int indent){
}
}
}
private boolean appendAttributesIndentText(Writer writer) throws IOException {
int i=0;
String tagName=getTagName();

/**
* @param indentScale scale of attributes indent relative to element tag start
* - when less than 0.0f indenting will be off and no new line
*/
public void setAttributesIndentScale(float indentScale){
setAttributesIndentScale(indentScale, true);
}
public void setAttributesIndentScale(float indentScale, boolean setToChildes){
this.mAttributeIndentScale = indentScale;
if(!setToChildes){
return;
}
for(XMLElement child:listChildElements()){
child.setAttributesIndentScale(indentScale, true);
}
}
private float getAttributeIndentScale(){
return mAttributeIndentScale;
}
private int calculateAttributesIndent(){
float scale = getAttributeIndentScale();
int indent = 0;
String tagName = getTagName();
if(tagName!=null){
i+=tagName.length();
indent += tagName.length();
}
i+=2;
if(i>15){
i=15;
indent += 2;
if(indent>MAX_ATTRIBUTE_INDENT){
indent = MAX_ATTRIBUTE_INDENT;
}
i+=getIndentWidth();
int j=0;
while (j<i){
writer.write(' ');
j++;
int baseIndent = getIndentWidth();
indent = (int) (scale * indent);
indent = baseIndent + indent;
if(indent<0){
indent = 0;
}
return indent;
}
private boolean appendAttributesIndentText(Writer writer, boolean appendOnce, int indent) throws IOException {
if(indent<=0){
return false;
}
if(appendOnce){
writer.write(XMLUtil.NEW_LINE);
for(int i=0;i<indent;i++){
writer.write(' ');
}
}
return true;
}
Expand Down Expand Up @@ -557,12 +596,14 @@ public void setTextContent(String text, boolean escape){
}
private boolean appendAttributes(Writer writer, boolean newLineAttributes) throws IOException {
boolean addedOnce=false;
int attributesIndent = calculateAttributesIndent();
boolean indentAppend = false;
for(XMLAttribute attr:listAttributes()){
if(newLineAttributes){
indentAppend = appendAttributesIndentText(writer, indentAppend, attributesIndent);
}
if(addedOnce){
if(newLineAttributes){
writer.write(XMLUtil.NEW_LINE);
appendAttributesIndentText(writer);
}else{
if(!indentAppend){
writer.write(' ');
}
}else {
Expand Down Expand Up @@ -745,4 +786,5 @@ private static XMLElement parseSpanSafe(String spanText){
}
}

private static final int MAX_ATTRIBUTE_INDENT = 20;
}

0 comments on commit 94c143c

Please sign in to comment.