annotate fix-ivy.groovy @ 88:9f2ab59a5333

improve ivy clean up and control order of attribs
author smith@nwoca.org
date Wed, 15 Feb 2012 17:11:10 -0500
parents da29f9bde0da
children
rev   line source
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
1
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
2 import groovy.xml.QName;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
3 import org.codehaus.groovy.runtime.InvokerHelper;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
4
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
5 import java.io.OutputStreamWriter;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
6 import java.io.PrintWriter;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
7 import java.util.HashMap;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
8 import java.util.List;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
9 import java.util.Map;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
10
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
11 import groovy.xml.*
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
12
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
13 println args
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
14
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
15 if (args.any { it.toUpperCase() == '-R'} ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
16
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
17
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
18 } else {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
19 processIvy(new File('ivy.xml')
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
20 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
21
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
22 def processIvy(file) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
23
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
24 def xml = new XmlSlurper()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
25 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
26
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
27 def cfgs = ivy.configurations
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
28
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
29 // default mapping for most dependencies:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
30 cfgs.@defaultconfmapping = 'compile->default'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
31
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
32 // Add default config, if missing:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
33 if (!cfgs.children().find { it.@name == 'default' } ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
34 println "adding default config"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
35 cfgs.appendNode {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
36 conf(name:"default")
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
37 }
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
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
41 ivy.dependencies.children().each { dep ->
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
42 if ( dep.@conf.text()?.contains('test') ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
43 dep.@conf = 'compile-test->default'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
44 } else {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
45 dep.attributes().remove('conf')
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
46 }
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
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
49 // find groovy dependencies (or null)
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
50 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
51
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
52 if (groovy) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
53 // Add a groovy conig for groovy projects:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
54 if (!cfgs.children().any { it.@name == 'groovy' } ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
55 println "added private groovy conf"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
56 cfgs.appendNode {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
57 conf(name:'groovy',visibility:'private',transitive: true)
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
58 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
59 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
60 // Adjust 'compile' to exend from 'groovy' conf
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
61 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
62 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
63 groovy.@conf = "groovy->default"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
64 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
65 // 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
66 if (groovy.@name == 'groovy') {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
67 println "changed groovy to groovy-all"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
68 groovy.@name = 'groovy-all'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
69 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
70 // remove exclude for groovy-all
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
71 ivy.dependencies.children().find { it.name() == 'exclude' && it.@module.text() == 'groovy-all' }.replaceNode {}
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
72 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
73
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
74 //def sameConfMappings = ivy.dependencies.children().findAll { it.@conf in ['*->@','*->*'] }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
75 //
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
76 //sameConfMappings.each {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
77 // it.@conf = null // "default->compile;%->@"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
78 // println "changing conf for ${it.@name} to ${it.@conf}"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
79 //}
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
80
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
81
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
82 cfgs.children().each { it.@visibility = 'private' }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
83 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
84 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
85 cfgs.children().find {it.@name == 'runtime-test' }.@extends = 'runtime,compile-test'
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
86 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
87 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
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
89 if (!cfgs.children().any { it.@name == 'archives' } ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
90 println "added public 'archives' conf"
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
91 cfgs.appendNode {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
92 conf(name:'archives',visibility:'public')
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
93 }
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 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
96
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
97
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
98 def outputBuilder = new StreamingMarkupBuilder()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
99 //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
100
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
101 def stringWriter = new StringWriter()
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
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
104 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
105 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
106 file.write(stringWriter.toString())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
107
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
108 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
109 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
110 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
111 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
112 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
113 class MyNodePrinter {
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
114
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
115 protected void printNameAttributes(Map attributes, ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
116
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
117 if (attributes == null || attributes.isEmpty()) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
118 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
119 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
120 def writer = new StringBuffer()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
121 attributes.entrySet().sort{
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
122 switch (it.key ) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
123 case 'org':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
124 1
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
125 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
126 case 'name': 2
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
127 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
128 case 'rev': 3
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
129 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
130 case 'extends': 5
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
131 break
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
132 default: 99
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
133 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
134 }.each { p ->
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
135 def tmp = new StringBuffer()
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
136 Map.Entry entry = (Map.Entry) p;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
137
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
138 tmp << " "
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
139 tmp << getName(entry.getKey())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
140 tmp << "="
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
141 Object value = entry.getValue();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
142 tmp << quote
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
143 tmp << value
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
144 // if (value instanceof String) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
145 // printEscaped((String) value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
146 // } else {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
147 // printEscaped(InvokerHelper.toString(value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
148 // }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
149 tmp << quote
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
150 writer << String.format(" %-35s",tmp.toString())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
151 // printNamespace(entry.getKey(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
152 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
153 out.print(" " + writer.toString().trim())
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
154 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
155
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
156 protected final IndentPrinter out;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
157 private String quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
158 private boolean namespaceAware = true;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
159 private boolean preserveWhitespace = false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
160
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
161 public MyNodePrinter(PrintWriter out) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
162 this(out, " ");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
163 }
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 public MyNodePrinter(PrintWriter out, String indent) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
166 this(out, indent, "\"");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
167 }
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 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
170 this(new IndentPrinter(out, indent), quote);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
171 }
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 public MyNodePrinter(IndentPrinter out) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
174 this(out, "\"");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
175 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
176
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
177 public MyNodePrinter(IndentPrinter out, String quote) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
178 if (out == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
179 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
180 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
181 this.out = out;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
182 this.quote = quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
183 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
184
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
185 public MyNodePrinter() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
186 this(new PrintWriter(new OutputStreamWriter(System.out)));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
187 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
188
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
189 public void print(Node node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
190 print(node, new NamespaceContext());
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
191 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
192
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
193 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
194 * Check if namespace handling is enabled.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
195 * Defaults to <code>true</code>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
196 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
197 * @return true if namespace handling is enabled
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
198 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
199 public boolean isNamespaceAware() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
200 return namespaceAware;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
201 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
202
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
203 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
204 * Enable and/or disable namespace handling.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
205 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
206 * @param namespaceAware the new desired value
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
207 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
208 public void setNamespaceAware(boolean namespaceAware) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
209 this.namespaceAware = namespaceAware;
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
210 }
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
211
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
212 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
213 * Check if whitespace preservation is enabled.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
214 * Defaults to <code>false</code>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
215 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
216 * @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
217 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
218 public boolean isPreserveWhitespace() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
219 return preserveWhitespace;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
220 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
221
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
222 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
223 * Enable and/or disable preservation of whitespace.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
224 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
225 * @param preserveWhitespace the new desired value
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
226 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
227 public void setPreserveWhitespace(boolean preserveWhitespace) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
228 this.preserveWhitespace = preserveWhitespace;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
229 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
230
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
231 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
232 * Get Quote to use when printing attributes.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
233 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
234 * @return the quote character
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
235 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
236 public String getQuote() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
237 return quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
238 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
239
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
240 /**
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
241 * Set Quote to use when printing attributes.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
242 *
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
243 * @param quote the quote character
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
244 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
245 public void setQuote(String quote) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
246 this.quote = quote;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
247 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
248
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
249 protected void print(Node node, NamespaceContext ctx) {
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 * 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
252 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
253 if (isEmptyElement(node)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
254 printLineBegin();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
255 out.print("<");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
256 out.print(getName(node));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
257 if (ctx != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
258 printNamespace(node, ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
259 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
260 printNameAttributes(node.attributes(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
261 out.print("/>");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
262 printLineEnd();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
263 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
264 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
265 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
266
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
267 /*
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
268 * 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
269 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
270 if (printSpecialNode(node)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
271 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
272 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
273 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
274
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 * Handle normal element like <html> ... </html>.
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
277 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
278 Object value = node.value();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
279 if (value instanceof List) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
280 printName(node, ctx, true, isListOfSimple((List) value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
281 printList((List) value, ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
282 printName(node, ctx, false, isListOfSimple((List) value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
283 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
284 return;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
285 }
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 // treat as simple type - probably a String
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
288 printName(node, ctx, true, preserveWhitespace);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
289 printSimpleItemWithIndent(value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
290 printName(node, ctx, false, preserveWhitespace);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
291 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
292 }
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 private boolean isListOfSimple(List value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
295 for (Object p : value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
296 if (p instanceof Node) return false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
297 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
298 return preserveWhitespace;
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
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
301 protected void printLineBegin() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
302 out.printIndent();
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
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
305 protected void printLineEnd() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
306 printLineEnd(null);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
307 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
308
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
309 protected void printLineEnd(String comment) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
310 if (comment != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
311 out.print(" <!-- ");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
312 out.print(comment);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
313 out.print(" -->");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
314 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
315 out.println();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
316 out.flush();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
317 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
318
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
319 protected void printList(List list, NamespaceContext ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
320 out.incrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
321 for (Object value : list) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
322 NamespaceContext context = new NamespaceContext(ctx);
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 * 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
325 */
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
326 if (value instanceof Node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
327 print((Node) value, context);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
328 continue;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
329 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
330 printSimpleItem(value);
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 out.decrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
333 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
334
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
335 protected void printSimpleItem(Object value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
336 if (!preserveWhitespace) printLineBegin();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
337 printEscaped(InvokerHelper.toString(value));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
338 if (!preserveWhitespace) printLineEnd();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
339 }
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 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
342 if (node == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
343 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
344 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
345 Object name = node.name();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
346 if (name == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
347 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
348 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
349 if (!preserve || begin) printLineBegin();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
350 out.print("<");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
351 if (!begin) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
352 out.print("/");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
353 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
354 out.print(getName(node));
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
355 if (ctx != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
356 printNamespace(node, ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
357 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
358 if (begin) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
359 printNameAttributes(node.attributes(), ctx);
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 out.print(">");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
362 if (!preserve || !begin) printLineEnd();
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
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
365 protected boolean printSpecialNode(Node node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
366 return false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
367 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
368
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
369 protected void printNamespace(Object object, NamespaceContext ctx) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
370 if (namespaceAware) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
371 if (object instanceof Node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
372 printNamespace(((Node) object).name(), ctx);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
373 } else if (object instanceof QName) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
374 QName qname = (QName) object;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
375 String namespaceUri = qname.getNamespaceURI();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
376 if (namespaceUri != null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
377 String prefix = qname.getPrefix();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
378 if (!ctx.isPrefixRegistered(prefix, namespaceUri)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
379 ctx.registerNamespacePrefix(prefix, namespaceUri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
380 out.print(" ");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
381 out.print("xmlns");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
382 if (prefix.length() > 0) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
383 out.print(":");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
384 out.print(prefix);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
385 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
386 out.print("=" + quote);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
387 out.print(namespaceUri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
388 out.print(quote);
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 }
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 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
393 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
394
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
395
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
396
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
397 private boolean isEmptyElement(Node node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
398 if (node == null) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
399 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
400 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
401 if (!node.children().isEmpty()) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
402 return false;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
403 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
404 return node.text().length() == 0;
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 private String getName(Object object) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
408 if (object instanceof String) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
409 return (String) object;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
410 } else if (object instanceof QName) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
411 QName qname = (QName) object;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
412 if (!namespaceAware) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
413 return qname.getLocalPart();
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 return qname.getQualifiedName();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
416 } else if (object instanceof Node) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
417 Object name = ((Node) object).name();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
418 return getName(name);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
419 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
420 return object.toString();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
421 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
422
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
423 private void printSimpleItemWithIndent(Object value) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
424 if (!preserveWhitespace) out.incrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
425 printSimpleItem(value);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
426 if (!preserveWhitespace) out.decrementIndent();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
427 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
428
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
429 // 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
430 // 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
431 private void printEscaped(String s) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
432 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
433 char c = s.charAt(i);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
434 switch (c) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
435 case '<':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
436 out.print("&lt;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
437 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
438 case '>':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
439 out.print("&gt;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
440 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
441 case '&':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
442 out.print("&amp;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
443 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
444 case '\'':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
445 if (quote.equals("'"))
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
446 out.print("&apos;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
447 else
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
448 out.print(c);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
449 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
450 case '"':
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
451 if (quote.equals("\""))
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
452 out.print("&quot;");
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
453 else
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
454 out.print(c);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
455 break;
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
456 default:
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
457 out.print(c);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
458 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
459 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
460 }
86
da29f9bde0da groovy script to 'fix' ivy scripts to use standard conventions for configurations
smith@nwoca.org
parents:
diff changeset
461
88
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
462 protected class NamespaceContext {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
463 private final Map<String, String> namespaceMap;
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 public NamespaceContext() {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
466 namespaceMap = new HashMap<String, String>();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
467 }
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 public NamespaceContext(NamespaceContext context) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
470 this();
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
471 namespaceMap.putAll(context.namespaceMap);
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 public boolean isPrefixRegistered(String prefix, String uri) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
475 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
476 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
477
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
478 public void registerNamespacePrefix(String prefix, String uri) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
479 if (!isPrefixRegistered(prefix, uri)) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
480 namespaceMap.put(prefix, uri);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
481 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
482 }
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 public String getNamespace(String prefix) {
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
485 Object uri = namespaceMap.get(prefix);
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
486 return (uri == null) ? null : uri.toString();
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 }
9f2ab59a5333 improve ivy clean up and control order of attribs
smith@nwoca.org
parents: 86
diff changeset
489 }