| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AbstractVisitor; |
| 4 |
import com.thaiopensource.relaxng.edit.AnyNameNameClass; |
| 5 |
import com.thaiopensource.relaxng.edit.AttributePattern; |
| 6 |
import com.thaiopensource.relaxng.edit.ComponentVisitor; |
| 7 |
import com.thaiopensource.relaxng.edit.CompositePattern; |
| 8 |
import com.thaiopensource.relaxng.edit.DataPattern; |
| 9 |
import com.thaiopensource.relaxng.edit.DefineComponent; |
| 10 |
import com.thaiopensource.relaxng.edit.DivComponent; |
| 11 |
import com.thaiopensource.relaxng.edit.ElementPattern; |
| 12 |
import com.thaiopensource.relaxng.edit.EmptyPattern; |
| 13 |
import com.thaiopensource.relaxng.edit.GroupPattern; |
| 14 |
import com.thaiopensource.relaxng.edit.IncludeComponent; |
| 15 |
import com.thaiopensource.relaxng.edit.InterleavePattern; |
| 16 |
import com.thaiopensource.relaxng.edit.ListPattern; |
| 17 |
import com.thaiopensource.relaxng.edit.MixedPattern; |
| 18 |
import com.thaiopensource.relaxng.edit.NsNameNameClass; |
| 19 |
import com.thaiopensource.relaxng.edit.OneOrMorePattern; |
| 20 |
import com.thaiopensource.relaxng.edit.OptionalPattern; |
| 21 |
import com.thaiopensource.relaxng.edit.Pattern; |
| 22 |
import com.thaiopensource.relaxng.edit.PatternVisitor; |
| 23 |
import com.thaiopensource.relaxng.edit.RefPattern; |
| 24 |
import com.thaiopensource.relaxng.edit.TextPattern; |
| 25 |
import com.thaiopensource.relaxng.edit.UnaryPattern; |
| 26 |
import com.thaiopensource.relaxng.edit.ValuePattern; |
| 27 |
import com.thaiopensource.util.VoidValue; |
| 28 |
import com.thaiopensource.relaxng.edit.ZeroOrMorePattern; |
| 29 |
import com.thaiopensource.relaxng.output.common.ErrorReporter; |
| 30 |
|
| 31 |
import java.util.HashSet; |
| 32 |
import java.util.Set; |
| 33 |
|
| 34 |
public class RestrictionsChecker { |
| 35 |
private final SchemaInfo si; |
| 36 |
private final ErrorReporter er; |
| 37 |
private final Set<Pattern> checkedPatterns = new HashSet<Pattern>(); |
| 38 |
|
| 39 |
private static final int DISALLOW_ELEMENT = 0x1; |
| 40 |
private static final int DISALLOW_ATTRIBUTE = 0x2; |
| 41 |
private static final int DISALLOW_LIST = 0x4; |
| 42 |
private static final int DISALLOW_TEXT = 0x8; |
| 43 |
private static final int DISALLOW_EMPTY = 0x10; |
| 44 |
private static final int DISALLOW_DATA = 0x20; |
| 45 |
private static final int DISALLOW_GROUP = 0x40; |
| 46 |
private static final int DISALLOW_INTERLEAVE = 0x80; |
| 47 |
private static final int DISALLOW_ONE_OR_MORE = 0x100; |
| 48 |
|
| 49 |
private static final int START_DISALLOW = |
| 50 |
DISALLOW_ATTRIBUTE|DISALLOW_LIST|DISALLOW_TEXT|DISALLOW_DATA|DISALLOW_EMPTY|DISALLOW_GROUP|DISALLOW_INTERLEAVE|DISALLOW_ONE_OR_MORE; |
| 51 |
private static final int LIST_DISALLOW = |
| 52 |
DISALLOW_ATTRIBUTE|DISALLOW_ELEMENT|DISALLOW_TEXT|DISALLOW_LIST|DISALLOW_INTERLEAVE; |
| 53 |
private static final int DATA_EXCEPT_DISALLOW = |
| 54 |
DISALLOW_ATTRIBUTE|DISALLOW_ELEMENT|DISALLOW_LIST|DISALLOW_EMPTY|DISALLOW_TEXT|DISALLOW_GROUP|DISALLOW_INTERLEAVE|DISALLOW_ONE_OR_MORE; |
| 55 |
private static final int ATTRIBUTE_DISALLOW = |
| 56 |
DISALLOW_ATTRIBUTE|DISALLOW_ELEMENT; |
| 57 |
|
| 58 |
private final PatternVisitor<VoidValue> startVisitor = new Visitor("start", START_DISALLOW); |
| 59 |
private final PatternVisitor<VoidValue> topLevelVisitor = new ListVisitor(null, 0); |
| 60 |
private final PatternVisitor<VoidValue> elementVisitor = new ElementVisitor(); |
| 61 |
private final PatternVisitor<VoidValue> elementRepeatVisitor = new ElementRepeatVisitor(); |
| 62 |
private final PatternVisitor<VoidValue> elementRepeatGroupVisitor = new Visitor("element_repeat_group", DISALLOW_ATTRIBUTE); |
| 63 |
private final PatternVisitor<VoidValue> elementRepeatInterleaveVisitor = new Visitor("element_repeat_interleave", DISALLOW_ATTRIBUTE); |
| 64 |
private final PatternVisitor<VoidValue> attributeVisitor = new Visitor("attribute", ATTRIBUTE_DISALLOW); |
| 65 |
private final PatternVisitor<VoidValue> listVisitor = new ListVisitor("list", LIST_DISALLOW); |
| 66 |
private final PatternVisitor<VoidValue> dataExceptVisitor = new Visitor("data_except", DATA_EXCEPT_DISALLOW); |
| 67 |
|
| 68 |
class Visitor extends AbstractVisitor { |
| 69 |
private final String contextKey; |
| 70 |
private final int flags; |
| 71 |
|
| 72 |
Visitor(String contextKey, int flags) { |
| 73 |
this.contextKey = contextKey; |
| 74 |
this.flags = flags; |
| 75 |
} |
| 76 |
|
| 77 |
private boolean checkContext(int flag, String patternName, Pattern p) { |
| 78 |
if ((flags & flag) != 0) { |
| 79 |
er.error("illegal_contains", er.getLocalizer().message(contextKey), patternName, p.getSourceLocation()); |
| 80 |
return false; |
| 81 |
} |
| 82 |
else |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
public VoidValue visitGroup(GroupPattern p) { |
| 87 |
if (checkContext(DISALLOW_GROUP, "group", p)) { |
| 88 |
checkGroup(p); |
| 89 |
super.visitGroup(p); |
| 90 |
} |
| 91 |
return VoidValue.VOID; |
| 92 |
} |
| 93 |
|
| 94 |
public VoidValue visitInterleave(InterleavePattern p) { |
| 95 |
if (checkContext(DISALLOW_INTERLEAVE, "interleave", p)) { |
| 96 |
checkGroup(p); |
| 97 |
super.visitInterleave(p); |
| 98 |
} |
| 99 |
return VoidValue.VOID; |
| 100 |
} |
| 101 |
|
| 102 |
public VoidValue visitElement(ElementPattern p) { |
| 103 |
if (checkContext(DISALLOW_ELEMENT, "element", p) && !alreadyChecked(p)) |
| 104 |
p.getChild().accept(elementVisitor); |
| 105 |
return VoidValue.VOID; |
| 106 |
} |
| 107 |
|
| 108 |
public VoidValue visitAttribute(AttributePattern p) { |
| 109 |
if (checkContext(DISALLOW_ATTRIBUTE, "attribute", p) && !alreadyChecked(p)) |
| 110 |
p.getChild().accept(attributeVisitor); |
| 111 |
return VoidValue.VOID; |
| 112 |
} |
| 113 |
|
| 114 |
public VoidValue visitData(DataPattern p) { |
| 115 |
if (checkContext(DISALLOW_DATA, "data", p) && !alreadyChecked(p)) { |
| 116 |
Pattern except = p.getExcept(); |
| 117 |
if (except != null) |
| 118 |
except.accept(dataExceptVisitor); |
| 119 |
} |
| 120 |
return VoidValue.VOID; |
| 121 |
} |
| 122 |
|
| 123 |
public VoidValue visitValue(ValuePattern p) { |
| 124 |
checkContext(DISALLOW_DATA, "value", p); |
| 125 |
return VoidValue.VOID; |
| 126 |
} |
| 127 |
|
| 128 |
public VoidValue visitList(ListPattern p) { |
| 129 |
if (checkContext(DISALLOW_LIST, "list", p) && !alreadyChecked(p)) |
| 130 |
p.getChild().accept(listVisitor); |
| 131 |
return VoidValue.VOID; |
| 132 |
} |
| 133 |
|
| 134 |
public VoidValue visitEmpty(EmptyPattern p) { |
| 135 |
checkContext(DISALLOW_EMPTY, "empty", p); |
| 136 |
return VoidValue.VOID; |
| 137 |
} |
| 138 |
|
| 139 |
public VoidValue visitOptional(OptionalPattern p) { |
| 140 |
if (checkContext(DISALLOW_EMPTY, "optional", p)) |
| 141 |
super.visitOptional(p); |
| 142 |
return VoidValue.VOID; |
| 143 |
} |
| 144 |
|
| 145 |
public VoidValue visitText(TextPattern p) { |
| 146 |
checkContext(DISALLOW_TEXT, "text", p); |
| 147 |
return VoidValue.VOID; |
| 148 |
} |
| 149 |
|
| 150 |
public VoidValue visitMixed(MixedPattern p) { |
| 151 |
if (checkContext(DISALLOW_TEXT, "mixed", p)) { |
| 152 |
if (si.getChildType(p.getChild()).contains(ChildType.DATA)) |
| 153 |
er.error("mixed_data", p.getSourceLocation()); |
| 154 |
super.visitMixed(p); |
| 155 |
} |
| 156 |
return VoidValue.VOID; |
| 157 |
} |
| 158 |
|
| 159 |
public VoidValue visitOneOrMore(OneOrMorePattern p) { |
| 160 |
if (checkContext(DISALLOW_ONE_OR_MORE, "oneOrMore", p)) { |
| 161 |
checkNoDataUnlessInList(p, "oneOrMore"); |
| 162 |
super.visitOneOrMore(p); |
| 163 |
} |
| 164 |
return VoidValue.VOID; |
| 165 |
} |
| 166 |
|
| 167 |
public VoidValue visitZeroOrMore(ZeroOrMorePattern p) { |
| 168 |
if (checkContext(DISALLOW_ONE_OR_MORE, "zeroOrMore", p)) { |
| 169 |
checkNoDataUnlessInList(p, "zeroOrMore"); |
| 170 |
super.visitZeroOrMore(p); |
| 171 |
} |
| 172 |
return VoidValue.VOID; |
| 173 |
} |
| 174 |
|
| 175 |
public VoidValue visitRef(RefPattern p) { |
| 176 |
return si.getBody(p).accept(this); |
| 177 |
} |
| 178 |
|
| 179 |
void checkNoDataUnlessInList(UnaryPattern p, String patternName) { |
| 180 |
if (!inList() && si.getChildType(p.getChild()).contains(ChildType.DATA)) |
| 181 |
er.error("not_in_list", patternName, p.getSourceLocation()); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void checkGroup(CompositePattern p) { |
| 186 |
int simpleCount = 0; |
| 187 |
boolean hadComplex = false; |
| 188 |
for (Pattern child : p.getChildren()) { |
| 189 |
ChildType ct = si.getChildType(child); |
| 190 |
boolean simple = ct.contains(ChildType.DATA); |
| 191 |
boolean complex = ct.contains(ChildType.TEXT) || ct.contains(ChildType.ELEMENT); |
| 192 |
if ((complex && simpleCount > 0) || (simple && hadComplex)) { |
| 193 |
er.error("group_data_other_children", |
| 194 |
p instanceof GroupPattern ? "group" : "interleave", |
| 195 |
p.getSourceLocation()); |
| 196 |
return; |
| 197 |
} |
| 198 |
if (simple) |
| 199 |
simpleCount++; |
| 200 |
if (complex) |
| 201 |
hadComplex = true; |
| 202 |
} |
| 203 |
if (simpleCount > 1) { |
| 204 |
if (p instanceof InterleavePattern) |
| 205 |
er.error("interleave_data", p.getSourceLocation()); |
| 206 |
else if (!inList()) |
| 207 |
er.error("group_data", p.getSourceLocation()); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
boolean inList() { |
| 212 |
return false; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
class ListVisitor extends Visitor { |
| 217 |
public ListVisitor(String contextKey, int flags) { |
| 218 |
super(contextKey, flags); |
| 219 |
} |
| 220 |
|
| 221 |
boolean inList() { |
| 222 |
return true; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
class ElementVisitor extends Visitor { |
| 228 |
ElementVisitor() { |
| 229 |
super(null, 0); |
| 230 |
} |
| 231 |
|
| 232 |
public VoidValue visitAttribute(AttributePattern p) { |
| 233 |
p.getNameClass().accept(this); |
| 234 |
return super.visitAttribute(p); |
| 235 |
} |
| 236 |
|
| 237 |
public VoidValue visitZeroOrMore(ZeroOrMorePattern p) { |
| 238 |
return elementRepeatVisitor.visitZeroOrMore(p); |
| 239 |
} |
| 240 |
|
| 241 |
public VoidValue visitOneOrMore(OneOrMorePattern p) { |
| 242 |
return elementRepeatVisitor.visitOneOrMore(p); |
| 243 |
} |
| 244 |
|
| 245 |
public VoidValue visitAnyName(AnyNameNameClass nc) { |
| 246 |
er.error("any_name_attribute_not_repeated", nc.getSourceLocation()); |
| 247 |
return VoidValue.VOID; |
| 248 |
} |
| 249 |
|
| 250 |
public VoidValue visitNsName(NsNameNameClass nc) { |
| 251 |
er.error("ns_name_attribute_not_repeated", nc.getSourceLocation()); |
| 252 |
return VoidValue.VOID; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
class ElementRepeatVisitor extends Visitor { |
| 257 |
ElementRepeatVisitor() { |
| 258 |
super(null, 0); |
| 259 |
} |
| 260 |
|
| 261 |
public VoidValue visitGroup(GroupPattern p) { |
| 262 |
return elementRepeatGroupVisitor.visitGroup(p); |
| 263 |
} |
| 264 |
|
| 265 |
public VoidValue visitInterleave(InterleavePattern p) { |
| 266 |
return elementRepeatInterleaveVisitor.visitInterleave(p); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
class GrammarVisitor implements ComponentVisitor<VoidValue> { |
| 271 |
public VoidValue visitDiv(DivComponent c) { |
| 272 |
c.componentsAccept(this); |
| 273 |
return VoidValue.VOID; |
| 274 |
} |
| 275 |
|
| 276 |
public VoidValue visitDefine(DefineComponent c) { |
| 277 |
if (c.getName() != DefineComponent.START) |
| 278 |
c.getBody().accept(topLevelVisitor); |
| 279 |
return VoidValue.VOID; |
| 280 |
} |
| 281 |
|
| 282 |
public VoidValue visitInclude(IncludeComponent c) { |
| 283 |
si.getSchema(c.getUri()).componentsAccept(this); |
| 284 |
return VoidValue.VOID; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
private RestrictionsChecker(SchemaInfo si, ErrorReporter er) { |
| 289 |
this.si = si; |
| 290 |
this.er = er; |
| 291 |
Pattern start = si.getStart(); |
| 292 |
if (start != null) |
| 293 |
start.accept(startVisitor); |
| 294 |
si.getGrammar().componentsAccept(new GrammarVisitor()); |
| 295 |
} |
| 296 |
|
| 297 |
static void check(SchemaInfo si, ErrorReporter er) { |
| 298 |
new RestrictionsChecker(si, er); |
| 299 |
} |
| 300 |
|
| 301 |
private boolean alreadyChecked(Pattern p) { |
| 302 |
if (checkedPatterns.contains(p)) |
| 303 |
return true; |
| 304 |
else { |
| 305 |
checkedPatterns.add(p); |
| 306 |
return false; |
| 307 |
} |
| 308 |
} |
| 309 |
} |