1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.extend.repository.maven;
14
15 import java.io.File;
16 import java.io.FileReader;
17 import java.io.IOException;
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.Map;
21 import java.util.Stack;
22
23 import javax.xml.parsers.SAXParser;
24 import javax.xml.parsers.SAXParserFactory;
25
26 import org.abstracthorizon.extend.repository.maven.pom.POM;
27 import org.xml.sax.Attributes;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.Locator;
30 import org.xml.sax.SAXException;
31 import org.xml.sax.helpers.DefaultHandler;
32
33
34
35
36
37
38 public class XMLProcessor extends DefaultHandler {
39
40
41
42
43 protected static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
44
45 protected static final Class<?>[] STRING_CLASS_ARRAY = new Class[]{String.class};
46
47 protected static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
48
49 protected static final Object MAP_MARKER = new Object();
50
51 protected static final Object PLACEHOLDER = new Object();
52
53 protected static final Object FIRST_OBJECT = new Object();
54
55 protected File xmlFile;
56
57 protected StringBuffer buffer = new StringBuffer();
58
59 protected Stack<Object> stack = new Stack<Object>();
60
61 protected Object startObject;
62
63 protected long startTime;
64
65 protected static SAXParserFactory myFactory;
66
67
68 public XMLProcessor(File xmlFile) {
69 this.xmlFile = xmlFile;
70
71 if (myFactory == null) {
72 myFactory = SAXParserFactory.newInstance();
73 }
74 }
75
76 public Object getStartObject() {
77 return startObject;
78 }
79
80 public void setStartObject(Object startObject) {
81 this.startObject = startObject;
82 }
83
84 public void process() throws Exception {
85 stack.clear();
86
87
88 myFactory.setFeature("http://apache.org/xml/features/validation/schema", true);
89
90
91 SAXParser parser = myFactory.newSAXParser();
92
93 FileReader reader = new FileReader(xmlFile);
94 try {
95 InputSource inputSource = new InputSource(reader);
96
97 parser.parse(inputSource, this);
98 } finally {
99 reader.close();
100 }
101
102 }
103
104 public void characters(char[] ch, int start, int length) throws SAXException {
105 buffer.append(ch, start, length);
106 }
107
108 public void startDocument() throws SAXException {
109 startTime = System.currentTimeMillis();
110 }
111
112 public void endDocument() throws SAXException {
113
114
115
116 }
117
118 public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
119 Object newObject = null;
120 Object object = null;
121 if (stack.size() == 0) {
122 object = getStartObject();
123 newObject = object;
124 } else {
125 object = stack.peek();
126 }
127
128 Class<?> cls = object.getClass();
129 if (object instanceof Map) {
130 newObject = MAP_MARKER;
131 }
132 String capitalisedName1 = null;
133 if (newObject == null) {
134 try {
135 capitalisedName1 = capitalise(name, true);
136 Method m = cls.getMethod("add" + capitalisedName1, EMPTY_CLASS_ARRAY);
137 newObject = m.invoke(object, EMPTY_OBJECT_ARRAY);
138 } catch (SecurityException ignore) {
139 } catch (NoSuchMethodException ignore) {
140 } catch (IllegalArgumentException ignore) {
141 } catch (IllegalAccessException ignore) {
142 } catch (InvocationTargetException ignore) {
143 }
144 }
145 String capitalisedName2 = null;
146 if (newObject == null) {
147 try {
148 capitalisedName2 = capitalise(name, false);
149 Method m = cls.getMethod("add" + capitalisedName2, EMPTY_CLASS_ARRAY);
150 newObject = m.invoke(object, EMPTY_OBJECT_ARRAY);
151 } catch (SecurityException ignore) {
152 } catch (NoSuchMethodException ignore) {
153 } catch (IllegalArgumentException ignore) {
154 } catch (IllegalAccessException ignore) {
155 } catch (InvocationTargetException ignore) {
156 }
157 }
158 if (newObject == null) {
159 try {
160 Method m = cls.getMethod("set" + capitalisedName1, STRING_CLASS_ARRAY);
161 if (m != null) {
162 newObject = m;
163 }
164 } catch (SecurityException ignore) {
165 } catch (NoSuchMethodException ignore) {
166 } catch (IllegalArgumentException ignore) {
167 }
168 }
169 if (newObject == null) {
170 try {
171 Method m = cls.getMethod("set" + capitalisedName2, STRING_CLASS_ARRAY);
172 if (m != null) {
173 newObject = m;
174 }
175 } catch (SecurityException ignore) {
176 } catch (NoSuchMethodException ignore) {
177 } catch (IllegalArgumentException ignore) {
178 }
179 }
180 if (newObject == null) {
181 newObject = PLACEHOLDER;
182 }
183 stack.push(newObject);
184 }
185
186 @SuppressWarnings("unchecked")
187 public void endElement(String uri, String localName, String name) throws SAXException {
188 String value = buffer.toString().trim();
189 Object thisObject = stack.pop();
190 if (thisObject == MAP_MARKER) {
191 Map<String, String> map = (Map<String, String>)stack.peek();
192 map.put(name, value);
193 } else if (thisObject instanceof Method) {
194 Object obj = stack.peek();
195 Method m = (Method)thisObject;
196 try {
197 m.invoke(obj, value);
198 } catch (IllegalArgumentException e) {
199 throw new RuntimeException(e);
200 } catch (IllegalAccessException e) {
201 throw new RuntimeException(e);
202 } catch (InvocationTargetException e) {
203 throw new RuntimeException(e);
204 }
205 }
206 buffer.delete(0, buffer.length());
207 }
208
209 public void endPrefixMapping(String prefix) throws SAXException {
210 }
211
212 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
213 }
214
215 public void processingInstruction(String target, String data) throws SAXException {
216 }
217
218 public void setDocumentLocator(Locator locator) {
219 }
220
221 public void skippedEntity(String name) throws SAXException {
222 }
223
224 public void startPrefixMapping(String prefix, String uri) throws SAXException {
225 }
226
227 public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
228 InputSource is = super.resolveEntity(publicId, systemId);
229 return is;
230 }
231
232 public static String capitalise(String string, boolean firstOnly) {
233 if (string.length() == 0) {
234 return string;
235 }
236 if (string.length() == 1) {
237 return string.toUpperCase();
238 }
239 if (firstOnly) {
240 return Character.toUpperCase(string.charAt(0)) + string.substring(1);
241 }
242 StringBuffer res = new StringBuffer(string.length());
243 int i = 0;
244 while ((i < string.length()) && Character.isLowerCase(i)) {
245 res.append(Character.toUpperCase(string.charAt(i)));
246 i++;
247 }
248 if (i < string.length()) {
249 res.append(string.substring(i));
250 }
251 return res.toString();
252 }
253
254 public static void main(String[] args) throws Exception {
255
256 POM pom = new POM();
257
258 File file = new File(System.getProperty("user.home") + "/.m2/repository/org/abstracthorizon/extend/extend/0.4.0/extend-0.4.0.pom");
259 XMLProcessor processor = new XMLProcessor(file);
260 processor.setStartObject(pom);
261 processor.process();
262
263 System.out.println(pom);
264
265 if (pom.getProperties() != null) {
266 Map<String, String> map = pom.getProperties();
267 if (pom.getVersion() != null) {
268 map.put("pom.version", pom.getVersion());
269 }
270 if (pom.getGroupId() != null) {
271 map.put("pom.groupId", pom.getGroupId());
272 }
273 if (pom.getArtifactId() != null) {
274 map.put("pom.artifactId", pom.getArtifactId());
275 }
276 SubstitutionTraverser.substitute(pom, map);
277 System.out.println("-----------------------------------------------------------------------");
278 System.out.println(pom);
279 }
280
281
282 }
283
284 }