2023-01-18 13:49:39 -05:00

95 lines
2.6 KiB
Java
Executable File

/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed 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 com.reandroid.xml;
import java.util.regex.Pattern;
public class XMLUtil {
public static String NEW_LINE="\n";
public static boolean isEmpty(String s){
if(s==null){
return true;
}
return s.length()==0;
}
public static String escapeXmlChars(String str){
if(str==null){
return null;
}
if(str.indexOf('&')<0 && str.indexOf('<')<0 && str.indexOf('>')<0){
return str;
}
str=str.replaceAll("&amp;", "&");
str=str.replaceAll("&lt;", "<");
str=str.replaceAll("&gt;", ">");
str=str.replaceAll("&", "&amp;");
str=str.replaceAll("<", "&lt;");
str=str.replaceAll(">", "&gt;");
return str;
}
public static String escapeQuote(String str){
if(str==null){
return null;
}
int i = str.indexOf('"');
if(i<0){
return str;
}
str=str.replaceAll("\"", "&quot;");
return str;
}
public static String unEscapeXmlChars(String str){
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;", "\"");
return str;
}
public static String trimQuote(String txt){
if(txt==null){
return null;
}
String tmp=txt.trim();
if(tmp.length()==0){
return txt;
}
char c1=tmp.charAt(0);
if(c1!='"'){
return txt;
}
int end=tmp.length()-1;
c1=tmp.charAt(end);
if(c1!='"'){
return txt;
}
if(end<=1){
return "";
}
return tmp.substring(1,end);
}
private static final Pattern PATTERN_ESCAPE = Pattern.compile("^.*[><&].*$");
}