| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.output.common.ErrorReporter; |
| 4 |
import com.thaiopensource.relaxng.output.xsd.basic.AbstractAttributeUseVisitor; |
| 5 |
import com.thaiopensource.relaxng.output.xsd.basic.Attribute; |
| 6 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroup; |
| 7 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroupDefinition; |
| 8 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroupRef; |
| 9 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeUse; |
| 10 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeUseChoice; |
| 11 |
import com.thaiopensource.relaxng.output.xsd.basic.ComplexType; |
| 12 |
import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeComplexContent; |
| 13 |
import com.thaiopensource.relaxng.output.xsd.basic.Element; |
| 14 |
import com.thaiopensource.relaxng.output.xsd.basic.Facet; |
| 15 |
import com.thaiopensource.relaxng.output.xsd.basic.GroupRef; |
| 16 |
import com.thaiopensource.relaxng.output.xsd.basic.Occurs; |
| 17 |
import com.thaiopensource.relaxng.output.xsd.basic.OptionalAttribute; |
| 18 |
import com.thaiopensource.relaxng.output.xsd.basic.Particle; |
| 19 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleAll; |
| 20 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleChoice; |
| 21 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleRepeat; |
| 22 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleSequence; |
| 23 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleVisitor; |
| 24 |
import com.thaiopensource.relaxng.output.xsd.basic.Schema; |
| 25 |
import com.thaiopensource.relaxng.output.xsd.basic.SchemaTransformer; |
| 26 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleType; |
| 27 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeRestriction; |
| 28 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeUnion; |
| 29 |
import com.thaiopensource.relaxng.output.xsd.basic.SingleAttributeUse; |
| 30 |
import com.thaiopensource.relaxng.output.xsd.basic.Wildcard; |
| 31 |
import com.thaiopensource.relaxng.output.xsd.basic.WildcardAttribute; |
| 32 |
import com.thaiopensource.relaxng.output.xsd.basic.WildcardElement; |
| 33 |
import com.thaiopensource.util.Equal; |
| 34 |
import com.thaiopensource.xml.util.Name; |
| 35 |
|
| 36 |
import java.util.Collections; |
| 37 |
import java.util.HashMap; |
| 38 |
import java.util.HashSet; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Map; |
| 41 |
import java.util.Set; |
| 42 |
import java.util.Vector; |
| 43 |
|
| 44 |
class Transformer extends SchemaTransformer { |
| 45 |
private final AttributeMapper attributeMapper = new AttributeMapper(); |
| 46 |
private final Set<String> transformedAttributeGroups = new HashSet<String>(); |
| 47 |
private final ErrorReporter er; |
| 48 |
private boolean preserveAllGroup = false; |
| 49 |
|
| 50 |
Transformer(Schema schema, ErrorReporter er) { |
| 51 |
super(schema); |
| 52 |
this.er = er; |
| 53 |
} |
| 54 |
|
| 55 |
public SimpleType visitUnion(SimpleTypeUnion t) { |
| 56 |
List<SimpleType> list = transformSimpleTypeList(t.getChildren()); |
| 57 |
SimpleType combined = combineEnumeration(t, list); |
| 58 |
if (combined != null) |
| 59 |
return combined; |
| 60 |
return new SimpleTypeUnion(t.getLocation(), t.getAnnotation(), list); |
| 61 |
} |
| 62 |
|
| 63 |
private static SimpleType combineEnumeration(SimpleTypeUnion orig, List<SimpleType> transformedChildren) { |
| 64 |
if (transformedChildren.size() < 2) |
| 65 |
return null; |
| 66 |
SimpleType first = transformedChildren.get(0); |
| 67 |
if (!(first instanceof SimpleTypeRestriction)) |
| 68 |
return null; |
| 69 |
String builtinTypeName = ((SimpleTypeRestriction)first).getName(); |
| 70 |
List<Facet> facets = new Vector<Facet>(); |
| 71 |
for (SimpleType child : transformedChildren) { |
| 72 |
if (!(child instanceof SimpleTypeRestriction)) |
| 73 |
return null; |
| 74 |
SimpleTypeRestriction restriction = (SimpleTypeRestriction)child; |
| 75 |
if (!restriction.getName().equals(builtinTypeName)) |
| 76 |
return null; |
| 77 |
if (restriction.getFacets().isEmpty()) |
| 78 |
return null; |
| 79 |
for (Facet facet : restriction.getFacets()) { |
| 80 |
if (!facet.getName().equals("enumeration")) |
| 81 |
return null; |
| 82 |
facets.add(facet); |
| 83 |
} |
| 84 |
} |
| 85 |
return new SimpleTypeRestriction(orig.getLocation(), orig.getAnnotation(), builtinTypeName, facets); |
| 86 |
} |
| 87 |
|
| 88 |
class SequenceDetector implements ParticleVisitor<Boolean> { |
| 89 |
public Boolean visitElement(Element p) { |
| 90 |
return Boolean.FALSE; |
| 91 |
} |
| 92 |
|
| 93 |
public Boolean visitWildcardElement(WildcardElement p) { |
| 94 |
return Boolean.FALSE; |
| 95 |
} |
| 96 |
|
| 97 |
public Boolean visitSequence(ParticleSequence p) { |
| 98 |
return Boolean.TRUE; |
| 99 |
} |
| 100 |
|
| 101 |
public Boolean visitGroupRef(GroupRef p) { |
| 102 |
return getSchema().getGroup(p.getName()).getParticle().accept(this); |
| 103 |
} |
| 104 |
|
| 105 |
public Boolean visitAll(ParticleAll p) { |
| 106 |
return Boolean.FALSE; |
| 107 |
} |
| 108 |
|
| 109 |
public Boolean visitRepeat(ParticleRepeat p) { |
| 110 |
return p.getChild().accept(this); |
| 111 |
} |
| 112 |
|
| 113 |
public Boolean visitChoice(ParticleChoice p) { |
| 114 |
for (Particle child : p.getChildren()) |
| 115 |
if (child.accept(this) == Boolean.TRUE) |
| 116 |
return Boolean.TRUE; |
| 117 |
return Boolean.FALSE; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
class AllBodyTransformer extends SchemaTransformer { |
| 122 |
public AllBodyTransformer(Schema schema) { |
| 123 |
super(schema); |
| 124 |
} |
| 125 |
|
| 126 |
public Particle visitGroupRef(GroupRef p) { |
| 127 |
if (new SequenceDetector().visitGroupRef(p) == Boolean.FALSE) |
| 128 |
return p; |
| 129 |
return getSchema().getGroup(p.getName()).getParticle().accept(this); |
| 130 |
} |
| 131 |
|
| 132 |
public Particle visitSequence(ParticleSequence p) { |
| 133 |
return new ParticleChoice(p.getLocation(), p.getAnnotation(), transformParticleList(p.getChildren())); |
| 134 |
} |
| 135 |
|
| 136 |
public Particle visitRepeat(ParticleRepeat p) { |
| 137 |
return p.getChild().accept(this); |
| 138 |
} |
| 139 |
|
| 140 |
public Particle visitElement(Element p) { |
| 141 |
return Transformer.this.visitElement(p); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
public Particle visitAll(ParticleAll p) { |
| 147 |
if (preserveAllGroup) { |
| 148 |
preserveAllGroup = false; |
| 149 |
return super.visitAll(p); |
| 150 |
} |
| 151 |
return new ParticleRepeat(p.getLocation(), |
| 152 |
p.getAnnotation(), |
| 153 |
new ParticleChoice(p.getLocation(), |
| 154 |
null, |
| 155 |
new AllBodyTransformer(getSchema()).transformParticleList(transformParticleList(p.getChildren()))), |
| 156 |
Occurs.ZERO_OR_MORE); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
public AttributeUse visitAttributeGroup(AttributeGroup a) { |
| 161 |
List<AttributeUse> children = transformAttributeUseList(a.getChildren()); |
| 162 |
Wildcard wildcard = null; |
| 163 |
boolean[] removeWildcard = new boolean[children.size()]; |
| 164 |
boolean multipleWildcards = false; |
| 165 |
int wildcardUseIndex = -1; |
| 166 |
for (int i = 0; i < removeWildcard.length; i++) { |
| 167 |
Wildcard wc = attributeMapper.getAttributeWildcard(children.get(i)); |
| 168 |
if (wc != null) { |
| 169 |
if (wildcard == null) { |
| 170 |
wildcard = wc; |
| 171 |
wildcardUseIndex = i; |
| 172 |
} |
| 173 |
else { |
| 174 |
multipleWildcards = true; |
| 175 |
Wildcard union = Wildcard.union(wildcard, wc); |
| 176 |
if (union.equals(wildcard)) |
| 177 |
removeWildcard[i] = true; |
| 178 |
else if (union.equals(wc)) { |
| 179 |
if (wildcardUseIndex >= 0) |
| 180 |
removeWildcard[wildcardUseIndex] = true; |
| 181 |
wildcardUseIndex = i; |
| 182 |
wildcard = wc; |
| 183 |
} |
| 184 |
else { |
| 185 |
removeWildcard[i] = true; |
| 186 |
if (wildcardUseIndex >= 0) |
| 187 |
removeWildcard[wildcardUseIndex] = true; |
| 188 |
wildcard = union; |
| 189 |
wildcardUseIndex = -1; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
if (!multipleWildcards) { |
| 195 |
if (children == a.getChildren()) |
| 196 |
return a; |
| 197 |
return new AttributeGroup(a.getLocation(), a.getAnnotation(), children); |
| 198 |
} |
| 199 |
List<AttributeUse> newChildren = new Vector<AttributeUse>(); |
| 200 |
for (int i = 0; i < removeWildcard.length; i++) { |
| 201 |
AttributeUse att = children.get(i); |
| 202 |
if (removeWildcard[i]) |
| 203 |
att = att.accept(new AttributeTransformer(null, null, false)); |
| 204 |
newChildren.add(att); |
| 205 |
} |
| 206 |
if (wildcardUseIndex == -1) |
| 207 |
newChildren.add(new WildcardAttribute(a.getLocation(), null, wildcard)); |
| 208 |
return new AttributeGroup(a.getLocation(), a.getAnnotation(), newChildren); |
| 209 |
} |
| 210 |
|
| 211 |
public AttributeUse visitAttributeUseChoice(AttributeUseChoice a) { |
| 212 |
List<AttributeUse> children = transformAttributeUseList(a.getChildren()); |
| 213 |
Map<Name, SingleAttributeUse>[] maps = (Map<Name, SingleAttributeUse>[])new Map[children.size()]; |
| 214 |
int wildcardUseIndex = -1; |
| 215 |
Wildcard wildcard = null; |
| 216 |
for (int i = 0; i < maps.length; i++) { |
| 217 |
maps[i] = attributeMapper.getAttributeMap(children.get(i)); |
| 218 |
Wildcard wc = attributeMapper.getAttributeWildcard(children.get(i)); |
| 219 |
if (wc != null) { |
| 220 |
if (wildcard == null) { |
| 221 |
wildcard = wc; |
| 222 |
wildcardUseIndex = i; |
| 223 |
} |
| 224 |
else { |
| 225 |
Wildcard union = Wildcard.union(wildcard, wc); |
| 226 |
if (!union.equals(wildcard)) { |
| 227 |
if (union.equals(wc)) |
| 228 |
wildcardUseIndex = i; |
| 229 |
else |
| 230 |
wildcardUseIndex = -1; |
| 231 |
wildcard = union; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
Set<Name> required = new HashSet<Name>(); |
| 237 |
Set<Name> union = new HashSet<Name>(maps[0].keySet()); |
| 238 |
for (int i = 1; i < maps.length; i++) |
| 239 |
union.addAll(maps[i].keySet()); |
| 240 |
Set<Name>[] retainAttributeNames = (Set<Name>[])new Set[children.size()]; |
| 241 |
for (int i = 0; i < retainAttributeNames.length; i++) |
| 242 |
retainAttributeNames[i] = new HashSet<Name>(); |
| 243 |
List<AttributeUse> newChildren = new Vector<AttributeUse>(); |
| 244 |
for (Name name : union) { |
| 245 |
if (wildcard == null || !wildcard.contains(name)) { |
| 246 |
SingleAttributeUse[] uses = new SingleAttributeUse[maps.length]; |
| 247 |
int useIndex = -1; |
| 248 |
boolean isRequired = true; |
| 249 |
for (int i = 0; i < maps.length; i++) { |
| 250 |
uses[i] = maps[i].get(name); |
| 251 |
if (uses[i] != null) { |
| 252 |
if (useIndex >= 0) |
| 253 |
useIndex = -2; |
| 254 |
else if (useIndex == -1) |
| 255 |
useIndex = i; |
| 256 |
if (uses[i].isOptional()) |
| 257 |
isRequired = false; |
| 258 |
} |
| 259 |
else |
| 260 |
isRequired = false; |
| 261 |
} |
| 262 |
if (isRequired) |
| 263 |
required.add(name); |
| 264 |
if (useIndex < 0) |
| 265 |
useIndex = chooseUseIndex(uses); |
| 266 |
if (useIndex >= 0) |
| 267 |
retainAttributeNames[useIndex].add(name); |
| 268 |
else { |
| 269 |
List<SimpleType> choices = new Vector<SimpleType>(); |
| 270 |
for (int i = 0; i < uses.length; i++) |
| 271 |
if (uses[i] != null && uses[i].getType() != null) |
| 272 |
choices.add(uses[i].getType()); |
| 273 |
Attribute tem = new Attribute(a.getLocation(), |
| 274 |
null, |
| 275 |
name, |
| 276 |
new SimpleTypeUnion(a.getLocation(), null, choices).accept(this)); |
| 277 |
if (isRequired) |
| 278 |
newChildren.add(tem); |
| 279 |
else |
| 280 |
newChildren.add(new OptionalAttribute(a.getLocation(), null, tem, null)); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
for (int i = 0; i < retainAttributeNames.length; i++) { |
| 285 |
AttributeUse tem = children.get(i).accept(new AttributeTransformer(retainAttributeNames[i], |
| 286 |
required, |
| 287 |
i == wildcardUseIndex)); |
| 288 |
if (!tem.equals(AttributeGroup.EMPTY)) |
| 289 |
newChildren.add(tem); |
| 290 |
} |
| 291 |
if (wildcard != null && wildcardUseIndex == -1) |
| 292 |
newChildren.add(new WildcardAttribute(a.getLocation(), null, wildcard)); |
| 293 |
return new AttributeGroup(a.getLocation(), a.getAnnotation(), newChildren); |
| 294 |
} |
| 295 |
|
| 296 |
private static int chooseUseIndex(SingleAttributeUse[] uses) { |
| 297 |
for (int i = 0; i < uses.length; i++) |
| 298 |
if (uses[i] != null && uses[i].getType() == null && uses[i].getDefaultValue() == null) |
| 299 |
return i; |
| 300 |
int firstIndex = -1; |
| 301 |
for (int i = 0; i < uses.length; i++) { |
| 302 |
if (uses[i] != null) { |
| 303 |
if (firstIndex == -1) |
| 304 |
firstIndex = i; |
| 305 |
else if (!Equal.equal(uses[i].getType(), uses[firstIndex].getType()) |
| 306 |
|| !Equal.equal(uses[i].getDefaultValue(), uses[firstIndex].getDefaultValue())) |
| 307 |
return -1; |
| 308 |
} |
| 309 |
} |
| 310 |
return firstIndex; |
| 311 |
} |
| 312 |
|
| 313 |
static class AttributeInfo { |
| 314 |
final Map<Name, SingleAttributeUse> map; |
| 315 |
final Wildcard wildcard; |
| 316 |
final static Map<Name, SingleAttributeUse> EMPTY_MAP = Collections.emptyMap(); |
| 317 |
|
| 318 |
AttributeInfo(Map<Name, SingleAttributeUse> map, Wildcard wildcard) { |
| 319 |
this.map = map; |
| 320 |
this.wildcard = wildcard; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
class AttributeMapper extends AbstractAttributeUseVisitor<AttributeInfo> { |
| 325 |
private final Map<AttributeUse, AttributeInfo> cache = new HashMap<AttributeUse, AttributeInfo>(); |
| 326 |
|
| 327 |
Map<Name, SingleAttributeUse> getAttributeMap(AttributeUse a) { |
| 328 |
return getAttributeInfo(a).map; |
| 329 |
} |
| 330 |
|
| 331 |
Wildcard getAttributeWildcard(AttributeUse a) { |
| 332 |
return getAttributeInfo(a).wildcard; |
| 333 |
} |
| 334 |
|
| 335 |
private AttributeInfo getAttributeInfo(AttributeUse a) { |
| 336 |
AttributeInfo info = cache.get(a); |
| 337 |
if (info == null) { |
| 338 |
info = a.accept(this); |
| 339 |
cache.put(a, info); |
| 340 |
} |
| 341 |
return info; |
| 342 |
} |
| 343 |
|
| 344 |
public AttributeInfo visitAttribute(Attribute a) { |
| 345 |
Map<Name, SingleAttributeUse> map = new HashMap<Name, SingleAttributeUse>(); |
| 346 |
map.put(a.getName(), a); |
| 347 |
return new AttributeInfo(map, null); |
| 348 |
} |
| 349 |
|
| 350 |
public AttributeInfo visitAttributeGroup(AttributeGroup a) { |
| 351 |
Map<Name, SingleAttributeUse> map = new HashMap<Name, SingleAttributeUse>(); |
| 352 |
Wildcard wildcard = null; |
| 353 |
for (AttributeUse child : a.getChildren()) { |
| 354 |
AttributeInfo info = getAttributeInfo(child); |
| 355 |
if (info.wildcard != null) |
| 356 |
wildcard = info.wildcard; |
| 357 |
map.putAll(info.map); |
| 358 |
} |
| 359 |
return new AttributeInfo(map, wildcard); |
| 360 |
} |
| 361 |
|
| 362 |
public AttributeInfo visitOptionalAttribute(OptionalAttribute a) { |
| 363 |
Map<Name, SingleAttributeUse> map = new HashMap<Name, SingleAttributeUse>(); |
| 364 |
map.put(a.getAttribute().getName(), a); |
| 365 |
return new AttributeInfo(map, null); |
| 366 |
} |
| 367 |
|
| 368 |
public AttributeInfo visitAttributeGroupRef(AttributeGroupRef a) { |
| 369 |
return getAttributeInfo(getTransformedAttributeGroup(a.getName())); |
| 370 |
} |
| 371 |
|
| 372 |
public AttributeInfo visitWildcardAttribute(WildcardAttribute a) { |
| 373 |
return new AttributeInfo(AttributeInfo.EMPTY_MAP, a.getWildcard()); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
class AttributeTransformer extends AbstractAttributeUseVisitor<AttributeUse> { |
| 378 |
private final Set<Name> retainNames; |
| 379 |
private final Set<Name> requiredNames; |
| 380 |
private final boolean retainWildcard; |
| 381 |
|
| 382 |
public AttributeTransformer(Set<Name> retainNames, Set<Name> requiredNames, boolean retainWildcard) { |
| 383 |
this.retainNames = retainNames; |
| 384 |
this.requiredNames = requiredNames; |
| 385 |
this.retainWildcard = retainWildcard; |
| 386 |
} |
| 387 |
|
| 388 |
public AttributeUse visitAttribute(Attribute a) { |
| 389 |
if (retainNames != null && !retainNames.contains(a.getName())) |
| 390 |
return AttributeGroup.EMPTY; |
| 391 |
if (requiredNames != null && !requiredNames.contains(a.getName())) |
| 392 |
return new OptionalAttribute(a.getLocation(), null, a, null); |
| 393 |
return a; |
| 394 |
} |
| 395 |
|
| 396 |
public AttributeUse visitOptionalAttribute(OptionalAttribute a) { |
| 397 |
if (retainNames != null && !retainNames.contains(a.getName())) |
| 398 |
return AttributeGroup.EMPTY; |
| 399 |
return a; |
| 400 |
} |
| 401 |
|
| 402 |
public AttributeUse visitWildcardAttribute(WildcardAttribute a) { |
| 403 |
if (!retainWildcard) |
| 404 |
return AttributeGroup.EMPTY; |
| 405 |
return a; |
| 406 |
} |
| 407 |
|
| 408 |
public AttributeUse visitAttributeGroupRef(AttributeGroupRef a) { |
| 409 |
AttributeUse refed = getTransformedAttributeGroup(a.getName()); |
| 410 |
if (isOk(attributeMapper.getAttributeMap(refed)) |
| 411 |
&& (retainWildcard || attributeMapper.getAttributeWildcard(refed) == null)) |
| 412 |
return a; |
| 413 |
return refed.accept(this); |
| 414 |
} |
| 415 |
|
| 416 |
private boolean isOk(Map<Name, SingleAttributeUse> map) { |
| 417 |
for (Map.Entry<Name, SingleAttributeUse> entry : map.entrySet()) { |
| 418 |
Name name = entry.getKey(); |
| 419 |
SingleAttributeUse use = entry.getValue(); |
| 420 |
if (retainNames != null && !retainNames.contains(name)) |
| 421 |
return false; |
| 422 |
if (requiredNames != null && !use.isOptional() && !requiredNames.contains(name)) |
| 423 |
return false; |
| 424 |
} |
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
public AttributeUse visitAttributeGroup(AttributeGroup a) { |
| 429 |
List<AttributeUse> children = a.getChildren(); |
| 430 |
List<AttributeUse> transformedChildren = null; |
| 431 |
for (int i = 0, len = children.size(); i < len; i++) { |
| 432 |
AttributeUse child = children.get(i).accept(this); |
| 433 |
if (transformedChildren != null) { |
| 434 |
if (!child.equals(AttributeGroup.EMPTY)) |
| 435 |
transformedChildren.add(child); |
| 436 |
} |
| 437 |
else if (child != children.get(i)) { |
| 438 |
transformedChildren = new Vector<AttributeUse>(); |
| 439 |
for (int j = 0; j < i; j++) |
| 440 |
transformedChildren.add(children.get(j)); |
| 441 |
if (!child.equals(AttributeGroup.EMPTY)) |
| 442 |
transformedChildren.add(child); |
| 443 |
} |
| 444 |
} |
| 445 |
if (transformedChildren == null) |
| 446 |
return a; |
| 447 |
return new AttributeGroup(a.getLocation(), a.getAnnotation(), transformedChildren); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
public void visitAttributeGroup(AttributeGroupDefinition def) { |
| 452 |
def.setAttributeUses(getTransformedAttributeGroup(def.getName())); |
| 453 |
} |
| 454 |
|
| 455 |
private AttributeUse getTransformedAttributeGroup(String name) { |
| 456 |
AttributeGroupDefinition def = getSchema().getAttributeGroup(name); |
| 457 |
if (!transformedAttributeGroups.contains(name)) { |
| 458 |
def.setAttributeUses(def.getAttributeUses().accept(this)); |
| 459 |
transformedAttributeGroups.add(name); |
| 460 |
} |
| 461 |
return def.getAttributeUses(); |
| 462 |
} |
| 463 |
|
| 464 |
public Particle visitElement(Element p) { |
| 465 |
if (containsLegalAllGroup(p)) |
| 466 |
preserveAllGroup = true; |
| 467 |
return super.visitElement(p); |
| 468 |
} |
| 469 |
|
| 470 |
private static boolean containsLegalAllGroup(Element p) { |
| 471 |
ComplexType t = p.getComplexType(); |
| 472 |
if (!(t instanceof ComplexTypeComplexContent)) |
| 473 |
return false; |
| 474 |
Particle particle = ((ComplexTypeComplexContent)t).getParticle(); |
| 475 |
if (!(particle instanceof ParticleAll)) |
| 476 |
return false; |
| 477 |
String ns = p.getName().getNamespaceUri(); |
| 478 |
for (Particle child : ((ParticleAll)particle).getChildren()) { |
| 479 |
if (child instanceof ParticleRepeat) { |
| 480 |
Occurs occur = ((ParticleRepeat)child).getOccurs(); |
| 481 |
if (occur.getMin() > 1 || occur.getMax() > 1) |
| 482 |
return false; |
| 483 |
child = ((ParticleRepeat)child).getChild(); |
| 484 |
} |
| 485 |
if (!(child instanceof Element)) |
| 486 |
return false; |
| 487 |
if (!((Element)child).getName().getNamespaceUri().equals(ns)) |
| 488 |
return false; |
| 489 |
} |
| 490 |
return true; |
| 491 |
} |
| 492 |
} |