1 package org.joshy.html.util; 2 3 import java.awt.Font ; 4 import org.w3c.dom.Node ; 5 import org.w3c.dom.Element ; 6 import org.joshy.u; 7 import org.joshy.html.Context; 8 9 public class TextUtil { 10 public static String transformText(Context c, Node node, String text) { 11 Element el = null; 12 if(node instanceof Element ) { 13 el = (Element )node; 14 } else { 15 el = (Element )node.getParentNode(); 16 } 17 String text_transform = c.css.getStringProperty(el,"text-transform"); 18 if(text_transform != null) { 19 if(text_transform.equals("lowercase")) { 20 return text.toLowerCase(); 21 } 22 if(text_transform.equals("uppercase")) { 23 return text.toUpperCase(); 24 } 25 if(text_transform.equals("capitalize")) { 26 return capitalizeWords(text); 27 } 28 } 29 return text; 30 } 31 32 public static String capitalizeWords(String text) { 33 if(text.length() == 0) { 35 return text; 36 } 37 38 StringBuffer sb = new StringBuffer (); 39 41 48 boolean cap = true; 49 for(int i=0; i<text.length(); i++) { 50 String ch = text.substring(i,i+1); 51 53 if(cap) { 54 sb.append(ch.toUpperCase()); 55 } else { 56 sb.append(ch); 57 } 58 cap = false; 59 if(ch.equals(" ")) { 60 cap = true; 61 } 62 } 63 64 if(sb.toString().length() != text.length()) { 66 u.p("error! to strings arent the same length = -"+sb.toString()+"-"+text+"-"); 67 } 68 return sb.toString(); 69 } 70 71 public static void stripWhitespace(Context c, Node node, Element containing_block) { 72 73 String white_space = c.css.getStringProperty(containing_block,"white-space"); 74 if(white_space!=null && white_space.equals("pre")) { 76 return; 77 } 78 79 80 if(node == null) { return; } 81 if(node.getNodeType() != node.TEXT_NODE) { return; } 82 String text = node.getNodeValue(); 83 85 104 text = text.replaceAll("^\\s+",""); 106 text = text.replaceAll("\n"," "); 110 text = text.replaceAll("\\s+"," "); 112 113 117 node.setNodeValue(text); 118 } 119 120 } 121 | Popular Tags |