KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > srcgen > builder > OcdBuilder


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8
package org.ozoneDB.tools.OPP.srcgen.builder;
9
10 import org.ozoneDB.tools.OPP.castor.*;
11 import org.ozoneDB.tools.OPP.OPPHelper;
12 import org.ozoneDB.tools.OPP.message.MessageWriter;
13 import org.ozoneDB.tools.OPP.srcgen.BuilderException;
14 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder;
15 import org.ozoneDB.tools.OPP.srcgen.streamfactory.OutputStreamFactory;
16 import org.ozoneDB.core.Lock;
17 import org.exolab.castor.xml.Marshaller;
18 import org.exolab.castor.xml.MarshalException;
19 import org.exolab.castor.xml.ValidationException;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.io.*;
23
24 /**
25  * A builder that builds OCD:s
26  * @author Joakim Ohlrogge
27  *
28  * To change this template use Options | File Templates.
29  */

30 public class OcdBuilder implements ClassBuilder {
31     private Writer wr;
32     private OzoneClassDescriptor ocd;
33     private OutputStreamFactory osf;
34     private MessageWriter msgWriter;
35
36     public OcdBuilder(OutputStreamFactory osf) {
37         this.osf = osf;
38     }
39
40     public void init(MessageWriter msgWriter) {
41         this.msgWriter = msgWriter;
42     }
43
44     public void beginClass(int modifer, String JavaDoc fullName, String JavaDoc superClass, String JavaDoc interfaces[]) throws BuilderException {
45         msgWriter.startGeneration("Generating OCD for " + fullName);
46         try {
47             wr = new PrintWriter(osf.newInstance(fullName));
48         } catch (IOException e) {
49             throw new BuilderException(e);
50         }
51         ocd = new OzoneClassDescriptor();
52         Constructors ctrs = new Constructors();
53         Methods mthds = new Methods();
54         ocd.setConstructors(ctrs);
55         ocd.setMethods(mthds);
56         if (superClass != null) {
57             ocd.setSuperclass(superClass);
58         }
59         ocd.setName(OPPHelper.simpleClassName(fullName));
60         ocd.setPackage(OPPHelper.packageName(fullName));
61         if (interfaces != null) {
62             for (int i = 0; i < interfaces.length; ++i) {
63                 ocd.addInterface(interfaces[i]);
64             }
65         }
66     }
67
68     public void makeConstructor(int modifier, ClassBuilder.Parameter parameters[], String JavaDoc exceptions[]) {
69         if (Modifier.isPublic(modifier)) {
70             PublicConstructor constructor = new PublicConstructor();
71             if (parameters != null) {
72                 for (int i = 0; i < parameters.length; ++i) {
73                     constructor.addParameter(parameters[i].getType());
74                 }
75             }
76             ocd.getConstructors().addPublicConstructor(constructor);
77         }
78     }
79
80     public void makeMethod(int modifier, String JavaDoc name, ClassBuilder.Parameter parameters[], String JavaDoc returnType, String JavaDoc exceptions[], int lockLevel) throws BuilderException {
81         if (Modifier.isPublic(modifier)) {
82             PublicMethod method = new PublicMethod();
83             method.setName(name);
84             switch (lockLevel) {
85                 case Lock.LEVEL_NONE:
86                     method.setLocklevel("NONE");
87                     break;
88                 case Lock.LEVEL_MAX:
89                     method.setLocklevel("MAX");
90                     break;
91                 case Lock.LEVEL_READ:
92                     method.setLocklevel("READ");
93                     break;
94                 case Lock.LEVEL_UPGRADE:
95                     method.setLocklevel("UPGRADE");
96                     break;
97                 case Lock.LEVEL_WRITE:
98                     method.setLocklevel("WRITE");
99                     break;
100                 default:
101                     throw new BuilderException("Unsupported lock level for method " + name + " level=" + lockLevel + ".");
102             }
103             if (parameters != null) {
104                 for (int i = 0; i < parameters.length; ++i) {
105                     method.addParameter(parameters[i].getOrigTypeName());
106                 }
107             }
108             ocd.getMethods().addPublicMethod(method);
109         }
110     }
111
112     public void endClass() throws BuilderException {
113         try {
114             Marshaller.marshal(ocd, wr);
115         } catch (MarshalException e) {
116             msgWriter.endGeneration();
117             throw new BuilderException(e);
118         } catch (ValidationException e) {
119             msgWriter.endGeneration();
120             throw new BuilderException(e);
121         } finally {
122             msgWriter.endGeneration();
123         }
124     }
125 }
126
Popular Tags