code cleanup

This commit is contained in:
REAndroid 2023-01-17 09:25:23 -05:00
parent c4142c2267
commit 3eda5edaf6
3 changed files with 13 additions and 87 deletions

View File

@ -32,9 +32,6 @@ public class XMLAttribute extends XMLNode{
public void setNameId(int id){
mNameId=id;
}
public void setValueId(String id){
setValueId(XMLUtil.hexToInt(id,0));
}
public void setValueId(int id){
mValueId=id;
}

View File

@ -250,8 +250,5 @@ public class XMLDocument extends XMLNode{
}
docEle.setIndent(indent);
}
public static String htmlToXml(String htmlString){
return XMLUtil.htmlToXml(htmlString);
}
}

View File

@ -15,7 +15,6 @@
*/
package com.reandroid.xml;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class XMLUtil {
@ -24,14 +23,15 @@ public class XMLUtil {
if(s==null){
return true;
}
//String txt=s.trim();
String txt=s;
return txt.length()==0;
return s.length()==0;
}
public static String escapeXmlChars(String str){
if(str==null){
return null;
}
if(!PATTERN_ESCAPE.matcher(str).matches()){
return str;
}
str=str.replaceAll("&", "&");
str=str.replaceAll("&lt;", "<");
str=str.replaceAll("&gt;", ">");
@ -44,6 +44,10 @@ public class XMLUtil {
if(str==null){
return null;
}
int i = str.indexOf('"');
if(i<0){
return str;
}
str=str.replaceAll("\"", "&quot;");
return str;
}
@ -51,30 +55,16 @@ public class XMLUtil {
if(str==null){
return null;
}
int i=str.indexOf('&');
if(i<0){
return str;
}
str=str.replaceAll("&amp;", "&");
str=str.replaceAll("&lt;", "<");
str=str.replaceAll("&gt;", ">");
str=str.replaceAll("&quot;", "\"");
if(str.startsWith("\"")&&str.endsWith("\"")){
// str=str.substring(1, str.length()-1);
}
return str;
}
public static String intToHex(int val){
return String.format("0x%08x", val);
}
public static int hexToInt(String hexStr, int def){
if(hexStr==null){
return def;
}
Matcher matcher=PATTERN_HEX.matcher(hexStr);
if(!matcher.find()){
return def;
}
hexStr=matcher.group("A");
return Integer.parseInt(hexStr, 16);
}
public static String trimQuote(String txt){
if(txt==null){
return null;
@ -97,66 +87,8 @@ public class XMLUtil {
}
return tmp.substring(1,end);
}
public static boolean isStringEqual(String s1, String s2) {
if(s1==null&&s2==null){
return true;
}
if(s1==null||s2==null){
return false;
}
return s1.equals(s2);
}
static String htmlToXml(String htmlString){
if(htmlString==null){
return null;
}
int i=0;
htmlString=htmlString.trim();
String result=htmlString;
Matcher matcher=PATTERN_HTML_HEADER_CHILDES.matcher(htmlString);
while (matcher.find()){
String openedTag=matcher.group("Element");
if(openedTag.contains("https://github.githubassets.com")){
openedTag.trim();
}
int len=openedTag.length();
String tagName=matcher.group("Tag");
if(isOpenHtmlTag(tagName) && !openedTag.endsWith("/>")&& !openedTag.endsWith("/ >")){
String rep=openedTag.substring(0, len-1);
rep=rep+"/>";
result=result.replace(openedTag, rep);
result=result.replace(" crossorigin/>", "/>");
result=result.replace(" data-pjax-transient/>", "/>");
}
i=htmlString.indexOf(openedTag);
i=i+len;
htmlString=htmlString.substring(i);
htmlString=htmlString.trim();
matcher=PATTERN_HTML_HEADER_CHILDES.matcher(htmlString);
}
result="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+result;
return result;
}
private static boolean isOpenHtmlTag(String tagName){
if("link".equals(tagName)){
return true;
}
if("meta".equals(tagName)){
return true;
}
if("img".equals(tagName)){
return true;
}
if("style".equals(tagName)){
return true;
}
return false;
}
private static Pattern PATTERN_HEX=Pattern.compile("^\\s*(0x)?(?<A>[a-f0-9]+)\\s*$");
//<link rel="preconnect" href="https://avatars.githubusercontent.com">
private static Pattern PATTERN_HTML_HEADER_CHILDES=Pattern.compile("(?<Element><\\s*(?<Tag>[a-zA-Z]+)\\s*[^<>]+>)");
private static final Pattern PATTERN_ESCAPE = Pattern.compile("^.*[><&].*$");
}