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.cmp;
|
11
|
|
|
12
|
|
import java.io.OutputStream;
|
13
|
|
import java.io.OutputStreamWriter;
|
14
|
|
import java.io.PrintWriter;
|
15
|
|
import java.io.Writer;
|
16
|
|
|
17
|
|
import org.jbind.xml.base.IRange;
|
18
|
|
import org.jbind.xml.core.cmp.IComponent;
|
19
|
|
import org.jbind.xml.core.content.IElemGroupDesc;
|
20
|
|
import org.jbind.xml.core.content.IElemRefOrDecl;
|
21
|
|
import org.jbind.xml.core.content.IElemWildcard;
|
22
|
|
import org.jbind.xml.core.type.IAnyType;
|
23
|
|
import org.jbind.xml.msg.XmlException;
|
24
|
|
|
25
|
|
public class MarshalVisitor extends ComponentVisitor {
|
26
|
|
|
27
|
|
private PrintWriter myWriter;
|
28
|
|
private StringBuffer myIndent;
|
29
|
|
|
30
|
0
|
public MarshalVisitor(OutputStream aStream) {
|
31
|
0
|
this(new OutputStreamWriter(aStream), new StringBuffer());
|
32
|
|
}
|
33
|
|
|
34
|
0
|
public MarshalVisitor(Writer aWriter, StringBuffer anIndent) {
|
35
|
0
|
myWriter = new PrintWriter(aWriter, true);
|
36
|
0
|
myIndent = anIndent;
|
37
|
|
}
|
38
|
|
|
39
|
0
|
protected boolean doVisitElemRefOrDeclStart(IElemRefOrDecl aComponent) throws XmlException {
|
40
|
0
|
indent();
|
41
|
0
|
print("<element name=\"" + aComponent.getName() + "\" ");
|
42
|
0
|
print(aComponent.getRange());
|
43
|
0
|
print("/>\n");
|
44
|
0
|
return false;
|
45
|
|
}
|
46
|
|
|
47
|
0
|
protected boolean doVisitElemWildcardStart(IElemWildcard aComponent) throws XmlException {
|
48
|
0
|
println("<any/>");
|
49
|
0
|
return false;
|
50
|
|
}
|
51
|
|
|
52
|
0
|
protected boolean doVisitElemGroupDescStart(IElemGroupDesc aComponent) throws XmlException {
|
53
|
0
|
indent();
|
54
|
0
|
print("<" + aComponent.getCompositor() + " ");
|
55
|
0
|
print(aComponent.getRange());
|
56
|
0
|
print(">\n");
|
57
|
0
|
return true;
|
58
|
|
}
|
59
|
0
|
protected void doVisitElemGroupDescEnd(IElemGroupDesc aComponent) throws XmlException {
|
60
|
0
|
println("</" + aComponent.getCompositor() + ">");
|
61
|
|
}
|
62
|
|
|
63
|
0
|
protected boolean doVisitAnyTypeStart(IAnyType aComponent) throws XmlException {
|
64
|
0
|
println("<type" + (aComponent.isAnonymous() ? "" : "name=\"" + aComponent.getName() + "\"") + ">");
|
65
|
0
|
return true;
|
66
|
|
}
|
67
|
|
|
68
|
0
|
protected void doVisitAnyTypeEnd(IAnyType aComponent) throws XmlException {
|
69
|
0
|
println("</type>");
|
70
|
|
}
|
71
|
|
|
72
|
0
|
protected void doBeforeVisit(IComponent aComponent) throws XmlException {
|
73
|
0
|
addIndent();
|
74
|
|
}
|
75
|
0
|
protected void doAfterVisit(IComponent aComponent) throws XmlException {
|
76
|
0
|
removeIndent();
|
77
|
|
}
|
78
|
|
|
79
|
0
|
private void addIndent() {
|
80
|
0
|
myIndent.append('\t');
|
81
|
|
}
|
82
|
0
|
private void removeIndent() {
|
83
|
0
|
myIndent.setLength(myIndent.length() - 1);
|
84
|
|
}
|
85
|
0
|
private void indent() {
|
86
|
0
|
myWriter.print(myIndent.toString());
|
87
|
|
}
|
88
|
0
|
private void println(String aString) {
|
89
|
0
|
indent();
|
90
|
0
|
myWriter.println(aString);
|
91
|
|
}
|
92
|
0
|
private void print(String aString) {
|
93
|
0
|
myWriter.print(aString);
|
94
|
|
}
|
95
|
0
|
private void print(IRange aRange) {
|
96
|
0
|
myWriter.print("minOccurs=\"" + aRange.getMinOccurs() + "\" maxOccurs=\"" + (aRange.isUnbounded() ? "unbounded" : "" + aRange.getMaxOccurs()) + "\"");
|
97
|
|
}
|
98
|
|
}
|
99
|
|
|