To check for an API / html tag support on various browsers and mobile devices. E.g.
http://caniuse.com/filesystem
http://caniuse.com/filesystem
String regexDecimal = "(\\d+(\\.\\d+)?)"; String regexPT = regexDecimal + "pt"; String regexPX = regexDecimal + "px"; String regexCSS = "\\s*:\\s*"; String convert( String line, String regX, float convertFactor, String suffix ) { Pattern pt = Pattern.compile( regX, Pattern.CASE_INSENSITIVE ); Matcher ptMatcher = pt.matcher( line ); StringBuffer s = new StringBuffer(); while( ptMatcher.find() ) { float i = Float.parseFloat( ptMatcher.group( 1 ) ); float f = i / convertFactor; String em = String.format( "%.3f", Float.valueOf( f ) ) + suffix; ptMatcher.appendReplacement( s, em ); } ptMatcher.appendTail( s ); return s.toString(); }
String input = "font-size: 12pt; hello 12pt; 12.0pt convert another14pt16px8px:"; convert( input, regexPT, (float) 0.75, "px" );
String line = "font-size: 12pt;"; convertCSS( line, "font-size:" + regexCSS, regexPX, (float) 16.0, "em" ); String convertCSS( String line, String style, String regX, float convertFactor, String suffix ) { Pattern pt1 = Pattern.compile( "(" + style + ")", Pattern.CASE_INSENSITIVE ); Matcher pt1Matcher = pt1.matcher( line ); boolean regXMatches = false; StringBuffer s = new StringBuffer(); while( pt1Matcher.find() ) { Pattern pt = Pattern.compile( regX, Pattern.CASE_INSENSITIVE ); Matcher ptMatcher = pt.matcher( line ); while( ptMatcher.find() ) { float i = Float.parseFloat( ptMatcher.group( 1 ) ); float f = i / convertFactor; String em = String.format( "%.3f", Float.valueOf( f ) ) + suffix; ptMatcher.appendReplacement( s, em ); } ptMatcher.appendTail( s ); regXMatches = true; } if( !regXMatches ) { pt1Matcher.appendTail( s ); } return s.toString(); }