Grammar object can be thought as a (possibly) cyclic graph
made from Expression. For example, the following simple
TREX pattern will be represented as following AGM.
<grammar>
<start name="X">
<element name="foo">
<choice>
<string> abc </string>
<ref name="Y" />
</choice>
</element>
</start>
<define name="Y">
<element name="bar">
<string> abc </string>
<optional>
<ref name="X" />
</optional>
</element>
</define>
</grammar>
Note that
To write these expressions into TREX XML representation, we have to take care of cycles, since cyclic references cannot be written into XML without first cut it and use <ref>/<define> pair.
First, this algorithm splits the grammar into "islands". Island is a tree of expressions; it has a head expression and most importantly it doesn't contain any cycles in it. Member of an island can be always reached from its head.
TREXWriter will make every ElementExp and
ReferenceExp a head of their own island. So each of them
has their own island.
It is guaranteed that this split will always give islands without inner cycles. Several islands can form a cycle, but one island can never have a cycle in it. This is because there is always at least one ElementExp in any cycle.
Note that since expressions are shared, one expression can be a member of several islands (although this isn't depicted in the above figure.)
Then, this algorithm merges some islands. For example, island E is referenced only once (from island D). This means that there is no need to give a name to this pattern. Instead, island E can simply written as a subordinate of island D.
In other words, any island who is only referenced at most once is merged into its referer. This step makes the output more compact.
Next, TREXWriter assigns a name to each island. It tries to use the name of
the head expression. If a head is anonymous ReferenceExp (ReferenceExp whose
name field is null) or there is a name conflict, TREXWriter
will add some suffix to make the name unique.
Finally, each island is written as one named pattern under <define> element. All inter-island references are replaced by <ref> element.
Due to the bug and insufficient supports for the serialization through SAX2, The decision is made to use SAX1. SAX1 allows us to control namespace prefix mappings better than SAX2.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv class | RELAXNGWriter.SmartPatternWriter
PatternWriter that performs some optimization for human eyes. |
| Modifier and Type | Field and Description |
|---|---|
| protected String | defaultNs
namespace URI currently implied through "ns" attribute propagation. |
| protected Map | exp2name
map from ReferenceExp/ElementExp to its unique name. |
| protected Grammar | grammar
Grammar object which we are writing. |
| protected NameClassVisitor | |
| protected RELAXNGWriter. | |
| protected XMLWriter |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private String | |
| protected NameClassVisitor | |
| public String | |
| public XMLWriter | |
| public void | setDocumentHandler(DocumentHandler handler)
Implements com.
Deprecated
as a consequence of
Sets DocumentHandler.
DocumentHandler being deprecated.
|
| protected String | sniffDefaultNs(Expression exp)
sniffs namespace URI that can be used as default 'ns' attribute from expression. |
| public void | write(Grammar g)
Implements com. |
| public void | |
| protected void | |
| public void |
| defaultNs | back to summary |
|---|---|
| protected String defaultNs namespace URI currently implied through "ns" attribute propagation. | |
| exp2name | back to summary |
|---|---|
| protected Map<Expression, String> exp2name map from ReferenceExp/ElementExp to its unique name. "unique name" is used to write/reference this ReferenceExp. ReferenceExps who are not in this list can be directly written into XML. | |
| grammar | back to summary |
|---|---|
| protected Grammar grammar Grammar object which we are writing. | |
| nameClassWriter | back to summary |
|---|---|
| protected NameClassVisitor nameClassWriter | |
| patternWriter | back to summary |
|---|---|
| protected RELAXNGWriter. | |
| writer | back to summary |
|---|---|
| protected XMLWriter writer | |
| RELAXNGWriter | back to summary |
|---|---|
| public RELAXNGWriter() | |
| cleanName | back to summary |
|---|---|
| private String cleanName(String name) | |
| createNameClassWriter | back to summary |
|---|---|
| protected NameClassVisitor createNameClassWriter() | |
| getTargetNamespace | back to summary |
|---|---|
| public String getTargetNamespace() Implements com. | |
| getWriter | back to summary |
|---|---|
| public XMLWriter getWriter() Implements com. | |
| setDocumentHandler | back to summary |
|---|---|
| public void setDocumentHandler(DocumentHandler handler) Implements com. Doc from com. Deprecated as a consequence of Sets DocumentHandler. This handler will receive the result of conversion. | |
| sniffDefaultNs | back to summary |
|---|---|
| protected String sniffDefaultNs(Expression exp) sniffs namespace URI that can be used as default 'ns' attribute from expression. find an element or attribute, then use its namespace URI. | |
| write | back to summary |
|---|---|
| public void write(Grammar g) throws SAXException Implements com. Doc from com. Converts this grammar to the XML representation.
| |
| write | back to summary |
|---|---|
| public void write(Grammar g, String _defaultNs) throws SAXException generates SAX2 events of the specified grammar.
| |
| writeIsland | back to summary |
|---|---|
| protected void writeIsland(Expression exp) writes a bunch of expression into one tree. | |
| writeNameClass | back to summary |
|---|---|
| public void writeNameClass(NameClass src) Implements com. | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | onElement(ElementExp exp)
Overrides com. Implements com. |
| public void | onOther(OtherExp exp)
Implements abstract com. Implements com. |
| public void | onRef(ReferenceExp exp)
Implements abstract com. Implements com. |
| public Expression | |
| protected void |
| SmartPatternWriter | back to summary |
|---|---|
| pack-priv SmartPatternWriter(Context context) | |
| onAttribute | back to summary |
|---|---|
| public void onAttribute(AttributeExp exp) Overrides com. Implements com. | |
| onElement | back to summary |
|---|---|
| public void onElement(ElementExp exp) Overrides com. Implements com. | |
| onOther | back to summary |
|---|---|
| public void onOther(OtherExp exp) Implements abstract com. Implements com. | |
| onRef | back to summary |
|---|---|
| public void onRef(ReferenceExp exp) Implements abstract com. Implements com. | |
| simplify | back to summary |
|---|---|
| public Expression simplify(Expression exp) remove unnecessary ReferenceExp from content model. this will sometimes makes content model smaller. | |
| writeElement | back to summary |
|---|---|
| protected void writeElement(ElementExp exp) | |