KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > jrexx > set > XML


1 /*
2 * 01/07/2003 - 15:19:32
3 *
4 * Pattern.java -
5 * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
6 * ralf.meyer@karneim.com
7 * http://jrexx.sf.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */

23 package com.tc.jrexx.set;
24
25 import java.util.*;
26 import java.lang.reflect.*;
27
28 /**
29  * @deprecated
30  */

31 public abstract class XML implements java.io.Serializable JavaDoc {
32   public String JavaDoc toXML(String JavaDoc tag) {
33     final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
34     buffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
35     buffer.append("\n<").append(tag).append('>');
36     final StringTokenizer strTkzr = new StringTokenizer(this.toInnerXML(),"\n");
37     for (int i=strTkzr.countTokens(); i>0; --i) {
38       buffer.append("\n ").append(strTkzr.nextToken());
39     }
40     buffer.append("\n</").append(tag).append('>');
41     return buffer.toString();
42   }
43
44   public String JavaDoc toXML() {
45     final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
46     buffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
47     buffer.append("\n").append(this.toInnerXML());
48     return buffer.toString();
49   }
50
51   protected String JavaDoc toInnerXML() {
52     try {
53       final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54
55       final HashSet methodNames = new HashSet();
56       java.lang.reflect.Method JavaDoc[] methods = this.getClass().getMethods();
57       for (int i=0; i<methods.length; ++i) {
58 // if (Modifier.isStatic(methods[i].getModifiers())) continue;
59

60         String JavaDoc name = methods[i].getName();
61         if (name.length()<=3) continue;
62         if (name.startsWith("get")==false) continue;
63         if (Character.toUpperCase(name.charAt(3))!=name.charAt(3)) continue;
64
65 // Class[] parameterTypes = methods[i].getParameterTypes();
66
// if (parameterTypes!=null || parameterTypes.length!=0) continue;
67

68         name = name.substring(3);
69         methodNames.add(name);
70         methodNames.add(Character.toLowerCase(name.charAt(0))+name.substring(1));
71         methodNames.add(name.toUpperCase());
72         methodNames.add(name.toLowerCase());
73     }
74
75     final Field[] fields = this.getClass().getFields();
76     for (int i=0; i<fields.length; ++i) {
77         if (Modifier.isStatic(fields[i].getModifiers())) continue;
78
79         String JavaDoc name = fields[i].getName();
80         if (methodNames.contains(name)) continue;
81
82         Object JavaDoc value = fields[i].get(this);
83         if (value!=null) {
84           if (value instanceof XML) {
85             buffer.append("\n<").append(name).append(">");
86             StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerXML(),"\n");
87             while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken());
88             buffer.append("\n</").append(name).append(">");
89           } else {
90             if (value.getClass().isArray()) {
91               Object JavaDoc[] array = (Object JavaDoc[])value;
92               for (int t=0; t<array.length; ++t) {
93                 buffer.append("\n<").append(name).append(">");
94                 if (array[t] instanceof XML) {
95                   StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerXML(),"\n");
96                   while (strTkzr.hasMoreTokens())
97                     buffer.append("\n ").append(strTkzr.nextToken());
98                 } else {
99                   buffer.append("\n ").append(array[t]);
100                 }
101                 buffer.append("\n</").append(name).append(">");
102               }
103             } else {
104               buffer.append("\n<").append(name).append(">");
105               buffer.append("\n ").append(value);
106               buffer.append("\n</").append(name).append(">");
107             }
108           }
109         }
110       }
111
112
113       for (int i=0; i<methods.length; ++i) {
114         String JavaDoc name = methods[i].getName();
115         name = name.substring(3);
116         if (methodNames.contains(name)==false) continue;
117         Object JavaDoc value = methods[i].invoke(this,null);
118         if (value!=null) {
119           if (value instanceof XML) {
120             buffer.append("\n<").append(name).append(">");
121             StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerXML(),"\n");
122             while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken());
123             buffer.append("\n</").append(name).append(">");
124           } else {
125             if (value.getClass().isArray()) {
126               Object JavaDoc[] array = (Object JavaDoc[])value;
127               for (int t=0; t<array.length; ++t) {
128                 buffer.append("\n<").append(name).append(">");
129                 if (array[t] instanceof XML) {
130                   StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerXML(),"\n");
131                   while (strTkzr.hasMoreTokens())
132                     buffer.append("\n ").append(strTkzr.nextToken());
133                 } else {
134                   buffer.append("\n ").append(array[t]);
135                 }
136                 buffer.append("\n</").append(name).append(">");
137               }
138             } else {
139               buffer.append("\n<").append(name).append(">");
140               buffer.append("\n ").append(value);
141               buffer.append("\n</").append(name).append(">");
142             }
143           }
144         }
145       }
146
147       return buffer.toString();
148     } catch(InvocationTargetException e) {
149       throw new RuntimeException JavaDoc(e.getMessage());
150     } catch(IllegalAccessException JavaDoc e) {
151       throw new RuntimeException JavaDoc(e.getMessage());
152     }
153   }
154
155
156   public String JavaDoc toString() {
157     final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
158     buffer.append(this.getClass().getName());
159     buffer.append("\n").append(this.toInnerString());
160     return buffer.toString();
161   }
162
163   protected String JavaDoc toInnerString() {
164     try {
165       final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
166
167       final HashSet methodNames = new HashSet();
168       java.lang.reflect.Method JavaDoc[] methods = this.getClass().getMethods();
169       for (int i=0; i<methods.length; ++i) {
170 // if (Modifier.isStatic(methods[i].getModifiers())) continue;
171

172         String JavaDoc name = methods[i].getName();
173         if (name.length()<=3) continue;
174         if (name.startsWith("get")==false) continue;
175         if (Character.toUpperCase(name.charAt(3))!=name.charAt(3)) continue;
176
177 // Class[] parameterTypes = methods[i].getParameterTypes();
178
// if (parameterTypes!=null || parameterTypes.length!=0) continue;
179

180         name = name.substring(3);
181         methodNames.add(name);
182         methodNames.add(Character.toLowerCase(name.charAt(0))+name.substring(1));
183         methodNames.add(name.toUpperCase());
184         methodNames.add(name.toLowerCase());
185     }
186
187     final Field[] fields = this.getClass().getFields();
188     for (int i=0; i<fields.length; ++i) {
189         if (Modifier.isStatic(fields[i].getModifiers())) continue;
190
191         String JavaDoc name = fields[i].getName();
192         if (methodNames.contains(name)) continue;
193
194         Object JavaDoc value = fields[i].get(this);
195         if (value!=null) {
196           if (value instanceof XML) {
197             buffer.append("\n").append(name);
198             StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerString(),"\n");
199             while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken());
200           } else {
201             if (value.getClass().isArray()) {
202               Object JavaDoc[] array = (Object JavaDoc[])value;
203               for (int t=0; t<array.length; ++t) {
204                 buffer.append("\n").append(name);
205                 if (array[t] instanceof XML) {
206                   StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerString(),"\n");
207                   while (strTkzr.hasMoreTokens())
208                     buffer.append("\n ").append(strTkzr.nextToken());
209                 } else {
210                   buffer.append("\n ").append(array[t]);
211                 }
212               }
213             } else {
214               buffer.append("\n").append(name).append(": ").append(value);
215             }
216           }
217         }
218       }
219
220
221       for (int i=0; i<methods.length; ++i) {
222         String JavaDoc name = methods[i].getName();
223         name = name.substring(3);
224         if (methodNames.contains(name)==false) continue;
225         Object JavaDoc value = methods[i].invoke(this,null);
226         if (value!=null) {
227           if (value instanceof XML) {
228             buffer.append("\n").append(name);
229             StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerString(),"\n");
230             while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken());
231           } else {
232             if (value.getClass().isArray()) {
233               Object JavaDoc[] array = (Object JavaDoc[])value;
234               for (int t=0; t<array.length; ++t) {
235                 buffer.append("\n").append(name);
236                 if (array[t] instanceof XML) {
237                   StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerString(),"\n");
238                   while (strTkzr.hasMoreTokens())
239                     buffer.append("\n ").append(strTkzr.nextToken());
240                 } else {
241                   buffer.append("\n ").append(array[t]);
242                 }
243               }
244             } else {
245               buffer.append("\n").append(name).append(": ").append(value);
246             }
247           }
248         }
249       }
250
251       return buffer.toString();
252     } catch(InvocationTargetException e) {
253       throw new RuntimeException JavaDoc(e.getMessage());
254     } catch(IllegalAccessException JavaDoc e) {
255       throw new RuntimeException JavaDoc(e.getMessage());
256     }
257   }
258 }
259
Popular Tags