📄 stringhelper.java
字号:
} if( i < max ) { sb.append( delim ); } } return sb.toString(); } /** * Escapes the string. * * <p> * Changes the entity characters (currently <,>, ",& and ') * with the XML entities. Empty strings are returned unchanged. * </p> * * @param s the string to escape * @return XML-safe string */ public static String escape( String s ) { if( isEmpty( s ) ) { return s; } StringBuffer sb = new StringBuffer(); int size = s.length(); for( int i = 0; i < size; i++ ) { char c = s.charAt( i ); switch( c ) { case '<': sb.append( "<" ); break; case '>': sb.append( ">" ); break; case '"': sb.append( """ ); break; case '\'': sb.append( "'" ); break; case '&': sb.append( "&" ); break; default: if( c != 0 ) { sb.append( ( char ) c ); } } } return sb.toString(); } /** * Unescapes the string. * * <p> * Changes the XML entities (currently <, >, ",& and ') * with the entity characters. Empty strings are returned unchanged. * </p> * * @param s the string to unescape * @return XML-unsafe string */ public static String unEscape( String s ) { if( isEmpty( s ) ) { return s; } int len = s.length(); StringBuffer sb = new StringBuffer(); int i = 0; while( i < s.length() ) { char c = s.charAt( i ); if( c == '&' ) { if( len >= ( i + 4 ) && s.substring( i, i + 4 ).equalsIgnoreCase( "<" ) ) { sb.append( '<' ); i += 4; } else if( len >= ( i + 4 ) && s.substring( i, i + 4 ).equalsIgnoreCase( ">" ) ) { sb.append( '>' ); i += 4; } else if( len >= ( i + 6 ) && s.substring( i, i + 6 ).equalsIgnoreCase( """ ) ) { sb.append( '"' ); i += 6; } else if( len >= ( i + 6 ) && s.substring( i, i + 6 ).equalsIgnoreCase( "'" ) ) { i += 6; sb.append( '\\' ); } else if( len >= ( i + 5 ) && s.substring( i, i + 5 ).equalsIgnoreCase( "&" ) ) { sb.append( '&' ); i += 5; } else { sb.append( c ); i++; } } else if( c != 0 ) { sb.append( c ); i++; } } return sb.toString(); } /** * Unescapes the string with Unicode characters entities (%uXXXX;) * Empty strings are returned unchanged. * * @param s the string to unescape * @return Unicode string */ public static String unEscapeUnicode( String s ) { if( isEmpty( s ) ) { return s; } int len = s.length(); StringBuffer sb = new StringBuffer(); int i = 0; while( i < s.length() ) { char c = s.charAt( i ); if( len >= ( i + 6 ) && s.substring( i, i + 2 ).equalsIgnoreCase( "%u" ) ) { String hex = s.substring( i + 2, i + 6 ); try { int _c = Integer.parseInt( hex, 16 ); sb.append( ( char ) _c ); i += 6; } catch( NumberFormatException ex ) { sb.append( c ); i++; } } else if( c != 0 ) { sb.append( c ); i++; } } return sb.toString(); } /** * Converts the string from HTML to plain text. * * @param s the string to convert * @return plain-text string */ public static String html2text( String s ) { if( isEmpty( s ) ) { return s; } String result = s; try { // Clear HTML. result = clearHtml( result ); // Platform-dependent linefeeds. RE re = new RE( "\\r\\n|\\r|\\n", RE.REPLACE_ALL ); result = re.subst( result, "" ); // <BR> -> linefeeds. re = new RE( "<br.*?[\\/]??>", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "\n" ); // <P> -> double linefeeds. re = new RE( "<p.*?[\\/]??>", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "\n\n" ); // Removes all other tags. re = new RE( "<[^>]+>", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "" ); // Removes all XML entities. result = replaceXMLEntities( result ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } return result; } /** * Converts the string from XML to plain text. * * @param s the string to convert * @return plain-text string */ public static String xml2text( String s ) { if( isEmpty( s ) ) { return s; } String result = s; try { // Clear XML. result = clearXml( result ); // Removes all tags. RE re = new RE( "<[^>]+>", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "" ); // Removes all XML entities. result = replaceXMLEntities( result ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } return result; } /** * Converts the string from plain text to HTML. * * @param s the string to convert * @return HTML string */ public static String text2html( String s ) { String result = s; if( !isEmpty( result ) ) { try { RE re = new RE( "\\r\\n|\\r|\\n", RE.REPLACE_ALL ); result = re.subst( result, "<br>" ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } } return "<html><body>" + result + "</body></html>"; } /** * Converts the string from plain text to HTML. * * @param s the string to convert * @return HTML string */ public static String text2htmlNoTag( String s ) { String result = s; if( !isEmpty( result ) ) { try { RE re = new RE( "\\r\\n|\\r|\\n", RE.REPLACE_ALL ); result = re.subst( result, "<br>" ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } } return result; } /** * Removes "bad" characters from XML. * Valid symbols: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] * (| [#x10000-#x10FFFF])??? * @param s the string to convert * @return cleared XML */ public static String clear( String s ) { // [ALB] don't use StringHelper#isEmpty here! if( s == null ) { return s; } StringBuffer sb = null; String result = s; int length = result.length(); for( int i = 0; i < length; i++ ) { char c = result.charAt( i ); if( ( c >= '\u0020' && c <= '\ud7ff' ) || ( c == 9 ) || ( c == 10 ) || ( c == 13 ) || ( c >= '\ue000' && c <= '\ufffd' ) ) { // Symbol is valid if( sb != null ) { // StringBuffer is created - just append valid symbol... sb.append( c ); } } else { // Symbol is not valid if( sb == null ) { // first occurance - create new StringBuffer sb = new StringBuffer( result.substring( 0, i ) ); } } } if( sb != null ) { // if StringBuffer is created - take value from it result = sb.toString(); } return result; } /** * Removes system XML tag <?xml .. ?>, XMS schema and DTD references from XML. * @param s the string to convert * @return cleared XML */ public static String clearXml( String s ) { return clearXml( s, true ); } /** * Removes system XML tag <?xml .. ?>, XMS schema and DTD references from XML. * @param s the string to convert * @param clearText clear bad characters * @return cleared XML */ public static String clearXml( String s, boolean clearText ) { if( isEmpty( s ) ) { return s; } // Remove special xml tag. String result = s; int pos = s.indexOf( "?>" ); if( pos > 0 ) { result = s.substring( pos + 2 ).trim(); } if( clearText ) { // Removes "bad" characters. return clear( result );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -