KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > xml > XmlModuleDescriptorUpdater


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.xml;
7
8 import java.io.BufferedInputStream JavaDoc;
9 import java.io.BufferedReader JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Stack JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.SAXParseException JavaDoc;
29 import org.xml.sax.ext.LexicalHandler JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 import fr.jayasoft.ivy.Ivy;
33 import fr.jayasoft.ivy.ModuleRevisionId;
34 import fr.jayasoft.ivy.namespace.Namespace;
35 import fr.jayasoft.ivy.util.Message;
36 import fr.jayasoft.ivy.util.XMLHelper;
37
38 /**
39  * Used to update ivy files. Uses ivy file as source and not ModuleDescriptor to preserve
40  * as much as possible the original syntax
41  *
42  * @author Hanin
43  *
44  */

45 public class XmlModuleDescriptorUpdater {
46     public static String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator");
47
48     /**
49      * used to copy a module descriptor xml file (also known as ivy file)
50      * and update the revisions of its dependencies, its status and revision
51      *
52      * @param srcURL the url of the source module descriptor file
53      * @param destFile The file to which the updated module descriptor should be output
54      * @param resolvedRevisions Map from ModuleId of dependencies to new revision (as String)
55      * @param status the new status, null to keep the old one
56      * @param revision the new revision, null to keep the old one
57      */

58     public static void update(URL JavaDoc srcURL, File JavaDoc destFile, final Map JavaDoc resolvedRevisions, final String JavaDoc status,
59             final String JavaDoc revision, final Date JavaDoc pubdate)
60                                 throws IOException JavaDoc, SAXException JavaDoc {
61         update(null, srcURL, destFile, resolvedRevisions, status, revision, pubdate, null, false);
62     }
63
64     public static void update(final Ivy ivy, URL JavaDoc srcURL, File JavaDoc destFile, final Map JavaDoc resolvedRevisions, final String JavaDoc status,
65             final String JavaDoc revision, final Date JavaDoc pubdate, final Namespace ns, final boolean replaceInclude)
66                                 throws IOException JavaDoc, SAXException JavaDoc {
67         update(ivy, srcURL.openStream(), destFile, resolvedRevisions, status, revision, pubdate, ns, replaceInclude);
68     }
69     
70     public static void update(final Ivy ivy, InputStream JavaDoc in, File JavaDoc destFile, final Map JavaDoc resolvedRevisions, final String JavaDoc status,
71             final String JavaDoc revision, final Date JavaDoc pubdate, final Namespace ns, final boolean replaceInclude)
72                                 throws IOException JavaDoc, SAXException JavaDoc {
73         if (destFile.getParentFile() != null) {
74             destFile.getParentFile().mkdirs();
75         }
76         OutputStream JavaDoc fos = new FileOutputStream JavaDoc(destFile);
77         try {
78            update(ivy, in, fos, resolvedRevisions, status, revision, pubdate, ns, replaceInclude);
79         } finally {
80            try {
81                in.close();
82            } catch (IOException JavaDoc e) {}
83            try {
84                fos.close();
85            } catch (IOException JavaDoc e) {}
86         }
87     }
88     
89     public static void update(final Ivy ivy, InputStream JavaDoc inStream, OutputStream JavaDoc outStream, final Map JavaDoc resolvedRevisions, final String JavaDoc status,
90             final String JavaDoc revision, final Date JavaDoc pubdate, final Namespace ns, final boolean replaceInclude)
91                                 throws IOException JavaDoc, SAXException JavaDoc {
92         final PrintWriter JavaDoc out = new PrintWriter JavaDoc(outStream);
93         final BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(inStream);
94         
95         in.mark(10000); // assume the header is never larger than 10000 bytes.
96
copyHeader(in, out);
97         in.reset(); // reposition the stream at the beginning
98

99         try {
100             XMLHelper.parse(in, null, new DefaultHandler JavaDoc() {
101                 // never print *ln* cause \n is found in copied characters stream
102
// nor do we need do handle indentation, original one is maintained except for attributes
103

104                 private String JavaDoc _organisation = null;
105                 private String JavaDoc _defaultConfMapping = null; // defaultConfMapping of imported configurations, if any
106
private Boolean JavaDoc _confMappingOverride = null; // confMappingOverride of imported configurations, if any
107
private String JavaDoc _justOpen = null; // used to know if the last open tag was empty, to adjust termination with /> instead of ></qName>
108
private Stack JavaDoc _context = new Stack JavaDoc();
109                 public void startElement(String JavaDoc uri, String JavaDoc localName,
110                         String JavaDoc qName, Attributes JavaDoc attributes)
111                         throws SAXException JavaDoc {
112                     if (_justOpen != null) {
113                         out.print(">");
114                     }
115                     _context.push(qName);
116                     if ("info".equals(qName)) {
117                         _organisation = substitute(ivy, attributes.getValue("organisation"));
118                         out.print("<info organisation=\""+_organisation
119                                                 +"\" module=\""+substitute(ivy, attributes.getValue("module"))+"\"");
120                         if (revision != null) {
121                             out.print(" revision=\""+revision+"\"");
122                         } else if (attributes.getValue("revision") != null) {
123                             out.print(" revision=\""+substitute(ivy, attributes.getValue("revision"))+"\"");
124                         }
125                         if (status != null) {
126                             out.print(" status=\""+status+"\"");
127                         } else {
128                             out.print(" status=\""+substitute(ivy, attributes.getValue("status"))+"\"");
129                         }
130                         if (pubdate != null) {
131                             out.print(" publication=\""+Ivy.DATE_FORMAT.format(pubdate)+"\"");
132                         } else if (attributes.getValue("publication") != null) {
133                             out.print(" publication=\""+substitute(ivy, attributes.getValue("publication"))+"\"");
134                         }
135                         Collection JavaDoc stdAtts = Arrays.asList(new String JavaDoc[] {"organisation", "module", "revision", "status", "publication", "namespace"});
136                         if (attributes.getValue("namespace") != null) {
137                             out.print(" namespace=\""+substitute(ivy, attributes.getValue("namespace"))+"\"");
138                         }
139                         for (int i=0; i<attributes.getLength(); i++) {
140                             if (!stdAtts.contains(attributes.getQName(i))) {
141                                 out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
142                             }
143                         }
144                     } else if (replaceInclude && "include".equals(qName) && _context.contains("configurations")) {
145                         try {
146                             URL JavaDoc url;
147                             String JavaDoc fileName = substitute(ivy, attributes.getValue("file"));
148                             if (fileName == null) {
149                                 String JavaDoc urlStr = substitute(ivy, attributes.getValue("url"));
150                                 url = new URL JavaDoc(urlStr);
151                             } else {
152                                 url = new File JavaDoc(fileName).toURL();
153                             }
154                             XMLHelper.parse(url, null, new DefaultHandler JavaDoc() {
155                                 boolean _first = true;
156                                 public void startElement(String JavaDoc uri, String JavaDoc localName,
157                                         String JavaDoc qName, Attributes JavaDoc attributes)
158                                         throws SAXException JavaDoc {
159                                     if ("configurations".equals(qName)) {
160                                         String JavaDoc defaultconf = substitute(ivy, attributes.getValue("defaultconfmapping"));
161                                         if (defaultconf != null) {
162                                             _defaultConfMapping = defaultconf;
163                                         }
164                                         String JavaDoc mappingOverride = substitute(ivy, attributes.getValue("confmappingoverride"));
165                                         if (mappingOverride != null) {
166                                            _confMappingOverride = Boolean.valueOf(mappingOverride);
167                                         }
168                                     } else if ("conf".equals(qName)) {
169                                         // copy
170
if (!_first) {
171                                             out.print("/>\n\t\t");
172                                         } else {
173                                             _first = false;
174                                         }
175                                         out.print("<"+qName);
176                                         for (int i=0; i<attributes.getLength(); i++) {
177                                             out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
178                                         }
179                                     }
180                                 }
181                             });
182                         } catch (Exception JavaDoc e) {
183                             Message.warn("exception occured while importing configurations: "+e.getMessage());
184                             throw new SAXException JavaDoc(e);
185                         }
186                     } else if ("dependency".equals(qName)) {
187                         out.print("<dependency");
188                         String JavaDoc org = substitute(ivy, attributes.getValue("org"));
189                         org = org == null ? _organisation : org;
190                         String JavaDoc module = substitute(ivy, attributes.getValue("name"));
191                         String JavaDoc branch = substitute(ivy, attributes.getValue("branch"));
192                         String JavaDoc revision = substitute(ivy, attributes.getValue("rev"));
193                         ModuleRevisionId localMid = ModuleRevisionId.newInstance(org, module, branch, revision);
194                         ModuleRevisionId systemMid = ns == null ?
195                                 localMid :
196                                 ns.getToSystemTransformer().transform(localMid);
197                         
198                         for (int i=0; i<attributes.getLength(); i++) {
199                             String JavaDoc attName = attributes.getQName(i);
200                             if ("rev".equals(attName)) {
201                                 String JavaDoc rev = (String JavaDoc)resolvedRevisions.get(systemMid);
202                                 if (rev != null) {
203                                     out.print(" rev=\""+rev+"\"");
204                                 } else {
205                                     out.print(" rev=\""+systemMid.getRevision()+"\"");
206                                 }
207                             } else if ("org".equals(attName)) {
208                                 out.print(" org=\""+systemMid.getOrganisation()+"\"");
209                             } else if ("name".equals(attName)) {
210                                 out.print(" name=\""+systemMid.getName()+"\"");
211                             } else if ("branch".equals(attName)) {
212                                 out.print(" branch=\""+systemMid.getBranch()+"\"");
213                             } else {
214                                 out.print(" "+attName+"=\""+substitute(ivy, attributes.getValue(attName))+"\"");
215                             }
216                         }
217                     } else if ("dependencies".equals(qName)) {
218                         // copy
219
out.print("<"+qName);
220                         for (int i=0; i<attributes.getLength(); i++) {
221                             out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
222                         }
223                         // add default conf mapping if needed
224
if (_defaultConfMapping != null && attributes.getValue("defaultconfmapping") == null) {
225                             out.print(" defaultconfmapping=\""+_defaultConfMapping+"\"");
226                         }
227                         // add confmappingoverride if needed
228
if (_confMappingOverride != null && attributes.getValue("confmappingoverride") == null) {
229                            out.print(" confmappingoverride=\""+_confMappingOverride.toString()+"\"");
230                         }
231                     } else {
232                         // copy
233
out.print("<"+qName);
234                         for (int i=0; i<attributes.getLength(); i++) {
235                             out.print(" "+attributes.getQName(i)+"=\""+substitute(ivy, attributes.getValue(i))+"\"");
236                         }
237                     }
238                     _justOpen = qName;
239 // indent.append("\t");
240
}
241
242                 private String JavaDoc substitute(Ivy ivy, String JavaDoc value) {
243                     return ivy == null ? value : ivy.substitute(value);
244                 }
245
246                 public void characters(char[] ch, int start, int length)
247                         throws SAXException JavaDoc {
248                     if (_justOpen != null) {
249                         out.print(">");
250                         _justOpen = null;
251                     }
252                     for (int i = start; i < start + length; i++) {
253                         out.print(ch[i]);
254                     }
255                 }
256
257                 public void endElement(String JavaDoc uri, String JavaDoc localName,
258                         String JavaDoc qName) throws SAXException JavaDoc {
259                     if (qName.equals(_justOpen)) {
260                         out.print("/>");
261                     } else {
262                         out.print("</"+qName+">");
263                     }
264                     _justOpen = null;
265                     _context.pop();
266                 }
267
268                 public void endDocument() throws SAXException JavaDoc {
269                     out.print(LINE_SEPARATOR);
270                     out.flush();
271                     out.close();
272                 }
273                 
274                 public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
275                     throw e;
276                 }
277                 public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
278                     throw e;
279                 }
280                 public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
281                     throw e;
282                 }
283             }, new LexicalHandler JavaDoc() {
284                 public void endCDATA() throws SAXException JavaDoc {
285                 }
286
287                 public void endDTD() throws SAXException JavaDoc {
288                 }
289
290                 public void startCDATA() throws SAXException JavaDoc {
291                 }
292
293                 public void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
294                     StringBuffer JavaDoc comment = new StringBuffer JavaDoc();
295                     comment.append(ch, start, length);
296                     out.print("<!--");
297                     out.print(comment.toString());
298                     out.print("-->");
299                 }
300
301                 public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
302                 }
303
304                 public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
305                 }
306
307                 public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
308                 }
309             });
310         } catch (ParserConfigurationException JavaDoc e) {
311             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc("impossible to update Ivy files: parser problem");
312             ise.initCause(e);
313             throw ise;
314         }
315     }
316     
317     /**
318      * Copy xml header from src url ivy file to given printwriter
319      * In fact, copies everything before <ivy-module to out, except
320      * if <ivy-module is not found, in which case nothing is copied.
321      *
322      * @param in
323      * @param out
324      * @throws IOException
325      */

326     private static void copyHeader(InputStream JavaDoc in, PrintWriter JavaDoc out) throws IOException JavaDoc {
327         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
328         BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
329         for (String JavaDoc line = r.readLine(); line != null; line = r.readLine()) {
330             int index = line.indexOf("<ivy-module");
331             if (index == -1) {
332                 buf.append(line).append(LINE_SEPARATOR);
333             } else {
334                 buf.append(line.substring(0, index));
335                 out.print(buf.toString());
336                 break;
337             }
338         }
339         //r.close();
340
}
341 }
342
Popular Tags