KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > util > TextUtil


1 package org.joshy.html.util;
2
3 import java.awt.Font JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5 import org.w3c.dom.Element JavaDoc;
6 import org.joshy.u;
7 import org.joshy.html.Context;
8
9 public class TextUtil {
10 public static String JavaDoc transformText(Context c, Node JavaDoc node, String JavaDoc text) {
11     Element JavaDoc el = null;
12     if(node instanceof Element JavaDoc) {
13         el = (Element JavaDoc)node;
14     } else {
15         el = (Element JavaDoc)node.getParentNode();
16     }
17     String JavaDoc 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 JavaDoc capitalizeWords(String JavaDoc text) {
33     //u.p("start = -"+text+"-");
34
if(text.length() == 0) {
35         return text;
36     }
37     
38     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
39     //u.p("text = -" + text + "-");
40

41     // do first letter
42
//u.p("first = " + text.substring(0,1));
43
/*
44     if(!text.substring(0,1).equals(" ")) {
45         sb.append(text.substring(0,1).toUpperCase());
46     }
47     */

48     boolean cap = true;
49     for(int i=0; i<text.length(); i++) {
50         String JavaDoc ch = text.substring(i,i+1);
51         //u.p("ch = " + ch + " cap = " + cap);
52

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     //u.p("final = -"+sb.toString()+"-");
65
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 JavaDoc node, Element JavaDoc containing_block) {
72     
73     String JavaDoc white_space = c.css.getStringProperty(containing_block,"white-space");
74     // if doing preformatted whitespace
75
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 JavaDoc text = node.getNodeValue();
83     //text = text.trim();
84

85     /*
86     if(text.indexOf("\n") > 0) {
87         u.p("before text = " + text);
88         StringBuffer sb = new StringBuffer();
89         int m = 0;
90         int n = 0;
91         while((n = text.indexOf("\n",m)) > 0) {
92             String span = text.substring(m,n);
93             u.p("span = " + span);
94             sb.append(span);
95             // add a space to replace the \n
96             sb.append(" ");
97             m = n+1;
98         }
99         sb.append(text.substring(m));
100         text = sb.toString();
101         u.p("after text = " + text);
102     }
103     */

104     // spaces at the start of the string -> nothing
105
text = text.replaceAll("^\\s+","");
106     // spaces at the start of lines -> ""
107
//text = text.replaceAll("\n(\\s*)","");
108
// all \n -> a single space
109
text = text.replaceAll("\n"," ");
110     // all extra spaces -> single space
111
text = text.replaceAll("\\s+"," ");
112     
113     // add one space to the end
114
//text = text+" ";
115
//u.p(text);
116

117     node.setNodeValue(text);
118 }
119
120 }
121
Popular Tags