source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/xhtml/XHTMLHandler.java @ 3102

Revision 3102, 7.6 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1package nu.fw.jeti.plugins.xhtml;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.List;
6
7import javax.swing.text.SimpleAttributeSet;
8import javax.swing.text.StyleConstants;
9
10import nu.fw.jeti.jabber.elements.Extension;
11import nu.fw.jeti.jabber.handlers.ExtensionHandler;
12import nu.fw.jeti.plugins.Word;
13import nu.fw.jeti.util.Log;
14
15import org.xml.sax.Attributes;
16
17/**
18 * @author E.S. de Boer
19 *
20 *
21 */
22public class XHTMLHandler extends ExtensionHandler
23{
24        private List wordList = new ArrayList();
25        private SimpleAttributeSet currentAttributes;
26       
27        private StringBuffer body;
28
29        public void startHandling(Attributes attr)
30        {
31                reset();
32                body = new StringBuffer(); //attr.getValue("from");
33        }
34
35        private void reset()
36        {
37                body = null;
38                wordList.clear();
39                currentAttributes = new SimpleAttributeSet();
40        }
41
42        public void startElement(String name, Attributes attr)
43        {
44                addToWordList(getUntrimmedText());
45               
46                //create xhtml string to print in log window
47                body.append(getText() + "<" + name + " ");
48                for (int i = 0; i < attr.getLength(); i++)
49                {
50                        body.append(attr.getQName(i) + "=\"" + attr.getValue(i) + "\"");
51                }
52                body.append(">");
53               
54                SimpleAttributeSet sas = new SimpleAttributeSet();
55                sas.setResolveParent(currentAttributes);
56                if(name.equals("body"))
57                {
58                        parseStyle(attr.getValue("style"),sas);
59                        currentAttributes = sas;
60                }
61                else if(name.equals("blockquote"))
62                {//TODO indent does not work
63                        StyleConstants.setLeftIndent(sas, 10f);
64                        StyleConstants.setRightIndent(sas, 10f);
65                        parseStyle(attr.getValue("style"),sas);
66                        currentAttributes = sas;
67                        wordList.add(new Word("\n",sas));
68                }
69                else if(name.equals("q"))
70                {//quote cite=url attribute not implemented???
71                        parseStyle(attr.getValue("style"),sas);
72                        currentAttributes = sas;
73                        wordList.add(new Word("'",sas));
74                }
75                else if(name.equals("pre"))
76                {
77                        StyleConstants.setFontFamily(sas,"courier");
78                        parseStyle(attr.getValue("style"),sas);
79                        currentAttributes = sas;
80                }
81                else if(name.equals("li") || name.equals("ol") || name.equals("ul"))
82                {//TODO implement lists
83                        parseStyle(attr.getValue("style"),sas);
84                        currentAttributes = sas;
85                }
86                else if(name.equals("h1"))
87                {//todo h1 =?
88                        StyleConstants.setFontSize(sas,32);
89                        parseStyle(attr.getValue("style"),sas);
90                        currentAttributes = sas;
91                }
92                else if(name.equals("h2"))
93                {
94                        StyleConstants.setFontSize(sas,24);
95                        parseStyle(attr.getValue("style"),sas);
96                        currentAttributes = sas;
97                }
98                else if(name.equals("h3"))
99                {
100                        StyleConstants.setFontSize(sas,18);
101                        parseStyle(attr.getValue("style"),sas);
102                        currentAttributes = sas;
103                }
104               
105               
106                else if(name.equals("a"))
107                {//TODO link
108                        StyleConstants.setFontFamily(sas,"courier");
109                        parseStyle(attr.getValue("style"),sas);
110                        currentAttributes = sas;
111                }
112               
113               
114                else if(name.equals("img"))
115                {//TODO img
116                        StyleConstants.setFontFamily(sas,"courier");
117                        parseStyle(attr.getValue("style"),sas);
118                        currentAttributes = sas;
119                }
120       
121                else if(name.equals("span") || name.equals("div") || name.equals("p")  )
122                {
123                        parseStyle(attr.getValue("style"),sas);
124                        currentAttributes = sas;
125                }
126                else if(name.equals("em")|| name.equals("cite") )
127                {
128                        StyleConstants.setItalic(sas,true);
129                        currentAttributes = sas;
130                }
131                else if(name.equals("strong"))
132                {
133                        StyleConstants.setBold(sas,true);
134                        currentAttributes = sas;
135                }
136                else if(name.equals("code"))
137                {//render in monospace font (courier)
138                        StyleConstants.setFontFamily(sas,"courier");
139                        currentAttributes = sas;
140                }
141                else
142                {
143                        StringBuffer temp = new StringBuffer();
144                        temp.append("<" + name + " ");
145                        for (int i = 0; i < attr.getLength(); i++)
146                        {
147                                temp.append(attr.getQName(i) + "=\"" + attr.getValue(i) + "\"");
148                        }
149                        temp.append(">");
150                        Log.notParsedXML(temp.toString());
151                }
152               
153                clearCurrentChars();
154        }
155       
156        private void parseStyle(String styleAttributes,SimpleAttributeSet sas)
157        {
158                if(styleAttributes==null)return;
159                String[] styles = styleAttributes.split(";");
160                for(int i=0;i<styles.length;i++)
161                {
162                        String[] temp = styles[i].split(":");
163                        if(temp.length!=2)continue;
164                        String style = temp[0].trim();
165                        String value = temp[1].trim();
166                        if(style.equals("color"))
167                        {
168                                if(value.length()!=7)
169                                {
170                                        value+="000000";
171                                        value =value.substring(0, 7);
172                                }
173                                Color color;
174                                try {
175                                        color = Color.decode(value);
176                                }catch (NumberFormatException e)
177                                {
178                                        color = Color.BLACK;
179                                }
180                                StyleConstants.setForeground(sas,color);
181                        }
182                        else if(style.equals("background-color"))
183                        {//not oficialy supported
184                                Color color;
185                                try {
186                                        color = Color.decode(value);
187                                }catch (NumberFormatException e)
188                                {
189                                        color = Color.WHITE;
190                                }
191                                StyleConstants.setBackground(sas,color);
192                        }
193                        else if(style.equals("font-family"))
194                        {
195                                StyleConstants.setFontFamily(sas,value);
196                        }
197                        else if(style.equals("font-size"))
198                        {
199                                int size;
200                                try
201                                {
202                                        size =  Integer.parseInt(value.substring(0,value.length()-2));
203                                }
204                                catch (NumberFormatException e)
205                                {
206                                        size = 12;
207                                }
208                                StyleConstants.setFontSize(sas,size);
209                        }
210                        else if(style.equals("text-decoration"))
211                        {//TODO overline || blink
212                                if(value.equals("underline"))StyleConstants.setUnderline(sas, true);
213                                else if(value.equals("line-through"))StyleConstants.setStrikeThrough(sas, true);
214                               
215                        }
216                        else if(style.equals("text-align"))
217                        {
218                                if(value.equals("left"))StyleConstants.setAlignment(sas,StyleConstants.ALIGN_LEFT);
219                                else if(value.equals("rigth"))StyleConstants.setAlignment(sas,StyleConstants.ALIGN_RIGHT);
220                                else if(value.equals("center"))StyleConstants.setAlignment(sas,StyleConstants.ALIGN_CENTER);
221                                else if(value.equals("justify"))StyleConstants.setAlignment(sas,StyleConstants.ALIGN_JUSTIFIED);
222                        }
223                }
224        }
225       
226
227        public void endElement(String name)
228        {
229                addToWordList(getUntrimmedText());
230                body.append(getText() + "</" + name + ">");
231               
232                if(name.equals("a") || name.equals("body") || name.equals("cite")
233                                || name.equals("code") || name.equals("div") || name.equals("em") || name.equals("h1")
234                                || name.equals("h2")|| name.equals("h3") || name.equals("img") || name.equals("li")
235                                || name.equals("ol")|| name.equals("p")|| name.equals("pre")|| name.equals("ul")
236                                ||  name.equals("strong") || name.equals("span"))
237                {
238                        currentAttributes =(SimpleAttributeSet)currentAttributes.getResolveParent();
239                }
240                else if(name.equals("br"))
241                {
242                        wordList.add(new Word("\n"));
243                }
244                else if(name.equals("q"))
245                {//quote cite=url attribute not implemented???
246                        wordList.add(new Word("'",currentAttributes));
247                        currentAttributes =(SimpleAttributeSet)currentAttributes.getResolveParent();
248                }
249                else if(name.equals("blockquote"))
250                {
251                        wordList.add(new Word("\n",null));
252                        currentAttributes =(SimpleAttributeSet)currentAttributes.getResolveParent();
253                }
254                clearCurrentChars();
255        }
256       
257        private void addToWordList(StringBuffer text)
258        {
259                if(text.length()<1)return;
260                StringBuffer temp = new StringBuffer();
261                for(int i = 0;i<text.length();i++)
262                {//split text up in words
263                        char c = text.charAt(i);
264                        switch (c)
265                        {
266                                case ' ':       addWordFromTemp(temp); wordList.add(new Word(" "));  temp = new StringBuffer(); break;
267                                case '\n':      addWordFromTemp(temp); wordList.add(new Word("\n")); temp = new StringBuffer(); break;
268                                case '\t':      addWordFromTemp(temp); wordList.add(new Word("\t")); temp = new StringBuffer();break;
269                                default: temp.append(c);
270                        }
271                }
272                addWordFromTemp(temp);
273        }
274       
275        private void addWordFromTemp(StringBuffer temp)
276        {
277                if(temp.length()>0)wordList.add(new Word(temp,currentAttributes));
278        }
279
280        public Extension build()
281        {
282                addToWordList(getUntrimmedText());
283                body.append(getText());
284                clearCurrentChars();
285                Extension e = new XHTML(body.toString(),new ArrayList(wordList));
286                reset();
287                return e;
288        }
289}
290/*
291 * Overrides for emacs
292 * Local variables:
293 * tab-width: 4
294 * End:
295 */
Note: See TracBrowser for help on using the repository browser.