1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.schema.compiler;
|
11
|
|
|
12
|
|
import java.util.Enumeration;
|
13
|
|
import java.util.StringTokenizer;
|
14
|
|
|
15
|
|
import org.jbind.util.other.StrBuffer;
|
16
|
|
import org.jbind.xml.base.IBindingAttributes;
|
17
|
|
import org.jbind.xml.core.cmp.IBinding;
|
18
|
|
import org.jbind.xml.core.cmp.IComponent;
|
19
|
|
import org.jbind.xml.core.content.IAttrDesc;
|
20
|
|
import org.jbind.xml.core.content.IAttrGroupDesc;
|
21
|
|
import org.jbind.xml.core.content.IElemDesc;
|
22
|
|
import org.jbind.xml.core.content.IElemGroupDesc;
|
23
|
|
import org.jbind.xml.core.type.IAnyType;
|
24
|
|
import org.jbind.xml.msg.XmlException;
|
25
|
|
|
26
|
|
public abstract class BaseCartridge extends AbstractCartridge {
|
27
|
|
|
28
|
|
private String myCartridgeAttributePrefix;
|
29
|
|
private String myNamePrefix;
|
30
|
|
private String myNameSuffix;
|
31
|
|
|
32
|
|
private StrBuffer myBuffer = null;
|
33
|
|
private StrBuffer myIndent = null;
|
34
|
|
private boolean myIsEmpty = true;
|
35
|
|
private IComponent myOuterComponent = null;
|
36
|
|
|
37
|
12
|
public BaseCartridge(String aCartridgeAttributePrefix, String aNamePrefix, String aNameSuffix) {
|
38
|
12
|
myCartridgeAttributePrefix = aCartridgeAttributePrefix;
|
39
|
12
|
myNamePrefix = aNamePrefix;
|
40
|
12
|
myNameSuffix = aNameSuffix;
|
41
|
|
}
|
42
|
|
|
43
|
2156
|
public String getNamePrefix(IComponent aComponent) {
|
44
|
2156
|
String bindingAttributeName = myCartridgeAttributePrefix + IBindingAttributes.ATTRIBUTE_NAME_SUFFIX_FOR_NAME_PREFIX;
|
45
|
2156
|
String res = aComponent.getStringBindingAttribute(bindingAttributeName);
|
46
|
2156
|
if (null == res) {
|
47
|
2156
|
res = myNamePrefix;
|
48
|
|
}
|
49
|
2156
|
return res;
|
50
|
|
}
|
51
|
2156
|
public String getNameSuffix(IComponent aComponent) {
|
52
|
2156
|
String bindingAttributeName = myCartridgeAttributePrefix + IBindingAttributes.ATTRIBUTE_NAME_SUFFIX_FOR_NAME_SUFFIX;
|
53
|
2156
|
String res = aComponent.getStringBindingAttribute(bindingAttributeName);
|
54
|
2156
|
if (null == res) {
|
55
|
2156
|
res = myNameSuffix;
|
56
|
|
}
|
57
|
2156
|
return res;
|
58
|
|
}
|
59
|
|
|
60
|
2236
|
public IFileInfo generate(IComponent aComponent) throws XmlException {
|
61
|
2236
|
if (null == getBinding(aComponent)) {
|
62
|
1172
|
return null;
|
63
|
|
}
|
64
|
1064
|
myIsEmpty = true;
|
65
|
1064
|
myOuterComponent = null;
|
66
|
1064
|
myBuffer = new StrBuffer();
|
67
|
1064
|
myIndent = new StrBuffer();
|
68
|
1064
|
aComponent.accept(this);
|
69
|
1064
|
IFileInfo fileInfo = null;
|
70
|
1064
|
if (!myIsEmpty) {
|
71
|
743
|
StringBuffer sb = new StringBuffer();
|
72
|
743
|
if (overwrite()) {
|
73
|
735
|
sb.append("/*\n").append(" * Generated file - DO NOT MODIFY - changes will be lost.\n").append(" */\n");
|
74
|
|
}
|
75
|
743
|
IBinding binding = getBinding(myOuterComponent);
|
76
|
743
|
if (null != binding.getPackage()) {
|
77
|
549
|
sb.append("package " + binding.getPackage() + ";\n");
|
78
|
|
}
|
79
|
743
|
sb.append(myBuffer.toString());
|
80
|
743
|
fileInfo = new FileInfo(myOuterComponent, this, sb.toString(), "java");
|
81
|
|
}
|
82
|
1064
|
return fileInfo;
|
83
|
|
}
|
84
|
|
|
85
|
34888
|
protected void writeLn(Object anObject) {
|
86
|
34888
|
myBuffer.append(myIndent.toString()).append(anObject).append('\n');
|
87
|
|
}
|
88
|
|
|
89
|
4354
|
protected void writeMultiLn(String aString) {
|
90
|
4354
|
if (null != aString) {
|
91
|
2904
|
for (Enumeration e = new StringTokenizer(aString, "\n");
|
92
|
35751
|
e.hasMoreElements(); ) {
|
93
|
32847
|
writeLn(e.nextElement());
|
94
|
|
}
|
95
|
|
}
|
96
|
|
}
|
97
|
|
|
98
|
876
|
protected void setNotEmpty() {
|
99
|
876
|
myIsEmpty = false;
|
100
|
|
}
|
101
|
|
|
102
|
984
|
protected void setOuterComponent(IComponent aComponent) {
|
103
|
984
|
if (null == myOuterComponent) {
|
104
|
848
|
myOuterComponent = aComponent;
|
105
|
|
}
|
106
|
|
}
|
107
|
|
|
108
|
976
|
protected void addIndent() {
|
109
|
976
|
myIndent.append('\t');
|
110
|
|
}
|
111
|
|
|
112
|
976
|
protected void removeIndent() {
|
113
|
976
|
myIndent.setLength(myIndent.length() - 1);
|
114
|
|
}
|
115
|
|
|
116
|
|
private ICartridge myDataInterfaceCartridge = null;
|
117
|
|
private ICartridge myDataClassCartridge = null;
|
118
|
|
private ICartridge myBehaviourInterfaceCartridge = null;
|
119
|
|
private ICartridge myBehaviourClassCartridge = null;
|
120
|
|
|
121
|
12
|
public void initCartridge(ICartridgeMgr aCartrigedMgr) {
|
122
|
12
|
myDataInterfaceCartridge = CartridgeMgr.instance.getCartridge(DataInterfaceCartridge.class);
|
123
|
12
|
myDataClassCartridge = CartridgeMgr.instance.getCartridge(DataClassCartridge.class);
|
124
|
12
|
myBehaviourInterfaceCartridge = CartridgeMgr.instance.getCartridge(BehaviourInterfaceCartridge.class);
|
125
|
12
|
myBehaviourClassCartridge = CartridgeMgr.instance.getCartridge(BehaviourClassCartridge.class);
|
126
|
|
}
|
127
|
|
|
128
|
2637
|
protected ICartridge getDataInterfaceCartridge() {
|
129
|
2637
|
return myDataInterfaceCartridge;
|
130
|
|
}
|
131
|
141
|
protected ICartridge getDataClassCartridge() {
|
132
|
141
|
return myDataClassCartridge;
|
133
|
|
}
|
134
|
3285
|
protected ICartridge getBehaviourInterfaceCartridge() {
|
135
|
3285
|
return myBehaviourInterfaceCartridge;
|
136
|
|
}
|
137
|
882
|
protected ICartridge getBehaviourClassCartridge() {
|
138
|
882
|
return myBehaviourClassCartridge;
|
139
|
|
}
|
140
|
|
|
141
|
|
protected abstract String interfaceOrClass();
|
142
|
|
|
143
|
874
|
protected String staticOrPublic() {
|
144
|
874
|
return (myOuterComponent != null) ? "static" : "public";
|
145
|
|
}
|
146
|
|
|
147
|
1256
|
private boolean groupStart(IComponent aComponent) throws XmlException {
|
148
|
1256
|
if (aComponent.isTopLevelComponent()) {
|
149
|
102
|
writeLn("public " + interfaceOrClass() + " " + getBinding(aComponent).getInnerName() + " {");
|
150
|
102
|
addIndent();
|
151
|
102
|
setOuterComponent(aComponent);
|
152
|
|
}
|
153
|
1256
|
return true;
|
154
|
|
}
|
155
|
|
|
156
|
1256
|
private void groupEnd(IComponent aComponent) {
|
157
|
1256
|
if (aComponent.isTopLevelComponent()) {
|
158
|
102
|
removeIndent();
|
159
|
102
|
writeLn("}");
|
160
|
|
}
|
161
|
|
}
|
162
|
|
|
163
|
524
|
protected boolean doVisitElemGroupDescStart(IElemGroupDesc aComponent) throws XmlException {
|
164
|
524
|
return groupStart(aComponent);
|
165
|
|
}
|
166
|
524
|
protected void doVisitElemGroupDescEnd(IElemGroupDesc aComponent) throws XmlException {
|
167
|
524
|
groupEnd(aComponent);
|
168
|
|
}
|
169
|
732
|
protected boolean doVisitAttrGroupDescStart(IAttrGroupDesc aComponent) throws XmlException {
|
170
|
732
|
return groupStart(aComponent);
|
171
|
|
}
|
172
|
732
|
protected void doVisitAttrGroupDescEnd(IAttrGroupDesc aComponent) throws XmlException {
|
173
|
732
|
groupEnd(aComponent);
|
174
|
|
}
|
175
|
|
|
176
|
662
|
protected boolean doVisitAttrDescStart(IAttrDesc aComponent) throws XmlException {
|
177
|
662
|
return true;
|
178
|
|
}
|
179
|
662
|
protected void doVisitAttrDescEnd(IAttrDesc aComponent) throws XmlException {}
|
180
|
1108
|
protected boolean doVisitElemDescStart(IElemDesc aComponent) throws XmlException {
|
181
|
1108
|
return true;
|
182
|
|
}
|
183
|
1108
|
protected void doVisitElemDescEnd(IElemDesc aComponent) throws XmlException {}
|
184
|
|
|
185
|
445
|
protected String behaviourBaseClass(IAnyType aType, String aPackage) {
|
186
|
445
|
IAnyType t = aType;
|
187
|
445
|
while (t.getBindings()[myBehaviourClassCartridge.getCartridgeNo()] == null) {
|
188
|
891
|
t = t.getBaseType();
|
189
|
|
}
|
190
|
445
|
String baseClass = getBehaviourClassCartridge().getNameForPackage(t, aPackage);
|
191
|
445
|
return baseClass;
|
192
|
|
}
|
193
|
|
|
194
|
|
}
|
195
|
|
|