Top Description Inners Fields Constructors Methods
com.sun.msv.writer.relaxng

public Class RELAXNGWriter

extends Object
implements GrammarWriter, Context
Class Inheritance
All Implemented Interfaces
com.sun.msv.writer.relaxng.Context, com.sun.msv.writer.GrammarWriter
Annotations
@SuppressWarnings:deprecation
Imports
java.util.HashMap, .HashSet, .Iterator, .Map, .Set, org.xml.sax.DocumentHandler, .SAXException, org.xml.sax.helpers.LocatorImpl, com.sun.msv.grammar.AttributeExp, .BinaryExp, .ChoiceExp, .ChoiceNameClass, .ConcurExp, .DataExp, .DifferenceNameClass, .ElementExp, .Expression, .ExpressionCloner, .ExpressionVisitor, .Grammar, .InterleaveExp, .ListExp, .MixedExp, .NameClass, .NameClassVisitor, .NamespaceNameClass, .OneOrMoreExp, .OtherExp, .ReferenceExp, .SequenceExp, .SimpleNameClass, .ValueExp, com.sun.msv.grammar.util.ExpressionWalker, .PossibleNamesCollector, com.sun.msv.reader.datatype.xsd.XSDVocabulary, com.sun.msv.reader.trex.ng.RELAXNGReader, com.sun.msv.util.StringPair, com.sun.msv.writer.GrammarWriter, .SAXRuntimeException, .XMLWriter

converts any Grammar into RELAX NG XML representation through SAX1 events.

How it works

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.

Why SAX1?

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.

Author
Kohsuke KAWAGUCHI

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
RELAXNGWriter.SmartPatternWriter

PatternWriter that performs some optimization for human eyes.

Field Summary

Modifier and TypeField and Description
protected String
defaultNs

namespace URI currently implied through "ns" attribute propagation.

protected Map<Expression, String>
exp2name

map from ReferenceExp/ElementExp to its unique name.

protected Grammar
grammar

Grammar object which we are writing.

protected NameClassVisitor
protected RELAXNGWriter.SmartPatternWriter
protected XMLWriter

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private String
protected NameClassVisitor
public String
public XMLWriter
public void
setDocumentHandler(DocumentHandler handler)

Implements com.sun.msv.writer.GrammarWriter.setDocumentHandler.

Deprecated as a consequence of DocumentHandler being deprecated.
Sets DocumentHandler.
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.sun.msv.writer.GrammarWriter.write.

Converts this grammar to the XML representation.
public void
write(Grammar
if specified, this namespace URI is used as "ns" attribute of grammar element. Can be null.
g
,
String _defaultNs)

generates SAX2 events of the specified grammar.

protected void
writeIsland(Expression exp)

writes a bunch of expression into one tree.

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultNsback to summary
protected String defaultNs

namespace URI currently implied through "ns" attribute propagation.

exp2nameback 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.

grammarback to summary
protected Grammar grammar

Grammar object which we are writing.

nameClassWriterback to summary
protected NameClassVisitor nameClassWriter
patternWriterback to summary
protected RELAXNGWriter.SmartPatternWriter patternWriter
writerback to summary
protected XMLWriter writer

Constructor Detail

RELAXNGWriterback to summary
public RELAXNGWriter()

Method Detail

cleanNameback to summary
private String cleanName(String name)
createNameClassWriterback to summary
protected NameClassVisitor createNameClassWriter()
getTargetNamespaceback to summary
public String getTargetNamespace()

Implements com.sun.msv.writer.relaxng.Context.getTargetNamespace.

getWriterback to summary
public XMLWriter getWriter()

Implements com.sun.msv.writer.relaxng.Context.getWriter.

setDocumentHandlerback to summary
public void setDocumentHandler(DocumentHandler handler)

Implements com.sun.msv.writer.GrammarWriter.setDocumentHandler.

Doc from com.sun.msv.writer.GrammarWriter.setDocumentHandler.

Deprecated

as a consequence of DocumentHandler being deprecated. See corresponding docs for further information.

Sets DocumentHandler. This handler will receive the result of conversion.

sniffDefaultNsback 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.

writeback to summary
public void write(Grammar g) throws SAXException

Implements com.sun.msv.writer.GrammarWriter.write.

Doc from com.sun.msv.writer.GrammarWriter.write.

Converts this grammar to the XML representation.

Exceptions
SAXException:
DocumentHandler may throw a SAXException.
writeback to summary
public void write(Grammar g, String _defaultNs) throws SAXException

generates SAX2 events of the specified grammar.

Parameters
g:Grammar

if specified, this namespace URI is used as "ns" attribute of grammar element. Can be null.

Exceptions
IllegalArgumentException:
If the given grammar is beyond the expressive power of TREX (e.g., some RELAX NG grammar), then this exception is thrown.
writeIslandback to summary
protected void writeIsland(Expression exp)

writes a bunch of expression into one tree.

writeNameClassback to summary
public void writeNameClass(NameClass src)

Implements com.sun.msv.writer.relaxng.Context.writeNameClass.

com.sun.msv.writer.relaxng back to summary

pack-priv Class RELAXNGWriter.SmartPatternWriter

extends PatternWriter
Class Inheritance

PatternWriter that performs some optimization for human eyes.

Field Summary

Inherited from com.sun.msv.writer.relaxng.PatternWriter:
contextwriter

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
public void
public void
public void
public Expression
simplify(Expression exp)

remove unnecessary ReferenceExp from content model.

protected void
Inherited from com.sun.msv.writer.relaxng.PatternWriter:
isPredefinedTypeonAnyStringonChoiceonConcuronDataonEpsilononInterleaveonListonMixedonNullSetonOneOrMoreonOptionalonSequenceonValueonZeroOrMoreparamserializeDataTypeserializeEnumerationserializeListTypeserializeUnionTypevisitBinExpvisitUnary

Constructor Detail

SmartPatternWriterback to summary
pack-priv SmartPatternWriter(Context context)

Method Detail

onAttributeback to summary
public void onAttribute(AttributeExp exp)

Overrides com.sun.msv.writer.relaxng.PatternWriter.onAttribute.

Implements com.sun.msv.grammar.ExpressionVisitorVoid.onAttribute.

onElementback to summary
public void onElement(ElementExp exp)

Overrides com.sun.msv.writer.relaxng.PatternWriter.onElement.

Implements com.sun.msv.grammar.ExpressionVisitorVoid.onElement.

onOtherback to summary
public void onOther(OtherExp exp)

Implements abstract com.sun.msv.writer.relaxng.PatternWriter.onOther.

Implements com.sun.msv.grammar.ExpressionVisitorVoid.onOther.

onRefback to summary
public void onRef(ReferenceExp exp)

Implements abstract com.sun.msv.writer.relaxng.PatternWriter.onRef.

Implements com.sun.msv.grammar.ExpressionVisitorVoid.onRef.

simplifyback to summary
public Expression simplify(Expression exp)

remove unnecessary ReferenceExp from content model. this will sometimes makes content model smaller.

writeElementback to summary
protected void writeElement(ElementExp exp)