001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: CDHelper.java,v 1.2 2002/04/17 09:29:39 per_nyfelt Exp $
008:
009: package org.ozoneDB.tools.OPP;
010:
011: import java.io.*;
012: import org.ozoneDB.DxLib.*;
013: import org.ozoneDB.*;
014: import org.xml.sax.InputSource;
015: import org.exolab.castor.xml.Marshaller;
016: import org.exolab.castor.xml.Unmarshaller;
017: import org.ozoneDB.tools.OPP.castor.*;
018:
019: /**
020: * @author <a href="http://www.softwarebuero.de/">SMB</a>
021: * @author <a href="http://www.medium.net/">Medium.net</a>
022: * @version $Revision: 1.2 $Date: 2002/04/17 09:29:39 $
023: */
024: public class CDHelper {
025:
026: public final static String DEFAULTLOCK = "READ";
027: public final static String WRITELOCK = "WRITE";
028: public final static String UPGRADELOCK = "UPGRADE";
029:
030: protected Class cl;
031:
032: protected boolean quiet;
033:
034: protected String outputDir;
035:
036: public CDHelper(Class _cl, String _outputDir, boolean _quiet) {
037: cl = _cl;
038: outputDir = _outputDir;
039: quiet = _quiet;
040: }
041:
042: /**
043: * Try to load the XML class descriptor to...
044: */
045: public void searchUpdateMethods(DxHashMap updateMethodsIf)
046: throws Exception {
047:
048: //search *.ocd XML file in current and in output directory
049: String filename = OPP.sourceDirName
050: + OPPHelper.classFileBasename(cl) + ".ocd";
051:
052: if (!quiet) {
053: System.out.print(" trying to process " + filename
054: + "... ");
055: }
056:
057: if (!new File(filename).exists()) {
058: filename = outputDir + OPPHelper.classFileBasename(cl)
059: + ".ocd";
060: if (!new File(filename).exists()) {
061: if (!quiet) {
062: System.out.println("(-).");
063: }
064: return;
065: }
066: }
067:
068: if (!quiet) {
069: System.out.println("(" + filename + ")");
070: }
071:
072: OzoneClassDescriptor descriptor = xml2Descriptor(filename);
073:
074: if (!descriptor.getName().equals(
075: OPPHelper.classFileBasename(cl))) {
076: throw new Exception(
077: "Descriptor name does not match class name.");
078: }
079:
080: Methods mths = descriptor.getMethods();
081: PublicMethod[] m = mths.getPublicMethod();
082: for (int i = 0; i < m.length; i++) {
083: String methodName = m[i].getName();
084: String locklevel = m[i].getLocklevel();
085:
086: boolean isUpdate = locklevel.equals(WRITELOCK)
087: || locklevel.equals(UPGRADELOCK);
088:
089: if (isUpdate && updateMethodsIf.contains(methodName)) {
090: System.out
091: .println(OPPHelper.classFileBasename(cl)
092: + ".ocd"
093: + ":0: warning: All '"
094: + methodName
095: + "' methods will be marked as update methods.");
096: }
097:
098: if (isUpdate) {
099: updateMethodsIf.addForKey(methodName, methodName);
100: }
101: }
102: }
103:
104: protected static void showDescriptor(OzoneClassDescriptor descriptor) {
105: System.out.println("The OzoneClassDescriptor:");
106: System.out.println("Name: " + descriptor.getName());
107: System.out
108: .println("Desc: " + descriptor.getDescription());
109: System.out.println("Package: " + descriptor.getPackage());
110: System.out.println("Superclass: " + descriptor.getSuperclass());
111: System.out.println("Interfaces: ");
112: String[] _I = descriptor.getInterface();
113: if (_I != null) {
114: for (int i = 0, l = _I.length; i < l; ++i) {
115: System.out.println(" " + _I[i]);
116: }
117: }
118:
119: System.out.println("Constructors: ");
120: Constructors ctors = descriptor.getConstructors();
121: PublicConstructor[] ctor = ctors.getPublicConstructor();
122: for (int i = 0, l = ctor.length; i < l; ++i) {
123: System.out.println(" - parameters: "
124: + ctor[i].getParameter());
125: if (ctor[i].getDescription() != null) {
126: System.out.println(" desc: "
127: + ctor[i].getDescription());
128: }
129: }
130:
131: System.out.println("Methods: ");
132: Methods mths = descriptor.getMethods();
133: PublicMethod[] m = mths.getPublicMethod();
134: for (int i = 0, l = m.length; i < l; ++i) {
135: System.out.println(" - name: " + m[i].getName());
136: System.out.println(" parameters: " + m[i].getParameter());
137: System.out.println(" locklevel : " + m[i].getLocklevel());
138: if (m[i].getDescription() != null) {
139: System.out.println(" desc: "
140: + m[i].getDescription());
141: }
142: }
143:
144: }
145:
146: public static OzoneClassDescriptor xml2Descriptor(String _sourcefile)
147: throws Exception {
148: return xml2Descriptor(new InputSource(_sourcefile));
149: }
150:
151: public static OzoneClassDescriptor xml2Descriptor(
152: InputSource _source) throws Exception {
153: Unmarshaller umrs = new Unmarshaller(OzoneClassDescriptor.class);
154: //umrs.setLogWriter( logger );
155: return (OzoneClassDescriptor) umrs.unmarshal(_source);
156: }
157:
158: /**
159: * Build OzoneClassDescriptor for the given class.
160: */
161: public static void class2xml(Class _clazz, PrintWriter _pw,
162: boolean quiet) throws Exception {
163:
164: OzoneClassDescriptor result = new OzoneClassDescriptor();
165: String classname = _clazz.getName();
166: int rcClassname = classname.lastIndexOf(".");
167:
168: result.setName(classname.substring(rcClassname + 1));
169: if (!quiet) {
170: System.out
171: .println("analyzing class: " + classname + " ...");
172: }
173:
174: if (rcClassname != -1) {
175: result.setPackage(classname.substring(0, rcClassname));
176: }
177:
178: Class cl = _clazz.getSuperclass();
179: if (cl != null) {
180: result.setSuperclass(cl.getName());
181: }
182:
183: Class[] interf = _clazz.getInterfaces();
184: for (int i = 0, l = interf.length; i < l; ++i) {
185: result.addInterface(interf[i].getName());
186: }
187:
188: OPPHelper.progressMsg(" processing constructors...", quiet);
189: Constructors constructors = new Constructors();
190: result.setConstructors(constructors);
191: java.lang.reflect.Constructor[] constr = _clazz
192: .getConstructors();
193:
194: if (constr.length == 0) {
195: throw new Exception(
196: "class needs at least one public constructor!");
197: }
198:
199: for (int i = 0, l = constr.length; i < l; ++i) {
200: PublicConstructor c = new PublicConstructor();
201: constructors.addPublicConstructor(c);
202:
203: Class[] params = constr[i].getParameterTypes();
204: for (int j = 0; j < params.length; j++) {
205: c.addParameter(params[j].getName());
206: }
207: }
208:
209: OPPHelper.progressMsg(" processing methods...", quiet);
210: Methods methods = new Methods();
211: result.setMethods(methods);
212:
213: java.lang.reflect.Method[] meth = _clazz.getMethods();
214: if (meth.length == 0) {
215: throw new Exception(
216: "class needs at least one public method!");
217: }
218:
219: for (int i = 0, l = meth.length; i < l; ++i) {
220: String name = meth[i].getName();
221:
222: // filter the system methods inherited from java.lang.Object
223: Class declaringClass = meth[i].getDeclaringClass();
224: if (meth[i].getDeclaringClass().equals(Object.class)
225: || meth[i].getDeclaringClass().equals(
226: OzoneObject.class)) {
227: continue;
228: }
229:
230: PublicMethod m = new PublicMethod();
231: methods.addPublicMethod(m);
232:
233: m.setName(name);
234:
235: Class[] params = meth[i].getParameterTypes();
236: for (int j = 0; j < params.length; j++) {
237: m.addParameter(params[j].getName());
238: }
239:
240: m.setLocklevel(DEFAULTLOCK);
241: }
242:
243: // now dump as XML...
244: Marshaller.marshal(result, _pw);
245: }
246:
247: /**
248: * Builds a valid signaturestring from the given classarray.
249: *
250: * THIS CODE IS TAKEN (and modified) FROM OPP AND SHOULD BECOME UNIQUE FOR BOTH!!!
251: * (See also filtering systemmethods few lines ahead!)
252: *
253: *
254: * @param _parameter array of classes representing the parametertypes
255: * @return the signature or null if _parameter was an empty array (null is needed
256: * to unset the attribute >signatur< of element xmethod)
257: */
258: private static String buildSignature(Class[] _parameter) {
259: if (_parameter.length == 0) {
260: return null;
261: }
262:
263: StringBuffer result = new StringBuffer("");
264: for (int i = 0, l = _parameter.length; i < l; i++) {
265: result.append(i > 0 ? "|" : "");
266: result.append(_parameter[i].getName());
267: }
268: return result.toString();
269: }
270:
271: }
|