Skip to content

Instantly share code, notes, and snippets.

@osima
Created November 12, 2010 06:36
Show Gist options
  • Select an option

  • Save osima/673805 to your computer and use it in GitHub Desktop.

Select an option

Save osima/673805 to your computer and use it in GitHub Desktop.
ミニDSL
class MyBuilder extends BuilderSupport{
def rootNode
def createNode(name){
def currentNode = new javax.swing.tree.DefaultMutableTreeNode(name)
if( name=='mystyle' )
this.rootNode = currentNode
return currentNode
}
def createNode(name,value){
createNode( name + ':' + value )
}
def createNode(name, Map attribute){
//println name + ':' + attribute
}
def createNode(name, Map attribute, value){
//println name + ':' + attribute + ':' + value
}
void setParent(parent, child){
parent.add( child )
}
//void nodeCompleted(parent, node){ }
void output(){
def br = System.getProperty('line.separator')
def sb = ''<<''
assert sb.getClass().name == 'java.lang.StringBuffer'
sb << tab(0)+ '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >' << br
sb << tab(1)+ '<xsl:template match="@*|node()" >' << br
sb << tab(2)+ '<xsl:copy>' << br
sb << tab(2)+ '<xsl:apply-templates select="@*|node()" />' << br
sb << tab(2)+ '</xsl:copy>' << br
sb << tab(1)+ '</xsl:template>' << br
for( nodeAsXPath in this.rootNode.children() ){
def xpath = nodeAsXPath.getUserObject()
sb << tab(1)+ "<xsl:template match=\"$xpath\" >" << br
sb << tab(2)+ '<xsl:copy>' << br
for( nodeAsMystyle in nodeAsXPath.children() ){
def attrname = nodeAsMystyle.getUserObject().split(':')[0]
def attrvalue = nodeAsMystyle.getUserObject().split(':')[1]
sb << tab(3)+ "<xsl:attribute name=\"$attrname\">$attrvalue</xsl:attribute>" << br
}
sb << tab(3)+ '<xsl:apply-templates select="@*|node()" />' << br
sb << tab(2)+ '</xsl:copy>' << br
sb << tab(1)+ '</xsl:template>' << br
}
sb << tab(0)+ '</xsl:stylesheet>' << br
print sb
}
def tab(int indentCount){
return ' '*indentCount
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment