1 33 34 package edu.rice.cs.util.jar; 35 36 import edu.rice.cs.util.UnexpectedException; 37 import java.util.jar.Manifest ; 38 import java.util.jar.Attributes ; 39 import java.util.LinkedList ; 40 import java.util.List ; 41 import java.util.Iterator ; 42 import java.io.*; 43 44 47 public class ManifestWriter { 48 private List <String > _classPaths; 49 private String _mainClass; 50 public static final Manifest DEFAULT = new ManifestWriter().getManifest(); 51 52 53 public ManifestWriter() { 54 _classPaths = new LinkedList <String >(); 55 _mainClass = null; 56 } 57 58 61 public void addClassPath(String path) { 62 _classPaths.add(_classPaths.size(), path); 63 } 64 65 69 public void setMainClass(String mainClass) { 70 _mainClass = mainClass; 71 } 72 73 77 protected InputStream getInputStream() { 78 80 final StringBuilder sbuf = new StringBuilder (); 81 sbuf.append(Attributes.Name.MANIFEST_VERSION.toString()); 82 sbuf.append(": 1.0"+System.getProperty("line.separator")); 83 if( !_classPaths.isEmpty() ) { 84 Iterator <String > iter = _classPaths.iterator(); 85 sbuf.append(Attributes.Name.CLASS_PATH.toString()); 86 sbuf.append(":"); 87 while (iter.hasNext()) { 88 sbuf.append(" "); 89 sbuf.append(iter.next()); 90 } 91 sbuf.append(System.getProperty("line.separator")); 92 } 93 if( _mainClass != null ) { 94 sbuf.append(Attributes.Name.MAIN_CLASS.toString()); 95 sbuf.append(": "); 96 sbuf.append(_mainClass); 97 sbuf.append(System.getProperty("line.separator")); 98 } 99 try { 100 return new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8")); 101 } 102 catch (UnsupportedEncodingException e) { 103 throw new UnexpectedException(e); 104 } 105 } 106 107 111 public Manifest getManifest() { 112 try { 113 Manifest m = new Manifest (); 114 m.read(getInputStream()); 115 return m; 116 } 117 catch (IOException e) { 118 e.printStackTrace(); 119 return null; 120 } 121 } 122 } | Popular Tags |