1 6 package fr.jayasoft.ivy.xml; 7 8 import java.io.BufferedInputStream ; 9 import java.io.BufferedReader ; 10 import java.io.File ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.io.OutputStream ; 16 import java.io.PrintWriter ; 17 import java.net.URL ; 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.Date ; 21 import java.util.Map ; 22 import java.util.Stack ; 23 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.SAXParseException ; 29 import org.xml.sax.ext.LexicalHandler ; 30 import org.xml.sax.helpers.DefaultHandler ; 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 45 public class XmlModuleDescriptorUpdater { 46 public static String LINE_SEPARATOR = System.getProperty("line.separator"); 47 48 58 public static void update(URL srcURL, File destFile, final Map resolvedRevisions, final String status, 59 final String revision, final Date pubdate) 60 throws IOException , SAXException { 61 update(null, srcURL, destFile, resolvedRevisions, status, revision, pubdate, null, false); 62 } 63 64 public static void update(final Ivy ivy, URL srcURL, File destFile, final Map resolvedRevisions, final String status, 65 final String revision, final Date pubdate, final Namespace ns, final boolean replaceInclude) 66 throws IOException , SAXException { 67 update(ivy, srcURL.openStream(), destFile, resolvedRevisions, status, revision, pubdate, ns, replaceInclude); 68 } 69 70 public static void update(final Ivy ivy, InputStream in, File destFile, final Map resolvedRevisions, final String status, 71 final String revision, final Date pubdate, final Namespace ns, final boolean replaceInclude) 72 throws IOException , SAXException { 73 if (destFile.getParentFile() != null) { 74 destFile.getParentFile().mkdirs(); 75 } 76 OutputStream fos = new FileOutputStream (destFile); 77 try { 78 update(ivy, in, fos, resolvedRevisions, status, revision, pubdate, ns, replaceInclude); 79 } finally { 80 try { 81 in.close(); 82 } catch (IOException e) {} 83 try { 84 fos.close(); 85 } catch (IOException e) {} 86 } 87 } 88 89 public static void update(final Ivy ivy, InputStream inStream, OutputStream outStream, final Map resolvedRevisions, final String status, 90 final String revision, final Date pubdate, final Namespace ns, final boolean replaceInclude) 91 throws IOException , SAXException { 92 final PrintWriter out = new PrintWriter (outStream); 93 final BufferedInputStream in = new BufferedInputStream (inStream); 94 95 in.mark(10000); copyHeader(in, out); 97 in.reset(); 99 try { 100 XMLHelper.parse(in, null, new DefaultHandler () { 101 104 private String _organisation = null; 105 private String _defaultConfMapping = null; private Boolean _confMappingOverride = null; private String _justOpen = null; private Stack _context = new Stack (); 109 public void startElement(String uri, String localName, 110 String qName, Attributes attributes) 111 throws SAXException { 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 stdAtts = Arrays.asList(new String [] {"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 url; 147 String fileName = substitute(ivy, attributes.getValue("file")); 148 if (fileName == null) { 149 String urlStr = substitute(ivy, attributes.getValue("url")); 150 url = new URL (urlStr); 151 } else { 152 url = new File (fileName).toURL(); 153 } 154 XMLHelper.parse(url, null, new DefaultHandler () { 155 boolean _first = true; 156 public void startElement(String uri, String localName, 157 String qName, Attributes attributes) 158 throws SAXException { 159 if ("configurations".equals(qName)) { 160 String defaultconf = substitute(ivy, attributes.getValue("defaultconfmapping")); 161 if (defaultconf != null) { 162 _defaultConfMapping = defaultconf; 163 } 164 String mappingOverride = substitute(ivy, attributes.getValue("confmappingoverride")); 165 if (mappingOverride != null) { 166 _confMappingOverride = Boolean.valueOf(mappingOverride); 167 } 168 } else if ("conf".equals(qName)) { 169 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 e) { 183 Message.warn("exception occured while importing configurations: "+e.getMessage()); 184 throw new SAXException (e); 185 } 186 } else if ("dependency".equals(qName)) { 187 out.print("<dependency"); 188 String org = substitute(ivy, attributes.getValue("org")); 189 org = org == null ? _organisation : org; 190 String module = substitute(ivy, attributes.getValue("name")); 191 String branch = substitute(ivy, attributes.getValue("branch")); 192 String 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 attName = attributes.getQName(i); 200 if ("rev".equals(attName)) { 201 String rev = (String )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 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 if (_defaultConfMapping != null && attributes.getValue("defaultconfmapping") == null) { 225 out.print(" defaultconfmapping=\""+_defaultConfMapping+"\""); 226 } 227 if (_confMappingOverride != null && attributes.getValue("confmappingoverride") == null) { 229 out.print(" confmappingoverride=\""+_confMappingOverride.toString()+"\""); 230 } 231 } else { 232 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 } 241 242 private String substitute(Ivy ivy, String value) { 243 return ivy == null ? value : ivy.substitute(value); 244 } 245 246 public void characters(char[] ch, int start, int length) 247 throws SAXException { 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 uri, String localName, 258 String qName) throws SAXException { 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 { 269 out.print(LINE_SEPARATOR); 270 out.flush(); 271 out.close(); 272 } 273 274 public void warning(SAXParseException e) throws SAXException { 275 throw e; 276 } 277 public void error(SAXParseException e) throws SAXException { 278 throw e; 279 } 280 public void fatalError(SAXParseException e) throws SAXException { 281 throw e; 282 } 283 }, new LexicalHandler () { 284 public void endCDATA() throws SAXException { 285 } 286 287 public void endDTD() throws SAXException { 288 } 289 290 public void startCDATA() throws SAXException { 291 } 292 293 public void comment(char[] ch, int start, int length) throws SAXException { 294 StringBuffer comment = new StringBuffer (); 295 comment.append(ch, start, length); 296 out.print("<!--"); 297 out.print(comment.toString()); 298 out.print("-->"); 299 } 300 301 public void endEntity(String name) throws SAXException { 302 } 303 304 public void startEntity(String name) throws SAXException { 305 } 306 307 public void startDTD(String name, String publicId, String systemId) throws SAXException { 308 } 309 }); 310 } catch (ParserConfigurationException e) { 311 IllegalStateException ise = new IllegalStateException ("impossible to update Ivy files: parser problem"); 312 ise.initCause(e); 313 throw ise; 314 } 315 } 316 317 326 private static void copyHeader(InputStream in, PrintWriter out) throws IOException { 327 StringBuffer buf = new StringBuffer (); 328 BufferedReader r = new BufferedReader (new InputStreamReader (in)); 329 for (String 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 } 341 } 342 | Popular Tags |