| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AbstractPatternVisitor; |
| 4 |
import com.thaiopensource.relaxng.edit.AttributePattern; |
| 5 |
import com.thaiopensource.relaxng.edit.ChoicePattern; |
| 6 |
import com.thaiopensource.relaxng.edit.Combine; |
| 7 |
import com.thaiopensource.relaxng.edit.ComponentVisitor; |
| 8 |
import com.thaiopensource.relaxng.edit.CompositePattern; |
| 9 |
import com.thaiopensource.relaxng.edit.DataPattern; |
| 10 |
import com.thaiopensource.relaxng.edit.DefineComponent; |
| 11 |
import com.thaiopensource.relaxng.edit.DivComponent; |
| 12 |
import com.thaiopensource.relaxng.edit.ElementPattern; |
| 13 |
import com.thaiopensource.relaxng.edit.EmptyPattern; |
| 14 |
import com.thaiopensource.relaxng.edit.GrammarPattern; |
| 15 |
import com.thaiopensource.relaxng.edit.GroupPattern; |
| 16 |
import com.thaiopensource.relaxng.edit.IncludeComponent; |
| 17 |
import com.thaiopensource.relaxng.edit.InterleavePattern; |
| 18 |
import com.thaiopensource.relaxng.edit.ListPattern; |
| 19 |
import com.thaiopensource.relaxng.edit.MixedPattern; |
| 20 |
import com.thaiopensource.relaxng.edit.NotAllowedPattern; |
| 21 |
import com.thaiopensource.relaxng.edit.OneOrMorePattern; |
| 22 |
import com.thaiopensource.relaxng.edit.OptionalPattern; |
| 23 |
import com.thaiopensource.relaxng.edit.Pattern; |
| 24 |
import com.thaiopensource.relaxng.edit.PatternVisitor; |
| 25 |
import com.thaiopensource.relaxng.edit.RefPattern; |
| 26 |
import com.thaiopensource.relaxng.edit.SchemaCollection; |
| 27 |
import com.thaiopensource.relaxng.edit.SchemaDocument; |
| 28 |
import com.thaiopensource.relaxng.edit.TextPattern; |
| 29 |
import com.thaiopensource.relaxng.edit.ValuePattern; |
| 30 |
import com.thaiopensource.util.VoidValue; |
| 31 |
import com.thaiopensource.relaxng.edit.ZeroOrMorePattern; |
| 32 |
import com.thaiopensource.relaxng.output.common.ErrorReporter; |
| 33 |
|
| 34 |
import java.util.HashMap; |
| 35 |
import java.util.HashSet; |
| 36 |
import java.util.List; |
| 37 |
import java.util.Map; |
| 38 |
import java.util.Set; |
| 39 |
import java.util.Vector; |
| 40 |
|
| 41 |
class SchemaInfo { |
| 42 |
private final SchemaCollection sc; |
| 43 |
private final GrammarPattern grammar; |
| 44 |
private final ErrorReporter er; |
| 45 |
private final Map<Pattern, ChildType> childTypeMap = new HashMap<Pattern, ChildType>(); |
| 46 |
private final Map<String, Define> defineMap = new HashMap<String, Define>(); |
| 47 |
private final Set<DefineComponent> ignoredDefines = new HashSet<DefineComponent>(); |
| 48 |
private final PatternVisitor<ChildType> childTypeVisitor = new ChildTypeVisitor(); |
| 49 |
|
| 50 |
private static final int DEFINE_KEEP = 0; |
| 51 |
private static final int DEFINE_IGNORE = 1; |
| 52 |
private static final int DEFINE_REQUIRE = 2; |
| 53 |
|
| 54 |
|
| 55 |
static private class Define { |
| 56 |
int status = DEFINE_KEEP; |
| 57 |
boolean hadImplicit; |
| 58 |
Combine combine; |
| 59 |
Pattern pattern; |
| 60 |
CompositePattern wrapper; |
| 61 |
DefineComponent head; |
| 62 |
} |
| 63 |
|
| 64 |
abstract class PatternAnalysisVisitor<T> extends AbstractPatternVisitor<T> { |
| 65 |
abstract T get(Pattern p); |
| 66 |
abstract T choice(T o1, T o2); |
| 67 |
abstract T group(T o1, T o2); |
| 68 |
T interleave(T o1, T o2) { |
| 69 |
return group(o1, o2); |
| 70 |
} |
| 71 |
T ref(T obj) { |
| 72 |
return obj; |
| 73 |
} |
| 74 |
T oneOrMore(T obj) { |
| 75 |
return group(obj, obj); |
| 76 |
} |
| 77 |
abstract T empty(); |
| 78 |
abstract T text(); |
| 79 |
abstract T data(); |
| 80 |
abstract T notAllowed(); |
| 81 |
T list(T obj) { |
| 82 |
return data(); |
| 83 |
} |
| 84 |
|
| 85 |
public T visitChoice(ChoicePattern p) { |
| 86 |
List<Pattern> list = p.getChildren(); |
| 87 |
T obj = get(list.get(0)); |
| 88 |
for (int i = 1, length = list.size(); i < length; i++) |
| 89 |
obj = choice(obj, get(list.get(i))); |
| 90 |
return obj; |
| 91 |
} |
| 92 |
|
| 93 |
public T visitGroup(GroupPattern p) { |
| 94 |
List<Pattern> list = p.getChildren(); |
| 95 |
T obj = get(list.get(0)); |
| 96 |
for (int i = 1, length = list.size(); i < length; i++) |
| 97 |
obj = group(obj, get(list.get(i))); |
| 98 |
return obj; |
| 99 |
} |
| 100 |
|
| 101 |
public T visitInterleave(InterleavePattern p) { |
| 102 |
List<Pattern> list = p.getChildren(); |
| 103 |
T obj = get(list.get(0)); |
| 104 |
for (int i = 1, length = list.size(); i < length; i++) |
| 105 |
obj = interleave(obj, get(list.get(i))); |
| 106 |
return obj; |
| 107 |
} |
| 108 |
|
| 109 |
public T visitZeroOrMore(ZeroOrMorePattern p) { |
| 110 |
return choice(empty(), oneOrMore(get(p.getChild()))); |
| 111 |
} |
| 112 |
|
| 113 |
public T visitOneOrMore(OneOrMorePattern p) { |
| 114 |
return oneOrMore(get(p.getChild())); |
| 115 |
} |
| 116 |
|
| 117 |
public T visitOptional(OptionalPattern p) { |
| 118 |
return choice(empty(), get(p.getChild())); |
| 119 |
} |
| 120 |
|
| 121 |
public T visitEmpty(EmptyPattern p) { |
| 122 |
return empty(); |
| 123 |
} |
| 124 |
|
| 125 |
public T visitRef(RefPattern p) { |
| 126 |
return ref(get(getBody(p))); |
| 127 |
} |
| 128 |
|
| 129 |
public T visitMixed(MixedPattern p) { |
| 130 |
return interleave(text(), get(p.getChild())); |
| 131 |
} |
| 132 |
|
| 133 |
public T visitText(TextPattern p) { |
| 134 |
return text(); |
| 135 |
} |
| 136 |
|
| 137 |
public T visitData(DataPattern p) { |
| 138 |
return data(); |
| 139 |
} |
| 140 |
|
| 141 |
public T visitValue(ValuePattern p) { |
| 142 |
return data(); |
| 143 |
} |
| 144 |
|
| 145 |
public T visitList(ListPattern p) { |
| 146 |
return list(get(p.getChild())); |
| 147 |
} |
| 148 |
|
| 149 |
public T visitNotAllowed(NotAllowedPattern p) { |
| 150 |
return notAllowed(); |
| 151 |
} |
| 152 |
|
| 153 |
public T visitPattern(Pattern p) { |
| 154 |
return null; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
class ChildTypeVisitor extends PatternAnalysisVisitor<ChildType> { |
| 159 |
ChildType get(Pattern p) { |
| 160 |
return getChildType(p); |
| 161 |
} |
| 162 |
|
| 163 |
ChildType empty() { |
| 164 |
return ChildType.EMPTY; |
| 165 |
} |
| 166 |
|
| 167 |
ChildType text() { |
| 168 |
return ChildType.choice(ChildType.TEXT, ChildType.EMPTY); |
| 169 |
} |
| 170 |
|
| 171 |
ChildType data() { |
| 172 |
return ChildType.DATA; |
| 173 |
} |
| 174 |
|
| 175 |
ChildType notAllowed() { |
| 176 |
return ChildType.NOT_ALLOWED; |
| 177 |
} |
| 178 |
|
| 179 |
ChildType list(ChildType t) { |
| 180 |
if (t == ChildType.NOT_ALLOWED) |
| 181 |
return t; |
| 182 |
return data(); |
| 183 |
} |
| 184 |
|
| 185 |
ChildType choice(ChildType t1, ChildType t2) { |
| 186 |
return ChildType.choice(t1, t2); |
| 187 |
} |
| 188 |
|
| 189 |
ChildType group(ChildType t1, ChildType t2) { |
| 190 |
return ChildType.group(t1, t2); |
| 191 |
} |
| 192 |
|
| 193 |
public ChildType visitElement(ElementPattern p) { |
| 194 |
return ChildType.ELEMENT; |
| 195 |
} |
| 196 |
|
| 197 |
public ChildType visitAttribute(AttributePattern p) { |
| 198 |
if (getChildType(p.getChild()) == ChildType.NOT_ALLOWED) |
| 199 |
return ChildType.NOT_ALLOWED; |
| 200 |
return ChildType.choice(ChildType.ATTRIBUTE, ChildType.EMPTY); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
static class Override { |
| 205 |
int status; |
| 206 |
final Define define; |
| 207 |
final String name; |
| 208 |
|
| 209 |
Override(Define define, String name) { |
| 210 |
this.define = define; |
| 211 |
this.name = name; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
class GrammarVisitor implements ComponentVisitor<VoidValue> { |
| 216 |
private final Set<String> openIncludes = new HashSet<String>(); |
| 217 |
private final Set<String> allIncludes = new HashSet<String>(); |
| 218 |
private List<Override> overrides = null; |
| 219 |
|
| 220 |
public VoidValue visitDefine(DefineComponent c) { |
| 221 |
Define define = lookupDefine(c.getName()); |
| 222 |
if (overrides != null) |
| 223 |
overrides.add(new Override(define, c.getName())); |
| 224 |
if (define.status != DEFINE_KEEP) { |
| 225 |
ignoredDefines.add(c); |
| 226 |
define.status = DEFINE_IGNORE; |
| 227 |
return VoidValue.VOID; |
| 228 |
} |
| 229 |
if (c.getCombine() == null) { |
| 230 |
if (define.hadImplicit) { |
| 231 |
er.error("multiple_define", c.getName(), c.getSourceLocation()); |
| 232 |
return VoidValue.VOID; |
| 233 |
} |
| 234 |
define.hadImplicit = true; |
| 235 |
} |
| 236 |
else if (define.combine == null) { |
| 237 |
define.combine = c.getCombine(); |
| 238 |
if (define.combine == Combine.CHOICE) |
| 239 |
define.wrapper = new ChoicePattern(); |
| 240 |
else |
| 241 |
define.wrapper = new InterleavePattern(); |
| 242 |
define.wrapper.setSourceLocation(c.getSourceLocation()); |
| 243 |
} |
| 244 |
else if (define.combine != c.getCombine()) { |
| 245 |
er.error("inconsistent_combine", c.getName(), c.getSourceLocation()); |
| 246 |
return VoidValue.VOID; |
| 247 |
} |
| 248 |
if (define.pattern == null) { |
| 249 |
define.pattern = c.getBody(); |
| 250 |
define.head = c; |
| 251 |
} |
| 252 |
else { |
| 253 |
if (define.pattern != define.wrapper) |
| 254 |
define.wrapper.getChildren().add(define.pattern); |
| 255 |
define.wrapper.getChildren().add(c.getBody()); |
| 256 |
define.pattern = define.wrapper; |
| 257 |
} |
| 258 |
return VoidValue.VOID; |
| 259 |
} |
| 260 |
|
| 261 |
public VoidValue visitDiv(DivComponent c) { |
| 262 |
c.componentsAccept(this); |
| 263 |
return VoidValue.VOID; |
| 264 |
} |
| 265 |
|
| 266 |
public VoidValue visitInclude(IncludeComponent c) { |
| 267 |
List<Override> overrides = new Vector<Override>(); |
| 268 |
List<Override> savedOverrides = this.overrides; |
| 269 |
this.overrides = overrides; |
| 270 |
c.componentsAccept(this); |
| 271 |
this.overrides = savedOverrides; |
| 272 |
String uri = c.getUri(); |
| 273 |
if (openIncludes.contains(uri)) |
| 274 |
er.error("include_loop", uri, c.getSourceLocation()); |
| 275 |
else if (allIncludes.contains(uri)) |
| 276 |
er.error("multiple_include", uri, c.getSourceLocation()); |
| 277 |
else { |
| 278 |
for (Override or : overrides) { |
| 279 |
or.status = or.define.status; |
| 280 |
or.define.status = DEFINE_REQUIRE; |
| 281 |
} |
| 282 |
allIncludes.add(uri); |
| 283 |
openIncludes.add(uri); |
| 284 |
getSchema(uri).componentsAccept(this); |
| 285 |
openIncludes.remove(uri); |
| 286 |
for (Override or : overrides) { |
| 287 |
if (or.define.status == DEFINE_REQUIRE) { |
| 288 |
if (or.name == DefineComponent.START) |
| 289 |
er.error("missing_start_replacement", c.getSourceLocation()); |
| 290 |
else |
| 291 |
er.error("missing_define_replacement", or.name, c.getSourceLocation()); |
| 292 |
} |
| 293 |
or.define.status = or.status; |
| 294 |
} |
| 295 |
} |
| 296 |
return VoidValue.VOID; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
SchemaInfo(SchemaCollection sc, ErrorReporter er) { |
| 301 |
this.sc = sc; |
| 302 |
this.er = er; |
| 303 |
forceGrammar(); |
| 304 |
grammar = getSchema(sc.getMainUri()); |
| 305 |
grammar.componentsAccept(new GrammarVisitor()); |
| 306 |
} |
| 307 |
|
| 308 |
private void forceGrammar() { |
| 309 |
SchemaDocument sd = sc.getSchemaDocumentMap().get(sc.getMainUri()); |
| 310 |
sd.setPattern(convertToGrammar(sd.getPattern())); |
| 311 |
// TODO convert other schemas |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
private static GrammarPattern convertToGrammar(Pattern p) { |
| 316 |
if (p instanceof GrammarPattern) |
| 317 |
return (GrammarPattern)p; |
| 318 |
GrammarPattern g = new GrammarPattern(); |
| 319 |
g.setSourceLocation(p.getSourceLocation()); |
| 320 |
g.setContext(p.getContext()); |
| 321 |
DefineComponent dc = new DefineComponent(DefineComponent.START, p); |
| 322 |
dc.setSourceLocation(p.getSourceLocation()); |
| 323 |
g.getComponents().add(dc); |
| 324 |
return g; |
| 325 |
} |
| 326 |
|
| 327 |
GrammarPattern getGrammar() { |
| 328 |
return grammar; |
| 329 |
} |
| 330 |
|
| 331 |
String getMainUri() { |
| 332 |
return sc.getMainUri(); |
| 333 |
} |
| 334 |
|
| 335 |
GrammarPattern getSchema(String sourceUri) { |
| 336 |
return (GrammarPattern)(sc.getSchemaDocumentMap().get(sourceUri)).getPattern(); |
| 337 |
} |
| 338 |
|
| 339 |
String getEncoding(String sourceUri) { |
| 340 |
return (sc.getSchemaDocumentMap().get(sourceUri)).getEncoding(); |
| 341 |
} |
| 342 |
|
| 343 |
ChildType getChildType(Pattern p) { |
| 344 |
ChildType ct = childTypeMap.get(p); |
| 345 |
if (ct == null) { |
| 346 |
ct = p.accept(childTypeVisitor); |
| 347 |
childTypeMap.put(p, ct); |
| 348 |
} |
| 349 |
return ct; |
| 350 |
} |
| 351 |
|
| 352 |
Pattern getStart() { |
| 353 |
return lookupDefine(DefineComponent.START).pattern; |
| 354 |
} |
| 355 |
|
| 356 |
Pattern getBody(RefPattern p) { |
| 357 |
return lookupDefine(p.getName()).pattern; |
| 358 |
} |
| 359 |
|
| 360 |
Pattern getBody(DefineComponent c) { |
| 361 |
Define def = lookupDefine(c.getName()); |
| 362 |
if (def == null || def.head != c) |
| 363 |
return null; |
| 364 |
return def.pattern; |
| 365 |
} |
| 366 |
|
| 367 |
boolean isIgnored(DefineComponent c) { |
| 368 |
return ignoredDefines.contains(c); |
| 369 |
} |
| 370 |
|
| 371 |
private Define lookupDefine(String name) { |
| 372 |
Define define = defineMap.get(name); |
| 373 |
if (define == null) { |
| 374 |
define = new Define(); |
| 375 |
defineMap.put(name, define); |
| 376 |
} |
| 377 |
return define; |
| 378 |
} |
| 379 |
|
| 380 |
} |