source: 3thparty/jmessenger/src/nu/fw/jeti/plugins/xhtml/XHTML.java @ 3952

Revision 3952, 9.0 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1710 - Adicao do codigo fonte java do componente jmessenger(jabberit_messenger)

  • Property svn:executable set to *
Line 
1package nu.fw.jeti.plugins.xhtml;
2
3import java.awt.Color;
4import java.util.List;
5
6import javax.swing.text.AttributeSet;
7import javax.swing.text.MutableAttributeSet;
8import javax.swing.text.StyleConstants;
9
10import nu.fw.jeti.jabber.elements.Extension;
11import nu.fw.jeti.jabber.elements.XExtension;
12import nu.fw.jeti.plugins.Word;
13
14/**
15 * @author E.S. de Boer
16 *
17 *
18 */
19public class XHTML extends Extension implements XExtension
20{
21        private String body;
22        private List wordList;
23       
24        public XHTML(){}
25
26        public XHTML(List wordList)
27        {
28                this.wordList = wordList;
29                //this.body = "<body>" + body + "</body>";
30        }
31       
32        public XHTML(String body,List text)
33        {
34                this.body = body;
35                this.wordList = text;
36        }
37       
38        public List getWordList()
39        {
40                return wordList;
41        }
42               
43        public String getBody(){return body;}
44
45        public void appendToXML(StringBuffer xml)
46        {
47                if(body==null) generateBody();
48                xml.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
49                xml.append(body);
50                xml.append("</html>");
51        }
52       
53        private void generateBody()
54        {
55                StringBuffer temp = new StringBuffer();
56                temp.append("<body>");
57                parseElement(temp,0,wordList.size(),false);
58                temp.append("</body>");
59                body=temp.toString();
60        }
61       
62       
63       
64       
65        /************************Methods to convert wordlist to xml*************************/
66       
67        private void parseElement(StringBuffer xhtml,int start, int end,boolean openStyle)
68        {//msn transport needs lettertype + color in 1 style to work
69                if(start>=end)return;
70                AttributeSet set = ((Word)wordList.get(start)).getAttributes();
71                int newEnd =0;
72                if(set==null);//remove empty set
73                else if (set.isDefined(StyleConstants.FontFamily))
74                {       
75                        String insert ="font-family: " + StyleConstants.getFontFamily(set);
76                        newEnd = addStyle(xhtml, start, end, openStyle, set,insert,StyleConstants.FontFamily);
77//                      xhtml.append("<span style=\"");
78//              xhtml.append();
79//              Object value = set.getAttribute(StyleConstants.FontFamily);
80//              newEnd = getStyleEnd(StyleConstants.FontFamily,value,start,end);
81//              parseElement(xhtml, start, newEnd,true);
82//              xhtml.append("</span>");
83                       
84                       
85                        openStyle=false;
86                }
87                else if (set.isDefined(StyleConstants.FontSize))
88                {       
89                        String insert =" font-size: " + StyleConstants.getFontSize(set) + "pt";
90                        newEnd = addStyle(xhtml, start, end, openStyle, set,insert,StyleConstants.FontSize);
91                        openStyle=false;
92                }
93                else if (set.isDefined(StyleConstants.Foreground))
94                {
95                        String hex = colorToHexString((Color)set.getAttribute(StyleConstants.Foreground));
96                        String insert =" color: #" + hex;
97                        newEnd = addStyle(xhtml, start, end, openStyle, set,insert,StyleConstants.Foreground);
98                        openStyle=false;
99                       
100//                      Object value = set.getAttribute(StyleConstants.Foreground);
101//                      if(!set.isDefined(StyleConstants.Foreground+value.toString()))
102//                      {       
103//                              newEnd = getStyleEnd(StyleConstants.Foreground,value,start,end);
104//                              if(openStyle)
105//                              {
106//                                      if(newEnd<end)xhtml.append("\"><span style=\"");
107//                                      else xhtml.append(';');
108//                              }
109//                              else if(!openStyle) xhtml.append("<span style=\"");
110//                              String hex = colorToHexString((Color)value);
111//                              xhtml.append(" color: #" + hex);
112//                              if(newEnd != 0)
113//                              {
114//                                      parseElement(xhtml, start, newEnd,true);
115//                              }
116//                              if(!openStyle)xhtml.append("</span>");
117//                              openStyle=false;
118//                      }
119                }
120                else if (set.isDefined(StyleConstants.Background))
121                {
122                        String hex = colorToHexString((Color)set.getAttribute(StyleConstants.Background));
123                        String insert =" background-color: #" + hex;
124                        newEnd = addStyle(xhtml, start, end, openStyle, set,insert,StyleConstants.Background);
125                        openStyle=false;
126                                               
127//                      Object value = set.getAttribute(StyleConstants.Background);
128//                      if(!set.isDefined(StyleConstants.Background+value.toString()))
129//                      {       
130//                              xhtml.append("<span style=\"");
131//                              String hex = colorToHexString((Color)value);
132//                              xhtml.append("background: #" + hex);
133//                              newEnd = getStyleEnd(StyleConstants.Background,value,start,end);
134//                              if(newEnd != 0)
135//                              {
136//                                      parseElement(xhtml, start, newEnd,true);
137//                              }
138//                              xhtml.append("</span>");
139//                      }
140                }
141                else if (set.isDefined(StyleConstants.StrikeThrough))
142                {       
143                        String insert =" text-decoration: line-through";
144                        newEnd = addStyle(xhtml, start, end, openStyle, set,insert,StyleConstants.StrikeThrough);
145                        openStyle=false;
146                       
147//                      Object value = set.getAttribute(StyleConstants.StrikeThrough);
148//                      if(!set.isDefined(StyleConstants.StrikeThrough+value.toString()))
149//                      {       
150//                              xhtml.append("<span style=\"");
151//                              xhtml.append("text-decoration: line-through");
152//                              newEnd = getStyleEnd(StyleConstants.StrikeThrough,value,start,end);
153//                              if(newEnd != 0)
154//                              {
155//                                      parseElement(xhtml, start, newEnd,true);
156//                              }
157//                              xhtml.append("</span>");
158//                      }
159                }
160                else if (set.containsAttribute(StyleConstants.Underline,Boolean.TRUE))
161                {
162                        String insert =" text-decoration: underline";
163                        newEnd = addStyle(xhtml, start, end, openStyle, set,insert,StyleConstants.Underline);
164                        openStyle=false;
165                       
166//                      Object value = set.getAttribute(StyleConstants.Underline);
167//                      xhtml.append("<span style=\"");
168//                      xhtml.append("text-decoration: underline");
169//                      newEnd = getStyleEnd(StyleConstants.Underline,value,start,end);
170//                      parseElement(xhtml, start, newEnd,true);
171//                      xhtml.append("</span>");
172                       
173                }
174                else if (StyleConstants.isBold(set))
175                {
176                        if(openStyle)xhtml.append("; margin:0 \">");
177                        openStyle=false;
178                        xhtml.append("<strong>");
179                        Object value = set.getAttribute(StyleConstants.Bold);
180                        newEnd = getStyleEnd(StyleConstants.Bold,value,start,end);
181                        parseElement(xhtml, start, newEnd,false);
182                        xhtml.append("</strong>");
183                }
184                else if (StyleConstants.isItalic(set))
185                {
186                        if(openStyle)xhtml.append("; margin:0 \">");
187                        openStyle=false;
188                        xhtml.append("<em>");
189                        Object value = set.getAttribute(StyleConstants.Italic);
190                        newEnd = getStyleEnd(StyleConstants.Italic,value,start,end);
191                        parseElement(xhtml, start, newEnd,false);
192                        xhtml.append("</em>");
193
194                }
195                if(openStyle)xhtml.append("; margin:0 \">");//margin because msn transport needs ; after all styles
196                //no more styles so print this one
197                if(newEnd==0)
198                {
199                        StringBuffer b = new StringBuffer();
200                        escapeString(b, wordList.get(start).toString());
201                        xhtml.append(b);
202                        if(start+1<end)parseElement(xhtml, start+1, end,false);
203                }//parse words after these words
204                else if(newEnd<end)parseElement(xhtml, newEnd, end,false);
205                       
206        }
207
208        private int addStyle(StringBuffer xhtml, int start, int end, boolean openStyle, AttributeSet set,String insert,Object styleConstant)
209        {
210                int newEnd;
211                Object value = set.getAttribute(styleConstant);
212                newEnd = getStyleEnd(styleConstant,value,start,end);
213                if(openStyle)
214                {
215                        if(newEnd<end)xhtml.append("\"><span style=\"");
216                        else xhtml.append(';');
217                }
218                else xhtml.append("<span style=\"");
219                xhtml.append(insert);
220                parseElement( xhtml, start, newEnd,true);
221                if(!openStyle)xhtml.append("</span>");
222                return newEnd;
223        }
224
225        private int getStyleEnd(Object styleConstant,Object value, int start, int end)
226        {
227                for(int i=start;i<end;i++)
228                {
229                        MutableAttributeSet set =((Word)wordList.get(i)).getAttributes();
230                        if(set==null)return i;
231                        if(set.isDefined(styleConstant))
232                        {       
233                                if(value.equals(set.getAttribute(styleConstant)))
234                                {
235                                        set.removeAttribute(styleConstant);
236                                        //set.addAttribute(styleConstant+value.toString(), value);
237                                }
238                        }
239                        else return i;
240                }
241                return end;
242        }
243       
244        private static final char[] HEX_DIG = {
245                                                '0', '1', '2', '3', '4', '5', '6', '7',
246                                                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
247                                                };
248       
249        public String colorToHexString(Color c)
250        {
251                int rgb = c.getRGB();
252                char[] cdata = new char[6];
253                for (int i = 0; i < 6; i++) {
254                        cdata[5-i] = HEX_DIG[rgb & 0xF];
255                        rgb = rgb >> 4;
256                }
257                return new String(cdata);
258        }
259       
260//        /**
261//   * resolve sets of attributes that are recursively stored in each other
262//   *
263//   * @param style  the set of attributes containing other sets of attributes
264//   */
265//  public static AttributeSet resolveAttributes(AttributeSet style) {
266//    SimpleAttributeSet set = new SimpleAttributeSet();
267//    if(style != null) {
268//      Enumeration names = style.getAttributeNames();
269//      Object value;
270//      Object key;
271//      while(names.hasMoreElements()) {
272//        key = names.nextElement();
273//        //System.out.println("Util resolveAttributes key=" + key);
274//        value = style.getAttribute(key);
275//        //System.out.println("Util resolveAttributes value=" + value);
276//        if( (!key.equals(StyleConstants.NameAttribute)) &&
277//            (!key.equals(StyleConstants.ResolveAttribute)) &&
278//            (!key.equals(AttributeSet.ResolveAttribute)) &&
279//            (!key.equals(AttributeSet.NameAttribute)))
280//        {
281//          set.addAttribute(key, value);
282//        }
283//        else {
284//          if(key.equals(StyleConstants.ResolveAttribute) ||
285//             key.equals(AttributeSet.ResolveAttribute)) {
286//            //System.out.println("Util resolveAttributes resolving key=" + key);
287//            set.addAttributes(resolveAttributes((AttributeSet) value));
288//          }
289//        }
290//      }
291//    }
292//    return set;
293//  }
294       
295}
296/*
297 * Overrides for emacs
298 * Local variables:
299 * tab-width: 4
300 * End:
301 */
Note: See TracBrowser for help on using the repository browser.