| 1 |
package com.thaiopensource.relaxng.output.dtd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AbstractPatternVisitor; |
| 4 |
import com.thaiopensource.relaxng.edit.Annotated; |
| 5 |
import com.thaiopensource.relaxng.edit.ChoicePattern; |
| 6 |
import com.thaiopensource.relaxng.edit.Component; |
| 7 |
import com.thaiopensource.relaxng.edit.ComponentVisitor; |
| 8 |
import com.thaiopensource.relaxng.edit.CompositePattern; |
| 9 |
import com.thaiopensource.relaxng.edit.Container; |
| 10 |
import com.thaiopensource.relaxng.edit.DefineComponent; |
| 11 |
import com.thaiopensource.relaxng.edit.DivComponent; |
| 12 |
import com.thaiopensource.relaxng.edit.EmptyPattern; |
| 13 |
import com.thaiopensource.relaxng.edit.GrammarPattern; |
| 14 |
import com.thaiopensource.relaxng.edit.IncludeComponent; |
| 15 |
import com.thaiopensource.relaxng.edit.InterleavePattern; |
| 16 |
import com.thaiopensource.relaxng.edit.MixedPattern; |
| 17 |
import com.thaiopensource.relaxng.edit.NotAllowedPattern; |
| 18 |
import com.thaiopensource.relaxng.edit.OneOrMorePattern; |
| 19 |
import com.thaiopensource.relaxng.edit.OptionalPattern; |
| 20 |
import com.thaiopensource.relaxng.edit.Pattern; |
| 21 |
import com.thaiopensource.relaxng.edit.SchemaCollection; |
| 22 |
import com.thaiopensource.relaxng.edit.SchemaDocument; |
| 23 |
import com.thaiopensource.relaxng.edit.TextPattern; |
| 24 |
import com.thaiopensource.relaxng.edit.UnaryPattern; |
| 25 |
import com.thaiopensource.util.VoidValue; |
| 26 |
import com.thaiopensource.relaxng.edit.ZeroOrMorePattern; |
| 27 |
|
| 28 |
import java.util.Iterator; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
class Simplifier extends AbstractPatternVisitor<Pattern> implements ComponentVisitor<VoidValue> { |
| 32 |
public static void simplify(SchemaCollection sc) { |
| 33 |
Simplifier simplifier = new Simplifier(); |
| 34 |
for (SchemaDocument sd : sc.getSchemaDocumentMap().values()) |
| 35 |
sd.setPattern(sd.getPattern().accept(simplifier)); |
| 36 |
} |
| 37 |
|
| 38 |
private Simplifier() { |
| 39 |
} |
| 40 |
|
| 41 |
public Pattern visitGrammar(GrammarPattern p) { |
| 42 |
visitContainer(p); |
| 43 |
return p; |
| 44 |
} |
| 45 |
|
| 46 |
public VoidValue visitContainer(Container c) { |
| 47 |
for (Component component : c.getComponents()) |
| 48 |
component.accept(this); |
| 49 |
return VoidValue.VOID; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
public VoidValue visitInclude(IncludeComponent c) { |
| 54 |
return visitContainer(c); |
| 55 |
} |
| 56 |
|
| 57 |
public VoidValue visitDiv(DivComponent c) { |
| 58 |
return visitContainer(c); |
| 59 |
} |
| 60 |
|
| 61 |
public VoidValue visitDefine(DefineComponent c) { |
| 62 |
c.setBody(c.getBody().accept(this)); |
| 63 |
return VoidValue.VOID; |
| 64 |
} |
| 65 |
|
| 66 |
public Pattern visitChoice(ChoicePattern p) { |
| 67 |
boolean hadEmpty = false; |
| 68 |
List<Pattern> list = p.getChildren(); |
| 69 |
for (int i = 0, len = list.size(); i < len; i++) |
| 70 |
list.set(i, list.get(i).accept(this)); |
| 71 |
for (Iterator<Pattern> iter = list.iterator(); iter.hasNext();) { |
| 72 |
Pattern child = iter.next(); |
| 73 |
if (child instanceof NotAllowedPattern) |
| 74 |
iter.remove(); |
| 75 |
else if (child instanceof EmptyPattern) { |
| 76 |
hadEmpty = true; |
| 77 |
iter.remove(); |
| 78 |
} |
| 79 |
} |
| 80 |
if (list.size() == 0) |
| 81 |
return copy(new NotAllowedPattern(), p); |
| 82 |
Pattern tem; |
| 83 |
if (list.size() == 1) |
| 84 |
tem = list.get(0); |
| 85 |
else |
| 86 |
tem = p; |
| 87 |
if (hadEmpty && !(tem instanceof OptionalPattern) && !(tem instanceof ZeroOrMorePattern)) { |
| 88 |
if (tem instanceof OneOrMorePattern) |
| 89 |
tem = new ZeroOrMorePattern(((OneOrMorePattern)tem).getChild()); |
| 90 |
else |
| 91 |
tem = new OptionalPattern(tem); |
| 92 |
copy(tem, p); |
| 93 |
} |
| 94 |
return tem; |
| 95 |
} |
| 96 |
|
| 97 |
public Pattern visitComposite(CompositePattern p) { |
| 98 |
List<Pattern> list = p.getChildren(); |
| 99 |
for (int i = 0, len = list.size(); i < len; i++) |
| 100 |
list.set(i, list.get(i).accept(this)); |
| 101 |
for (Iterator<Pattern> iter = list.iterator(); iter.hasNext();) { |
| 102 |
Pattern child = iter.next(); |
| 103 |
if (child instanceof EmptyPattern) |
| 104 |
iter.remove(); |
| 105 |
} |
| 106 |
if (list.size() == 0) |
| 107 |
return copy(new EmptyPattern(), p); |
| 108 |
if (list.size() == 1) |
| 109 |
return p.getChildren().get(0); |
| 110 |
return p; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
public Pattern visitInterleave(InterleavePattern p) { |
| 115 |
boolean hadText = false; |
| 116 |
for (Iterator<Pattern> iter = p.getChildren().iterator(); iter.hasNext();) { |
| 117 |
Pattern child = iter.next(); |
| 118 |
if (child instanceof TextPattern) { |
| 119 |
iter.remove(); |
| 120 |
hadText = true; |
| 121 |
} |
| 122 |
} |
| 123 |
if (!hadText) |
| 124 |
return visitComposite(p); |
| 125 |
return copy(new MixedPattern(visitComposite(p)), p); |
| 126 |
} |
| 127 |
|
| 128 |
public Pattern visitUnary(UnaryPattern p) { |
| 129 |
p.setChild(p.getChild().accept(this)); |
| 130 |
return p; |
| 131 |
} |
| 132 |
|
| 133 |
private static <T extends Annotated> T copy(T to, T from) { |
| 134 |
to.setSourceLocation(from.getSourceLocation()); |
| 135 |
return to; |
| 136 |
} |
| 137 |
|
| 138 |
public Pattern visitPattern(Pattern p) { |
| 139 |
return p; |
| 140 |
} |
| 141 |
} |