| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AbstractVisitor; |
| 4 |
import com.thaiopensource.relaxng.edit.CompositePattern; |
| 5 |
import com.thaiopensource.relaxng.edit.DefineComponent; |
| 6 |
import com.thaiopensource.relaxng.edit.DivComponent; |
| 7 |
import com.thaiopensource.relaxng.edit.ElementPattern; |
| 8 |
import com.thaiopensource.relaxng.edit.ExternalRefPattern; |
| 9 |
import com.thaiopensource.relaxng.edit.GrammarPattern; |
| 10 |
import com.thaiopensource.relaxng.edit.IncludeComponent; |
| 11 |
import com.thaiopensource.relaxng.edit.ParentRefPattern; |
| 12 |
import com.thaiopensource.relaxng.edit.Pattern; |
| 13 |
import com.thaiopensource.relaxng.edit.RefPattern; |
| 14 |
import com.thaiopensource.relaxng.edit.UnaryPattern; |
| 15 |
import com.thaiopensource.util.VoidValue; |
| 16 |
import com.thaiopensource.relaxng.output.common.ErrorReporter; |
| 17 |
|
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.Map; |
| 20 |
|
| 21 |
class RefChecker extends AbstractVisitor { |
| 22 |
private final SchemaInfo schema; |
| 23 |
private final ErrorReporter er; |
| 24 |
private final Map<String, Ref> refMap = new HashMap<String, Ref>(); |
| 25 |
private int currentDepth = 0; |
| 26 |
|
| 27 |
static private class Ref { |
| 28 |
int checkRecursionDepth; |
| 29 |
|
| 30 |
Ref(int checkRecursionDepth) { |
| 31 |
this.checkRecursionDepth = checkRecursionDepth; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
private RefChecker(SchemaInfo schema, ErrorReporter er) { |
| 36 |
this.schema = schema; |
| 37 |
this.er = er; |
| 38 |
} |
| 39 |
|
| 40 |
static void check(SchemaInfo schema, ErrorReporter er) { |
| 41 |
schema.getGrammar().componentsAccept(new RefChecker(schema, er)); |
| 42 |
} |
| 43 |
|
| 44 |
public VoidValue visitDiv(DivComponent c) { |
| 45 |
c.componentsAccept(this); |
| 46 |
return VoidValue.VOID; |
| 47 |
} |
| 48 |
|
| 49 |
public VoidValue visitDefine(DefineComponent c) { |
| 50 |
String name = c.getName(); |
| 51 |
if (name == DefineComponent.START || refMap.get(name) == null) |
| 52 |
c.getBody().accept(this); |
| 53 |
return VoidValue.VOID; |
| 54 |
} |
| 55 |
|
| 56 |
public VoidValue visitInclude(IncludeComponent c) { |
| 57 |
schema.getSchema(c.getUri()).componentsAccept(this); |
| 58 |
return VoidValue.VOID; |
| 59 |
} |
| 60 |
|
| 61 |
public VoidValue visitElement(ElementPattern p) { |
| 62 |
currentDepth++; |
| 63 |
p.getChild().accept(this); |
| 64 |
currentDepth--; |
| 65 |
return VoidValue.VOID; |
| 66 |
} |
| 67 |
|
| 68 |
public VoidValue visitUnary(UnaryPattern p) { |
| 69 |
return p.getChild().accept(this); |
| 70 |
} |
| 71 |
|
| 72 |
public VoidValue visitComposite(CompositePattern p) { |
| 73 |
p.childrenAccept(this); |
| 74 |
return VoidValue.VOID; |
| 75 |
} |
| 76 |
|
| 77 |
public VoidValue visitRef(RefPattern p) { |
| 78 |
Ref ref = refMap.get(p.getName()); |
| 79 |
if (ref == null) { |
| 80 |
ref = new Ref(currentDepth); |
| 81 |
refMap.put(p.getName(), ref); |
| 82 |
Pattern body = schema.getBody(p); |
| 83 |
if (body == null) |
| 84 |
er.error("undefined_reference", p.getName(), p.getSourceLocation()); |
| 85 |
else |
| 86 |
schema.getBody(p).accept(this); |
| 87 |
ref.checkRecursionDepth = -1; |
| 88 |
} |
| 89 |
else if (currentDepth == ref.checkRecursionDepth) |
| 90 |
er.error("recursive_reference", p.getName(), p.getSourceLocation()); |
| 91 |
return VoidValue.VOID; |
| 92 |
} |
| 93 |
|
| 94 |
public VoidValue visitExternalRef(ExternalRefPattern p) { |
| 95 |
er.error("external_ref_not_supported", p.getSourceLocation()); |
| 96 |
return VoidValue.VOID; |
| 97 |
} |
| 98 |
|
| 99 |
public VoidValue visitGrammar(GrammarPattern p) { |
| 100 |
er.error("nested_grammar_not_supported", p.getSourceLocation()); |
| 101 |
return VoidValue.VOID; |
| 102 |
} |
| 103 |
|
| 104 |
public VoidValue visitParentRef(ParentRefPattern p) { |
| 105 |
er.error("parent_ref_no_grammar", p.getSourceLocation()); |
| 106 |
return VoidValue.VOID; |
| 107 |
} |
| 108 |
} |