annotate fixivy.groovy @ 90:c207cdcaf13e

latest version
author smith@nwoca.org
date Tue, 21 Feb 2012 14:50:19 +0000
parents 15f4da8bdbd3
children 90325a10fe95
rev   line source
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
1 /*
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
2
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
3 This script is (was) used for 'fixing' early SSDT ivy.xml files created by NetBeans/ivyBeans.
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
4 It's primary goal is to migrate configurations from NetBeans conventions to Maven/Gradle conventions.
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
5 In order to accomplish this, it needs to correct dependencies, excludes and configurations. However,
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
6 it's not able to perfectly correct every ssdt project. Some parts of this script may be commented
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
7 out. Results of this script much be reviewed carefully before committing.
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
8 */
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
9
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
10
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
11 import groovy.xml.QName;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
12 import org.codehaus.groovy.runtime.InvokerHelper;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
13
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
14 import java.io.OutputStreamWriter;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
15 import java.io.PrintWriter;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
16 import java.util.HashMap;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
17 import java.util.List;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
18 import java.util.Map;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
19
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
20 import groovy.xml.*
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
21
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
22 //args = [ '-r']
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
23 //println args
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
24
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
25 ant = new AntBuilder()
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
26
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
27
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
28 if (args.any { it.toUpperCase() == '-R'} ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
29
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
30 new File('.').traverse ([ type: groovy.io.FileType.DIRECTORIES, maxDepth: 3 ]) { project ->
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
31 if (new File(project,'ivy.xml').exists() ) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
32 processIvy(new File(project,'ivy.xml'))
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
33 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
34 }
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
35
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
36 } else {
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
37 processIvy(new File('ivy.xml'))
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
38 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
39
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
40 def processIvy(file) {
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
41 println "-" * 60
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
42 println "processing: $file"
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
43 def xml = new XmlSlurper()
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
44 def ivy = xml.parse(file)
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
45
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
46 def cfgs = ivy.configurations
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
47
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
48 // default mapping for most dependencies:
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
49 cfgs.@defaultconfmapping = 'compile->default(*)'
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
50
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
51 // Add default config, if missing:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
52 if (!cfgs.children().find { it.@name == 'default' } ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
53 println "adding default config"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
54 cfgs.appendNode {
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
55 conf(name:"default",extends:'compile',visibility:'public')
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
56 }
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
57 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
58
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
59 if (ivy.dependencies.children().find { it.@name == 'org.springframework.web.servlet' } &&
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
60 !ivy.dependencies.children().find { it.@name == 'servlet-api' } ) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
61 ivy.dependencies.children().findAll { it.name() == 'dependency' }.list().last() + {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
62 dependency(name:'servlet-api', org: 'javax.servlet', rev: '2.5')
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
63 dependency(name:'jsp-api', org: 'javax.servlet', rev: '2.0')
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
64 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
65
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
66 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
67
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
68 ivy.dependencies.children().each { dep ->
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
69 if ( dep.@conf.text()?.contains('test') ) {
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
70 dep.@conf = 'compile-test->default(*)'
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
71 } else if ( dep.@conf.text().contains('castor-ant') ) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
72 dep.@conf = 'castor-ant->default(*)'
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
73 } else if (dep.@conf.text() == 'runtime->default' ) {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
74 // preserve
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
75 } else{
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
76 dep.attributes().remove('conf')
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
77 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
78 }
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
79
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
80 // ensure(ivy.dependencies,["org.spockframework:spock-core:0.5-groovy-1.8"])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
81
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
82 // ensure(ivy.dependencies, 'hibernate-validator', ['org.slf4j:slf4j-api:1.5.6','org.slf4j:slf4j-log4j12:1.5.6'])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
83 // ensure(ivy.dependencies, 'usas.repository', ["org.springframework:org.springframework.test:3.0.5.RELEASE","org.codehaus.groovy:groovy-all:1.8.2"])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
84 // ensure(ivy.dependencies, 'usps.repository', ["org.springframework:org.springframework.test:3.0.5.RELEASE","org.codehaus.groovy:groovy-all:1.8.2"])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
85
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
86
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
87 // ensure(ivy.dependencies, 'spock-core', ["org.springframework:org.springframework.test:3.0.5.RELEASE"])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
88 // ensure(ivy.dependencies, 'org.springframework.test', ['org.junit:com.springsource.org.junit:4.9.0'])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
89 // ensure(ivy.dependencies, 'org.springframework.test', ["org.spockframework:spock-spring:0.5-groovy-1.8"])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
90
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
91
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
92 // find groovy dependencies (or null)
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
93 groovy = ivy.dependencies.children().find { it.@org == 'org.codehaus.groovy' && it.@name.text().startsWith('groovy') }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
94
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
95 if (groovy) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
96 // Add a groovy conig for groovy projects:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
97 if (!cfgs.children().any { it.@name == 'groovy' } ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
98 println "added private groovy conf"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
99 cfgs.appendNode {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
100 conf(name:'groovy',visibility:'private',transitive: true)
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
101 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
102 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
103 // Adjust 'compile' to exend from 'groovy' conf
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
104 cfgs.children().find { it.@name == 'compile' }.@extends = 'groovy'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
105 cfgs.children().find { it.@name == 'groovy' }.@transitive = 'true'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
106 groovy.@conf = "groovy->default"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
107 println "Groovy ${groovy.@rev} conf changed to ${groovy.@conf}"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
108 // Switch project to use groovy-all instead of groovy + deps
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
109 if (groovy.@name == 'groovy') {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
110 println "changed groovy to groovy-all"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
111 groovy.@name = 'groovy-all'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
112 }
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
113
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
114 def template = new File(file.parent ?: ".",'template.mf')
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
115 if (template.exists()) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
116 println "updating groovy in $template"
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
117 ant.replace(file: template, token: 'org.codehaus.groovy-groovy:major',value:'org.codehaus.groovy-groovy-all:major')
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
118 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
119
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
120 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
121
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
122 // resolveJunit(ivy.dependencies)
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
123
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
124 // remove exclude for groovy-all
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
125 ivy.dependencies.children().find { it.name() == 'exclude' && it.@module.text() == 'groovy-all' }.replaceNode {}
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
126 // remove exclude for commons logging
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
127 ivy.dependencies.children().find { it.name() == 'exclude' && it.@module.text() == 'com.springsource.org.apache.commons.logging' }.replaceNode {}
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
128
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
129 // Add exclude for regular groovy:
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
130 ensureExcluded(ivy.dependencies,"org.codehaus.groovy:groovy")
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
131
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
132 // ensure modules covered by springsource are exluded;
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
133 // ensureSpringSourceExcludes(ivy.dependencies)
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
134 /*
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
135 ensureConfiguration(ivy.dependencies,'org.springframework.test','compile-test->*')
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
136 ensureConfiguration(ivy.dependencies,'com.springsource.org.junit','compile-test')
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
137 ensureConfiguration(ivy.dependencies,'spock-core','compile-test')
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
138 ensureConfiguration(ivy.dependencies,'spock-spring','compile-test')
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
139 ensureConfiguration(ivy.dependencies,'httpunit','compile-test->*')
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
140 ensureConfiguration(ivy.dependencies,'org.springframework.aspects','compile->*')
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
141
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
142 // patch bad junit exludes
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
143 ivy.dependencies.children().findAll {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
144 it.name() == 'exclude' && ("${it.@org}" == 'org.junit' || "${it.@module}" == 'org.junit' )
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
145 }.each {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
146 println "replacing ${it.name()} ${it.@org} ${it.@module} as junit:junit"
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
147 it.@org = 'junit'
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
148 it.@module = 'junit'
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
149 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
150 */
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
151 cfgs.children().each { it.@visibility = 'private' } // Should be 'private' after initial conversions.
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
152
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
153 cfgs.children().find {it.@name == 'runtime' }.@extends = 'compile'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
154 cfgs.children().find {it.@name == 'compile-test' }.@extends = 'compile'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
155 cfgs.children().find {it.@name == 'runtime-test' }.@extends = 'runtime,compile-test'
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
156 cfgs.children().find {it.@name == 'runtime-test' }.@visibility = 'public'
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
157 cfgs.children().find {it.@name == 'default' }.@visibility = 'public'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
158 cfgs.children().find {it.@name == 'default' }.@extends = 'runtime'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
159
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
160 if (!cfgs.children().any { it.@name == 'archives' } ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
161 println "added public 'archives' conf"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
162 cfgs.appendNode {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
163 conf(name:'archives',visibility:'public')
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
164 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
165 }
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
166
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
167 cfgs.children().find {it.@name == 'archives' }.@visibility = 'public'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
168
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
169
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
170 def outputBuilder = new StreamingMarkupBuilder()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
171 //new File('ivy.xml').write( XmlUtil.serialize( outputBuilder.bind { mkp.yield ivy }).replaceAll('>','>'))
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
172
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
173 def stringWriter = new StringWriter()
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
174
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
175 def node = new XmlParser().parseText( XmlUtil.serialize( outputBuilder.bind { mkp.yield ivy }))
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
176 new MyNodePrinter(new IndentPrinter( new PrintWriter(stringWriter))).print(node)
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
177 file.write(stringWriter.toString())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
178
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
179 }
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
180
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
181 def resolveJunit(depends) {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
182
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
183 if (depends.children().any { it.@name == 'com.springsource.org.junit' } ) {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
184 depends.children().findAll { "${it.@name}" == 'junit' || it.@module == 'junit' }.each { it.replaceNode {} }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
185 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
186
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
187 if (!depends.children().any {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
188 def name = "${it.@name}"
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
189 name.contains ('junit') || name.contains('spock') } ) {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
190 ensure(depends,["junit:junit:4.8.2"])
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
191 depends.children().findAll { it.@module == 'junit' }.each { it.replaceNode {} }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
192 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
193 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
194
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
195 def ensureConfiguration(depends,name,cfg) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
196 depends.children().findAll { it.name() == 'dependency' && it.@name == name}.each {
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
197 it.@conf = cfg.contains('->') ? cfg : "$cfg->default(*)"
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
198 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
199 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
200
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
201 def ensureExcluded(depends,excludeModule) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
202 def (o,m) = excludeModule.split(':')
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
203 if (!depends.children().any { it.name() == 'exclude' && it.@module.text() == m}) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
204 depends.appendNode {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
205 println "excluding $o:$m"
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
206 exclude(org: o, module: m)
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
207 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
208 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
209 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
210
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
211 def ensure(depends,exists,requires) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
212
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
213 if (depends.children().any { it.name() == 'dependency' && it.@name == exists } ) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
214 requires.each {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
215 def (org,name,rev) = it.split(":")
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
216 if (!depends.children().any { it.@name == name } ) {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
217 println " adding requirement $org:$name for $exists"
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
218 depends.children().findAll { it.name() == 'dependency' }.list().last() + {
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
219 dependency(name: name, org: org, rev: rev)
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
220 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
221 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
222 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
223 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
224 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
225
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
226 def ensure(depends,requires) {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
227
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
228 requires.each {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
229 def (org,name,rev) = it.split(":")
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
230 if (!depends.children().any { it.@name == name } ) {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
231 println " adding requirement $org:$name "
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
232 depends.children().findAll { it.name() == 'dependency' }.list().last() + {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
233 dependency(name: name, org: org, rev: rev)
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
234 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
235 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
236 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
237 }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
238
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
239 def ensureSpringSourceExcludes(depends) {
90
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
240
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
241 depends.children().findAll { it.name() == 'dependency' && it.@name.text().startsWith('com.springsource.') }.collect {
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
242 "${it.@org}:${it.@name.text() - 'com.springsource.'}"
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
243 }.each { ensureExcluded(depends,it) }
c207cdcaf13e latest version
smith@nwoca.org
parents: 89
diff changeset
244
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
245 }
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
246
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
247 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
248 The class below is a copy/paste of groovy's XmlNodePrinter customized to order Ivy.xml attributes in
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
249 the desired order and to add some whitespace.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
250 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
251 class MyNodePrinter {
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
252
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
253 protected void printNameAttributes(Map attributes, ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
254
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
255 if (attributes == null || attributes.isEmpty()) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
256 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
257 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
258 def writer = new StringBuffer()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
259 attributes.entrySet().sort{
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
260 switch (it.key ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
261 case 'org':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
262 1
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
263 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
264 case 'name': 2
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
265 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
266 case 'rev': 3
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
267 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
268 case 'extends': 5
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
269 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
270 default: 99
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
271 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
272 }.each { p ->
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
273 def tmp = new StringBuffer()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
274 Map.Entry entry = (Map.Entry) p;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
275
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
276 tmp << " "
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
277 tmp << getName(entry.getKey())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
278 tmp << "="
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
279 Object value = entry.getValue();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
280 tmp << quote
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
281 tmp << value
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
282 // if (value instanceof String) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
283 // printEscaped((String) value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
284 // } else {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
285 // printEscaped(InvokerHelper.toString(value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
286 // }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
287 tmp << quote
89
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
288 def size = entry.getKey() in ['org','name'] ? 35 : 22
15f4da8bdbd3 add ability to control order of attributes, whitespace. more cleanup up of dependencies and exclusions
smith@nwoca.org
parents: 88
diff changeset
289 writer << String.format(" %-${size}s",tmp.toString())
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
290 // printNamespace(entry.getKey(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
291 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
292 out.print(" " + writer.toString().trim())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
293 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
294
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
295 protected final IndentPrinter out;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
296 private String quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
297 private boolean namespaceAware = true;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
298 private boolean preserveWhitespace = false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
299
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
300 public MyNodePrinter(PrintWriter out) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
301 this(out, " ");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
302 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
303
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
304 public MyNodePrinter(PrintWriter out, String indent) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
305 this(out, indent, "\"");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
306 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
307
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
308 public MyNodePrinter(PrintWriter out, String indent, String quote) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
309 this(new IndentPrinter(out, indent), quote);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
310 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
311
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
312 public MyNodePrinter(IndentPrinter out) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
313 this(out, "\"");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
314 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
315
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
316 public MyNodePrinter(IndentPrinter out, String quote) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
317 if (out == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
318 throw new IllegalArgumentException("Argument 'IndentPrinter out' must not be null!");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
319 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
320 this.out = out;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
321 this.quote = quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
322 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
323
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
324 public MyNodePrinter() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
325 this(new PrintWriter(new OutputStreamWriter(System.out)));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
326 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
327
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
328 public void print(Node node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
329 print(node, new NamespaceContext());
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
330 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
331
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
332 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
333 * Check if namespace handling is enabled.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
334 * Defaults to <code>true</code>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
335 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
336 * @return true if namespace handling is enabled
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
337 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
338 public boolean isNamespaceAware() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
339 return namespaceAware;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
340 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
341
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
342 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
343 * Enable and/or disable namespace handling.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
344 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
345 * @param namespaceAware the new desired value
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
346 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
347 public void setNamespaceAware(boolean namespaceAware) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
348 this.namespaceAware = namespaceAware;
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
349 }
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
350
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
351 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
352 * Check if whitespace preservation is enabled.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
353 * Defaults to <code>false</code>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
354 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
355 * @return true if whitespaces are honoured when printing simple text nodes
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
356 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
357 public boolean isPreserveWhitespace() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
358 return preserveWhitespace;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
359 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
360
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
361 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
362 * Enable and/or disable preservation of whitespace.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
363 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
364 * @param preserveWhitespace the new desired value
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
365 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
366 public void setPreserveWhitespace(boolean preserveWhitespace) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
367 this.preserveWhitespace = preserveWhitespace;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
368 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
369
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
370 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
371 * Get Quote to use when printing attributes.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
372 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
373 * @return the quote character
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
374 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
375 public String getQuote() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
376 return quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
377 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
378
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
379 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
380 * Set Quote to use when printing attributes.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
381 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
382 * @param quote the quote character
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
383 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
384 public void setQuote(String quote) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
385 this.quote = quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
386 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
387
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
388 protected void print(Node node, NamespaceContext ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
389 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
390 * Handle empty elements like '<br/>', '<img/> or '<hr noshade="noshade"/>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
391 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
392 if (isEmptyElement(node)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
393 printLineBegin();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
394 out.print("<");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
395 out.print(getName(node));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
396 if (ctx != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
397 printNamespace(node, ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
398 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
399 printNameAttributes(node.attributes(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
400 out.print("/>");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
401 printLineEnd();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
402 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
403 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
404 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
405
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
406 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
407 * Hook for extra processing, e.g. GSP tag element!
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
408 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
409 if (printSpecialNode(node)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
410 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
411 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
412 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
413
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
414 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
415 * Handle normal element like <html> ... </html>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
416 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
417 Object value = node.value();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
418 if (value instanceof List) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
419 printName(node, ctx, true, isListOfSimple((List) value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
420 printList((List) value, ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
421 printName(node, ctx, false, isListOfSimple((List) value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
422 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
423 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
424 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
425
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
426 // treat as simple type - probably a String
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
427 printName(node, ctx, true, preserveWhitespace);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
428 printSimpleItemWithIndent(value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
429 printName(node, ctx, false, preserveWhitespace);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
430 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
431 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
432
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
433 private boolean isListOfSimple(List value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
434 for (Object p : value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
435 if (p instanceof Node) return false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
436 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
437 return preserveWhitespace;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
438 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
439
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
440 protected void printLineBegin() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
441 out.printIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
442 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
443
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
444 protected void printLineEnd() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
445 printLineEnd(null);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
446 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
447
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
448 protected void printLineEnd(String comment) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
449 if (comment != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
450 out.print(" <!-- ");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
451 out.print(comment);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
452 out.print(" -->");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
453 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
454 out.println();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
455 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
456 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
457
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
458 protected void printList(List list, NamespaceContext ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
459 out.incrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
460 for (Object value : list) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
461 NamespaceContext context = new NamespaceContext(ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
462 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
463 * If the current value is a node, recurse into that node.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
464 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
465 if (value instanceof Node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
466 print((Node) value, context);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
467 continue;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
468 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
469 printSimpleItem(value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
470 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
471 out.decrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
472 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
473
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
474 protected void printSimpleItem(Object value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
475 if (!preserveWhitespace) printLineBegin();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
476 printEscaped(InvokerHelper.toString(value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
477 if (!preserveWhitespace) printLineEnd();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
478 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
479
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
480 protected void printName(Node node, NamespaceContext ctx, boolean begin, boolean preserve) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
481 if (node == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
482 throw new NullPointerException("Node must not be null.");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
483 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
484 Object name = node.name();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
485 if (name == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
486 throw new NullPointerException("Name must not be null.");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
487 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
488 if (!preserve || begin) printLineBegin();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
489 out.print("<");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
490 if (!begin) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
491 out.print("/");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
492 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
493 out.print(getName(node));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
494 if (ctx != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
495 printNamespace(node, ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
496 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
497 if (begin) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
498 printNameAttributes(node.attributes(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
499 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
500 out.print(">");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
501 if (!preserve || !begin) printLineEnd();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
502 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
503
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
504 protected boolean printSpecialNode(Node node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
505 return false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
506 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
507
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
508 protected void printNamespace(Object object, NamespaceContext ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
509 if (namespaceAware) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
510 if (object instanceof Node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
511 printNamespace(((Node) object).name(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
512 } else if (object instanceof QName) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
513 QName qname = (QName) object;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
514 String namespaceUri = qname.getNamespaceURI();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
515 if (namespaceUri != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
516 String prefix = qname.getPrefix();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
517 if (!ctx.isPrefixRegistered(prefix, namespaceUri)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
518 ctx.registerNamespacePrefix(prefix, namespaceUri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
519 out.print(" ");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
520 out.print("xmlns");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
521 if (prefix.length() > 0) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
522 out.print(":");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
523 out.print(prefix);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
524 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
525 out.print("=" + quote);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
526 out.print(namespaceUri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
527 out.print(quote);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
528 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
529 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
530 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
531 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
532 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
533
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
534
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
535
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
536 private boolean isEmptyElement(Node node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
537 if (node == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
538 throw new IllegalArgumentException("Node must not be null!");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
539 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
540 if (!node.children().isEmpty()) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
541 return false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
542 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
543 return node.text().length() == 0;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
544 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
545
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
546 private String getName(Object object) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
547 if (object instanceof String) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
548 return (String) object;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
549 } else if (object instanceof QName) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
550 QName qname = (QName) object;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
551 if (!namespaceAware) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
552 return qname.getLocalPart();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
553 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
554 return qname.getQualifiedName();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
555 } else if (object instanceof Node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
556 Object name = ((Node) object).name();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
557 return getName(name);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
558 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
559 return object.toString();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
560 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
561
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
562 private void printSimpleItemWithIndent(Object value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
563 if (!preserveWhitespace) out.incrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
564 printSimpleItem(value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
565 if (!preserveWhitespace) out.decrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
566 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
567
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
568 // For ' and " we only escape if needed. As far as XML is concerned,
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
569 // we could always escape if we wanted to.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
570 private void printEscaped(String s) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
571 for (int i = 0; i < s.length(); i++) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
572 char c = s.charAt(i);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
573 switch (c) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
574 case '<':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
575 out.print("&lt;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
576 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
577 case '>':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
578 out.print("&gt;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
579 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
580 case '&':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
581 out.print("&amp;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
582 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
583 case '\'':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
584 if (quote.equals("'"))
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
585 out.print("&apos;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
586 else
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
587 out.print(c);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
588 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
589 case '"':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
590 if (quote.equals("\""))
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
591 out.print("&quot;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
592 else
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
593 out.print(c);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
594 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
595 default:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
596 out.print(c);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
597 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
598 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
599 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
600
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
601 protected class NamespaceContext {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
602 private final Map<String, String> namespaceMap;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
603
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
604 public NamespaceContext() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
605 namespaceMap = new HashMap<String, String>();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
606 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
607
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
608 public NamespaceContext(NamespaceContext context) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
609 this();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
610 namespaceMap.putAll(context.namespaceMap);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
611 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
612
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
613 public boolean isPrefixRegistered(String prefix, String uri) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
614 return namespaceMap.containsKey(prefix) && namespaceMap.get(prefix).equals(uri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
615 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
616
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
617 public void registerNamespacePrefix(String prefix, String uri) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
618 if (!isPrefixRegistered(prefix, uri)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
619 namespaceMap.put(prefix, uri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
620 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
621 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
622
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
623 public String getNamespace(String prefix) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
624 Object uri = namespaceMap.get(prefix);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
625 return (uri == null) ? null : uri.toString();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
626 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
627 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
628 }