Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/rxm.git
source/java/com/whitemagicsoftware/rxm/tree/TransformationTree.java
/**
- * Wrap the given payload in the class responsible for transforming it.
+ * Recursively traverses this tree and generates a string that is the
+ * transformed version of an input AST.
*
- * @param payload The data to wrap in a corresponding transformation
- * instance.
+ * @return A non-null string, possibly empty.
*/
- @Override
- protected void setPayload( T payload ) {
- super.setPayload( transform( payload ) );
+ public String toString() {
+ return toString( this, 0 );
+ }
+
+ /**
+ * Depth-first traversal.
+ */
+ protected String toString( Tree<T> tree, int depth ) {
+ StringBuilder sb = new StringBuilder( indent( depth ) );
+ T payload = tree.getPayload();
+
+ sb.append( start( payload ) );
+
+ for( Tree<T> branch : tree.getLeaves() ) {
+ sb.append( System.lineSeparator() );
+
+ // Recurse for all the branches at this level of the tree.
+ sb.append( toString( branch, depth + getIndent() ) );
+ }
+
+ sb.append( System.lineSeparator() ).append( indent( depth ) );
+
+ return sb.append( stop( payload ) ).toString();
}
+
+ /**
+ * Called when entering a new hierarchy in the transformation tree.
+ *
+ * @param payload The data to transform into its opening text.
+ */
+ protected abstract String start( T payload );
+
+ /**
+ * Called when exiting a new hierarchy in the transformation tree.
+ *
+ * @param payload The data to transform into its closing text.
+ */
+ protected abstract String stop( T payload );
/**
/**
- * Recursively traverses this tree and generates a string that is the
- * transformed version of an input AST.
+ * Wrap the given payload in the class responsible for transforming it.
*
- * @return A non-null string, possibly empty.
- */
- public String toString() {
- return toString( this, 0 );
- }
-
- /**
- * Depth-first traversal.
+ * @param payload The data to wrap in a corresponding transformation
+ * instance.
*/
- protected String toString( Tree<T> tree, int depth ) {
- StringBuilder sb = new StringBuilder( indent( depth ) );
- sb.append( tree.getPayload().toString() );
-
- for( Tree<T> branch : tree.getLeaves() ) {
- sb.append( System.lineSeparator() );
-
- // Recurse for all the branches at this level of the tree.
- sb.append( toString( branch, depth + getIndent() ) );
- }
-
- return sb.toString();
+ @Override
+ protected void setPayload( T payload ) {
+ super.setPayload( transform( payload ) );
}
source/java/com/whitemagicsoftware/rxm/tree/xml/ASTParserRuleContext.java
package com.whitemagicsoftware.rxm.tree.xml;
+import com.whitemagicsoftware.rxm.grammar.QueryParser;
+
import org.antlr.v4.runtime.ParserRuleContext;
/**
- * Subclasses override this method to create the correctly transformed
- * string.
+ * Allows subclasses to retrieve the payload.
+ *
+ * @return The original (unwrapped) payload.
*/
- public String toString() {
- return getParserRuleContext().getText();
- }
-
protected ParserRuleContext getParserRuleContext() {
return this.ctx;
}
+ /**
+ * Sets the payload.
+ *
+ * @param ctx The original (unwrapped) payload.
+ */
private void setParserRuleContext( ParserRuleContext ctx ) {
this.ctx = ctx;
+ }
+
+ /**
+ *
+ * @param name The name assigned to the element.
+ */
+ protected String startElement( String name ) {
+ return String.format( "XMLELEMENT( NAME \"%s\"", name );
+ }
+
+ /**
+ * @param name The name assigned to the attribute.
+ */
+ protected String startAttributes( String name ) {
+ return String.format( "XMLATTRIBUTES( %s AS ", name );
+ }
+
+ /**
+ * Casts the payload into the proper context, then extracts the element.
+ * This should not be called if the wrapped payload does not have an
+ * element.
+ *
+ * @return The element associated with the root parser rule.
+ */
+ protected QueryParser.ElementContext getElementContext() {
+ return ((QueryParser.RootContext)getParserRuleContext()).element();
+ }
+
+ /**
+ * Subclass override this to change the content returned by the parser
+ * rule. This is called during transformation.
+ */
+ @Override
+ public abstract Token getStart();
+
+ /**
+ * Subclass override this to change the content returned by the parser
+ * rule. This is called during transformation.
+ */
+ @Override
+ public abstract Token getStop();
+
+ /**
+ * Convenience method.
+ *
+ * @return The payload's text.
+ */
+ @Override
+ public String toString() {
+ return getParserRuleContext().getText();
}
}
source/java/com/whitemagicsoftware/rxm/tree/xml/ColumnMapContext.java
}
- public String toString() {
- return "COLUMN MAP:" + getParserRuleContext().getText();
+ @Override
+ public Token getStart() {
+ return new Token( "COLUMN MAP:" + toString() );
+ }
+
+ @Override
+ public Token getStop() {
+ return new Token( ")" );
}
}
source/java/com/whitemagicsoftware/rxm/tree/xml/QueryTree.java
super( payload );
}
+
+ /**
+ * Returns the transformed syntax for beginning the payload item.
+ *
+ * @return A non-null string, possibly empty.
+ */
+ protected String start( T payload ) {
+ return payload.getStart().toString();
+ }
+
+ /**
+ * Returns the transformed syntax for ending the payload item.
+ *
+ * @return A non-null string, possibly empty.
+ */
+ protected String stop( T payload ) {
+ return payload.getStop().toString();
+ }
}
source/java/com/whitemagicsoftware/rxm/tree/xml/RootContext.java
package com.whitemagicsoftware.rxm.tree.xml;
+import com.whitemagicsoftware.rxm.grammar.QueryParser;
+
import org.antlr.v4.runtime.ParserRuleContext;
+/**
+ * Transforms <code>root &gt; element</code>.
+ */
public class RootContext extends ASTParserRuleContext {
public RootContext( ParserRuleContext ctx ) {
super( ctx );
}
- public String toString() {
- return "ROOT:" + getParserRuleContext().getText();
+ /**
+ * Transforms into SELECT, XMLROOT, and XMLELEMENT.
+ */
+ @Override
+ public Token getStart() {
+ QueryParser.ElementContext element = getElementContext();
+ String e = element.getText();
+
+ String s = String.format( "SELECT%nXMLROOT( %s", startElement( e ) );
+ return new Token( s );
+ }
+
+ /**
+ * Closes the XMLROOT and XMLELEMENT.
+ */
+ @Override
+ public Token getStop() {
+ return new Token( String.format( ")%s )", getDeclaration() ) );
+ }
+
+ protected String getDeclaration() {
+ return String.format( ", VERSION '%.1f', STANDALONE %s",
+ getVersion(), isStandalone() ? "YES" : "NO" );
+ }
+
+ protected float getVersion() {
+ return 1.0f;
+ }
+
+ protected boolean isStandalone() {
+ return true;
}
}
source/java/com/whitemagicsoftware/rxm/tree/xml/TableMapContext.java
import org.antlr.v4.runtime.ParserRuleContext;
+/**
+ * Transforms <code>element &gt; element</code>.
+ */
public class TableMapContext extends ASTParserRuleContext {
public TableMapContext( ParserRuleContext ctx ) {
super( ctx );
}
- public String toString() {
- return "TABLE MAP:" + getParserRuleContext().getText();
+ @Override
+ public Token getStart() {
+ return new Token( "TABLE MAP:" + getParserRuleContext().getText() );
+ }
+
+ @Override
+ public Token getStop() {
+ return new Token( ")" );
}
}

Open root, root element, and close them.

Author Dave Jarvis <email>
Date 2015-03-07 19:04:47 GMT-0800
Commit 8e416edbdcf5fe44ef46255bee8c987d604826c3
Parent 35140a8
Delta 179 lines added, 40 lines removed, 139-line increase