-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathMSVExpressionVisitorDescendantsAsHTMLString.java
More file actions
635 lines (554 loc) · 20.8 KB
/
MSVExpressionVisitorDescendantsAsHTMLString.java
File metadata and controls
635 lines (554 loc) · 20.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
* **********************************************************************
*
* <p>DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
*
* <p>Copyright 2009, 2010 Oracle and/or its affiliates. All rights reserved.
*
* <p>Use is subject to license terms.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at
* http://odftoolkit.org/docs/license.txt
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
*
* <p>See the License for the specific language governing permissions and limitations under the
* License.
*
* <p>**********************************************************************
*/
package schema2template.grammar;
import com.sun.msv.datatype.SerializationContext;
import com.sun.msv.datatype.xsd.*;
import com.sun.msv.grammar.*;
import com.sun.msv.grammar.util.ExpressionWalker;
import java.util.*;
import org.relaxng.datatype.Datatype;
import org.relaxng.datatype.ValidationContext;
/**
* This visitor visits an Expression and returns an HTML String with its children (or descendants)
* relationship as brief pseudo reg-ex for documentation purpose.
*
* <p>Usage example: <em>(String) myExpression.visit(myMSVExpressionVisitorDescendantsAsHTMLString,
* XMLModel.getHeadsOfIslands(grammar), 1)</em>
*
* <p>The HeadOfIslands are necessary to avoid endless loops, where islands are parts of the grammar
* that are being reused/referenced multiple times. The final parameter express how many child
* element level can be found before stopping. 1 will stop right after writing the name of the first
* encountered child element.
*
* <p>Please note that you do not use any method of this class directly!
*/
public class MSVExpressionVisitorDescendantsAsHTMLString extends ExpressionWalker {
// ExpressionWalker class traverses expressions in depth-first order.
// So this invocation traverses the all reachable expressions from
// the top level expression.
public MSVExpressionVisitorDescendantsAsHTMLString(
StringBuilder builder, Set<Expression> headsOfIslands) {
this(builder, headsOfIslands, 1);
}
public MSVExpressionVisitorDescendantsAsHTMLString(
StringBuilder builder, Set<Expression> headsOfIslands, int maxElementDepth) {
this.builder = builder;
this.maxElementDepth = maxElementDepth;
// an island is a sub(tree) of the grammar being reused and should be written only once to avoid
// endless-loops
this.headsOfIslands = headsOfIslands;
}
StringBuilder builder = null;
int maxElementDepth = 1;
int currentElementDepth = 0;
final Set<Expression> headsOfIslands;
final Set<Expression> islandHeadPassed = new HashSet<Expression>();
boolean isWithinAttributeValue = false;
public void onElement(ElementExp exp) {
String elementName = evaluateNameClass(exp.getNameClass());
builder.append("<" + elementName + " ");
// prevent infinite loops by marking earlier multiple used/referenced expression (being
// elements/references) as "head of islands"
boolean isIslandHead = headsOfIslands.contains(exp);
if ((!isIslandHead || (isIslandHead && !islandHeadPassed.contains(exp)))
&& (maxElementDepth > currentElementDepth)) {
currentElementDepth++;
if (isIslandHead) {
// making sure that an island is only entered once
islandHeadPassed.add(exp);
}
visitUnary(exp.contentModel);
} else {
builder.append(" ... ");
}
builder.append(">");
}
public void onEpsilon() {
builder.append("EMPTY");
}
public void onNullSet() {
builder.append("\nEXPR_notAllowed");
}
public void onAnyString() {
builder.append("TEXT");
}
public void onInterleave(InterleaveExp exp) {
visitBinExp("interleave", exp, InterleaveExp.class);
}
public void onConcur(ConcurExp exp) {
throw new IllegalArgumentException("the grammar includes concur, which is not supported");
}
public void onList(ListExp exp) {
builder.append("\nSTART_list");
visitUnary(exp.exp);
builder.append("\nEND_list");
}
protected void onOptional(Expression exp) {
if (exp instanceof OneOrMoreExp) {
// (X+)? == X*
onZeroOrMore((OneOrMoreExp) exp);
return;
}
if (isWithinAttributeValue) {
builder.append("(");
} else {
builder.append(" (");
}
visitUnary(exp);
// context/state driven layout
if (isWithinAttributeValue) {
builder.append(")?");
} else {
builder.append(")?\n\t");
}
}
public void onChoice(ChoiceExp exp) {
// use optional instead of <choice> p <empty/> </choice>
if (exp.exp1 == Expression.epsilon) {
onOptional(exp.exp2);
return;
}
if (exp.exp2 == Expression.epsilon) {
onOptional(exp.exp1);
return;
}
Expression[] children = exp.getChildren();
for (int i = 0; i < children.length; i++) {
children[i].visit(this);
if ((i + 1) < children.length) {
builder.append(" | ");
}
}
}
public void onSequence(SequenceExp exp) {
builder.append("\n\t(");
visitBinExp("group", exp, SequenceExp.class);
builder.append(")");
}
public void visitBinExp(String elementName, BinaryExp exp, Class<?> type) {
// since AGM is binarized,
// <choice> a b c </choice> is represented as
// <choice> a <choice> b c </choice></choice>
// this method print them as <choice> a b c </choice>
// builder.append("\nSTART: " + elementName);
Expression[] children = exp.getChildren();
for (int i = 0; i < children.length; i++) children[i].visit(this);
// builder.append("\nEND: " + elementName);
}
public void onMixed(MixedExp exp) {
builder.append("<MIXED>");
visitUnary(exp.exp);
builder.append("</MIXED>");
}
public void onOneOrMore(OneOrMoreExp exp) {
builder.append("(");
visitUnary(exp.exp);
builder.append(")+ ");
}
protected void onZeroOrMore(OneOrMoreExp exp) {
// note that this method is not a member of TREXPatternVisitor.
builder.append("(");
visitUnary(exp.exp);
builder.append(")* ");
}
public void onAttribute(AttributeExp exp) {
builder.append(evaluateNameClass(exp.nameClass) + "=\"");
isWithinAttributeValue = true;
visitUnary(exp.exp);
builder.append("\" ");
isWithinAttributeValue = false;
}
/** print expression but suppress unnecessary sequence. */
public void visitUnary(Expression exp) {
// TREX treats <zeroOrMore> p q </zeroOrMore>
// as <zeroOrMore><group> p q </group></zeroOrMore>
// This method tries to exploit this property to
// simplify the result.
if (exp instanceof SequenceExp) {
SequenceExp seq = (SequenceExp) exp;
visitUnary(seq.exp1);
seq.exp2.visit(this);
} else {
exp.visit(this);
}
}
public void onValue(ValueExp exp) {
if (exp.dt instanceof XSDatatypeImpl) {
XSDatatypeImpl base = (XSDatatypeImpl) exp.dt;
final List<String> ns = new ArrayList<String>();
String lex =
base.convertToLexicalValue(
exp.value,
new SerializationContext() {
public String getNamespacePrefix(String namespaceURI) {
int cnt = ns.size() / 2;
ns.add("xmlns:ns" + cnt);
ns.add(namespaceURI);
return "ns" + cnt;
}
});
if (base != TokenType.theInstance) {
// if the type is token, we don't need @type.
// builder.append("\ntype: " + base.getName());
builder.append(base.getName());
}
builder.append(lex);
// builder.append("\nEND_value");
return;
}
throw new UnsupportedOperationException(exp.dt.getClass().getName());
}
public void onData(DataExp exp) {
Datatype dt = exp.dt;
if (dt instanceof XSDatatypeImpl) {
XSDatatypeImpl dti = (XSDatatypeImpl) dt;
if (dti.getName() != null
&& (isPrimitiveDatatypeType(dti.getName()) || isDerivedDataType(dti.getName()))) {
builder.append(
"<a href=\"https://www.w3.org/TR/xmlschema-2/#"
+ dti.getName()
+ "\">#"
+ dti.getName()
+ "</a>");
} else if (exp.name.equals("anyIRI")) {
builder.append("<a href=\"https://www.rfc-editor.org/rfc/rfc3987\">#anyIRI</a>");
} else if (isPredefinedType(dt)) {
// it's a MSV pre-defined type.
builder.append("<!" + dti.getName() + "!>");
} else {
serializeDataType(dti);
}
return;
}
// unknown datatype
builder.append("\nEXPR_data-unknown: " + "class " + dt.getClass().getName());
}
public void onOther(OtherExp exp) {
exp.exp.visit(this); // ignore otherexp
}
public void onRef(ReferenceExp exp) {
// prevent infinite loops by marking earlier multiple used/referenced expression (being
// elements/references) as "head of islands"
boolean isIslandHead = headsOfIslands.contains(exp);
if (!isIslandHead || (isIslandHead && !islandHeadPassed.contains(exp))) {
if (isIslandHead) {
// making sure that an island is only entered once
islandHeadPassed.add(exp);
}
// this expression will not be written as a named pattern.
if (isPrimitiveDatatypeType(exp.name) || isDerivedDataType(exp.name)) {
builder.append(
"<a href=\"https://www.w3.org/TR/xmlschema-2/#"
+ exp.name
+ "\">#"
+ exp.name
+ "</a>");
} else if (exp.name.equals("anyIRI")) {
builder.append("<a href=\"https://www.rfc-editor.org/rfc/rfc3987\">#anyIRI</a>");
} else {
exp.exp.visit(this);
}
if (isIslandHead) {
// if the ref was left, we can re-enter it
islandHeadPassed.remove(exp);
}
} else {
if (isPrimitiveDatatypeType(exp.name) || isDerivedDataType(exp.name)) {
builder.append(
"<a href=\"https://www.w3.org/TR/xmlschema-2/#"
+ exp.name
+ "\">#"
+ exp.name
+ "</a>");
} else if (exp.name.equals("anyIRI")) {
builder.append("<a href=\"https://www.rfc-editor.org/rfc/rfc3987\">#anyIRI</a>");
} else {
builder.append("<xsd:ref name=\"" + exp.name + "\"/>");
}
}
}
/** if XML Schema primitive datatypes https://www.w3.org/TR/xmlschema-2/#built-in-datatypes */
private static boolean isPrimitiveDatatypeType(String s) {
return (s.equals("string")
|| s.equals("boolean")
|| s.equals("decimal")
|| s.equals("float")
|| s.equals("double")
|| s.equals("duration")
|| s.equals("dateTime")
|| s.equals("time")
|| s.equals("date")
|| s.equals("gYearMonth")
|| s.equals("gYear")
|| s.equals("gMonthDay")
|| s.equals("gDay")
|| s.equals("gMonth")
|| s.equals("hexBinary")
|| s.equals("base64Binary")
|| s.equals("anyURI")
|| s.equals("QName")
|| s.equals("NOTATION"));
}
/** if XML Schema primitive datatypes https://www.w3.org/TR/xmlschema-2/#built-in-datatypes */
private static boolean isDerivedDataType(String s) {
return s.equals("normalizedString")
|| s.equals("token")
|| s.equals("language")
|| s.equals("NMTOKEN")
|| s.equals("NMTOKENS")
|| s.equals("Name")
|| s.equals("NCName")
|| s.equals("ID")
|| s.equals("IDREF")
|| s.equals("IDREFS")
|| s.equals("ENTITY")
|| s.equals("ENTITIES")
|| s.equals("integer")
|| s.equals("nonPositiveInteger")
|| s.equals("negativeInteger")
|| s.equals("long")
|| s.equals("int")
|| s.equals("short")
|| s.equals("byte")
|| s.equals("nonNegativeInteger")
|| s.equals("unsignedLong")
|| s.equals("unsignedInt")
|| s.equals("unsignedShort")
|| s.equals("unsignedByte")
|| s.equals("positiveInteger");
}
private static String evaluateNameClass(NameClass nc) {
String elementName = "";
if ((nc instanceof SimpleNameClass)
|| (nc instanceof AnyNameClass)
|| (nc instanceof ChoiceNameClass)) {
String elementPrefix = null;
if ((nc instanceof SimpleNameClass)) {
elementName = ((SimpleNameClass) nc).localName;
// the feature below works only with the namespace-prefix2 branch on MSV
elementPrefix = ((SimpleNameClass) nc).prefix;
if (elementPrefix != null && !elementPrefix.isEmpty()) {
elementName = elementPrefix.concat(":").concat(elementName);
}
} else if (nc instanceof AnyNameClass) {
elementName = "*:*";
} else if (nc instanceof ChoiceNameClass) {
elementName = "CHOICE_NAME_CLASS";
}
if (elementName.equals("ExtensionContent")) {
elementName = "ExtensionContent";
}
}
return elementName;
}
/**
* serializes the given datatype.
*
* <p>The caller should generate events for <simpleType> element if necessary.
*/
protected void serializeDataType(XSDatatype dt) {
if (dt instanceof UnionType) {
serializeUnionType((UnionType) dt);
return;
}
// store names of the applied facets into this set
Set<String> appliedFacets = new HashSet<String>();
// store effective facets (those which are not shadowed by another facet).
Vector<XSDatatype> effectiveFacets = new Vector<XSDatatype>();
XSDatatype x = dt;
while (x instanceof DataTypeWithFacet || x instanceof FinalComponent) {
if (x instanceof FinalComponent) {
// skip FinalComponent
x = x.getBaseType();
continue;
}
String facetName = ((DataTypeWithFacet) x).facetName;
if (facetName.equals(XSDatatypeImpl.FACET_ENUMERATION)) {
// if it contains enumeration, then we will serialize this
// by using <value>s.
serializeEnumeration((XSDatatypeImpl) dt, (EnumerationFacet) x);
return;
}
if (facetName.equals(XSDatatypeImpl.FACET_WHITESPACE)) {
System.err.println("warning: unsupported whiteSpace facet is ignored");
x = x.getBaseType();
continue;
}
// find the same facet twice.
// pattern is allowed more than once.
if (!appliedFacets.contains(facetName)
|| appliedFacets.equals(XSDatatypeImpl.FACET_PATTERN)) {
appliedFacets.add(facetName);
effectiveFacets.add(x);
}
x = ((DataTypeWithFacet) x).baseType;
}
if (x instanceof ListType) {
// the base type is list.
serializeListType((XSDatatypeImpl) dt);
return;
}
// it cannot be the union type. Union type cannot be derived by
// restriction.
// so this must be one of the pre-defined types.
if (!(x instanceof ConcreteType)) throw new Error(x.getClass().getName());
if (x instanceof com.sun.msv.grammar.relax.EmptyStringType) {
// empty string
builder.append("\"\"");
return;
}
if (x instanceof com.sun.msv.grammar.relax.NoneType) {
// "none" is equal to <notAllowed/>
builder.append("notAllowed");
return;
}
builder.append(x.getName());
// serialize effective facets
for (int i = effectiveFacets.size() - 1; i >= 0; i--) {
DataTypeWithFacet dtf = (DataTypeWithFacet) effectiveFacets.get(i);
if (dtf instanceof LengthFacet) {
param("length", Long.toString(((LengthFacet) dtf).length));
} else if (dtf instanceof MinLengthFacet) {
param("minLength", Long.toString(((MinLengthFacet) dtf).minLength));
} else if (dtf instanceof MaxLengthFacet) {
param("maxLength", Long.toString(((MaxLengthFacet) dtf).maxLength));
} else if (dtf instanceof PatternFacet) {
String pattern = "";
PatternFacet pf = (PatternFacet) dtf;
for (int j = 0; j < pf.getRegExps().length; j++) {
if (pattern.length() != 0) pattern += "|";
pattern += pf.patterns[j];
}
param("pattern", pattern);
} else if (dtf instanceof TotalDigitsFacet) {
param("totalDigits", Long.toString(((TotalDigitsFacet) dtf).precision));
} else if (dtf instanceof FractionDigitsFacet) {
param("fractionDigits", Long.toString(((FractionDigitsFacet) dtf).scale));
} else if (dtf instanceof RangeFacet) {
param(dtf.facetName, dtf.convertToLexicalValue(((RangeFacet) dtf).limitValue, null));
// we don't need to pass SerializationContext because it is only
// for QName.
} else if (dtf instanceof WhiteSpaceFacet) {; // do nothing.
} else
// undefined facet type
throw new Error();
}
builder.append("]");
}
protected void param(String name, String value) {
/*2DO
writer.start("param", new String[] { "name", name });
writer.characters(value);
writer.end("param");
*/
}
/** returns true if the specified type is a pre-defined XSD type without any facet. */
protected boolean isPredefinedType(Datatype x) {
return !(x instanceof DataTypeWithFacet
|| x instanceof UnionType
|| x instanceof ListType
|| x instanceof FinalComponent
|| x instanceof com.sun.msv.grammar.relax.EmptyStringType
|| x instanceof com.sun.msv.grammar.relax.NoneType);
}
/** serializes a union type. this method is called by serializeDataType method. */
protected void serializeUnionType(UnionType dt) {
// serialize member types.
for (int i = 0; i < dt.memberTypes.length; i++) serializeDataType(dt.memberTypes[i]);
}
/** serializes a list type. this method is called by serializeDataType method. */
protected void serializeListType(XSDatatypeImpl dt) {
ListType base = (ListType) dt.getConcreteType();
if (dt.getFacetObject(XSDatatype.FACET_LENGTH) != null) {
// with the length facet.
int len = ((LengthFacet) dt.getFacetObject(XSDatatype.FACET_LENGTH)).length;
builder.append("<rng:list>");
for (int i = 0; i < len; i++) serializeDataType(base.itemType);
builder.append("</rng:list>");
return;
}
if (dt.getFacetObject(XSDatatype.FACET_MAXLENGTH) != null)
throw new UnsupportedOperationException(
"warning: maxLength facet to list type is not properly converted.");
MinLengthFacet minLength = (MinLengthFacet) dt.getFacetObject(XSDatatype.FACET_MINLENGTH);
builder.append("<list>");
if (minLength != null) {
// list n times
for (int i = 0; i < minLength.minLength; i++) serializeDataType(base.itemType);
}
builder.append("<zeroOrMore>");
serializeDataType(base.itemType);
builder.append("</zeroOrMore>");
builder.append("</list>");
}
/** serializes a type with enumeration. this method is called by serializeDataType method. */
protected void serializeEnumeration(XSDatatypeImpl dt, EnumerationFacet enums) {
Object[] values = enums.values.toArray();
if (values.length > 1) builder.append("rng:choice");
for (int i = 0; i < values.length; i++) {
final Vector<String> ns = new Vector<String>();
String lex =
dt.convertToLexicalValue(
values[i],
new SerializationContext() {
public String getNamespacePrefix(String namespaceURI) {
int cnt = ns.size() / 2;
ns.add("xmlns:ns" + cnt);
ns.add(namespaceURI);
return "ns" + cnt;
}
});
// make sure that the converted lexical value is allowed by this type.
// sometimes, facets that are added later rejects some of
// enumeration values.
boolean allowed =
dt.isValid(
lex,
new ValidationContext() {
public String resolveNamespacePrefix(String prefix) {
if (!prefix.startsWith("ns")) return null;
int i = Integer.parseInt(prefix.substring(2));
return (String) ns.get(i * 2 + 1);
}
public boolean isUnparsedEntity(String name) {
return true;
}
public boolean isNotation(String name) {
return true;
}
public String getBaseUri() {
return null;
}
});
ns.add("type");
ns.add(dt.getConcreteType().getName());
if (allowed) {
builder.append("<rng:value type=" + dt.getConcreteType().getName() + "\">");
builder.append(lex);
builder.append("</rng:value>");
}
}
if (values.length > 1) builder.append("</choice>");
}
}