set attributes indent scale #9

This commit is contained in:
REAndroid 2023-03-19 13:52:10 -04:00
parent 3d4fe269a5
commit 94c143ca72
2 changed files with 66 additions and 17 deletions

View File

@ -25,10 +25,17 @@ public class XMLAttribute extends XMLNode{
private int mValueId; private int mValueId;
private String mName; private String mName;
private String mValue; private String mValue;
private XMLElement mParent;
public XMLAttribute(String name, String val){ public XMLAttribute(String name, String val){
mName=name; mName=name;
mValue= XMLUtil.escapeXmlChars(val); mValue= XMLUtil.escapeXmlChars(val);
} }
public XMLElement getParent(){
return mParent;
}
void setParent(XMLElement parent){
this.mParent = parent;
}
public void setNameId(int id){ public void setNameId(int id){
mNameId=id; mNameId=id;
} }

View File

@ -32,6 +32,7 @@ public class XMLElement extends XMLNode{
private final List<XMLText> mTexts = new ArrayList<>(); private final List<XMLText> mTexts = new ArrayList<>();
private XMLElement mParent; private XMLElement mParent;
private int mIndent; private int mIndent;
private float mAttributeIndentScale = 1.0f;
private Object mTag; private Object mTag;
private int mResId; private int mResId;
private float mIndentScale; private float mIndentScale;
@ -301,7 +302,11 @@ public class XMLElement extends XMLNode{
return mAttributes.get(name); return mAttributes.get(name);
} }
public XMLAttribute removeAttribute(String 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){ public XMLAttribute setAttribute(String name, int value){
return setAttribute(name, String.valueOf(value)); return setAttribute(name, String.valueOf(value));
@ -348,6 +353,7 @@ public class XMLElement extends XMLNode{
return; return;
} }
mAttributes.put(name, attr); mAttributes.put(name, attr);
attr.setParent(this);
} }
public void sortChildes(Comparator<XMLElement> comparator){ public void sortChildes(Comparator<XMLElement> comparator){
if(comparator==null){ if(comparator==null){
@ -425,21 +431,54 @@ public class XMLElement extends XMLNode{
} }
} }
} }
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){ if(tagName!=null){
i+=tagName.length(); indent += tagName.length();
} }
i+=2; indent += 2;
if(i>15){ if(indent>MAX_ATTRIBUTE_INDENT){
i=15; indent = MAX_ATTRIBUTE_INDENT;
} }
i+=getIndentWidth(); int baseIndent = getIndentWidth();
int j=0; indent = (int) (scale * indent);
while (j<i){ indent = baseIndent + indent;
writer.write(' '); if(indent<0){
j++; 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; return true;
} }
@ -557,12 +596,14 @@ public class XMLElement extends XMLNode{
} }
private boolean appendAttributes(Writer writer, boolean newLineAttributes) throws IOException { private boolean appendAttributes(Writer writer, boolean newLineAttributes) throws IOException {
boolean addedOnce=false; boolean addedOnce=false;
int attributesIndent = calculateAttributesIndent();
boolean indentAppend = false;
for(XMLAttribute attr:listAttributes()){ for(XMLAttribute attr:listAttributes()){
if(newLineAttributes){
indentAppend = appendAttributesIndentText(writer, indentAppend, attributesIndent);
}
if(addedOnce){ if(addedOnce){
if(newLineAttributes){ if(!indentAppend){
writer.write(XMLUtil.NEW_LINE);
appendAttributesIndentText(writer);
}else{
writer.write(' '); writer.write(' ');
} }
}else { }else {
@ -745,4 +786,5 @@ public class XMLElement extends XMLNode{
} }
} }
private static final int MAX_ATTRIBUTE_INDENT = 20;
} }