001: /*
002: * 01/07/2003 - 15:19:32
003: *
004: * Pattern.java -
005: * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
006: * ralf.meyer@karneim.com
007: * http://jrexx.sf.net
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023: package com.tc.jrexx.set;
024:
025: import java.util.*;
026: import java.lang.reflect.*;
027:
028: /**
029: * @deprecated
030: */
031: public abstract class XML implements java.io.Serializable {
032: public String toXML(String tag) {
033: final StringBuffer buffer = new StringBuffer();
034: buffer
035: .append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
036: buffer.append("\n<").append(tag).append('>');
037: final StringTokenizer strTkzr = new StringTokenizer(this
038: .toInnerXML(), "\n");
039: for (int i = strTkzr.countTokens(); i > 0; --i) {
040: buffer.append("\n ").append(strTkzr.nextToken());
041: }
042: buffer.append("\n</").append(tag).append('>');
043: return buffer.toString();
044: }
045:
046: public String toXML() {
047: final StringBuffer buffer = new StringBuffer();
048: buffer
049: .append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
050: buffer.append("\n").append(this .toInnerXML());
051: return buffer.toString();
052: }
053:
054: protected String toInnerXML() {
055: try {
056: final StringBuffer buffer = new StringBuffer();
057:
058: final HashSet methodNames = new HashSet();
059: java.lang.reflect.Method[] methods = this .getClass()
060: .getMethods();
061: for (int i = 0; i < methods.length; ++i) {
062: // if (Modifier.isStatic(methods[i].getModifiers())) continue;
063:
064: String name = methods[i].getName();
065: if (name.length() <= 3)
066: continue;
067: if (name.startsWith("get") == false)
068: continue;
069: if (Character.toUpperCase(name.charAt(3)) != name
070: .charAt(3))
071: continue;
072:
073: // Class[] parameterTypes = methods[i].getParameterTypes();
074: // if (parameterTypes!=null || parameterTypes.length!=0) continue;
075:
076: name = name.substring(3);
077: methodNames.add(name);
078: methodNames.add(Character.toLowerCase(name.charAt(0))
079: + name.substring(1));
080: methodNames.add(name.toUpperCase());
081: methodNames.add(name.toLowerCase());
082: }
083:
084: final Field[] fields = this .getClass().getFields();
085: for (int i = 0; i < fields.length; ++i) {
086: if (Modifier.isStatic(fields[i].getModifiers()))
087: continue;
088:
089: String name = fields[i].getName();
090: if (methodNames.contains(name))
091: continue;
092:
093: Object value = fields[i].get(this );
094: if (value != null) {
095: if (value instanceof XML) {
096: buffer.append("\n<").append(name).append(">");
097: StringTokenizer strTkzr = new StringTokenizer(
098: ((XML) value).toInnerXML(), "\n");
099: while (strTkzr.hasMoreTokens())
100: buffer.append("\n ").append(
101: strTkzr.nextToken());
102: buffer.append("\n</").append(name).append(">");
103: } else {
104: if (value.getClass().isArray()) {
105: Object[] array = (Object[]) value;
106: for (int t = 0; t < array.length; ++t) {
107: buffer.append("\n<").append(name)
108: .append(">");
109: if (array[t] instanceof XML) {
110: StringTokenizer strTkzr = new StringTokenizer(
111: ((XML) array[t])
112: .toInnerXML(), "\n");
113: while (strTkzr.hasMoreTokens())
114: buffer.append("\n ").append(
115: strTkzr.nextToken());
116: } else {
117: buffer.append("\n ").append(
118: array[t]);
119: }
120: buffer.append("\n</").append(name)
121: .append(">");
122: }
123: } else {
124: buffer.append("\n<").append(name).append(
125: ">");
126: buffer.append("\n ").append(value);
127: buffer.append("\n</").append(name).append(
128: ">");
129: }
130: }
131: }
132: }
133:
134: for (int i = 0; i < methods.length; ++i) {
135: String name = methods[i].getName();
136: name = name.substring(3);
137: if (methodNames.contains(name) == false)
138: continue;
139: Object value = methods[i].invoke(this , null);
140: if (value != null) {
141: if (value instanceof XML) {
142: buffer.append("\n<").append(name).append(">");
143: StringTokenizer strTkzr = new StringTokenizer(
144: ((XML) value).toInnerXML(), "\n");
145: while (strTkzr.hasMoreTokens())
146: buffer.append("\n ").append(
147: strTkzr.nextToken());
148: buffer.append("\n</").append(name).append(">");
149: } else {
150: if (value.getClass().isArray()) {
151: Object[] array = (Object[]) value;
152: for (int t = 0; t < array.length; ++t) {
153: buffer.append("\n<").append(name)
154: .append(">");
155: if (array[t] instanceof XML) {
156: StringTokenizer strTkzr = new StringTokenizer(
157: ((XML) array[t])
158: .toInnerXML(), "\n");
159: while (strTkzr.hasMoreTokens())
160: buffer.append("\n ").append(
161: strTkzr.nextToken());
162: } else {
163: buffer.append("\n ").append(
164: array[t]);
165: }
166: buffer.append("\n</").append(name)
167: .append(">");
168: }
169: } else {
170: buffer.append("\n<").append(name).append(
171: ">");
172: buffer.append("\n ").append(value);
173: buffer.append("\n</").append(name).append(
174: ">");
175: }
176: }
177: }
178: }
179:
180: return buffer.toString();
181: } catch (InvocationTargetException e) {
182: throw new RuntimeException(e.getMessage());
183: } catch (IllegalAccessException e) {
184: throw new RuntimeException(e.getMessage());
185: }
186: }
187:
188: public String toString() {
189: final StringBuffer buffer = new StringBuffer();
190: buffer.append(this .getClass().getName());
191: buffer.append("\n").append(this .toInnerString());
192: return buffer.toString();
193: }
194:
195: protected String toInnerString() {
196: try {
197: final StringBuffer buffer = new StringBuffer();
198:
199: final HashSet methodNames = new HashSet();
200: java.lang.reflect.Method[] methods = this .getClass()
201: .getMethods();
202: for (int i = 0; i < methods.length; ++i) {
203: // if (Modifier.isStatic(methods[i].getModifiers())) continue;
204:
205: String name = methods[i].getName();
206: if (name.length() <= 3)
207: continue;
208: if (name.startsWith("get") == false)
209: continue;
210: if (Character.toUpperCase(name.charAt(3)) != name
211: .charAt(3))
212: continue;
213:
214: // Class[] parameterTypes = methods[i].getParameterTypes();
215: // if (parameterTypes!=null || parameterTypes.length!=0) continue;
216:
217: name = name.substring(3);
218: methodNames.add(name);
219: methodNames.add(Character.toLowerCase(name.charAt(0))
220: + name.substring(1));
221: methodNames.add(name.toUpperCase());
222: methodNames.add(name.toLowerCase());
223: }
224:
225: final Field[] fields = this .getClass().getFields();
226: for (int i = 0; i < fields.length; ++i) {
227: if (Modifier.isStatic(fields[i].getModifiers()))
228: continue;
229:
230: String name = fields[i].getName();
231: if (methodNames.contains(name))
232: continue;
233:
234: Object value = fields[i].get(this );
235: if (value != null) {
236: if (value instanceof XML) {
237: buffer.append("\n").append(name);
238: StringTokenizer strTkzr = new StringTokenizer(
239: ((XML) value).toInnerString(), "\n");
240: while (strTkzr.hasMoreTokens())
241: buffer.append("\n ").append(
242: strTkzr.nextToken());
243: } else {
244: if (value.getClass().isArray()) {
245: Object[] array = (Object[]) value;
246: for (int t = 0; t < array.length; ++t) {
247: buffer.append("\n").append(name);
248: if (array[t] instanceof XML) {
249: StringTokenizer strTkzr = new StringTokenizer(
250: ((XML) array[t])
251: .toInnerString(),
252: "\n");
253: while (strTkzr.hasMoreTokens())
254: buffer.append("\n ").append(
255: strTkzr.nextToken());
256: } else {
257: buffer.append("\n ").append(
258: array[t]);
259: }
260: }
261: } else {
262: buffer.append("\n").append(name).append(
263: ": ").append(value);
264: }
265: }
266: }
267: }
268:
269: for (int i = 0; i < methods.length; ++i) {
270: String name = methods[i].getName();
271: name = name.substring(3);
272: if (methodNames.contains(name) == false)
273: continue;
274: Object value = methods[i].invoke(this , null);
275: if (value != null) {
276: if (value instanceof XML) {
277: buffer.append("\n").append(name);
278: StringTokenizer strTkzr = new StringTokenizer(
279: ((XML) value).toInnerString(), "\n");
280: while (strTkzr.hasMoreTokens())
281: buffer.append("\n ").append(
282: strTkzr.nextToken());
283: } else {
284: if (value.getClass().isArray()) {
285: Object[] array = (Object[]) value;
286: for (int t = 0; t < array.length; ++t) {
287: buffer.append("\n").append(name);
288: if (array[t] instanceof XML) {
289: StringTokenizer strTkzr = new StringTokenizer(
290: ((XML) array[t])
291: .toInnerString(),
292: "\n");
293: while (strTkzr.hasMoreTokens())
294: buffer.append("\n ").append(
295: strTkzr.nextToken());
296: } else {
297: buffer.append("\n ").append(
298: array[t]);
299: }
300: }
301: } else {
302: buffer.append("\n").append(name).append(
303: ": ").append(value);
304: }
305: }
306: }
307: }
308:
309: return buffer.toString();
310: } catch (InvocationTargetException e) {
311: throw new RuntimeException(e.getMessage());
312: } catch (IllegalAccessException e) {
313: throw new RuntimeException(e.getMessage());
314: }
315: }
316: }
|