|
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.facade;
|
|
11
|
|
|
|
12
|
|
import java.net.URL;
|
|
13
|
|
import java.util.Iterator;
|
|
14
|
|
|
|
15
|
|
import org.jbind.xml.code.IConfiguration;
|
|
16
|
|
import org.jbind.xml.code.IConfigurationCode;
|
|
17
|
|
import org.jbind.xml.code.ISetupAndTearDown;
|
|
18
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
|
19
|
|
import org.jbind.xml.core.data.IDataContext;
|
|
20
|
|
import org.jbind.xml.instance.data.DataVisitor;
|
|
21
|
|
import org.jbind.xml.msg.XmlException;
|
|
22
|
|
import org.jbind.xml.msg.XmlMessages;
|
|
23
|
|
import org.xml.sax.InputSource;
|
|
24
|
|
|
|
25
|
|
public class ConfigurationCode extends XmlCode implements IConfigurationCode {
|
|
26
|
|
|
|
27
|
|
/**
|
|
28
|
|
* Creates a configurer.
|
|
29
|
|
*
|
|
30
|
|
* @param aUrl <i>(required)</i>.
|
|
31
|
|
* @param aNbSetupAndTearDownLevel Used for leveled setup and tear down of the
|
|
32
|
|
* configuration.
|
|
33
|
|
* @param aDataContext <i>(optional)</i>.
|
|
34
|
|
*/
|
|
35
|
1
|
public ConfigurationCode(URL aUrl, IDataContext aDataContext) {
|
|
36
|
1
|
super(aUrl, aDataContext);
|
|
37
|
|
}
|
|
38
|
|
|
|
39
|
0
|
public ConfigurationCode(InputSource anInputSource, IDataContext aDataContext) {
|
|
40
|
0
|
super(anInputSource, aDataContext);
|
|
41
|
|
}
|
|
42
|
|
|
|
43
|
2
|
public IConfiguration getConfiguration() throws XmlException {
|
|
44
|
2
|
IAnyTypeData code = getCode();
|
|
45
|
2
|
if (!(code instanceof IConfiguration)) {
|
|
46
|
0
|
throw new XmlException(XmlMessages.notAConfigurationInstance(getUrl(), code.getClass().getName(), IConfiguration.class.getName(), null));
|
|
47
|
|
}
|
|
48
|
2
|
return (IConfiguration)code;
|
|
49
|
|
}
|
|
50
|
|
|
|
51
|
1
|
public void setup() throws XmlException {
|
|
52
|
1
|
IConfiguration c = getConfiguration();
|
|
53
|
1
|
int nbLevels = c.getNbSetupAndTearDownLevels();
|
|
54
|
1
|
if (nbLevels > 0) {
|
|
55
|
1
|
SetupVisitor visitor = new SetupVisitor();
|
|
56
|
1
|
for (int level = 0; level < nbLevels; level++) {
|
|
57
|
3
|
visitor.setLevel(level);
|
|
58
|
3
|
c.accept_(visitor);
|
|
59
|
|
}
|
|
60
|
|
}
|
|
61
|
|
}
|
|
62
|
|
|
|
63
|
1
|
public void tearDown() throws XmlException {
|
|
64
|
1
|
IConfiguration c = getConfiguration();
|
|
65
|
1
|
int nbLevels = c.getNbSetupAndTearDownLevels();
|
|
66
|
1
|
if (nbLevels > 0) {
|
|
67
|
1
|
TearDownVisitor visitor = new TearDownVisitor();
|
|
68
|
1
|
for (int level = nbLevels; --level >= 0; ) {
|
|
69
|
3
|
visitor.setLevel(level);
|
|
70
|
3
|
c.accept_(visitor);
|
|
71
|
|
}
|
|
72
|
|
}
|
|
73
|
|
}
|
|
74
|
|
|
|
75
|
|
/**
|
|
76
|
|
* Sets up children before there ancestors.
|
|
77
|
|
*/
|
|
78
|
|
protected static class SetupVisitor extends DataVisitor {
|
|
79
|
|
|
|
80
|
|
private int myLevel = 0;
|
|
81
|
3
|
public void setLevel(int aLevel) {
|
|
82
|
3
|
myLevel = aLevel;
|
|
83
|
|
}
|
|
84
|
15
|
public void visitAnyTypeDataStarts(IAnyTypeData aData) {}
|
|
85
|
15
|
public void visitAnyTypeDataEnds(IAnyTypeData aData) {
|
|
86
|
15
|
for (Iterator i = aData.iterAttributes_(null, null); i.hasNext(); ) {
|
|
87
|
12
|
IAnyTypeData d = (IAnyTypeData)i.next();
|
|
88
|
12
|
if (d instanceof ISetupAndTearDown) {
|
|
89
|
0
|
((ISetupAndTearDown)d).setup(myLevel);
|
|
90
|
|
}
|
|
91
|
|
}
|
|
92
|
15
|
if (aData instanceof ISetupAndTearDown) {
|
|
93
|
9
|
((ISetupAndTearDown)aData).setup(myLevel);
|
|
94
|
|
}
|
|
95
|
|
}
|
|
96
|
|
}
|
|
97
|
|
|
|
98
|
|
/**
|
|
99
|
|
* Tears down ancestors before their children.
|
|
100
|
|
*/
|
|
101
|
|
protected static class TearDownVisitor extends DataVisitor {
|
|
102
|
|
|
|
103
|
|
private int myLevel = 0;
|
|
104
|
3
|
public void setLevel(int aLevel) {
|
|
105
|
3
|
myLevel = aLevel;
|
|
106
|
|
}
|
|
107
|
15
|
public void visitAnyTypeDataStarts(IAnyTypeData aData) {
|
|
108
|
15
|
if (aData instanceof ISetupAndTearDown) {
|
|
109
|
9
|
((ISetupAndTearDown)aData).tearDown(myLevel);
|
|
110
|
|
}
|
|
111
|
15
|
for (Iterator i = aData.iterAttributes_(null, null); i.hasNext(); ) {
|
|
112
|
12
|
IAnyTypeData d = (IAnyTypeData)i.next();
|
|
113
|
12
|
if (d instanceof ISetupAndTearDown) {
|
|
114
|
0
|
((ISetupAndTearDown)d).tearDown(myLevel);
|
|
115
|
|
}
|
|
116
|
|
}
|
|
117
|
|
}
|
|
118
|
15
|
public void visitAnyTypeDataEnds(IAnyTypeData aData) {}
|
|
119
|
|
}
|
|
120
|
|
|
|
121
|
|
|
|
122
|
|
}
|
|
123
|
|
|