forked from eed3si9n/scalaxb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseSchema.scala
More file actions
219 lines (171 loc) · 6.54 KB
/
parseSchema.scala
File metadata and controls
219 lines (171 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
object parseSchema {
import scala.collection.immutable.{ Map, ListMap };
import scala.io.Source ;
import scala.xml._ ;
import scala.xml.xsd._ ;
import scala.xml.parsing.ConstructingParser;
/** - names and lifts anonymous types
* - desugarizes
* complexContent(x) => complexContent(restriction(@base="xs:anyType"), x)
* if x not in (extension|restriction)
* - removes "annotation"
*/
class Lifter {
var ii = 0;
def fresh(name:String) = {
val res = name + "$"+ii.toString();
ii = ii + 1;
res
}
var moreDecls: List[Node] = Nil;
/** */
def desugarize(ct: Node) =
if(isComplexType(ct)) ct.child(0).label match {
case "simpleContent" | "complexContent" => ct // ok
case _ =>
val xs = xsPrefix(ct.scope);
val md = new UnprefixedAttribute("base", xs+":AnyType", Null);
Elem(xs,"complexContent", Null, ct.scope,
Elem(xs,"restriction", md, ct.scope, ct.child:_*))
} else ct;
def transform(x: Seq[Node]): Seq[Node] =
x.toList filter { z => !isAnnotation(z) } map transform;
def transform(x: Node): Node = x match {
case _ if isElem(x) && ((x \ "@type") concat (x \ "@ref")).length == 0 =>
// anonymous
val z = desugarize(x.child(0)); // complexType/simpleType
val nchild = transform(z.child);
val tpeName = fresh(x.attribute("name").toString());
val md = new UnprefixedAttribute("name", tpeName, z.attributes);
val decl = Elem( z.prefix, z.label, md, z.scope, nchild:_* );
moreDecls = decl :: moreDecls;
val md2 = new UnprefixedAttribute("name", tpeName, Null);
Elem( z.prefix, z.label, md2, z.scope, Nil:_*);
case Elem(pre,lab,md,scp,child@_*) =>
val z = Elem(pre,lab,md,scp,transform(child):_*);
desugarize(z)
case _ => x
}
def lift(x:Node) = {
val y = transform(x);
val nchild = moreDecls concat y.child;
Elem(y.prefix, y.label, y.attributes, y.scope, nchild:_*);
}
}
final val xsdns = "http://www.w3.org/2001/XMLSchema";
final def xsPrefix(scp:NamespaceBinding): String =
if(scp==TopScope)
error("cannot find xsd namespace ?!"); // cannot happen
else if (scp.uri == xsdns)
scp.prefix;
else
xsPrefix(scp.parent);
def isAnnotation(x:Node) = x.label == "annotation" && x.namespace == xsdns;
def isElem(x:Node) = x.label == "element" && x.namespace == xsdns;
def isComplexType(x:Node) = x.label == "complexType" && x.namespace == xsdns;
def isSimpleType(x:Node) = x.label == "simpleType" && x.namespace == xsdns;
def isExtension(x:Node) = x.label == "extension" && x.namespace == xsdns;
def isRestriction(x:Node) = x.label == "restriction" && x.namespace == xsdns;
var elems: Map[Pair[String,String],ElemDecl] = _;
var symbols: Map[Pair[String,String],XsTypeSymbol] = _;
var targetNamespace: String = null;
def initElems = {
elems = ListMap.Empty[Pair[String,String],ElemDecl];
}
def initSymbols = {
symbols =
ListMap.Empty[Pair[String,String],XsTypeSymbol]
.update(Pair(xsdns, "boolean"), xsBoolean)
.update(Pair(xsdns, "double"), xsDouble)
.update(Pair(xsdns, "float"), xsFloat)
.update(Pair(xsdns, "int"), xsInt)
.update(Pair(xsdns, "long"), xsLong)
.update(Pair(xsdns, "short"), xsShort)
.update(Pair(xsdns, "string"), xsString);
}
def symbol(s1:String, s2:String) = symbols.get(Pair(s1,s2));
/** returns null if tpe is not bound by a builtin */
def lookup(scp:NamespaceBinding, tpe:String): Option[XsTypeSymbol] = {
tpe.indexOf(':') match {
case -1 =>
val ns = scp.getURI(null);
symbol(ns,tpe);
case i =>
val pr = tpe.substring(0,i);
val ns = scp.getURI(pr);
val id = tpe.substring(i+1,tpe.length());
//Console.println("[lookup] pr="+pr);
//Console.println("[lookup] ns="+ns);
//Console.println("[lookup] id="+id);
symbol(ns, id)
}
}
def main(args:Array[String]): Unit = {
if(args.length != 1)
return {};
val p = new ConstructingParser(Source.fromFile(args(0)), false);
p.nextch; //init
Console.println("init symbol, elem table");
initSymbols;
initElems;
Console.println("parse schema");
var xsd = p.element(TopScope)(0);
Console.print("set target namespace to: ");
targetNamespace = xsd.attribute("targetNamespace").toString();
Console.println(targetNamespace);
Console.println("lift anonymous types");
xsd = new Lifter().lift(xsd);
Console.println(new PrettyPrinter(80,3).format(xsd));
Console.println("make type symbols");
Console.println("--complex types:");
for(val c <- xsd \ "complexType") {
val name = c.attribute("name").toString();
val csym = new ComplexTypeSymbol(name);
Console.println(" "+name);
symbols = symbols.update(Pair(targetNamespace, name), csym);
}
Console.println("--simple types:");
for(val c <- xsd \ "simpleType") {
val name = c.attribute("name").toString();
val csym = new SimpleTypeSymbol(name);
Console.println(" "+name);
symbols = symbols.update(Pair(targetNamespace, name), csym);
}
Console.println("--top-level elements:");
for(val c <- xsd \ "element") { // these all have a name
val name = c.attribute("name").toString();
val tpeRef = c.attribute("type").toString();
// lookup type
lookup(c.scope, tpeRef) match {
case None =>
Console.println("error: type \""+tpeRef+"\" not found");
case Some(tpe) =>
val decl = ElemDecl(name, tpe);
Console.println(" "+decl);
elems = elems.update(Pair(targetNamespace,name), decl);
}
}
/*
Console.println("--all elements:");
Console.println((xsd \\ "element").length);
for(val c <- xsd \\ "element") {
val ref: String = c.attribute("ref");
if (ref!=null) { // reference .... is an element reference anyway
} else {
val tpe: String = c.attribute("type"); // anonymous type
if (tpe!=null) {
val ed = ElemDecl(c.attribute("name"), c.attribute("tpe"));
Console.println(ed);
} else {
// anonymous type
}
}
*/
Console.println("end (for now)");
Console.println("symbols:");
for(val Pair(k,v) <- symbols.elements) {
Console.println(" "+k+" -> "+v);
}
Console.println("elems:"+elems.toString());
}
}